Skip to content

Commit 12016a3

Browse files
Dai.OtsukaAdmiral-Piett
Dai.Otsuka
authored andcommitted
fixed data race
1 parent e9edcf5 commit 12016a3

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

app/models/requests_test.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -634,23 +634,26 @@ func TestPublishRequest_SetAttributesFromForm_success_concurrent(t *testing.T) {
634634
form.Add("MessageAttributes.entry.2.Value.DataType", "Binary")
635635
form.Add("MessageAttributes.entry.2.Value.BinaryValue", "YmluYXJ5LXZhbHVl")
636636

637-
cqr := &PublishRequest{
638-
MessageAttributes: make(map[string]MessageAttribute),
639-
}
637+
// if the code is not thread-safe, repeated runs increase the chance of detecting a race.
640638
for r := 0; r < 10; r++ {
641639
var wg sync.WaitGroup
642640
goroutineCount := 40
641+
// launch goroutines in parallel to simulate concurrent access.
643642
for g := 0; g < goroutineCount; g++ {
644643
wg.Add(1)
645644
go func() {
646645
defer wg.Done()
647-
// Introduce a random delay to encourage goroutine interleaving
646+
// introduce a random delay to encourage goroutine interleaving
648647
time.Sleep(time.Duration(rand.Intn(5)) * time.Millisecond)
648+
cqr := &PublishRequest{
649+
MessageAttributes: make(map[string]MessageAttribute),
650+
}
651+
649652
cqr.SetAttributesFromForm(form)
653+
650654
// validate the expected DataType values
651655
assert.Equal(t, "String", cqr.MessageAttributes["test1"].DataType)
652656
assert.Equal(t, "Binary", cqr.MessageAttributes["test2"].DataType)
653-
654657
}()
655658
}
656659
wg.Wait()

0 commit comments

Comments
 (0)