-
Notifications
You must be signed in to change notification settings - Fork 3
/
orbitdb_test.go
97 lines (71 loc) · 1.56 KB
/
orbitdb_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
package orbitdb
import (
"fmt"
"time"
"github.com/keks/go-ipfs-colog"
)
const topic = "testTopic"
func assert(check bool, args ...interface{}) {
if !check {
fmt.Print("assert: ")
fmt.Println(args...)
}
}
func ExampleOrbitDB_twoParty() {
s1, err := NewOrbitDB(topic)
assert(err == nil, err)
time.Sleep(5 * time.Millisecond)
s2, err := NewOrbitDB(topic)
assert(err == nil, err)
fmt.Println("s1.colog.Watch()")
eCh := s1.colog.Watch()
fmt.Println(`s2.Add("foo")`)
e1, err := s2.Add("foo")
if err != nil {
fmt.Println(err)
}
var e2 *colog.Entry
fmt.Println("<-eCh")
e2 = <-eCh
assert(e2.GetString() == e1.GetString(), "e1!=e2", e1, e2)
fmt.Println("ok.")
// Output:
// s1.colog.Watch()
// s2.Add("foo")
// <-eCh
// ok.
}
func ExampleOrbitDB_threeParty() {
s1, err := NewOrbitDB(topic)
assert(err == nil, err)
time.Sleep(5 * time.Millisecond)
s2, err := NewOrbitDB(topic)
assert(err == nil, err)
time.Sleep(5 * time.Millisecond)
s3, err := NewOrbitDB(topic)
assert(err == nil, err)
fmt.Println("s1.colog.Watch()")
eCh1 := s1.colog.Watch()
fmt.Println("s2.colog.Watch()")
eCh2 := s2.colog.Watch()
fmt.Println(`s3.Add("foo")`)
e1, err := s3.Add("foo")
if err != nil {
fmt.Println(err)
}
var e2, e3 *colog.Entry
fmt.Println("<-eCh1")
e2 = <-eCh1
fmt.Println("<-eCh2")
e3 = <-eCh2
assert(e2.GetString() == e1.GetString(), "e1!=e2", e1, e2)
assert(e2.GetString() == e3.GetString(), "e2!=e3", e2, e3)
fmt.Println("ok.")
// Output:
// s1.colog.Watch()
// s2.colog.Watch()
// s3.Add("foo")
// <-eCh1
// <-eCh2
// ok.
}