-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Expand file tree
/
Copy pathnodes.go
More file actions
59 lines (51 loc) · 1.3 KB
/
nodes.go
File metadata and controls
59 lines (51 loc) · 1.3 KB
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
package harness
import (
"sync"
. "github.com/ipfs/kubo/test/cli/testutils"
"github.com/multiformats/go-multiaddr"
)
// Nodes is a collection of Kubo nodes along with operations on groups of nodes.
type Nodes []*Node
func (n Nodes) Init(args ...string) Nodes {
ForEachPar(n, func(node *Node) { node.Init(args...) })
return n
}
func (n Nodes) ForEachPar(f func(*Node)) {
var wg sync.WaitGroup
for _, node := range n {
wg.Add(1)
node := node
go func() {
defer wg.Done()
f(node)
}()
}
wg.Wait()
}
// Connect establishes connections between all nodes in the collection
func (n Nodes) Connect() Nodes {
for i, node := range n {
for j, otherNode := range n {
if i == j {
continue
}
// Do not connect in parallel, because that can cause TLS handshake problems on some platforms.
node.Connect(otherNode)
}
}
for _, node := range n {
firstPeer := node.Peers()[0]
if _, err := firstPeer.ValueForProtocol(multiaddr.P_P2P); err != nil {
log.Panicf("unexpected state for node %d with peer ID %s: %s", node.ID, node.PeerID(), err)
}
}
return n
}
func (n Nodes) StartDaemons(args ...string) Nodes {
ForEachPar(n, func(node *Node) { node.StartDaemon(args...) })
return n
}
func (n Nodes) StopDaemons() Nodes {
ForEachPar(n, func(node *Node) { node.StopDaemon() })
return n
}