pubsub_test.go 7.2 KB

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