Skip to content

Commit 20b98bf

Browse files
authored
Merge pull request #106 from jpmcb/unflake-aws-tests
Remove race conditions in tests that execute `go` routines
2 parents cdaf5f7 + 9acd736 commit 20b98bf

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

updater/aws_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"fmt"
66
"strconv"
7+
"sync"
78
"testing"
89

910
"github.com/aws/aws-sdk-go/aws"
@@ -62,6 +63,10 @@ func TestFilterAvailableUpdates(t *testing.T) {
6263
"inst-id-4": `{"update_state": "Staged", "active_partition": { "image": { "version": "v1.1.1"}}}`,
6364
"inst-id-5": `{"update_state": "Available", "active_partition": { "image": { "version": "v1.0.5"}}}`,
6465
}
66+
67+
// mutex needed to prevent race condition when incrementing counter in concurrent
68+
// execution of WaitUntilCommandExecutedWithContextFn
69+
var m sync.Mutex
6570
sendCommandCalls := 0
6671
commandWaiterCalls := 0
6772
getCommandInvocationCalls := 0
@@ -83,7 +88,9 @@ func TestFilterAvailableUpdates(t *testing.T) {
8388
}, nil
8489
},
8590
WaitUntilCommandExecutedWithContextFn: func(ctx aws.Context, input *ssm.GetCommandInvocationInput, opts ...request.WaiterOption) error {
91+
m.Lock()
8692
commandWaiterCalls++
93+
m.Unlock()
8794
assert.Equal(t, "command-id", aws.StringValue(input.CommandId))
8895
return nil
8996
},
@@ -120,6 +127,9 @@ func TestPaginatedFilterAvailableUpdatesSuccess(t *testing.T) {
120127
})
121128
}
122129

130+
// mutex needed to prevent race condition when incrementing counter in concurrent
131+
// execution of WaitUntilCommandExecutedWithContextFn
132+
var m sync.Mutex
123133
sendCommandCalls := 0
124134
commandWaiterCalls := 0
125135
getCommandInvocationCalls := 0
@@ -138,7 +148,9 @@ func TestPaginatedFilterAvailableUpdatesSuccess(t *testing.T) {
138148
}, nil
139149
},
140150
WaitUntilCommandExecutedWithContextFn: func(ctx aws.Context, input *ssm.GetCommandInvocationInput, opts ...request.WaiterOption) error {
151+
m.Lock()
141152
commandWaiterCalls++
153+
m.Unlock()
142154
assert.Equal(t, "command-id", aws.StringValue(input.CommandId))
143155
return nil
144156
},
@@ -191,6 +203,9 @@ func TestPaginatedFilterAvailableUpdatesInPageFailures(t *testing.T) {
191203
})
192204
}
193205

206+
// mutex needed to prevent race condition when incrementing counter in concurrent
207+
// execution of WaitUntilCommandExecutedWithContextFn
208+
var m sync.Mutex
194209
sendCommandCalls := 0
195210
commandWaiterCalls := 0
196211
getCommandInvocationCalls := 0
@@ -226,7 +241,9 @@ func TestPaginatedFilterAvailableUpdatesInPageFailures(t *testing.T) {
226241
},
227242
WaitUntilCommandExecutedWithContextFn: func(ctx aws.Context, input *ssm.GetCommandInvocationInput, opts ...request.WaiterOption) error {
228243
assert.Equal(t, "command-id", aws.StringValue(input.CommandId))
244+
m.Lock()
229245
commandWaiterCalls++
246+
m.Unlock()
230247
return nil
231248
},
232249
}
@@ -264,6 +281,9 @@ func TestPaginatedFilterAvailableUpdatesSingleErr(t *testing.T) {
264281

265282
pageErrors := []error{errors.New("Failed to send document"), nil}
266283

284+
// mutex needed to prevent race condition when incrementing counter in concurrent
285+
// execution of WaitUntilCommandExecutedWithContextFn
286+
var m sync.Mutex
267287
sendCommandCalls := 0
268288
commandWaiterCalls := 0
269289
getCommandInvocationCalls := 0
@@ -287,7 +307,9 @@ func TestPaginatedFilterAvailableUpdatesSingleErr(t *testing.T) {
287307
},
288308
WaitUntilCommandExecutedWithContextFn: func(ctx aws.Context, input *ssm.GetCommandInvocationInput, opts ...request.WaiterOption) error {
289309
assert.Equal(t, "command-id", aws.StringValue(input.CommandId))
310+
m.Lock()
290311
commandWaiterCalls++
312+
m.Unlock()
291313
return nil
292314
},
293315
}
@@ -358,6 +380,9 @@ func TestGetCommandResult(t *testing.T) {
358380

359381
func TestSendCommandSuccess(t *testing.T) {
360382
instances := []string{"inst-id-1", "inst-id-2"}
383+
// mutex needed to prevent race condition when appending to instances slice in concurrent
384+
// execution of WaitUntilCommandExecutedWithContextFn
385+
var m sync.Mutex
361386
waitInstanceIDs := []string{}
362387
mockSSM := MockSSM{
363388
SendCommandFn: func(input *ssm.SendCommandInput) (*ssm.SendCommandOutput, error) {
@@ -368,7 +393,9 @@ func TestSendCommandSuccess(t *testing.T) {
368393
},
369394
WaitUntilCommandExecutedWithContextFn: func(ctx aws.Context, input *ssm.GetCommandInvocationInput, opts ...request.WaiterOption) error {
370395
assert.Equal(t, "command-id", aws.StringValue(input.CommandId))
396+
m.Lock()
371397
waitInstanceIDs = append(waitInstanceIDs, aws.StringValue(input.InstanceId))
398+
m.Unlock()
372399
return nil
373400
},
374401
}
@@ -479,12 +506,17 @@ func TestSendCommandWaitSuccess(t *testing.T) {
479506
})
480507
t.Run("wait all success", func(t *testing.T) {
481508
instances := []string{"inst-id-1", "inst-id-2"}
509+
// mutex needed to prevent race condition when appending to instances slice in concurrent
510+
// execution of WaitUntilCommandExecutedWithContextFn
511+
var m sync.Mutex
482512
waitInstanceIDs := []string{}
483513
mockSSM := MockSSM{
484514
SendCommandFn: mockSendCommand,
485515
WaitUntilCommandExecutedWithContextFn: func(ctx aws.Context, input *ssm.GetCommandInvocationInput, opts ...request.WaiterOption) error {
486516
assert.Equal(t, "command-id", aws.StringValue(input.CommandId))
517+
m.Lock()
487518
waitInstanceIDs = append(waitInstanceIDs, aws.StringValue(input.InstanceId))
519+
m.Unlock()
488520
return nil
489521
},
490522
}

updater/mock_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ type MockECS struct {
2020
var _ ECSAPI = (*MockECS)(nil)
2121

2222
type MockSSM struct {
23+
// WaitUntilCommandExecutedWithContextFn is executed concurrently through
24+
// ECS code paths and tests should treat any data in a parallel safe manner
2325
WaitUntilCommandExecutedWithContextFn func(ctx aws.Context, input *ssm.GetCommandInvocationInput, opts ...request.WaiterOption) error
2426
SendCommandFn func(input *ssm.SendCommandInput) (*ssm.SendCommandOutput, error)
2527
GetCommandInvocationFn func(input *ssm.GetCommandInvocationInput) (*ssm.GetCommandInvocationOutput, error)

0 commit comments

Comments
 (0)