From 43862b68d13ca5c5cf6e0d2e7fa506d956be8b32 Mon Sep 17 00:00:00 2001 From: Lars Gohr Date: Tue, 12 Sep 2023 20:50:39 +0200 Subject: [PATCH] :recycle: Enable start of individual services via start Deprecate StartWithContext as shutdown is unreliable due to go routine firing to late --- .github/workflows/codeql-analysis.yml | 2 +- .github/workflows/test.yml | 8 ++++---- localstack.go | 6 +++--- localstack_test.go | 29 ++++++++++++++++++++++++++- 4 files changed, 36 insertions(+), 9 deletions(-) diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index 0e61db21..2a4cfa4a 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -15,7 +15,7 @@ jobs: - uses: actions/checkout@v4 - uses: actions/setup-go@v4 with: - go-version: '^1.20' + go-version: '^1.21' - uses: github/codeql-action/init@v2 with: languages: 'go' diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 19855503..af84d6bd 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -3,14 +3,14 @@ on: push jobs: test: runs-on: ubuntu-latest - timeout-minutes: 20 + timeout-minutes: 30 steps: - uses: actions/checkout@v4 - uses: actions/setup-go@v4 with: - go-version: '^1.20' + go-version: '^1.21' - name: Test - run: go test -race -timeout 20m -coverprofile=coverage.txt -covermode=atomic ./... + run: go test -race -timeout 30m -coverprofile=coverage.txt -covermode=atomic ./... - name: Coverage uses: codecov/codecov-action@v3 with: @@ -26,5 +26,5 @@ jobs: - uses: actions/checkout@v4 - uses: actions/setup-go@v4 with: - go-version: '^1.20' + go-version: '^1.21' - run: make install-release-tool new-patch-release diff --git a/localstack.go b/localstack.go index 36ef8410..cc824976 100644 --- a/localstack.go +++ b/localstack.go @@ -150,12 +150,12 @@ func NewInstanceCtx(ctx context.Context, opts ...InstanceOption) (*Instance, err } // Start starts the localstack -func (i *Instance) Start() error { - return i.start(context.Background()) +func (i *Instance) Start(services ...Service) error { + return i.start(context.Background(), services...) } // StartWithContext starts the localstack and ends it when the context is done. -// Use it to also start individual services, by default all are started. +// Deprecated: Use Start/Stop instead, as shutdown is not reliable func (i *Instance) StartWithContext(ctx context.Context, services ...Service) error { go func() { <-ctx.Done() diff --git a/localstack_test.go b/localstack_test.go index 9472635a..fc1f55a9 100644 --- a/localstack_test.go +++ b/localstack_test.go @@ -268,10 +268,37 @@ func TestLocalStackWithIndividualServicesOnContext(t *testing.T) { } } cancel() + + // wait until service was shutdown + require.Eventually(t, func() bool { + _, err := cl.Get(l.EndpointV2(service)) + return err != nil + }, time.Minute, 300*time.Millisecond) + }) + } +} + +func TestLocalStackWithIndividualServices(t *testing.T) { + cl := http.Client{Timeout: time.Second} + for service := range localstack.AvailableServices { + t.Run(service.Name, func(t *testing.T) { + l, err := localstack.NewInstance() + require.NoError(t, err) + require.NoError(t, l.Start(service)) + for testService := range localstack.AvailableServices { + conn, err := net.DialTimeout("tcp", strings.TrimPrefix(l.EndpointV2(testService), "http://"), time.Second) + if testService == service || testService == localstack.DynamoDB { + require.NoError(t, err, testService) + require.NoError(t, conn.Close()) + } + } + assert.NoError(t, l.Stop()) + + // wait until service was shutdown require.Eventually(t, func() bool { _, err := cl.Get(l.EndpointV2(service)) return err != nil - }, time.Minute, time.Second) + }, time.Minute, 300*time.Millisecond) }) } }