Skip to content

Commit ce11d86

Browse files
committed
[tmpnet] Move WaitForHealthy method to tmpnet.Node
1 parent dae0f00 commit ce11d86

File tree

5 files changed

+38
-33
lines changed

5 files changed

+38
-33
lines changed

tests/e2e/faultinjection/duplicate_node_id.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ var _ = ginkgo.Describe("Duplicate node handling", func() {
4444
_ = e2e.AddEphemeralNode(tc, network, node2)
4545

4646
tc.By("checking that the second new node fails to become healthy before timeout")
47-
err := tmpnet.WaitForHealthy(tc.DefaultContext(), node2)
47+
err := node2.WaitForHealthy(tc.DefaultContext())
4848
require.ErrorIs(err, context.DeadlineExceeded)
4949

5050
tc.By("stopping the first new node")

tests/fixture/e2e/helpers.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ func WaitForHealthy(t require.TestingT, node *tmpnet.Node) {
154154
// Need to use explicit context (vs DefaultContext()) to support use with DeferCleanup
155155
ctx, cancel := context.WithTimeout(context.Background(), DefaultTimeout)
156156
defer cancel()
157-
require.NoError(t, tmpnet.WaitForHealthy(ctx, node))
157+
require.NoError(t, node.WaitForHealthy(ctx))
158158
}
159159

160160
// Sends an eth transaction and waits for the transaction receipt from the
@@ -248,7 +248,7 @@ func CheckBootstrapIsPossible(tc tests.TestContext, network *tmpnet.Network) *tm
248248
})
249249

250250
// Check that the node becomes healthy within timeout
251-
require.NoError(tmpnet.WaitForHealthy(tc.DefaultContext(), node))
251+
require.NoError(node.WaitForHealthy(tc.DefaultContext()))
252252

253253
// Ensure that the primary validators are still healthy
254254
for _, node := range network.Nodes {

tests/fixture/tmpnet/network.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ func (n *Network) RestartNode(ctx context.Context, node *Node) error {
507507
n.log.Info("waiting for node to report healthy",
508508
zap.Stringer("nodeID", node.NodeID),
509509
)
510-
return WaitForHealthy(ctx, node)
510+
return node.WaitForHealthy(ctx)
511511
}
512512

513513
// Stops all nodes in the network.

tests/fixture/tmpnet/node.go

+34
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package tmpnet
66
import (
77
"context"
88
"encoding/base64"
9+
"errors"
910
"fmt"
1011
"io"
1112
"net"
@@ -14,6 +15,8 @@ import (
1415
"strings"
1516
"time"
1617

18+
"go.uber.org/zap"
19+
1720
"github.com/ava-labs/avalanchego/config"
1821
"github.com/ava-labs/avalanchego/ids"
1922
"github.com/ava-labs/avalanchego/staking"
@@ -335,3 +338,34 @@ func (n *Node) SaveAPIPort() error {
335338
n.Flags[config.HTTPPortKey] = port
336339
return nil
337340
}
341+
342+
// WaitForHealthy blocks until node health is true or an error (including context timeout) is observed.
343+
func (n *Node) WaitForHealthy(ctx context.Context) error {
344+
if _, ok := ctx.Deadline(); !ok {
345+
return fmt.Errorf("unable to wait for health for node %q with a context without a deadline", n.NodeID)
346+
}
347+
ticker := time.NewTicker(DefaultNodeTickerInterval)
348+
defer ticker.Stop()
349+
350+
for {
351+
healthy, err := n.IsHealthy(ctx)
352+
switch {
353+
case errors.Is(err, ErrUnrecoverableNodeHealthCheck):
354+
return fmt.Errorf("%w for node %q", err, n.NodeID)
355+
case err != nil:
356+
n.network.log.Verbo("failed to query node health",
357+
zap.Stringer("nodeID", n.NodeID),
358+
zap.Error(err),
359+
)
360+
continue
361+
case healthy:
362+
return nil
363+
}
364+
365+
select {
366+
case <-ctx.Done():
367+
return fmt.Errorf("failed to wait for health of node %q before timeout: %w", n.NodeID, ctx.Err())
368+
case <-ticker.C:
369+
}
370+
}
371+
}

tests/fixture/tmpnet/utils.go

-29
Original file line numberDiff line numberDiff line change
@@ -47,35 +47,6 @@ func CheckNodeHealth(ctx context.Context, uri string) (*health.APIReply, error)
4747
return nil, fmt.Errorf("%w: %w", ErrUnrecoverableNodeHealthCheck, err)
4848
}
4949

50-
// WaitForHealthy blocks until Node.IsHealthy returns true or an error (including context timeout) is observed.
51-
func WaitForHealthy(ctx context.Context, node *Node) error {
52-
if _, ok := ctx.Deadline(); !ok {
53-
return fmt.Errorf("unable to wait for health for node %q with a context without a deadline", node.NodeID)
54-
}
55-
ticker := time.NewTicker(DefaultNodeTickerInterval)
56-
defer ticker.Stop()
57-
58-
for {
59-
healthy, err := node.IsHealthy(ctx)
60-
switch {
61-
case errors.Is(err, ErrUnrecoverableNodeHealthCheck):
62-
return fmt.Errorf("%w for node %q", err, node.NodeID)
63-
case err != nil:
64-
// Error is recoverable
65-
// TODO(marun) Log the error to aid in troubleshooting once a logger is available
66-
continue
67-
case healthy:
68-
return nil
69-
}
70-
71-
select {
72-
case <-ctx.Done():
73-
return fmt.Errorf("failed to wait for health of node %q before timeout: %w", node.NodeID, ctx.Err())
74-
case <-ticker.C:
75-
}
76-
}
77-
}
78-
7950
// NodeURI associates a node ID with its API URI.
8051
type NodeURI struct {
8152
NodeID ids.NodeID

0 commit comments

Comments
 (0)