-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathservice_test.go
73 lines (60 loc) · 1.12 KB
/
service_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
package main
import (
"context"
"runtime"
"testing"
"time"
)
func TestNew(t *testing.T) {
s := newService(runtime.NumCPU())
if s == nil {
t.Fail()
}
}
func TestSubscribePublish(t *testing.T) {
s := newService(runtime.NumCPU())
ctx := context.Background()
c := make(chan error)
handler := func(ctx context.Context, _ []byte) {
c <- nil
}
if err := s.Subscribe("topic", handler); err != nil {
t.Fatal(err)
}
s.Publish(ctx, "topic", []byte("ok"))
ctxDoneCh := ctx.Done()
for {
select {
case <-ctxDoneCh:
t.Fatal(ctx.Err())
return
case err := <-c:
if err != nil {
t.Error(err)
}
return
}
}
}
func TestUnsubscribe(t *testing.T) {
s := newService(runtime.NumCPU())
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
handler := func(ctx context.Context, _ []byte) {
t.Fail()
}
if err := s.Subscribe("topic", handler); err != nil {
t.Fatal(err)
}
if err := s.Unsubscribe("topic", handler); err != nil {
t.Fatal(err)
}
s.Publish(ctx, "topic", []byte("ok"))
ctxDoneCh := ctx.Done()
for {
select {
case <-ctxDoneCh:
return
}
}
}