-
Notifications
You must be signed in to change notification settings - Fork 4
/
pd_test.go
131 lines (107 loc) · 2.37 KB
/
pd_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package pd
import (
"log"
"testing"
"time"
)
func TestBasicFunction(t *testing.T) {
ser := NewPubsub()
c1 := ser.Subscribe("ch1")
ser.Publish([]byte("test1"), "ch1")
if _, ok := <-c1; !ok {
t.Error(" Error found on subscribed.\n")
}
}
func TestTwoSubscribetor(t *testing.T) {
ser := NewPubsub()
c1 := ser.Subscribe("ch1")
c2 := ser.Subscribe("ch2")
ser.Publish([]byte("test2"), "ch1")
ser.Publish([]byte("test1"), "ch2")
val, ok := <-c1
if !ok || string(val) != "test2" {
t.Errorf("Error found \n")
}
val, ok = <-c2
if !ok || string(val) != "test1" {
t.Errorf("Error found \n")
}
}
func TestAddSub(t *testing.T) {
ser := NewPubsub()
c1 := ser.Subscribe("ch1")
ser.AddSubscription(c1, "ch2")
ser.Publish([]byte("test2"), "ch2")
if val, ok := <-c1; !ok {
t.Errorf("error on c1:%v", val)
}
}
func TestRemoveSub(t *testing.T) {
ser := NewPubsub()
c1 := ser.Subscribe("ch1", "ch2")
ser.Publish([]byte("test1"), "ch2")
if val, ok := <-c1; !ok {
t.Errorf("error on addsub c1:%v", val)
}
ser.RemoveSubscription(c1, "ch1")
ser.Publish([]byte("test2"), "ch1")
select {
case val := <-c1:
t.Errorf("Should not get %v notify on remove topic\n", val)
break
case <-time.After(time.Second):
break
}
}
func TestTopicList(t *testing.T) {
ser := NewPubsub()
t0 := ser.ListTopics()
if len(t0) != 0 {
t.Error("List error on empty")
}
c1 := ser.Subscribe("ch1", "ch2")
t1 := ser.ListTopics()
if len(t1) != 2 {
t.Error("List error on basic count")
}
ser.AddSubscription(c1, "ch1", "ch2", "ch3")
t2 := ser.ListTopics()
if len(t2) != 3 {
t.Error("List add error")
}
ser.RemoveSubscription(c1, "ch1")
t3 := ser.ListTopics()
if len(t3) != 2 {
t.Error("List remove error")
}
}
func BenchmarkAddSub(b *testing.B) {
big := NewPubsub()
b.ResetTimer()
for i := 0; i < b.N; i++ {
big.Subscribe("1234567890")
}
}
func BenchmarkRemoveSub(b *testing.B) {
big := NewPubsub()
var subChans []chan []byte
b.ResetTimer()
for i := 0; i < b.N; i++ {
c1 := big.Subscribe("1234567890")
subChans = append(subChans, c1)
}
b.ResetTimer()
for _, v := range subChans {
big.RemoveSubscription(v, "1234567890")
}
}
func BenchmarkBasicFunction(b *testing.B) {
ser := NewPubsub()
c1 := ser.Subscribe("ch1")
for i := 0; i < b.N; i++ {
ser.Publish([]byte("test1"), "ch1")
if _, ok := <-c1; !ok {
log.Println(" Error found on subscribed.")
}
}
}