|
| 1 | +package strategies |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "sync" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/unpackdev/fdb/pkg/logger" |
| 9 | + "github.com/unpackdev/fdb/playground/suite" |
| 10 | +) |
| 11 | + |
| 12 | +// NetworkStrategy tests the network connectivity between nodes |
| 13 | +type NetworkStrategy struct { |
| 14 | + logger logger.Logger |
| 15 | + nodes suite.TestNodes |
| 16 | + doneCh chan struct{} |
| 17 | + cancel context.CancelFunc |
| 18 | + duration time.Duration |
| 19 | + wg sync.WaitGroup |
| 20 | + pingTimeout time.Duration |
| 21 | +} |
| 22 | + |
| 23 | +// NewNetworkStrategy creates a new network strategy |
| 24 | +func NewNetworkStrategy(logger logger.Logger, nodes suite.TestNodes) *NetworkStrategy { |
| 25 | + return &NetworkStrategy{ |
| 26 | + logger: logger, |
| 27 | + nodes: nodes, |
| 28 | + doneCh: make(chan struct{}), |
| 29 | + duration: 60 * time.Second, // Run for 1 minute by default |
| 30 | + pingTimeout: 5 * time.Second, |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +// Info returns information about the network strategy |
| 35 | +func (s *NetworkStrategy) Info() Info { |
| 36 | + return Info{ |
| 37 | + Name: "network", |
| 38 | + Description: "Tests network connectivity between nodes", |
| 39 | + DefaultArgs: map[string]any{ |
| 40 | + "duration": 60, // seconds |
| 41 | + "ping_timeout": 5, // seconds |
| 42 | + }, |
| 43 | + ArgMappings: []ArgMapping{ |
| 44 | + { |
| 45 | + Flag: "duration", |
| 46 | + ParamKey: "duration", |
| 47 | + Description: "Duration to run the network test in seconds", |
| 48 | + DefaultValue: 60, |
| 49 | + }, |
| 50 | + { |
| 51 | + Flag: "ping-timeout", |
| 52 | + ParamKey: "ping_timeout", |
| 53 | + Description: "Timeout for each ping operation in seconds", |
| 54 | + DefaultValue: 5, |
| 55 | + }, |
| 56 | + }, |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +// Start begins the network connectivity testing |
| 61 | +func (s *NetworkStrategy) Start(ctx context.Context) error { |
| 62 | + s.logger.Info("Starting network connectivity testing", |
| 63 | + "node_count", len(s.nodes), |
| 64 | + "duration", s.duration.String(), |
| 65 | + "ping_timeout", s.pingTimeout.String()) |
| 66 | + |
| 67 | + // Create a new context we can cancel when Stop is called |
| 68 | + ctx, s.cancel = context.WithCancel(ctx) |
| 69 | + |
| 70 | + s.wg.Add(1) |
| 71 | + go s.runConnectivityTest(ctx) |
| 72 | + |
| 73 | + // Set up the completion timer |
| 74 | + go func() { |
| 75 | + timer := time.NewTimer(s.duration) |
| 76 | + defer timer.Stop() |
| 77 | + |
| 78 | + select { |
| 79 | + case <-ctx.Done(): |
| 80 | + return |
| 81 | + case <-timer.C: |
| 82 | + s.logger.Info("Network test completed successfully") |
| 83 | + close(s.doneCh) |
| 84 | + if s.cancel != nil { |
| 85 | + s.cancel() |
| 86 | + } |
| 87 | + } |
| 88 | + }() |
| 89 | + |
| 90 | + return nil |
| 91 | +} |
| 92 | + |
| 93 | +// Stop halts the network strategy |
| 94 | +func (s *NetworkStrategy) Stop() error { |
| 95 | + if s.cancel != nil { |
| 96 | + s.cancel() |
| 97 | + } |
| 98 | + s.wg.Wait() |
| 99 | + return nil |
| 100 | +} |
| 101 | + |
| 102 | +// CompletionCh returns a channel that is closed when the strategy completes |
| 103 | +func (s *NetworkStrategy) CompletionCh() <-chan struct{} { |
| 104 | + return s.doneCh |
| 105 | +} |
| 106 | + |
| 107 | +// CreateFn returns a function that can create new instances of this strategy |
| 108 | +func (s *NetworkStrategy) CreateFn() StrategyFn { |
| 109 | + return func(logger logger.Logger, nodes suite.TestNodes, args map[string]any) (Strategy, error) { |
| 110 | + strategy := NewNetworkStrategy(logger, nodes) |
| 111 | + |
| 112 | + // Apply configuration from args |
| 113 | + if durationSec, ok := args["duration"].(int); ok && durationSec > 0 { |
| 114 | + strategy.duration = time.Duration(durationSec) * time.Second |
| 115 | + } |
| 116 | + |
| 117 | + if timeout, ok := args["ping_timeout"].(int); ok && timeout > 0 { |
| 118 | + strategy.pingTimeout = time.Duration(timeout) * time.Second |
| 119 | + } |
| 120 | + |
| 121 | + return strategy, nil |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +// runConnectivityTest periodically checks connectivity between nodes |
| 126 | +func (s *NetworkStrategy) runConnectivityTest(ctx context.Context) { |
| 127 | + defer s.wg.Done() |
| 128 | + |
| 129 | + ticker := time.NewTicker(10 * time.Second) |
| 130 | + defer ticker.Stop() |
| 131 | + |
| 132 | + for { |
| 133 | + select { |
| 134 | + case <-ctx.Done(): |
| 135 | + return |
| 136 | + case <-ticker.C: |
| 137 | + s.checkConnectivity() |
| 138 | + } |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +// checkConnectivity verifies that all nodes can see each other |
| 143 | +func (s *NetworkStrategy) checkConnectivity() { |
| 144 | + totalNodes := len(s.nodes) |
| 145 | + s.logger.Info("Checking network connectivity", "total_nodes", totalNodes) |
| 146 | + |
| 147 | + for i, node := range s.nodes { |
| 148 | + peers := node.Node().Network().Host().Network().Peers() |
| 149 | + s.logger.Info("Node connectivity status", |
| 150 | + "node_index", i, |
| 151 | + "peer_id", node.PeerID().String(), |
| 152 | + "connected_peers", len(peers), |
| 153 | + "expected_peers", totalNodes-1) // Exclude self |
| 154 | + } |
| 155 | +} |
0 commit comments