123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- package pubsub_test
- import (
- "context"
- "fmt"
- "net/url"
- "testing"
- "github.com/vladimirok5959/golang-twitch/pubsub"
- . "github.com/onsi/ginkgo"
- . "github.com/onsi/gomega"
- )
- var _ = Describe("PubSub", func() {
- var ctx = context.Background()
- Context("PubSub", func() {
- var ps *pubsub.PubSub
- BeforeEach(func() {
- ps = pubsub.NewWithURL(url.URL{Scheme: "ws", Host: "example.com", Path: ""})
- })
- AfterEach(func() {
- ps.Close()
- })
- Context("Listen", func() {
- It("create new connection for each 50 topics", func() {
- Expect(len(ps.Connections)).To(Equal(0))
- for i := 1; i <= 45; i++ {
- ps.Listen(ctx, "community-points-channel-v1", 1, i)
- }
- Expect(len(ps.Connections)).To(Equal(1))
- for i := 1; i <= 5; i++ {
- ps.Listen(ctx, "community-points-channel-v1", 1, i)
- }
- Expect(len(ps.Connections)).To(Equal(1))
- for i := 1; i <= 50; i++ {
- ps.Listen(ctx, "community-points-channel-v1", 2, i)
- }
- Expect(len(ps.Connections)).To(Equal(2))
- for i := 1; i <= 50; i++ {
- ps.Listen(ctx, "community-points-channel-v1", 3, i)
- }
- Expect(len(ps.Connections)).To(Equal(3))
- })
- })
- Context("Unlisten", func() {
- It("remove connection without topics", func() {
- Expect(len(ps.Connections)).To(Equal(0))
- for i := 1; i <= 50; i++ {
- ps.Listen(ctx, "community-points-channel-v1", 1, i)
- }
- Expect(len(ps.Connections)).To(Equal(1))
- ps.Listen(ctx, "community-points-channel-v1", 2, 1)
- Expect(len(ps.Connections)).To(Equal(2))
- ps.Unlisten(ctx, "community-points-channel-v1", 2, 1)
- Expect(len(ps.Connections)).To(Equal(1))
- for i := 1; i <= 50; i++ {
- ps.Unlisten(ctx, "community-points-channel-v1", 1, i)
- }
- Expect(len(ps.Connections)).To(Equal(0))
- })
- })
- Context("Topics", func() {
- It("return topics", func() {
- Expect(len(ps.Connections)).To(Equal(0))
- for i := 1; i <= 50; i++ {
- ps.Listen(ctx, "community-points-channel-v1", 1, i)
- }
- Expect(len(ps.Connections)).To(Equal(1))
- ps.Listen(ctx, "community-points-channel-v1", 2, 1)
- Expect(len(ps.Connections)).To(Equal(2))
- Expect(ps.Topics()).To(ContainElements(
- "community-points-channel-v1.2.1",
- ))
- })
- })
- Context("HasTopic", func() {
- It("checks topics", func() {
- Expect(len(ps.Connections)).To(Equal(0))
- ps.Listen(ctx, "community-points-channel-v1", 1)
- Expect(ps.HasTopic("unknown")).To(BeFalse())
- Expect(ps.HasTopic("community-points-channel-v1", 1)).To(BeTrue())
- })
- })
- Context("TopicsCount", func() {
- It("return topics count", func() {
- Expect(ps.TopicsCount()).To(Equal(0))
- for i := 1; i <= 50; i++ {
- ps.Listen(ctx, "community-points-channel-v1", 1, i)
- }
- Expect(ps.TopicsCount()).To(Equal(50))
- for i := 1; i <= 5; i++ {
- ps.Listen(ctx, "community-points-channel-v1", 2, i)
- }
- Expect(ps.TopicsCount()).To(Equal(55))
- })
- })
- Context("Topic", func() {
- It("generate correct topic", func() {
- Expect(ps.Topic("channel-bits-events-v1.123")).To(Equal("channel-bits-events-v1.123"))
- Expect(ps.Topic("channel-bits-events-v1", 123)).To(Equal("channel-bits-events-v1.123"))
- Expect(ps.Topic("channel-bits-events-v1", "123")).To(Equal("channel-bits-events-v1.123"))
- Expect(ps.Topic("channel-bits-events-v1", 123, 456)).To(Equal("channel-bits-events-v1.123.456"))
- Expect(ps.Topic("channel-bits-events-v1", 123, "456")).To(Equal("channel-bits-events-v1.123.456"))
- Expect(ps.Topic("channel-bits-events-v1", "123", 456)).To(Equal("channel-bits-events-v1.123.456"))
- Expect(ps.Topic("channel-bits-events-v1", "123", "456")).To(Equal("channel-bits-events-v1.123.456"))
- Expect(ps.Topic("channel-bits-events-v1", 123, 456, 789)).To(Equal("channel-bits-events-v1.123.456.789"))
- })
- })
- })
- Context("Connection", func() {
- var c *pubsub.Connection
- BeforeEach(func() {
- c = pubsub.NewConnection(url.URL{Scheme: "ws", Host: "example.com", Path: ""})
- })
- AfterEach(func() {
- c.Close()
- })
- Context("AddTopic", func() {
- It("add new topic", func() {
- Expect(c.TopicsCount()).To(Equal(0))
- Expect(c.Topics()).To(Equal([]string{}))
- c.AddTopic("community-points-channel-v1.1")
- Expect(c.TopicsCount()).To(Equal(1))
- Expect(c.Topics()).To(ContainElements(
- "community-points-channel-v1.1",
- ))
- c.AddTopic("community-points-channel-v1.2")
- Expect(c.TopicsCount()).To(Equal(2))
- Expect(c.Topics()).To(ContainElements(
- "community-points-channel-v1.1",
- "community-points-channel-v1.2",
- ))
- })
- It("not add the same topics", func() {
- Expect(c.TopicsCount()).To(Equal(0))
- Expect(c.Topics()).To(Equal([]string{}))
- c.AddTopic("community-points-channel-v1.1")
- Expect(c.TopicsCount()).To(Equal(1))
- Expect(c.Topics()).To(ContainElements(
- "community-points-channel-v1.1",
- ))
- c.AddTopic("community-points-channel-v1.1")
- Expect(c.TopicsCount()).To(Equal(1))
- Expect(c.Topics()).To(ContainElements(
- "community-points-channel-v1.1",
- ))
- })
- It("add not more than 50 topics", func() {
- Expect(c.TopicsCount()).To(Equal(0))
- Expect(c.Topics()).To(Equal([]string{}))
- for i := 1; i <= 60; i++ {
- c.AddTopic(fmt.Sprintf("community-points-channel-v1.%d", i))
- }
- Expect(c.TopicsCount()).To(Equal(50))
- })
- })
- Context("RemoveTopic", func() {
- It("remove topic", func() {
- Expect(c.TopicsCount()).To(Equal(0))
- Expect(c.Topics()).To(Equal([]string{}))
- c.AddTopic("community-points-channel-v1.1")
- Expect(c.TopicsCount()).To(Equal(1))
- Expect(c.Topics()).To(ContainElements(
- "community-points-channel-v1.1",
- ))
- c.RemoveTopic("community-points-channel-v1.1")
- Expect(c.TopicsCount()).To(Equal(0))
- Expect(c.Topics()).To(Equal([]string{}))
- })
- })
- Context("RemoveAllTopics", func() {
- It("remove all topic", func() {
- Expect(c.TopicsCount()).To(Equal(0))
- Expect(c.Topics()).To(Equal([]string{}))
- c.AddTopic("community-points-channel-v1.1")
- c.AddTopic("community-points-channel-v1.2")
- Expect(c.TopicsCount()).To(Equal(2))
- Expect(c.Topics()).To(ContainElements(
- "community-points-channel-v1.1",
- "community-points-channel-v1.2",
- ))
- c.RemoveAllTopics()
- Expect(c.TopicsCount()).To(Equal(0))
- Expect(c.Topics()).To(Equal([]string{}))
- })
- })
- Context("Topics", func() {
- It("return topics", func() {
- Expect(c.TopicsCount()).To(Equal(0))
- Expect(c.Topics()).To(Equal([]string{}))
- c.AddTopic("community-points-channel-v1.1")
- Expect(c.TopicsCount()).To(Equal(1))
- Expect(c.Topics()).To(ContainElements(
- "community-points-channel-v1.1",
- ))
- })
- })
- Context("HasTopic", func() {
- It("checks topics", func() {
- Expect(c.TopicsCount()).To(Equal(0))
- Expect(c.Topics()).To(Equal([]string{}))
- c.AddTopic("community-points-channel-v1.1")
- Expect(c.HasTopic("unknown")).To(BeFalse())
- Expect(c.HasTopic("community-points-channel-v1.1")).To(BeTrue())
- })
- })
- Context("TopicsCount", func() {
- It("return topics count", func() {
- Expect(c.TopicsCount()).To(Equal(0))
- c.AddTopic("community-points-channel-v1.1")
- Expect(c.TopicsCount()).To(Equal(1))
- c.AddTopic("community-points-channel-v1.2")
- Expect(c.TopicsCount()).To(Equal(2))
- })
- })
- })
- })
- func TestSuite(t *testing.T) {
- RegisterFailHandler(Fail)
- RunSpecs(t, "PubSub")
- }
|