-
Notifications
You must be signed in to change notification settings - Fork 0
/
spread_test.go
58 lines (45 loc) · 1.04 KB
/
spread_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
package spread_test
import (
"context"
"fmt"
"log"
"time"
"github.com/egemengol/spread"
)
func Example() {
ctx, cancel := context.WithCancel(context.Background())
t := spread.NewTopic[int](ctx, 5)
removeSync, _ := t.HandleSync(func(data int) {
fmt.Printf("sync handler: %d\n", data)
})
recvChan, removeRecvChan, _ := t.GetRecvChannel(2)
go func() {
for data := range recvChan {
fmt.Printf("recv channel: %d\n", data)
}
}()
t.Publish(1)
time.Sleep(100 * time.Millisecond)
removeRecvChan()
time.Sleep(100 * time.Millisecond)
t.Publish(2)
time.Sleep(100 * time.Millisecond)
removeSync()
t.HandleAsync(func(_ctx context.Context, data int) {
fmt.Printf("async handler: %d\n", data)
})
time.Sleep(100 * time.Millisecond)
t.Publish(3)
time.Sleep(100 * time.Millisecond)
cancel()
time.Sleep(100 * time.Millisecond)
err := t.Publish(4)
if err == nil {
log.Fatal("should not be able to publish after closing the topic")
}
// Output:
// sync handler: 1
// recv channel: 1
// sync handler: 2
// async handler: 3
}