Skip to content

Commit

Permalink
test: reproduce DiscoverPollEndpoint race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
Isaac Feldman committed Feb 19, 2025
1 parent 39c99d0 commit 546aa27
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions ecs-agent/api/ecs/client/ecs_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"errors"
"fmt"
"strings"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -838,6 +839,36 @@ func TestDiscoverNilSystemLogsEndpoint(t *testing.T) {
"Expected error getting system logs endpoint with old response")
}

func TestDiscoverPollEndpointRace(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

tester := setup(t, ctrl, ec2.NewBlackholeEC2MetadataClient(), nil)
pollEndpoint := "http://127.0.0.1"
// SDK call to DiscoverPollEndpoint should only happen once.
tester.mockStandardClient.EXPECT().DiscoverPollEndpoint(gomock.Any()).Times(1).Do(func (interface{}) {
// Wait before returning to try and induce the race condition.
time.Sleep(500 * time.Millisecond)
}).Return(&ecsmodel.DiscoverPollEndpointOutput{Endpoint: &pollEndpoint}, nil)

var wg sync.WaitGroup
wg.Add(2)

// First caller.
go func() {
defer wg.Done()
tester.client.DiscoverPollEndpoint(containerInstanceARN)
}()
// Second caller.
go func() {
defer wg.Done()
time.Sleep(200 * time.Millisecond)
tester.client.DiscoverPollEndpoint(containerInstanceARN)
}()

wg.Wait()
}

func TestUpdateContainerInstancesState(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
Expand Down

0 comments on commit 546aa27

Please sign in to comment.