channel_test.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. package stats_test
  2. import (
  3. "context"
  4. "fmt"
  5. "testing"
  6. "time"
  7. . "github.com/xtls/xray-core/app/stats"
  8. "github.com/xtls/xray-core/common"
  9. "github.com/xtls/xray-core/features/stats"
  10. )
  11. func TestStatsChannel(t *testing.T) {
  12. // At most 2 subscribers could be registered
  13. c := NewChannel(&ChannelConfig{SubscriberLimit: 2, Blocking: true})
  14. a, err := stats.SubscribeRunnableChannel(c)
  15. common.Must(err)
  16. if !c.Running() {
  17. t.Fatal("unexpected failure in running channel after first subscription")
  18. }
  19. b, err := c.Subscribe()
  20. common.Must(err)
  21. // Test that third subscriber is forbidden
  22. _, err = c.Subscribe()
  23. if err == nil {
  24. t.Fatal("unexpected successful subscription")
  25. }
  26. t.Log("expected error: ", err)
  27. stopCh := make(chan struct{})
  28. errCh := make(chan string)
  29. go func() {
  30. c.Publish(context.Background(), 1)
  31. c.Publish(context.Background(), 2)
  32. c.Publish(context.Background(), "3")
  33. c.Publish(context.Background(), []int{4})
  34. stopCh <- struct{}{}
  35. }()
  36. go func() {
  37. if v, ok := (<-a).(int); !ok || v != 1 {
  38. errCh <- fmt.Sprint("unexpected receiving: ", v, ", wanted ", 1)
  39. }
  40. if v, ok := (<-a).(int); !ok || v != 2 {
  41. errCh <- fmt.Sprint("unexpected receiving: ", v, ", wanted ", 2)
  42. }
  43. if v, ok := (<-a).(string); !ok || v != "3" {
  44. errCh <- fmt.Sprint("unexpected receiving: ", v, ", wanted ", "3")
  45. }
  46. if v, ok := (<-a).([]int); !ok || v[0] != 4 {
  47. errCh <- fmt.Sprint("unexpected receiving: ", v, ", wanted ", []int{4})
  48. }
  49. stopCh <- struct{}{}
  50. }()
  51. go func() {
  52. if v, ok := (<-b).(int); !ok || v != 1 {
  53. errCh <- fmt.Sprint("unexpected receiving: ", v, ", wanted ", 1)
  54. }
  55. if v, ok := (<-b).(int); !ok || v != 2 {
  56. errCh <- fmt.Sprint("unexpected receiving: ", v, ", wanted ", 2)
  57. }
  58. if v, ok := (<-b).(string); !ok || v != "3" {
  59. errCh <- fmt.Sprint("unexpected receiving: ", v, ", wanted ", "3")
  60. }
  61. if v, ok := (<-b).([]int); !ok || v[0] != 4 {
  62. errCh <- fmt.Sprint("unexpected receiving: ", v, ", wanted ", []int{4})
  63. }
  64. stopCh <- struct{}{}
  65. }()
  66. timeout := time.After(2 * time.Second)
  67. for i := 0; i < 3; i++ {
  68. select {
  69. case <-timeout:
  70. t.Fatal("Test timeout after 2s")
  71. case e := <-errCh:
  72. t.Fatal(e)
  73. case <-stopCh:
  74. }
  75. }
  76. // Test the unsubscription of channel
  77. common.Must(c.Unsubscribe(b))
  78. // Test the last subscriber will close channel with `UnsubscribeClosableChannel`
  79. common.Must(stats.UnsubscribeClosableChannel(c, a))
  80. if c.Running() {
  81. t.Fatal("unexpected running channel after unsubscribing the last subscriber")
  82. }
  83. }
  84. func TestStatsChannelUnsubcribe(t *testing.T) {
  85. c := NewChannel(&ChannelConfig{Blocking: true})
  86. common.Must(c.Start())
  87. defer c.Close()
  88. a, err := c.Subscribe()
  89. common.Must(err)
  90. defer c.Unsubscribe(a)
  91. b, err := c.Subscribe()
  92. common.Must(err)
  93. pauseCh := make(chan struct{})
  94. stopCh := make(chan struct{})
  95. errCh := make(chan string)
  96. {
  97. var aSet, bSet bool
  98. for _, s := range c.Subscribers() {
  99. if s == a {
  100. aSet = true
  101. }
  102. if s == b {
  103. bSet = true
  104. }
  105. }
  106. if !(aSet && bSet) {
  107. t.Fatal("unexpected subscribers: ", c.Subscribers())
  108. }
  109. }
  110. go func() { // Blocking publish
  111. c.Publish(context.Background(), 1)
  112. <-pauseCh // Wait for `b` goroutine to resume sending message
  113. c.Publish(context.Background(), 2)
  114. }()
  115. go func() {
  116. if v, ok := (<-a).(int); !ok || v != 1 {
  117. errCh <- fmt.Sprint("unexpected receiving: ", v, ", wanted ", 1)
  118. }
  119. if v, ok := (<-a).(int); !ok || v != 2 {
  120. errCh <- fmt.Sprint("unexpected receiving: ", v, ", wanted ", 2)
  121. }
  122. }()
  123. go func() {
  124. if v, ok := (<-b).(int); !ok || v != 1 {
  125. errCh <- fmt.Sprint("unexpected receiving: ", v, ", wanted ", 1)
  126. }
  127. // Unsubscribe `b` while publishing is paused
  128. c.Unsubscribe(b)
  129. { // Test `b` is not in subscribers
  130. var aSet, bSet bool
  131. for _, s := range c.Subscribers() {
  132. if s == a {
  133. aSet = true
  134. }
  135. if s == b {
  136. bSet = true
  137. }
  138. }
  139. if !(aSet && !bSet) {
  140. errCh <- fmt.Sprint("unexpected subscribers: ", c.Subscribers())
  141. }
  142. }
  143. // Resume publishing progress
  144. close(pauseCh)
  145. // Test `b` is neither closed nor able to receive any data
  146. select {
  147. case v, ok := <-b:
  148. if ok {
  149. errCh <- fmt.Sprint("unexpected data received: ", v)
  150. } else {
  151. errCh <- fmt.Sprint("unexpected closed channel: ", b)
  152. }
  153. default:
  154. }
  155. close(stopCh)
  156. }()
  157. select {
  158. case <-time.After(2 * time.Second):
  159. t.Fatal("Test timeout after 2s")
  160. case e := <-errCh:
  161. t.Fatal(e)
  162. case <-stopCh:
  163. }
  164. }
  165. func TestStatsChannelBlocking(t *testing.T) {
  166. // Do not use buffer so as to create blocking scenario
  167. c := NewChannel(&ChannelConfig{BufferSize: 0, Blocking: true})
  168. common.Must(c.Start())
  169. defer c.Close()
  170. a, err := c.Subscribe()
  171. common.Must(err)
  172. defer c.Unsubscribe(a)
  173. pauseCh := make(chan struct{})
  174. stopCh := make(chan struct{})
  175. errCh := make(chan string)
  176. ctx, cancel := context.WithCancel(context.Background())
  177. // Test blocking channel publishing
  178. go func() {
  179. // Dummy messsage with no subscriber receiving, will block broadcasting goroutine
  180. c.Publish(context.Background(), nil)
  181. <-pauseCh
  182. // Publishing should be blocked here, for last message was not cleared and buffer was full
  183. c.Publish(context.Background(), nil)
  184. pauseCh <- struct{}{}
  185. // Publishing should still be blocked here
  186. c.Publish(ctx, nil)
  187. // Check publishing is done because context is canceled
  188. select {
  189. case <-ctx.Done():
  190. if ctx.Err() != context.Canceled {
  191. errCh <- fmt.Sprint("unexpected error: ", ctx.Err())
  192. }
  193. default:
  194. errCh <- "unexpected non-blocked publishing"
  195. }
  196. close(stopCh)
  197. }()
  198. go func() {
  199. pauseCh <- struct{}{}
  200. select {
  201. case <-pauseCh:
  202. errCh <- "unexpected non-blocked publishing"
  203. case <-time.After(100 * time.Millisecond):
  204. }
  205. // Receive first published message
  206. <-a
  207. select {
  208. case <-pauseCh:
  209. case <-time.After(100 * time.Millisecond):
  210. errCh <- "unexpected blocking publishing"
  211. }
  212. // Manually cancel the context to end publishing
  213. cancel()
  214. }()
  215. select {
  216. case <-time.After(2 * time.Second):
  217. t.Fatal("Test timeout after 2s")
  218. case e := <-errCh:
  219. t.Fatal(e)
  220. case <-stopCh:
  221. }
  222. }
  223. func TestStatsChannelNonBlocking(t *testing.T) {
  224. // Do not use buffer so as to create blocking scenario
  225. c := NewChannel(&ChannelConfig{BufferSize: 0, Blocking: false})
  226. common.Must(c.Start())
  227. defer c.Close()
  228. a, err := c.Subscribe()
  229. common.Must(err)
  230. defer c.Unsubscribe(a)
  231. pauseCh := make(chan struct{})
  232. stopCh := make(chan struct{})
  233. errCh := make(chan string)
  234. ctx, cancel := context.WithCancel(context.Background())
  235. // Test blocking channel publishing
  236. go func() {
  237. c.Publish(context.Background(), nil)
  238. c.Publish(context.Background(), nil)
  239. pauseCh <- struct{}{}
  240. <-pauseCh
  241. c.Publish(ctx, nil)
  242. c.Publish(ctx, nil)
  243. // Check publishing is done because context is canceled
  244. select {
  245. case <-ctx.Done():
  246. if ctx.Err() != context.Canceled {
  247. errCh <- fmt.Sprint("unexpected error: ", ctx.Err())
  248. }
  249. case <-time.After(100 * time.Millisecond):
  250. errCh <- "unexpected non-cancelled publishing"
  251. }
  252. }()
  253. go func() {
  254. // Check publishing won't block even if there is no subscriber receiving message
  255. select {
  256. case <-pauseCh:
  257. case <-time.After(100 * time.Millisecond):
  258. errCh <- "unexpected blocking publishing"
  259. }
  260. // Receive first and second published message
  261. <-a
  262. <-a
  263. pauseCh <- struct{}{}
  264. // Manually cancel the context to end publishing
  265. cancel()
  266. // Check third and forth published message is cancelled and cannot receive
  267. <-time.After(100 * time.Millisecond)
  268. select {
  269. case <-a:
  270. errCh <- "unexpected non-cancelled publishing"
  271. default:
  272. }
  273. select {
  274. case <-a:
  275. errCh <- "unexpected non-cancelled publishing"
  276. default:
  277. }
  278. close(stopCh)
  279. }()
  280. select {
  281. case <-time.After(2 * time.Second):
  282. t.Fatal("Test timeout after 2s")
  283. case e := <-errCh:
  284. t.Fatal(e)
  285. case <-stopCh:
  286. }
  287. }
  288. func TestStatsChannelConcurrency(t *testing.T) {
  289. // Do not use buffer so as to create blocking scenario
  290. c := NewChannel(&ChannelConfig{BufferSize: 0, Blocking: true})
  291. common.Must(c.Start())
  292. defer c.Close()
  293. a, err := c.Subscribe()
  294. common.Must(err)
  295. defer c.Unsubscribe(a)
  296. b, err := c.Subscribe()
  297. common.Must(err)
  298. defer c.Unsubscribe(b)
  299. stopCh := make(chan struct{})
  300. errCh := make(chan string)
  301. go func() { // Blocking publish
  302. c.Publish(context.Background(), 1)
  303. c.Publish(context.Background(), 2)
  304. }()
  305. go func() {
  306. if v, ok := (<-a).(int); !ok || v != 1 {
  307. errCh <- fmt.Sprint("unexpected receiving: ", v, ", wanted ", 1)
  308. }
  309. if v, ok := (<-a).(int); !ok || v != 2 {
  310. errCh <- fmt.Sprint("unexpected receiving: ", v, ", wanted ", 2)
  311. }
  312. }()
  313. go func() {
  314. // Block `b` for a time so as to ensure source channel is trying to send message to `b`.
  315. <-time.After(25 * time.Millisecond)
  316. // This causes concurrency scenario: unsubscribe `b` while trying to send message to it
  317. c.Unsubscribe(b)
  318. // Test `b` is not closed and can still receive data 1:
  319. // Because unsubscribe won't affect the ongoing process of sending message.
  320. select {
  321. case v, ok := <-b:
  322. if v1, ok1 := v.(int); !(ok && ok1 && v1 == 1) {
  323. errCh <- fmt.Sprint("unexpected failure in receiving data: ", 1)
  324. }
  325. default:
  326. errCh <- fmt.Sprint("unexpected block from receiving data: ", 1)
  327. }
  328. // Test `b` is not closed but cannot receive data 2:
  329. // Because in a new round of messaging, `b` has been unsubscribed.
  330. select {
  331. case v, ok := <-b:
  332. if ok {
  333. errCh <- fmt.Sprint("unexpected receiving: ", v)
  334. } else {
  335. errCh <- "unexpected closing of channel"
  336. }
  337. default:
  338. }
  339. close(stopCh)
  340. }()
  341. select {
  342. case <-time.After(2 * time.Second):
  343. t.Fatal("Test timeout after 2s")
  344. case e := <-errCh:
  345. t.Fatal(e)
  346. case <-stopCh:
  347. }
  348. }