master
go 58 lines 1.23 KB
Raw
1 package harness
2
3 import (
4 "sync"
5
6 . "github.com/ipfs/kubo/test/cli/testutils"
7 "github.com/multiformats/go-multiaddr"
8 )
9
10 // Nodes is a collection of Kubo nodes along with operations on groups of nodes.
11 type Nodes []*Node
12
13 func (n Nodes) Init(args ...string) Nodes {
14 ForEachPar(n, func(node *Node) { node.Init(args...) })
15 return n
16 }
17
18 func (n Nodes) ForEachPar(f func(*Node)) {
19 var wg sync.WaitGroup
20 for _, node := range n {
21 wg.Add(1)
22 node := node
23 go func() {
24 defer wg.Done()
25 f(node)
26 }()
27 }
28 wg.Wait()
29 }
30
31 func (n Nodes) Connect() Nodes {
32 for i, node := range n {
33 for j, otherNode := range n {
34 if i == j {
35 continue
36 }
37 // Do not connect in parallel, because that can cause TLS handshake problems on some platforms.
38 node.Connect(otherNode)
39 }
40 }
41 for _, node := range n {
42 firstPeer := node.Peers()[0]
43 if _, err := firstPeer.ValueForProtocol(multiaddr.P_P2P); err != nil {
44 log.Panicf("unexpected state for node %d with peer ID %s: %s", node.ID, node.PeerID(), err)
45 }
46 }
47 return n
48 }
49
50 func (n Nodes) StartDaemons(args ...string) Nodes {
51 ForEachPar(n, func(node *Node) { node.StartDaemon(args...) })
52 return n
53 }
54
55 func (n Nodes) StopDaemons() Nodes {
56 ForEachPar(n, func(node *Node) { node.StopDaemon() })
57 return n
58 }