pubsub_test.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. package pubsub_test
  2. import (
  3. "context"
  4. "fmt"
  5. "net/url"
  6. "testing"
  7. "github.com/vladimirok5959/golang-twitch/pubsub"
  8. . "github.com/onsi/ginkgo"
  9. . "github.com/onsi/gomega"
  10. )
  11. var _ = Describe("PubSub", func() {
  12. var ctx = context.Background()
  13. Context("PubSub", func() {
  14. var ps *pubsub.PubSub
  15. BeforeEach(func() {
  16. ps = pubsub.NewWithURL(url.URL{Scheme: "ws", Host: "example.com", Path: ""})
  17. })
  18. AfterEach(func() {
  19. ps.Close()
  20. })
  21. Context("Listen", func() {
  22. It("create new connection for each 50 topics", func() {
  23. Expect(len(ps.Connections)).To(Equal(0))
  24. for i := 1; i <= 45; i++ {
  25. ps.Listen(ctx, "community-points-channel-v1", 1, i)
  26. }
  27. Expect(len(ps.Connections)).To(Equal(1))
  28. for i := 1; i <= 5; i++ {
  29. ps.Listen(ctx, "community-points-channel-v1", 1, i)
  30. }
  31. Expect(len(ps.Connections)).To(Equal(1))
  32. for i := 1; i <= 50; i++ {
  33. ps.Listen(ctx, "community-points-channel-v1", 2, i)
  34. }
  35. Expect(len(ps.Connections)).To(Equal(2))
  36. for i := 1; i <= 50; i++ {
  37. ps.Listen(ctx, "community-points-channel-v1", 3, i)
  38. }
  39. Expect(len(ps.Connections)).To(Equal(3))
  40. })
  41. })
  42. Context("Unlisten", func() {
  43. It("remove connection without topics", func() {
  44. Expect(len(ps.Connections)).To(Equal(0))
  45. for i := 1; i <= 50; i++ {
  46. ps.Listen(ctx, "community-points-channel-v1", 1, i)
  47. }
  48. Expect(len(ps.Connections)).To(Equal(1))
  49. ps.Listen(ctx, "community-points-channel-v1", 2, 1)
  50. Expect(len(ps.Connections)).To(Equal(2))
  51. ps.Unlisten(ctx, "community-points-channel-v1", 2, 1)
  52. Expect(len(ps.Connections)).To(Equal(1))
  53. for i := 1; i <= 50; i++ {
  54. ps.Unlisten(ctx, "community-points-channel-v1", 1, i)
  55. }
  56. Expect(len(ps.Connections)).To(Equal(0))
  57. })
  58. })
  59. Context("Topics", func() {
  60. It("return topics", func() {
  61. Expect(len(ps.Connections)).To(Equal(0))
  62. for i := 1; i <= 50; i++ {
  63. ps.Listen(ctx, "community-points-channel-v1", 1, i)
  64. }
  65. Expect(len(ps.Connections)).To(Equal(1))
  66. ps.Listen(ctx, "community-points-channel-v1", 2, 1)
  67. Expect(len(ps.Connections)).To(Equal(2))
  68. Expect(ps.Topics()).To(ContainElements(
  69. "community-points-channel-v1.2.1",
  70. ))
  71. })
  72. })
  73. Context("HasTopic", func() {
  74. It("checks topics", func() {
  75. Expect(len(ps.Connections)).To(Equal(0))
  76. ps.Listen(ctx, "community-points-channel-v1", 1)
  77. Expect(ps.HasTopic("unknown")).To(BeFalse())
  78. Expect(ps.HasTopic("community-points-channel-v1", 1)).To(BeTrue())
  79. })
  80. })
  81. Context("TopicsCount", func() {
  82. It("return topics count", func() {
  83. Expect(ps.TopicsCount()).To(Equal(0))
  84. for i := 1; i <= 50; i++ {
  85. ps.Listen(ctx, "community-points-channel-v1", 1, i)
  86. }
  87. Expect(ps.TopicsCount()).To(Equal(50))
  88. for i := 1; i <= 5; i++ {
  89. ps.Listen(ctx, "community-points-channel-v1", 2, i)
  90. }
  91. Expect(ps.TopicsCount()).To(Equal(55))
  92. })
  93. })
  94. Context("Topic", func() {
  95. It("generate correct topic", func() {
  96. Expect(ps.Topic("channel-bits-events-v1.123")).To(Equal("channel-bits-events-v1.123"))
  97. Expect(ps.Topic("channel-bits-events-v1", 123)).To(Equal("channel-bits-events-v1.123"))
  98. Expect(ps.Topic("channel-bits-events-v1", "123")).To(Equal("channel-bits-events-v1.123"))
  99. Expect(ps.Topic("channel-bits-events-v1", 123, 456)).To(Equal("channel-bits-events-v1.123.456"))
  100. Expect(ps.Topic("channel-bits-events-v1", 123, "456")).To(Equal("channel-bits-events-v1.123.456"))
  101. Expect(ps.Topic("channel-bits-events-v1", "123", 456)).To(Equal("channel-bits-events-v1.123.456"))
  102. Expect(ps.Topic("channel-bits-events-v1", "123", "456")).To(Equal("channel-bits-events-v1.123.456"))
  103. Expect(ps.Topic("channel-bits-events-v1", 123, 456, 789)).To(Equal("channel-bits-events-v1.123.456.789"))
  104. })
  105. })
  106. })
  107. Context("Connection", func() {
  108. var c *pubsub.Connection
  109. BeforeEach(func() {
  110. c = pubsub.NewConnection(url.URL{Scheme: "ws", Host: "example.com", Path: ""})
  111. })
  112. AfterEach(func() {
  113. c.Close()
  114. })
  115. Context("AddTopic", func() {
  116. It("add new topic", func() {
  117. Expect(c.TopicsCount()).To(Equal(0))
  118. Expect(c.Topics()).To(Equal([]string{}))
  119. c.AddTopic("community-points-channel-v1.1")
  120. Expect(c.TopicsCount()).To(Equal(1))
  121. Expect(c.Topics()).To(ContainElements(
  122. "community-points-channel-v1.1",
  123. ))
  124. c.AddTopic("community-points-channel-v1.2")
  125. Expect(c.TopicsCount()).To(Equal(2))
  126. Expect(c.Topics()).To(ContainElements(
  127. "community-points-channel-v1.1",
  128. "community-points-channel-v1.2",
  129. ))
  130. })
  131. It("not add the same topics", func() {
  132. Expect(c.TopicsCount()).To(Equal(0))
  133. Expect(c.Topics()).To(Equal([]string{}))
  134. c.AddTopic("community-points-channel-v1.1")
  135. Expect(c.TopicsCount()).To(Equal(1))
  136. Expect(c.Topics()).To(ContainElements(
  137. "community-points-channel-v1.1",
  138. ))
  139. c.AddTopic("community-points-channel-v1.1")
  140. Expect(c.TopicsCount()).To(Equal(1))
  141. Expect(c.Topics()).To(ContainElements(
  142. "community-points-channel-v1.1",
  143. ))
  144. })
  145. It("add not more than 50 topics", func() {
  146. Expect(c.TopicsCount()).To(Equal(0))
  147. Expect(c.Topics()).To(Equal([]string{}))
  148. for i := 1; i <= 60; i++ {
  149. c.AddTopic(fmt.Sprintf("community-points-channel-v1.%d", i))
  150. }
  151. Expect(c.TopicsCount()).To(Equal(50))
  152. })
  153. })
  154. Context("RemoveTopic", func() {
  155. It("remove topic", func() {
  156. Expect(c.TopicsCount()).To(Equal(0))
  157. Expect(c.Topics()).To(Equal([]string{}))
  158. c.AddTopic("community-points-channel-v1.1")
  159. Expect(c.TopicsCount()).To(Equal(1))
  160. Expect(c.Topics()).To(ContainElements(
  161. "community-points-channel-v1.1",
  162. ))
  163. c.RemoveTopic("community-points-channel-v1.1")
  164. Expect(c.TopicsCount()).To(Equal(0))
  165. Expect(c.Topics()).To(Equal([]string{}))
  166. })
  167. })
  168. Context("RemoveAllTopics", func() {
  169. It("remove all topic", func() {
  170. Expect(c.TopicsCount()).To(Equal(0))
  171. Expect(c.Topics()).To(Equal([]string{}))
  172. c.AddTopic("community-points-channel-v1.1")
  173. c.AddTopic("community-points-channel-v1.2")
  174. Expect(c.TopicsCount()).To(Equal(2))
  175. Expect(c.Topics()).To(ContainElements(
  176. "community-points-channel-v1.1",
  177. "community-points-channel-v1.2",
  178. ))
  179. c.RemoveAllTopics()
  180. Expect(c.TopicsCount()).To(Equal(0))
  181. Expect(c.Topics()).To(Equal([]string{}))
  182. })
  183. })
  184. Context("Topics", func() {
  185. It("return topics", func() {
  186. Expect(c.TopicsCount()).To(Equal(0))
  187. Expect(c.Topics()).To(Equal([]string{}))
  188. c.AddTopic("community-points-channel-v1.1")
  189. Expect(c.TopicsCount()).To(Equal(1))
  190. Expect(c.Topics()).To(ContainElements(
  191. "community-points-channel-v1.1",
  192. ))
  193. })
  194. })
  195. Context("HasTopic", func() {
  196. It("checks topics", func() {
  197. Expect(c.TopicsCount()).To(Equal(0))
  198. Expect(c.Topics()).To(Equal([]string{}))
  199. c.AddTopic("community-points-channel-v1.1")
  200. Expect(c.HasTopic("unknown")).To(BeFalse())
  201. Expect(c.HasTopic("community-points-channel-v1.1")).To(BeTrue())
  202. })
  203. })
  204. Context("TopicsCount", func() {
  205. It("return topics count", func() {
  206. Expect(c.TopicsCount()).To(Equal(0))
  207. c.AddTopic("community-points-channel-v1.1")
  208. Expect(c.TopicsCount()).To(Equal(1))
  209. c.AddTopic("community-points-channel-v1.2")
  210. Expect(c.TopicsCount()).To(Equal(2))
  211. })
  212. })
  213. })
  214. })
  215. func TestSuite(t *testing.T) {
  216. RegisterFailHandler(Fail)
  217. RunSpecs(t, "PubSub")
  218. }