Browse Source

Implement PubSub.TopicsCount

Volodymyr Tkach 2 years ago
parent
commit
ea72e5ce0f
2 changed files with 29 additions and 0 deletions
  1. 14 0
      pubsub/pubsub.go
  2. 15 0
      pubsub/pubsub_test.go

+ 14 - 0
pubsub/pubsub.go

@@ -175,6 +175,20 @@ func (p *PubSub) HasTopic(topic string, params ...interface{}) bool {
 	return false
 }
 
+// TopicsCount return count of topics.
+func (p *PubSub) TopicsCount() int {
+	p.Lock()
+	defer p.Unlock()
+
+	count := 0
+
+	for _, c := range p.Connections {
+		count += c.TopicsCount()
+	}
+
+	return count
+}
+
 // Topic generate correct topic for API.
 // Params can be as number or string.
 //

+ 15 - 0
pubsub/pubsub_test.go

@@ -99,6 +99,21 @@ var _ = Describe("PubSub", func() {
 			})
 		})
 
+		Context("TopicsCount", func() {
+			It("return topics count", func() {
+				Expect(ps.TopicsCount()).To(Equal(0))
+				for i := 1; i <= 50; i++ {
+					ps.Listen("community-points-channel-v1", 1, i)
+				}
+				Expect(ps.TopicsCount()).To(Equal(50))
+
+				for i := 1; i <= 5; i++ {
+					ps.Listen("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"))