update checkChangeFn test usage

This commit is contained in:
Maycon Santos
2025-07-20 21:24:51 +02:00
parent 7b4cc63054
commit 84988b4d53
2 changed files with 20 additions and 6 deletions

View File

@@ -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)
}

View File

@@ -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()