Skip to content

Commit 8ff5e04

Browse files
committed
fixup: Make WaitForHealthy a function instead of an Node interface method
1 parent 912fb73 commit 8ff5e04

File tree

7 files changed

+49
-37
lines changed

7 files changed

+49
-37
lines changed

tests/e2e/e2e.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,5 +139,5 @@ func AddEphemeralNode(network testnet.Network, flags testnet.FlagsMap) testnet.N
139139
func WaitForHealthy(node testnet.Node) {
140140
ctx, cancel := context.WithTimeout(context.Background(), DefaultTimeout)
141141
defer cancel()
142-
require.NoError(ginkgo.GinkgoT(), node.WaitForHealthy(ctx))
142+
require.NoError(ginkgo.GinkgoT(), testnet.WaitForHealthy(ctx, node))
143143
}

tests/e2e/faultinjection/duplicate_node_id.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ var _ = ginkgo.Describe("Duplicate node handling", func() {
4747
ginkgo.By("checking that the second new node fails to become healthy before timeout")
4848
ctx, cancel := context.WithTimeout(context.Background(), e2e.DefaultNodeStartTimeout)
4949
defer cancel()
50-
err := node2.WaitForHealthy(ctx)
50+
err := testnet.WaitForHealthy(ctx, node2)
5151
require.ErrorIs(err, context.DeadlineExceeded)
5252

5353
ginkgo.By("stopping the first new node")

tests/fixture/testnet/common.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
2+
// See the file LICENSE for licensing terms.
3+
4+
package testnet
5+
6+
import (
7+
"context"
8+
"errors"
9+
"fmt"
10+
"time"
11+
)
12+
13+
const (
14+
DefaultNodeTickerInterval = 50 * time.Millisecond
15+
)
16+
17+
var ErrNotRunning = errors.New("not running")
18+
19+
// WaitForHealthy blocks until Node.IsHealthy returns true or an error (including context timeout) is observed.
20+
func WaitForHealthy(ctx context.Context, node Node) error {
21+
if _, ok := ctx.Deadline(); !ok {
22+
return fmt.Errorf("unable to wait for health for node %q with a context without a deadline", node.GetID())
23+
}
24+
ticker := time.NewTicker(DefaultNodeTickerInterval)
25+
defer ticker.Stop()
26+
27+
for {
28+
healthy, err := node.IsHealthy(ctx)
29+
if err != nil && !errors.Is(err, ErrNotRunning) {
30+
return fmt.Errorf("failed to wait for health of node %q: %w", node.GetID(), err)
31+
}
32+
if healthy {
33+
return nil
34+
}
35+
36+
select {
37+
case <-ctx.Done():
38+
return fmt.Errorf("failed to wait for health of node %q before timeout: %w", node.GetID(), ctx.Err())
39+
case <-ticker.C:
40+
}
41+
}
42+
}

tests/fixture/testnet/interfaces.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,5 @@ type Node interface {
2424
GetConfig() NodeConfig
2525
GetProcessContext() node.NodeProcessContext
2626
IsHealthy(ctx context.Context) (bool, error)
27-
WaitForHealthy(ctx context.Context) error
2827
Stop() error
2928
}

tests/fixture/testnet/local/config.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ const (
2020
DefaultNetworkStartTimeout = 2 * time.Minute
2121
DefaultNodeInitTimeout = 10 * time.Second
2222
DefaultNodeStopTimeout = 5 * time.Second
23-
DefaultNodeTickerInterval = 50 * time.Millisecond
2423
)
2524

2625
// A set of flags appropriate for local testing.

tests/fixture/testnet/local/network.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ func (ln *LocalNetwork) WaitForHealthy(ctx context.Context, w io.Writer) error {
385385
}
386386

387387
healthy, err := node.IsHealthy(ctx)
388-
if err != nil && !errors.Is(err, errProcessNotRunning) {
388+
if err != nil && !errors.Is(err, testnet.ErrNotRunning) {
389389
return err
390390
}
391391
if !healthy {

tests/fixture/testnet/local/node.go

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@ import (
2727
"github.com/ava-labs/avalanchego/utils/perms"
2828
)
2929

30-
var (
31-
errProcessNotRunning = errors.New("process not running")
32-
errNodeAlreadyRunning = errors.New("failed to start local node: node is already running")
33-
)
30+
var errNodeAlreadyRunning = errors.New("failed to start local node: node is already running")
3431

3532
// Defines local-specific node configuration. Supports setting default
3633
// and node-specific values.
@@ -268,7 +265,7 @@ func (n *LocalNode) Stop() error {
268265
}
269266

270267
// Wait for the node process to stop
271-
ticker := time.NewTicker(DefaultNodeTickerInterval)
268+
ticker := time.NewTicker(testnet.DefaultNodeTickerInterval)
272269
defer ticker.Stop()
273270
ctx, cancel := context.WithTimeout(context.Background(), DefaultNodeStopTimeout)
274271
defer cancel()
@@ -298,7 +295,7 @@ func (n *LocalNode) IsHealthy(ctx context.Context) (bool, error) {
298295
return false, fmt.Errorf("failed to determine process status: %w", err)
299296
}
300297
if proc == nil {
301-
return false, errProcessNotRunning
298+
return false, testnet.ErrNotRunning
302299
}
303300

304301
// Check that the node is reporting healthy
@@ -323,33 +320,8 @@ func (n *LocalNode) IsHealthy(ctx context.Context) (bool, error) {
323320
return false, fmt.Errorf("failed to query node health: %w", err)
324321
}
325322

326-
// WaitForHealthy blocks until IsHealthy returns true or an error (including context timeout) is observed.
327-
func (n *LocalNode) WaitForHealthy(ctx context.Context) error {
328-
if _, ok := ctx.Deadline(); !ok {
329-
return fmt.Errorf("unable to wait for health for node %q with a context without a deadline", n.NodeID)
330-
}
331-
ticker := time.NewTicker(DefaultNodeTickerInterval)
332-
defer ticker.Stop()
333-
334-
for {
335-
healthy, err := n.IsHealthy(ctx)
336-
if err != nil && !errors.Is(err, errProcessNotRunning) {
337-
return fmt.Errorf("failed to wait for health of node %q: %w", n.NodeID, err)
338-
}
339-
if healthy {
340-
return nil
341-
}
342-
343-
select {
344-
case <-ctx.Done():
345-
return fmt.Errorf("failed to wait for health of node %q before timeout: %w", n.NodeID, ctx.Err())
346-
case <-ticker.C:
347-
}
348-
}
349-
}
350-
351323
func (n *LocalNode) WaitForProcessContext(ctx context.Context) error {
352-
ticker := time.NewTicker(DefaultNodeTickerInterval)
324+
ticker := time.NewTicker(testnet.DefaultNodeTickerInterval)
353325
defer ticker.Stop()
354326

355327
ctx, cancel := context.WithTimeout(ctx, DefaultNodeInitTimeout)

0 commit comments

Comments
 (0)