pubsub_test.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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("Topic", func() {
  80. It("generate correct topic", func() {
  81. Expect(ps.Topic("channel-bits-events-v1.123")).To(Equal("channel-bits-events-v1.123"))
  82. Expect(ps.Topic("channel-bits-events-v1", 123)).To(Equal("channel-bits-events-v1.123"))
  83. Expect(ps.Topic("channel-bits-events-v1", "123")).To(Equal("channel-bits-events-v1.123"))
  84. Expect(ps.Topic("channel-bits-events-v1", 123, 456)).To(Equal("channel-bits-events-v1.123.456"))
  85. Expect(ps.Topic("channel-bits-events-v1", 123, "456")).To(Equal("channel-bits-events-v1.123.456"))
  86. Expect(ps.Topic("channel-bits-events-v1", "123", 456)).To(Equal("channel-bits-events-v1.123.456"))
  87. Expect(ps.Topic("channel-bits-events-v1", "123", "456")).To(Equal("channel-bits-events-v1.123.456"))
  88. Expect(ps.Topic("channel-bits-events-v1", 123, 456, 789)).To(Equal("channel-bits-events-v1.123.456.789"))
  89. })
  90. })
  91. })
  92. Context("Connection", func() {
  93. var c *pubsub.Connection
  94. BeforeEach(func() {
  95. c = pubsub.NewConnection(url.URL{Scheme: "ws", Host: "example.com", Path: ""})
  96. })
  97. AfterEach(func() {
  98. c.Close()
  99. })
  100. Context("AddTopic", func() {
  101. It("add new topic", func() {
  102. Expect(c.TopicsCount()).To(Equal(0))
  103. Expect(c.Topics()).To(Equal([]string{}))
  104. c.AddTopic("community-points-channel-v1.1")
  105. Expect(c.TopicsCount()).To(Equal(1))
  106. Expect(c.Topics()).To(ContainElements(
  107. "community-points-channel-v1.1",
  108. ))
  109. c.AddTopic("community-points-channel-v1.2")
  110. Expect(c.TopicsCount()).To(Equal(2))
  111. Expect(c.Topics()).To(ContainElements(
  112. "community-points-channel-v1.1",
  113. "community-points-channel-v1.2",
  114. ))
  115. })
  116. It("not add the same topics", 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.1")
  125. Expect(c.TopicsCount()).To(Equal(1))
  126. Expect(c.Topics()).To(ContainElements(
  127. "community-points-channel-v1.1",
  128. ))
  129. })
  130. It("add not more than 50 topics", func() {
  131. Expect(c.TopicsCount()).To(Equal(0))
  132. Expect(c.Topics()).To(Equal([]string{}))
  133. for i := 1; i <= 60; i++ {
  134. c.AddTopic(fmt.Sprintf("community-points-channel-v1.%d", i))
  135. }
  136. Expect(c.TopicsCount()).To(Equal(50))
  137. })
  138. })
  139. Context("RemoveTopic", func() {
  140. It("remove topic", func() {
  141. Expect(c.TopicsCount()).To(Equal(0))
  142. Expect(c.Topics()).To(Equal([]string{}))
  143. c.AddTopic("community-points-channel-v1.1")
  144. Expect(c.TopicsCount()).To(Equal(1))
  145. Expect(c.Topics()).To(ContainElements(
  146. "community-points-channel-v1.1",
  147. ))
  148. c.RemoveTopic("community-points-channel-v1.1")
  149. Expect(c.TopicsCount()).To(Equal(0))
  150. Expect(c.Topics()).To(Equal([]string{}))
  151. })
  152. })
  153. Context("RemoveAllTopics", func() {
  154. It("remove all topic", func() {
  155. Expect(c.TopicsCount()).To(Equal(0))
  156. Expect(c.Topics()).To(Equal([]string{}))
  157. c.AddTopic("community-points-channel-v1.1")
  158. c.AddTopic("community-points-channel-v1.2")
  159. Expect(c.TopicsCount()).To(Equal(2))
  160. Expect(c.Topics()).To(ContainElements(
  161. "community-points-channel-v1.1",
  162. "community-points-channel-v1.2",
  163. ))
  164. c.RemoveAllTopics()
  165. Expect(c.TopicsCount()).To(Equal(0))
  166. Expect(c.Topics()).To(Equal([]string{}))
  167. })
  168. })
  169. Context("Topics", func() {
  170. It("return topics", func() {
  171. Expect(c.TopicsCount()).To(Equal(0))
  172. Expect(c.Topics()).To(Equal([]string{}))
  173. c.AddTopic("community-points-channel-v1.1")
  174. Expect(c.TopicsCount()).To(Equal(1))
  175. Expect(c.Topics()).To(ContainElements(
  176. "community-points-channel-v1.1",
  177. ))
  178. })
  179. })
  180. Context("HasTopic", func() {
  181. It("checks topics", func() {
  182. Expect(c.TopicsCount()).To(Equal(0))
  183. Expect(c.Topics()).To(Equal([]string{}))
  184. c.AddTopic("community-points-channel-v1.1")
  185. Expect(c.HasTopic("unknown")).To(BeFalse())
  186. Expect(c.HasTopic("community-points-channel-v1.1")).To(BeTrue())
  187. })
  188. })
  189. Context("TopicsCount", func() {
  190. It("return topics count", func() {
  191. Expect(c.TopicsCount()).To(Equal(0))
  192. c.AddTopic("community-points-channel-v1.1")
  193. Expect(c.TopicsCount()).To(Equal(1))
  194. c.AddTopic("community-points-channel-v1.2")
  195. Expect(c.TopicsCount()).To(Equal(2))
  196. })
  197. })
  198. })
  199. })
  200. func TestSuite(t *testing.T) {
  201. RegisterFailHandler(Fail)
  202. RunSpecs(t, "PubSub")
  203. }