From 482ebec14f92a407fb6c651dc99d97486a3e0e06 Mon Sep 17 00:00:00 2001 From: Aaron U'Ren Date: Fri, 11 Oct 2024 14:52:31 -0500 Subject: [PATCH] fix(host_routes): remove race conditions from tests --- pkg/controllers/routing/host_route_sync_test.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/pkg/controllers/routing/host_route_sync_test.go b/pkg/controllers/routing/host_route_sync_test.go index ce197818f..2ff2a6a50 100644 --- a/pkg/controllers/routing/host_route_sync_test.go +++ b/pkg/controllers/routing/host_route_sync_test.go @@ -20,6 +20,9 @@ var ( "192.168.0.1": "192.168.0.0/24", "10.255.0.1": "10.255.0.0/16", } + testRoute = map[string]string{ + "192.168.0.1": "192.168.0.0/24", + } _, testAddRouteIPNet, _ = net.ParseCIDR("192.168.1.0/24") testAddRouteRoute = testGenerateRoute("192.168.1.0/24", "192.168.1.1") ) @@ -146,7 +149,7 @@ func Test_syncLocalRouteTable(t *testing.T) { // Create a route replacer and seed it with some routes to iterate over syncer := NewRouteSyncer(15 * time.Second) - syncer.routeTableStateMap = testGenerateRouteMap(testRoutes) + syncer.routeTableStateMap = testGenerateRouteMap(testRoute) // Replace the netlink.RouteReplace function with our own mock function that includes a WaitGroup for syncing // and an artificial pause and won't interact with the OS @@ -212,25 +215,28 @@ func Test_routeSyncer_run(t *testing.T) { // Setup routeSyncer to run 10 times a second syncer := NewRouteSyncer(100 * time.Millisecond) myNetLink := mockNetlink{} + myNetLink.pause = 0 + myNetLink.wg = &sync.WaitGroup{} syncer.routeReplacer = myNetLink.mockRouteAction - syncer.routeTableStateMap = testGenerateRouteMap(testRoutes) + syncer.routeTableStateMap = testGenerateRouteMap(testRoute) stopCh := make(chan struct{}) wg := sync.WaitGroup{} // For a sanity check that the currentRoute on the mock object is nil to start with as we'll rely on this later assert.Nil(t, myNetLink.currentRoute, "currentRoute should be nil when the syncer hasn't run") + myNetLink.wg.Add(1) syncer.Run(stopCh, &wg) - time.Sleep(110 * time.Millisecond) - + timedOut := waitTimeout(myNetLink.wg, 110*time.Millisecond) + assert.False(t, timedOut, "Run should have not timed out and instead should have added a route") assert.NotNil(t, myNetLink.currentRoute, "the syncer should have run by now and populated currentRoute") // Simulate a shutdown close(stopCh) // WaitGroup should close out before our timeout - timedOut := waitTimeout(&wg, 110*time.Millisecond) + timedOut = waitTimeout(&wg, 110*time.Millisecond) assert.False(t, timedOut, "WaitGroup should have marked itself as done instead of timing out") })