pubsub_test.go 6.1 KB

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