-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
nodes.go
70 lines (60 loc) · 1.63 KB
/
nodes.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
package testnet
import (
"errors"
"fmt"
"time"
"github.com/cometbft/cometbft/node"
)
// Nodes is a slice of comet nodes,
// with some additional convenience methods.
//
// Nodes may contain nil elements,
// so that a partially failed call to NewNetwork
// can still be properly cleaned up.
type Nodes []*node.Node
// Stop stops each node sequentially.
// All errors occurring during stop are returned as a joined error.
//
// Nil elements in ns are skipped.
func (ns Nodes) Stop() error {
var errs []error
for i, n := range ns {
if n == nil {
continue
}
if err := n.Stop(); err != nil {
errs = append(errs, fmt.Errorf("failed to stop node %d: %w", i, err))
}
}
if len(errs) > 0 {
return errors.Join(errs...)
}
return nil
}
// Wait blocks until every node in ns has completely stopped.
//
// Nil elements in ns are skipped.
func (ns Nodes) Wait() {
for _, n := range ns {
if n == nil {
continue
}
n.Wait()
}
}
// StopAndWait is shorthand for calling both Stop() and Wait(),
// useful as a deferred call in tests.
func (ns Nodes) StopAndWait() error {
err := ns.Stop()
ns.Wait()
// TODO(mr): remove this sleep call after we are using a version of Comet
// that includes a fix for https://github.com/cometbft/cometbft/issues/646.
//
// On my machine, this sleep appears to completely eliminate the late file write.
// It also almost always works around https://github.com/cometbft/cometbft/issues/652.
//
// Presumably the fix for those two issues will be included in a v0.37.1 release.
// If not, I assume they will be part of the first v0.38 series release.
time.Sleep(250 * time.Millisecond)
return err
}