channel_test.go 7.7 KB

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