mirror of
https://github.com/netbirdio/netbird.git
synced 2026-07-23 17:01:29 +02:00
[client] Make next-hop check injectable for network monitor tests
Move the next-hop comparison behind a NetworkMonitor field set by New(), so tests can supply a stub instead of hitting the host's real default route. Fixes the Event/MultiEvent tests hanging after the unchanged-route check was added.
This commit is contained in:
@@ -28,11 +28,16 @@ type NetworkMonitor struct {
|
||||
cancel context.CancelFunc
|
||||
wg sync.WaitGroup
|
||||
mu sync.Mutex
|
||||
|
||||
// nexthopChanged reports whether the default next hop changed; overridable in tests.
|
||||
nexthopChanged func(oldv4, oldv6 systemops.Nexthop) bool
|
||||
}
|
||||
|
||||
// New creates a new network monitor.
|
||||
func New() *NetworkMonitor {
|
||||
return &NetworkMonitor{}
|
||||
return &NetworkMonitor{
|
||||
nexthopChanged: nexthopChanged,
|
||||
}
|
||||
}
|
||||
|
||||
// Listen begins monitoring network changes. When a change is detected, this function will return without error.
|
||||
@@ -99,7 +104,7 @@ func (nw *NetworkMonitor) Listen(ctx context.Context) (err error) {
|
||||
case <-timer.C:
|
||||
// A flapping NIC may return to the same default route after the
|
||||
// debounce window; only restart if the next hop actually changed.
|
||||
if nexthopChanged(nexthop4, nexthop6) {
|
||||
if nw.nexthopChanged(nexthop4, nexthop6) {
|
||||
return nil
|
||||
}
|
||||
log.Debug("Network monitor: default route unchanged after debounce, ignoring")
|
||||
|
||||
@@ -61,6 +61,7 @@ func TestNetworkMonitor_Event(t *testing.T) {
|
||||
}
|
||||
}
|
||||
nw := New()
|
||||
nw.nexthopChanged = func(_, _ systemops.Nexthop) bool { return true }
|
||||
defer nw.Stop()
|
||||
|
||||
var resErr error
|
||||
@@ -82,6 +83,7 @@ func TestNetworkMonitor_MultiEvent(t *testing.T) {
|
||||
checkChangeFn = me.checkChange
|
||||
|
||||
nw := New()
|
||||
nw.nexthopChanged = func(_, _ systemops.Nexthop) bool { return true }
|
||||
defer nw.Stop()
|
||||
|
||||
done := make(chan struct{})
|
||||
|
||||
Reference in New Issue
Block a user