-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpubsub_test.go
More file actions
45 lines (42 loc) · 740 Bytes
/
pubsub_test.go
File metadata and controls
45 lines (42 loc) · 740 Bytes
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
package algorithm
import (
"fmt"
"sync"
"testing"
"time"
)
func TestPubSub(t *testing.T) {
pub := NewPublisher(10, 3*time.Second)
defer pub.Close()
strSub := pub.Subcrib(func(v interface{}) bool {
_, ok := v.(string)
return ok
})
intSub := pub.Subcrib(func(v interface{}) bool {
_, ok := v.(int)
return ok
})
allSub := pub.Subcrib(nil)
var wg sync.WaitGroup
wg.Add(3)
go func() {
defer wg.Done()
v := <-strSub
fmt.Println("str subcrib:", v)
}()
go func() {
defer wg.Done()
v := <-intSub
fmt.Println("int subcrib:", v)
}()
go func() {
defer wg.Done()
for i := 0; i < 2; i++ {
v := <-allSub
fmt.Println("all subcrib:", v)
}
}()
pub.Publish("hello string")
pub.Publish(123)
wg.Wait()
}