Skip to content

Commit 34c3aea

Browse files
committed
added stop channels to unit tests to avoid race condtiions
1 parent dd0869f commit 34c3aea

File tree

3 files changed

+80
-13
lines changed

3 files changed

+80
-13
lines changed

npm/pkg/dataplane/dataplane_linux_test.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,14 @@ func TestNetPolInBackgroundUpdatePolicy(t *testing.T) {
7575
calls = append(calls, getAddPolicyTestCallsForDP(&updatedTestPolicyobj)...)
7676
ioshim := common.NewMockIOShim(calls)
7777
defer ioshim.VerifyCalls(t, calls)
78-
dp, err := NewDataPlane("testnode", ioshim, netpolInBackgroundCfg, nil)
78+
79+
stopCh := make(chan struct{}, 1)
80+
dp, err := NewDataPlane("testnode", ioshim, netpolInBackgroundCfg, stopCh)
7981
require.NoError(t, err)
82+
defer func() {
83+
stopCh <- struct{}{}
84+
time.Sleep(100 * time.Millisecond)
85+
}()
8086

8187
dp.RunPeriodicTasks()
8288

@@ -99,8 +105,13 @@ func TestNetPolInBackgroundSkipAddAfterRemove(t *testing.T) {
99105
calls := getBootupTestCalls()
100106
ioshim := common.NewMockIOShim(calls)
101107
defer ioshim.VerifyCalls(t, calls)
102-
dp, err := NewDataPlane("testnode", ioshim, netpolInBackgroundCfg, nil)
108+
stopCh := make(chan struct{}, 1)
109+
dp, err := NewDataPlane("testnode", ioshim, netpolInBackgroundCfg, stopCh)
103110
require.NoError(t, err)
111+
defer func() {
112+
stopCh <- struct{}{}
113+
time.Sleep(100 * time.Millisecond)
114+
}()
104115

105116
require.NoError(t, dp.AddPolicy(&testPolicyobj))
106117
require.NoError(t, dp.RemovePolicy(testPolicyobj.PolicyKey))
@@ -159,8 +170,13 @@ func TestNetPolInBackgroundFailureToAddFirstTime(t *testing.T) {
159170
)
160171
ioshim := common.NewMockIOShim(calls)
161172
defer ioshim.VerifyCalls(t, calls)
162-
dp, err := NewDataPlane("testnode", ioshim, netpolInBackgroundCfg, nil)
173+
stopCh := make(chan struct{}, 1)
174+
dp, err := NewDataPlane("testnode", ioshim, netpolInBackgroundCfg, stopCh)
163175
require.NoError(t, err)
176+
defer func() {
177+
stopCh <- struct{}{}
178+
time.Sleep(100 * time.Millisecond)
179+
}()
164180

165181
require.NoError(t, dp.AddPolicy(&testPolicyobj))
166182
require.NoError(t, dp.AddPolicy(&testPolicy2))

npm/pkg/dataplane/dataplane_test.go

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package dataplane
22

33
import (
44
"testing"
5+
"time"
56

67
"github.com/Azure/azure-container-networking/common"
78
"github.com/Azure/azure-container-networking/npm/metrics"
@@ -79,8 +80,13 @@ func TestNewDataPlane(t *testing.T) {
7980
calls := getBootupTestCalls()
8081
ioshim := common.NewMockIOShim(calls)
8182
defer ioshim.VerifyCalls(t, calls)
82-
dp, err := NewDataPlane("testnode", ioshim, dpCfg, nil)
83+
stopCh := make(chan struct{}, 1)
84+
dp, err := NewDataPlane("testnode", ioshim, dpCfg, stopCh)
8385
require.NoError(t, err)
86+
defer func() {
87+
stopCh <- struct{}{}
88+
time.Sleep(100 * time.Millisecond)
89+
}()
8490
assert.NotNil(t, dp)
8591
}
8692

@@ -90,8 +96,13 @@ func TestCreateAndDeleteIpSets(t *testing.T) {
9096
calls := getBootupTestCalls()
9197
ioshim := common.NewMockIOShim(calls)
9298
defer ioshim.VerifyCalls(t, calls)
93-
dp, err := NewDataPlane("testnode", ioshim, dpCfg, nil)
99+
stopCh := make(chan struct{}, 1)
100+
dp, err := NewDataPlane("testnode", ioshim, dpCfg, stopCh)
94101
require.NoError(t, err)
102+
defer func() {
103+
stopCh <- struct{}{}
104+
time.Sleep(100 * time.Millisecond)
105+
}()
95106
assert.NotNil(t, dp)
96107
setsTocreate := []*ipsets.IPSetMetadata{
97108
{
@@ -132,8 +143,13 @@ func TestAddToSet(t *testing.T) {
132143
calls := getBootupTestCalls()
133144
ioshim := common.NewMockIOShim(calls)
134145
defer ioshim.VerifyCalls(t, calls)
135-
dp, err := NewDataPlane("testnode", ioshim, dpCfg, nil)
146+
stopCh := make(chan struct{}, 1)
147+
dp, err := NewDataPlane("testnode", ioshim, dpCfg, stopCh)
136148
require.NoError(t, err)
149+
defer func() {
150+
stopCh <- struct{}{}
151+
time.Sleep(100 * time.Millisecond)
152+
}()
137153

138154
setsTocreate := []*ipsets.IPSetMetadata{
139155
{
@@ -196,8 +212,13 @@ func TestApplyPolicy(t *testing.T) {
196212
calls := append(getBootupTestCalls(), getAddPolicyTestCallsForDP(&testPolicyobj)...)
197213
ioshim := common.NewMockIOShim(calls)
198214
defer ioshim.VerifyCalls(t, calls)
199-
dp, err := NewDataPlane("testnode", ioshim, dpCfg, nil)
215+
stopCh := make(chan struct{}, 1)
216+
dp, err := NewDataPlane("testnode", ioshim, dpCfg, stopCh)
200217
require.NoError(t, err)
218+
defer func() {
219+
stopCh <- struct{}{}
220+
time.Sleep(100 * time.Millisecond)
221+
}()
201222

202223
err = dp.AddPolicy(&testPolicyobj)
203224
require.NoError(t, err)
@@ -210,8 +231,13 @@ func TestRemovePolicy(t *testing.T) {
210231
calls = append(calls, getRemovePolicyTestCallsForDP(&testPolicyobj)...)
211232
ioshim := common.NewMockIOShim(calls)
212233
defer ioshim.VerifyCalls(t, calls)
213-
dp, err := NewDataPlane("testnode", ioshim, dpCfg, nil)
234+
stopCh := make(chan struct{}, 1)
235+
dp, err := NewDataPlane("testnode", ioshim, dpCfg, stopCh)
214236
require.NoError(t, err)
237+
defer func() {
238+
stopCh <- struct{}{}
239+
time.Sleep(100 * time.Millisecond)
240+
}()
215241

216242
err = dp.AddPolicy(&testPolicyobj)
217243
require.NoError(t, err)
@@ -234,8 +260,13 @@ func TestHandle2977(t *testing.T) {
234260
calls = append(calls, getAddPolicyTestCallsForDP(&testPolicyobj)...)
235261
ioshim := common.NewMockIOShim(calls)
236262
defer ioshim.VerifyCalls(t, calls)
237-
dp, err := NewDataPlane("testnode", ioshim, dpCfg, nil)
263+
stopCh := make(chan struct{}, 1)
264+
dp, err := NewDataPlane("testnode", ioshim, dpCfg, stopCh)
238265
require.NoError(t, err)
266+
defer func() {
267+
stopCh <- struct{}{}
268+
time.Sleep(100 * time.Millisecond)
269+
}()
239270

240271
err = dp.AddPolicy(&testPolicyobj)
241272
require.NoError(t, err)
@@ -263,8 +294,13 @@ func TestUpdatePolicy(t *testing.T) {
263294
calls = append(calls, getAddPolicyTestCallsForDP(&updatedTestPolicyobj)...)
264295
ioshim := common.NewMockIOShim(calls)
265296
defer ioshim.VerifyCalls(t, calls)
266-
dp, err := NewDataPlane("testnode", ioshim, dpCfg, nil)
297+
stopCh := make(chan struct{}, 1)
298+
dp, err := NewDataPlane("testnode", ioshim, dpCfg, stopCh)
267299
require.NoError(t, err)
300+
defer func() {
301+
stopCh <- struct{}{}
302+
time.Sleep(100 * time.Millisecond)
303+
}()
268304

269305
err = dp.AddPolicy(&testPolicyobj)
270306
require.NoError(t, err)

npm/pkg/dataplane/dataplane_windows_test.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,14 @@ func TestMetrics(t *testing.T) {
2828
hns := ipsets.GetHNSFake(t, cfg.NetworkName)
2929
hns.Delay = defaultHNSLatency
3030
io := common.NewMockIOShimWithFakeHNS(hns)
31-
dp, err := NewDataPlane(thisNode, io, cfg, nil)
31+
stopCh := make(chan struct{}, 1)
32+
dp, err := NewDataPlane(thisNode, io, cfg, stopCh)
3233
require.NoError(t, err, "failed to initialize dp")
3334
require.NotNil(t, dp, "failed to initialize dp (nil)")
35+
defer func() {
36+
stopCh <- struct{}{}
37+
time.Sleep(100 * time.Millisecond)
38+
}()
3439

3540
count, err := metrics.TotalGetNetworkLatencyCalls()
3641
require.Nil(t, err, "failed to get metric")
@@ -166,9 +171,14 @@ func testSerialCases(t *testing.T, tests []*SerialTestCase, finalSleep time.Dura
166171
require.Nil(t, err, "failed to create initial endpoint %+v", ep)
167172
}
168173

169-
dp, err := NewDataPlane(thisNode, io, tt.DpCfg, nil)
174+
stopCh := make(chan struct{}, 1)
175+
dp, err := NewDataPlane(thisNode, io, tt.DpCfg, stopCh)
170176
require.NoError(t, err, "failed to initialize dp")
171177
require.NotNil(t, dp, "failed to initialize dp (nil)")
178+
defer func() {
179+
stopCh <- struct{}{}
180+
time.Sleep(100 * time.Millisecond)
181+
}()
172182

173183
dp.RunPeriodicTasks()
174184

@@ -206,8 +216,13 @@ func testMultiJobCases(t *testing.T, tests []*MultiJobTestCase, finalSleep time.
206216
}
207217

208218
// the dp is necessary for NPM tests
209-
dp, err := NewDataPlane(thisNode, io, tt.DpCfg, nil)
219+
stopCh := make(chan struct{}, 1)
220+
dp, err := NewDataPlane(thisNode, io, tt.DpCfg, stopCh)
210221
require.NoError(t, err, "failed to initialize dp")
222+
defer func() {
223+
stopCh <- struct{}{}
224+
time.Sleep(100 * time.Millisecond)
225+
}()
211226

212227
dp.RunPeriodicTasks()
213228

0 commit comments

Comments
 (0)