From 5d0c65f3f51102a0f553c80745edeebb93886fcf Mon Sep 17 00:00:00 2001 From: Sina Darbouy Date: Mon, 9 Dec 2024 16:31:55 +0100 Subject: [PATCH] Improve Redis container setup and async test handling - Updated `createTestRedis` in `act_helpers_test.go` to use `wait.ForAll` for better reliability by ensuring both log readiness and port listening. - Enhanced `Test_Run_Async_Redis` in `registry_test.go` by adding a context with a timeout to the consumer subscription for improved test robustness. - Simplified the sleep duration in `Test_Run_Async_Redis` to reduce unnecessary wait time. --- act/act_helpers_test.go | 5 ++++- act/registry_test.go | 12 +++++++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/act/act_helpers_test.go b/act/act_helpers_test.go index 7805192b..8157892d 100644 --- a/act/act_helpers_test.go +++ b/act/act_helpers_test.go @@ -62,7 +62,10 @@ func createTestRedis(t *testing.T) string { req := testcontainers.ContainerRequest{ Image: "redis:6", ExposedPorts: []string{"6379/tcp"}, - WaitingFor: wait.ForLog("Ready to accept connections"), + WaitingFor: wait.ForAll( + wait.ForLog("Ready to accept connections"), + wait.ForListeningPort("6379/tcp"), + ), } redisContainer, err := testcontainers.GenericContainer( ctx, testcontainers.GenericContainerRequest{ diff --git a/act/registry_test.go b/act/registry_test.go index 44204de1..d620865e 100644 --- a/act/registry_test.go +++ b/act/registry_test.go @@ -747,10 +747,12 @@ func Test_Run_Async_Redis(t *testing.T) { consumer, err := sdkAct.NewConsumer(hclogger, rdb, 5, "test-async-chan") require.NoError(t, err) - require.NoError(t, consumer.Subscribe(context.Background(), func(ctx context.Context, task []byte) error { - err := actRegistry.runAsyncActionFn(ctx, task) - waitGroup.Done() - return err + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + + require.NoError(t, consumer.Subscribe(ctx, func(ctx context.Context, task []byte) error { + defer waitGroup.Done() + return actRegistry.runAsyncActionFn(ctx, task) })) outputs := actRegistry.Apply([]sdkAct.Signal{ @@ -781,7 +783,7 @@ func Test_Run_Async_Redis(t *testing.T) { assert.Equal(t, err, gerr.ErrAsyncAction, "expected async action sentinel error") assert.Nil(t, result, "expected nil result") - time.Sleep(time.Millisecond * 2) // wait for async action to complete + time.Sleep(time.Millisecond) // wait for async action to complete // The following is the expected log output from running the async action. assert.Contains(t, out.String(), "{\"level\":\"debug\",\"action\":\"log\",\"executionMode\":\"async\",\"message\":\"Running action\"}") //nolint:lll