-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
43 lines (33 loc) · 966 Bytes
/
example_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
package subscriber
import "fmt"
func Example_basic() {
var s subscriber
if s.Subscribed() == s.Canceled() {
fmt.Println("subscriber can't be both subscribed and canceled")
}
if s.Canceled() {
fmt.Println("subscriber should be subscribed by default")
}
s.Unsubscribe()
if s.Subscribed() {
fmt.Println("subscriber should be canceled after Unsubscribe call")
}
fmt.Println("OK")
// Output: OK
}
func Example_subscriberLoop() {
parent := &subscriber{}
child1 := parent.Add()
child2 := parent.Add()
child2.OnUnsubscribe(parent.Unsubscribe)
child3 := parent.Add()
if parent.Canceled() || child1.Canceled() || child2.Canceled() || child3.Canceled() {
fmt.Println("none of the subscribers should be cancelled here")
}
child2.Unsubscribe()
if parent.Subscribed() || child1.Subscribed() || child2.Subscribed() || child3.Subscribed() {
fmt.Println("all of the subscribers should be cancelled here")
}
fmt.Println("OK")
// Output: OK
}