kubo/test/cli/harness/nodes.go
Gus Eggert 676e557daf test: port peering test from sharness to Go
This is the slowest test in the sharness test suite, because it has
very long sleeps. It usually takes 2+ minutes to run.

This new impl runs all peering tests in about 20 seconds, since it
polls for conditions instead of sleeping, and runs the tests in
parallel.

This also has an additional test case for a peer that was never online
and then connects.
2023-03-13 10:54:53 -04:00

69 lines
1.3 KiB
Go

package harness
import (
"sync"
. "github.com/ipfs/kubo/test/cli/testutils"
"github.com/multiformats/go-multiaddr"
"golang.org/x/sync/errgroup"
)
// 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)) {
group := &errgroup.Group{}
for _, node := range n {
node := node
group.Go(func() error {
f(node)
return nil
})
}
err := group.Wait()
if err != nil {
panic(err)
}
}
func (n Nodes) Connect() Nodes {
wg := sync.WaitGroup{}
for i, node := range n {
for j, otherNode := range n {
if i == j {
continue
}
node := node
otherNode := otherNode
wg.Add(1)
go func() {
defer wg.Done()
node.Connect(otherNode)
}()
}
}
wg.Wait()
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() Nodes {
ForEachPar(n, func(node *Node) { node.StartDaemon() })
return n
}
func (n Nodes) StopDaemons() Nodes {
ForEachPar(n, func(node *Node) { node.StopDaemon() })
return n
}