-
Notifications
You must be signed in to change notification settings - Fork 0
/
pubsub_test.go
158 lines (107 loc) · 3.05 KB
/
pubsub_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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package pubsub_test
import (
"context"
"log"
"strconv"
"sync"
"testing"
"time"
"github.com/arunmurugan78/pubsub"
)
type Func func(message pubsub.Message, expectedChannelName string, expectedValue interface{})
func getAsserter(t *testing.T) Func {
return func(message pubsub.Message, expectedChannelName string, expectedValue interface{}) {
if message.ChannelName != expectedChannelName {
log.Fatalf("Expected %s channel, got %s \n", expectedChannelName, message.ChannelName)
}
if message.Value != expectedValue {
log.Fatalf("Expected %s Value, got %s \n", expectedValue, message.Value)
}
}
}
func TestPubSub(t *testing.T) {
assert := getAsserter(t)
t.Run("Should be able to publish and subscribe to channels and should be able to receive published values", func(t *testing.T) {
ctx := context.Background()
p := pubsub.New(ctx)
sub := p.NewSubscription([]string{"one", "two", "three"})
p.Publish([]string{"one"}, 100)
ch := sub.Channel()
message := <-ch
assert(message, "one", 100)
p.Publish([]string{"two"}, 201)
message = <-ch
assert(message, "two", 201)
p.Publish([]string{"four"}, 300)
p.Publish([]string{"three"}, 400)
message = <-ch
assert(message, "three", 400)
})
t.Run("should not receive published changes after un subscribe", func(t *testing.T) {
ctx := context.Background()
p := pubsub.New(ctx)
sub := p.NewSubscription([]string{"one", "two", "three", "four"})
ch := sub.Channel()
timeout := time.After(3 * time.Second)
sub.UnSubscribe()
p.Publish([]string{"one"}, 101)
select {
case <-ch:
log.Fatalf("Got published updates")
case <-timeout:
return
}
})
t.Run("should not receive published changes after context is canceled", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
p := pubsub.New(ctx)
sub := p.NewSubscription([]string{"one", "two", "three", "four"})
ch := sub.Channel()
timeout := time.After(3 * time.Second)
cancel()
p.Publish([]string{"one"}, 101)
time.Sleep(1 * time.Second)
for {
select {
case <-ch:
log.Fatalf("Got published updates after calling cancel")
case <-timeout:
return
}
}
})
}
func TestPubSubWorstCase(t *testing.T) {
for i :=1; i<30; i++ {
var wg sync.WaitGroup
ctx := context.Background()
pbsb := pubsub.New(ctx)
channels := make([]string, 0)
subs := make([]*pubsub.Subscription, 0)
for j:=0;j<i;j++ {
channels = append(channels, strconv.Itoa(j))
subs = append(subs, pbsb.NewSubscription(channels))
}
channels = make([]string, 0)
for j:=0;j<i;j++ {
channels = append(channels, strconv.Itoa(j))
pbsb.Publish(channels, j)
for k:=j; k<i;k ++ {
wg.Add(1)
go func (idx int, j int, l int){
defer wg.Done()
for c := 0; c< l-len(subs[idx].GetSubscribedChannels());c++ {
message := <-subs[idx].Channel()
if message.Value != j {
log.Fatalf("Expected %d but go %d", j, message.Value)
}
}
}(k, j, len(channels))
}
wg.Wait()
}
for _, sub := range subs {
sub.UnSubscribe()
}
}
}