-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmulticast_test.go
118 lines (101 loc) · 1.83 KB
/
multicast_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
package multicast_test
import (
"fmt"
"sync"
"github.com/reactivego/multicast"
)
func Example_fastSend1x2() {
ch := multicast.NewChan(128, 2)
// FastSend allows only a single goroutine sending and does not store
// timestamps with messages.
ch.FastSend("Hello")
ch.FastSend("World!")
ch.Close(nil)
if ch.Closed() {
fmt.Println("channel closed")
}
print := func(value interface{}, err error, closed bool) bool {
switch {
case !closed:
fmt.Println(value)
case err != nil:
fmt.Println(err)
default:
fmt.Println("closed")
}
return true
}
var wg sync.WaitGroup
wg.Add(2)
ep1, _ := ch.NewEndpoint(multicast.ReplayAll)
go func() {
ep1.Range(print, 0)
wg.Done()
}()
ep2, _ := ch.NewEndpoint(multicast.ReplayAll)
go func() {
ep2.Range(print, 0)
wg.Done()
}()
wg.Wait()
// Unordered Output:
// channel closed
// Hello
// Hello
// World!
// World!
// closed
// closed
}
func Example_send2x2() {
ch := multicast.NewChan(128, 2)
// Send suppports multiple goroutine sending and stores a timestamp with
// every message sent.
var wgs sync.WaitGroup
wgs.Add(2)
go func() {
ch.Send("Hello")
wgs.Done()
}()
go func() {
ch.Send("World!")
wgs.Done()
}()
print := func(value interface{}, err error, closed bool) bool {
switch {
case !closed:
fmt.Println(value)
case err != nil:
fmt.Println(err)
default:
fmt.Println("closed")
}
return true
}
var wgr sync.WaitGroup
wgr.Add(2)
ep1, _ := ch.NewEndpoint(multicast.ReplayAll)
go func() {
ep1.Range(print, 0)
wgr.Done()
}()
ep2, _ := ch.NewEndpoint(multicast.ReplayAll)
go func() {
ep2.Range(print, 0)
wgr.Done()
}()
wgs.Wait()
ch.Close(nil)
if ch.Closed() {
fmt.Println("channel closed")
}
wgr.Wait()
// Unordered Output:
// Hello
// Hello
// World!
// World!
// closed
// closed
// channel closed
}