pubsub_test.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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("HasTopic", func() {
  58. It("checks topics", func() {
  59. Expect(len(ps.Connections)).To(Equal(0))
  60. ps.Listen("community-points-channel-v1", 1)
  61. Expect(ps.HasTopic("unknown")).To(BeFalse())
  62. Expect(ps.HasTopic("community-points-channel-v1", 1)).To(BeTrue())
  63. })
  64. })
  65. Context("Topic", func() {
  66. It("generate correct topic", func() {
  67. Expect(ps.Topic("channel-bits-events-v1.123")).To(Equal("channel-bits-events-v1.123"))
  68. Expect(ps.Topic("channel-bits-events-v1", 123)).To(Equal("channel-bits-events-v1.123"))
  69. Expect(ps.Topic("channel-bits-events-v1", "123")).To(Equal("channel-bits-events-v1.123"))
  70. Expect(ps.Topic("channel-bits-events-v1", 123, 456)).To(Equal("channel-bits-events-v1.123.456"))
  71. Expect(ps.Topic("channel-bits-events-v1", 123, "456")).To(Equal("channel-bits-events-v1.123.456"))
  72. Expect(ps.Topic("channel-bits-events-v1", "123", 456)).To(Equal("channel-bits-events-v1.123.456"))
  73. Expect(ps.Topic("channel-bits-events-v1", "123", "456")).To(Equal("channel-bits-events-v1.123.456"))
  74. Expect(ps.Topic("channel-bits-events-v1", 123, 456, 789)).To(Equal("channel-bits-events-v1.123.456.789"))
  75. })
  76. })
  77. })
  78. Context("Connection", func() {
  79. var c *pubsub.Connection
  80. BeforeEach(func() {
  81. c = pubsub.NewConnection(url.URL{Scheme: "ws", Host: "example.com", Path: ""})
  82. })
  83. AfterEach(func() {
  84. c.Close()
  85. })
  86. Context("AddTopic", func() {
  87. It("add new topic", func() {
  88. Expect(c.TopicsCount()).To(Equal(0))
  89. Expect(c.Topics()).To(Equal([]string{}))
  90. c.AddTopic("community-points-channel-v1.1")
  91. Expect(c.TopicsCount()).To(Equal(1))
  92. Expect(c.Topics()).To(ContainElements(
  93. "community-points-channel-v1.1",
  94. ))
  95. c.AddTopic("community-points-channel-v1.2")
  96. Expect(c.TopicsCount()).To(Equal(2))
  97. Expect(c.Topics()).To(ContainElements(
  98. "community-points-channel-v1.1",
  99. "community-points-channel-v1.2",
  100. ))
  101. })
  102. It("not add the same topics", func() {
  103. Expect(c.TopicsCount()).To(Equal(0))
  104. Expect(c.Topics()).To(Equal([]string{}))
  105. c.AddTopic("community-points-channel-v1.1")
  106. Expect(c.TopicsCount()).To(Equal(1))
  107. Expect(c.Topics()).To(ContainElements(
  108. "community-points-channel-v1.1",
  109. ))
  110. c.AddTopic("community-points-channel-v1.1")
  111. Expect(c.TopicsCount()).To(Equal(1))
  112. Expect(c.Topics()).To(ContainElements(
  113. "community-points-channel-v1.1",
  114. ))
  115. })
  116. It("add not more than 50 topics", func() {
  117. Expect(c.TopicsCount()).To(Equal(0))
  118. Expect(c.Topics()).To(Equal([]string{}))
  119. for i := 1; i <= 60; i++ {
  120. c.AddTopic(fmt.Sprintf("community-points-channel-v1.%d", i))
  121. }
  122. Expect(c.TopicsCount()).To(Equal(50))
  123. })
  124. })
  125. Context("RemoveTopic", func() {
  126. It("remove topic", func() {
  127. Expect(c.TopicsCount()).To(Equal(0))
  128. Expect(c.Topics()).To(Equal([]string{}))
  129. c.AddTopic("community-points-channel-v1.1")
  130. Expect(c.TopicsCount()).To(Equal(1))
  131. Expect(c.Topics()).To(ContainElements(
  132. "community-points-channel-v1.1",
  133. ))
  134. c.RemoveTopic("community-points-channel-v1.1")
  135. Expect(c.TopicsCount()).To(Equal(0))
  136. Expect(c.Topics()).To(Equal([]string{}))
  137. })
  138. })
  139. Context("RemoveAllTopics", func() {
  140. It("remove all 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. c.AddTopic("community-points-channel-v1.2")
  145. Expect(c.TopicsCount()).To(Equal(2))
  146. Expect(c.Topics()).To(ContainElements(
  147. "community-points-channel-v1.1",
  148. "community-points-channel-v1.2",
  149. ))
  150. c.RemoveAllTopics()
  151. Expect(c.TopicsCount()).To(Equal(0))
  152. Expect(c.Topics()).To(Equal([]string{}))
  153. })
  154. })
  155. Context("Topics", func() {
  156. It("return topics", func() {
  157. Expect(c.TopicsCount()).To(Equal(0))
  158. Expect(c.Topics()).To(Equal([]string{}))
  159. c.AddTopic("community-points-channel-v1.1")
  160. Expect(c.TopicsCount()).To(Equal(1))
  161. Expect(c.Topics()).To(ContainElements(
  162. "community-points-channel-v1.1",
  163. ))
  164. })
  165. })
  166. Context("HasTopic", func() {
  167. It("checks topics", func() {
  168. Expect(c.TopicsCount()).To(Equal(0))
  169. Expect(c.Topics()).To(Equal([]string{}))
  170. c.AddTopic("community-points-channel-v1.1")
  171. Expect(c.HasTopic("unknown")).To(BeFalse())
  172. Expect(c.HasTopic("community-points-channel-v1.1")).To(BeTrue())
  173. })
  174. })
  175. Context("TopicsCount", func() {
  176. It("return topics count", func() {
  177. Expect(c.TopicsCount()).To(Equal(0))
  178. c.AddTopic("community-points-channel-v1.1")
  179. Expect(c.TopicsCount()).To(Equal(1))
  180. c.AddTopic("community-points-channel-v1.2")
  181. Expect(c.TopicsCount()).To(Equal(2))
  182. })
  183. })
  184. })
  185. })
  186. func TestSuite(t *testing.T) {
  187. RegisterFailHandler(Fail)
  188. RunSpecs(t, "PubSub")
  189. }