forked from haadcode/go-ipfs-log
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatch_test.go
85 lines (60 loc) · 963 Bytes
/
watch_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
package colog
import (
"fmt"
)
func ExampleCoLog_Watch() {
l1 := New(ipfsdb)
l2 := New(ipfsdb)
l3 := New(ipfsdb)
l4 := New(ipfsdb)
ch := l1.Watch()
wait := make(chan struct{})
go func() {
for e := range ch {
fmt.Println(e.GetString())
wait <- struct{}{}
}
}()
l1.Add("abcdef")
<-wait
l2.Join(l1)
l2.Add("ghijk")
l1.Join(l2)
<-wait
l3.Join(l1)
e3 := checkadd(l3.Add("lmnop"))
l1.FetchFromHead(e3.Hash)
<-wait
e4 := checkadd(l4.Add("qrstu"))
l1.FetchFromHead(e4.Hash)
<-wait
//Output:
//abcdef
//ghijk
//lmnop
//qrstu
}
func ExampleCoLog_Unwatch() {
l1 := New(ipfsdb)
l2 := New(ipfsdb)
ch := l1.Watch()
wait := make(chan struct{})
go func() {
for e := range ch {
fmt.Println(e.GetString())
wait <- struct{}{}
}
}()
l1.Add("abcdef")
<-wait
ch2 := l1.Watch()
l1.Unwatch(ch)
l2.Join(l1)
l2.Add("ghijk")
l1.Join(l2)
<-ch2
fmt.Println(l1.chans.Count())
//Output:
// abcdef
// 1
}