This repository has been archived by the owner on Sep 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 143
/
node_test_util.go
187 lines (163 loc) · 4.83 KB
/
node_test_util.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package holochain
import (
"context"
"fmt"
ic "github.com/libp2p/go-libp2p-crypto"
peer "github.com/libp2p/go-libp2p-peer"
pstore "github.com/libp2p/go-libp2p-peerstore"
"math/rand"
"os"
"testing"
)
// -------------------------------------------------------------------------------------------
// node testing functions
func makePeer(id string) (pid peer.ID, key ic.PrivKey) {
// use a constant reader so the key will be the same each time for the test...
var err error
key, _, err = ic.GenerateEd25519Key(MakeTestSeed(id))
if err != nil {
panic(err)
}
pid, _ = peer.IDFromPrivateKey(key)
return
}
func makeNode(port int, id string) (*Node, error) {
listenaddr := fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", port)
_, key := makePeer(id)
agent := LibP2PAgent{identity: AgentIdentity(id), priv: key, pub: key.GetPublic()}
return NewNode(listenaddr, "fakednahash", &agent, false, &debugLog)
}
func addTestPeers(h *Holochain, peers []peer.ID, start int, count int) []peer.ID {
for i := start; i < count; i++ {
p, _ := makePeer(fmt.Sprintf("peer_%d", i))
// fmt.Printf("Peer %d: %s\n", i, peer.IDB58Encode(p))
peers = append(peers, p)
pi := pstore.PeerInfo{ID: p}
err := h.addPeer(pi, false)
if err != nil {
panic(err)
}
}
return peers
}
func fullConnect(t *testing.T, ctx context.Context, nodes []*Holochain, nodesCount int) {
for i := 0; i < nodesCount; i++ {
for j := 0; j < nodesCount; j++ {
if j != i {
connect(t, ctx, nodes[i], nodes[j])
}
}
}
}
func ringConnect(t *testing.T, ctx context.Context, nodes []*Holochain, nodesCount int) {
for i := 0; i < nodesCount; i++ {
connect(t, ctx, nodes[i], nodes[(i+1)%len(nodes)])
}
}
func ringConnectMutual(t *testing.T, ctx context.Context, nodes []*Holochain, nodesCount int) {
for i := 0; i < nodesCount; i++ {
n2 := nodes[(i+1)%len(nodes)]
connect(t, ctx, nodes[i], n2)
connect(t, ctx, n2, nodes[i])
}
}
func starConnect(t *testing.T, ctx context.Context, nodes []*Holochain, nodesCount int) {
for i := 1; i < nodesCount; i++ {
connect(t, ctx, nodes[0], nodes[i])
}
}
func starConnectMutual(t *testing.T, ctx context.Context, nodes []*Holochain, nodesCount int) {
for i := 1; i < nodesCount; i++ {
connect(t, ctx, nodes[0], nodes[i])
connect(t, ctx, nodes[i], nodes[0])
}
}
func randConnect(t *testing.T, ctx context.Context, nodes []*Holochain, nodesCount, connectFromCount, connectToCount int) {
// connect nodes[1->connectFromCount] to connectToCount randomly selected nodes in
// nodes[(nodesCount-connectFromCount)->randConnect]
mrand := rand.New(rand.NewSource(42))
guy := nodes[0]
others := nodes[1:]
for i := 0; i < connectFromCount; i++ {
for j := 0; j < connectToCount; j++ { // 16, high enough to probably not have any partitions
v := mrand.Intn(nodesCount - connectFromCount - 1)
connect(t, ctx, others[i], others[connectFromCount+v])
}
}
for i := 0; i < connectFromCount; i++ {
connect(t, ctx, guy, others[i])
}
}
type multiNodeTest struct {
ctx context.Context
cancel context.CancelFunc
s *Service
d string
nodes []*Holochain
count int
}
func setupMultiNodeTesting(n int) (mt *multiNodeTest) {
ctx, cancel := context.WithCancel(context.Background())
d, s := SetupTestService()
mt = &multiNodeTest{
ctx: ctx,
cancel: cancel,
s: s,
d: d,
count: n,
}
mt.nodes = makeTestNodes(mt.ctx, mt.s, n)
return
}
func (mt *multiNodeTest) cleanupMultiNodeTesting() {
for i := 0; i < mt.count; i++ {
mt.nodes[i].Close()
}
mt.cancel()
CleanupTestDir(mt.d)
}
func makeTestNodes(ctx context.Context, s *Service, n int) (nodes []*Holochain) {
nodes = make([]*Holochain, n)
for i := 0; i < n; i++ {
nodeName := fmt.Sprintf("node%d", i)
os.Setenv("HCLOG_PREFIX", nodeName+"_")
nodes[i] = setupTestChain(nodeName, i, s)
nodes[i].Config.EnableMDNS = false
prepareTestChain(nodes[i])
}
for i := 0; i < n; i++ {
nodeName := fmt.Sprintf("node%d", i)
nodes[i].dht.dlog.Logf("SETUP: %s is %v", nodeName, nodes[i].nodeID)
}
os.Unsetenv("HCLOG_PREFIX")
return
}
func connectNoSync(t *testing.T, ctx context.Context, ah, bh *Holochain) {
a := ah.node
b := bh.node
idB := b.HashAddr
addrB := b.peerstore.Addrs(idB)
if len(addrB) == 0 {
t.Fatal("peers setup incorrectly: no local address")
}
pi := pstore.PeerInfo{ID: idB, Addrs: addrB}
err := ah.AddPeer(pi)
if err != nil {
t.Fatal(err)
}
if err = a.host.Connect(ctx, pi); err != nil {
t.Fatal(err)
}
}
func connect(t *testing.T, ctx context.Context, a, b *Holochain) {
connectNoSync(t, ctx, a, b)
// loop until connection notification has been received.
// under high load, this may not happen as immediately as we would like.
/* for a.node.routingTable.Find(b.nodeID) == "" {
time.Sleep(time.Millisecond * 5)
}
for b.node.routingTable.Find(a.nodeID) == "" {
time.Sleep(time.Millisecond * 5)
}*/
// time.Sleep(100 * time.Millisecond)
}