Skip to content

Commit 9b83289

Browse files
committed
test: improve test reliability for Redis and cache operations
- Add waiting for both log message and listening port to improve Redis container startup reliability - Force use of IPv4 loopback address to prevent IPv6 connection issues in tests - Insert short delays after cache invalidation steps to ensure propagation during tests Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
1 parent 8f79c75 commit 9b83289

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

pkg/store/redis_test.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ func setupRedisContainer(ctx context.Context) (string, error) {
2020
req := testcontainers.ContainerRequest{
2121
Image: "redis:7-alpine",
2222
ExposedPorts: []string{"6379/tcp"},
23-
WaitingFor: wait.ForLog("Ready to accept connections").WithStartupTimeout(30 * time.Second),
23+
WaitingFor: wait.ForAll(
24+
wait.ForLog("Ready to accept connections").WithStartupTimeout(30 * time.Second),
25+
wait.ForListeningPort("6379/tcp").WithStartupTimeout(30 * time.Second),
26+
),
2427
}
2528

2629
container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
@@ -45,6 +48,11 @@ func setupRedisContainer(ctx context.Context) (string, error) {
4548
return "", fmt.Errorf("failed to get container port: %w", err)
4649
}
4750

51+
// Force IPv4 127.0.0.1 instead of localhost to avoid IPv6 issues
52+
if host == "localhost" {
53+
host = "127.0.0.1"
54+
}
55+
4856
return fmt.Sprintf("%s:%s", host, port.Port()), nil
4957
}
5058

@@ -659,6 +667,9 @@ func TestRedisStore_ClientLifecycle(t *testing.T) {
659667
t.Fatalf("UpdateClient() failed: %v", err)
660668
}
661669

670+
// Small delay to allow cache invalidation to propagate
671+
time.Sleep(100 * time.Millisecond)
672+
662673
// Verify update
663674
updated, err := store.GetClient(ctx, client.ID)
664675
if err != nil {
@@ -673,6 +684,9 @@ func TestRedisStore_ClientLifecycle(t *testing.T) {
673684
t.Fatalf("DeleteClient() failed: %v", err)
674685
}
675686

687+
// Small delay to allow cache invalidation to propagate
688+
time.Sleep(100 * time.Millisecond)
689+
676690
// Verify deletion
677691
_, err = store.GetClient(ctx, client.ID)
678692
if err != ErrClientNotFound {
@@ -720,6 +734,9 @@ func TestRedisStore_AuthorizationCodeLifecycle(t *testing.T) {
720734
t.Fatalf("DeleteAuthorizationCode() failed: %v", err)
721735
}
722736

737+
// Small delay to allow cache invalidation to propagate
738+
time.Sleep(100 * time.Millisecond)
739+
723740
// Verify deletion
724741
_, err = store.GetAuthorizationCode(ctx, code.ClientID)
725742
if err != ErrCodeNotFound {

0 commit comments

Comments
 (0)