channel_test.go 8.2 KB

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