mirror of
https://github.com/netbirdio/netbird.git
synced 2026-08-31 03:51:29 +02:00
update checkChangeFn test usage
This commit is contained in:
@@ -22,6 +22,19 @@ const (
|
||||
)
|
||||
|
||||
var checkChangeFn = checkChange
|
||||
var mux sync.Mutex
|
||||
|
||||
func getCheckChangeFn() func(ctx context.Context, nexthopv4, nexthopv6 systemops.Nexthop) error {
|
||||
mux.Lock()
|
||||
defer mux.Unlock()
|
||||
return checkChangeFn
|
||||
}
|
||||
|
||||
func setCheckChangeFn(fn func(ctx context.Context, nexthopv4, nexthopv6 systemops.Nexthop) error) {
|
||||
mux.Lock()
|
||||
defer mux.Unlock()
|
||||
checkChangeFn = fn
|
||||
}
|
||||
|
||||
// NetworkMonitor watches for changes in network configuration.
|
||||
type NetworkMonitor struct {
|
||||
@@ -120,7 +133,8 @@ func (nw *NetworkMonitor) Stop() {
|
||||
func (nw *NetworkMonitor) checkChanges(ctx context.Context, event chan struct{}, nexthop4 systemops.Nexthop, nexthop6 systemops.Nexthop) {
|
||||
defer close(event)
|
||||
for {
|
||||
if err := checkChangeFn(ctx, nexthop4, nexthop6); err != nil {
|
||||
checkFn := getCheckChangeFn()
|
||||
if err := checkFn(ctx, nexthop4, nexthop6); err != nil {
|
||||
if !errors.Is(err, context.Canceled) {
|
||||
log.Errorf("Network monitor: failed to check for changes: %v", err)
|
||||
}
|
||||
|
||||
@@ -25,10 +25,10 @@ func (m *MocMultiEvent) checkChange(ctx context.Context, nexthopv4, nexthopv6 sy
|
||||
}
|
||||
|
||||
func TestNetworkMonitor_Close(t *testing.T) {
|
||||
checkChangeFn = func(ctx context.Context, nexthopv4, nexthopv6 systemops.Nexthop) error {
|
||||
setCheckChangeFn(func(ctx context.Context, nexthopv4, nexthopv6 systemops.Nexthop) error {
|
||||
<-ctx.Done()
|
||||
return ctx.Err()
|
||||
}
|
||||
})
|
||||
nw := New()
|
||||
|
||||
var resErr error
|
||||
@@ -48,7 +48,7 @@ func TestNetworkMonitor_Close(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestNetworkMonitor_Event(t *testing.T) {
|
||||
checkChangeFn = func(ctx context.Context, nexthopv4, nexthopv6 systemops.Nexthop) error {
|
||||
setCheckChangeFn(func(ctx context.Context, nexthopv4, nexthopv6 systemops.Nexthop) error {
|
||||
timeout, cancel := context.WithTimeout(ctx, 3*time.Second)
|
||||
defer cancel()
|
||||
select {
|
||||
@@ -57,7 +57,7 @@ func TestNetworkMonitor_Event(t *testing.T) {
|
||||
case <-timeout.Done():
|
||||
return nil
|
||||
}
|
||||
}
|
||||
})
|
||||
nw := New()
|
||||
defer nw.Stop()
|
||||
|
||||
@@ -77,7 +77,7 @@ func TestNetworkMonitor_Event(t *testing.T) {
|
||||
func TestNetworkMonitor_MultiEvent(t *testing.T) {
|
||||
eventsRepeated := 3
|
||||
me := &MocMultiEvent{counter: eventsRepeated}
|
||||
checkChangeFn = me.checkChange
|
||||
setCheckChangeFn(me.checkChange)
|
||||
|
||||
nw := New()
|
||||
defer nw.Stop()
|
||||
|
||||
Reference in New Issue
Block a user