refactor(backend): standardize errors and simplify configuration and test setup
This commit is contained in:
1 parent
1535c19b8c
commit
138666a7bb
92 files changed
+3994
-210
No files matched your search
@@ -0,0 +1,121 @@
|
||||
//go:build integration
|
||||
|
||||
package integration_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"lincodex/backend/internal/port/outbound"
|
||||
)
|
||||
|
||||
func TestRedisCacheAgainstContainer(t *testing.T) {
|
||||
env := bootstrap(t, redisService)
|
||||
ctx := t.Context()
|
||||
settings, cache := env.Config, env.Redis
|
||||
|
||||
t.Run("values misses and deletion", func(t *testing.T) {
|
||||
if _, err := cache.Get(ctx, "absent"); !errors.Is(err, outbound.ErrCacheMiss) {
|
||||
t.Fatalf("missing key: %v", err)
|
||||
}
|
||||
if err := cache.Set(ctx, "value", []byte{0, 1, 255}, time.Minute); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
value, err := cache.Get(ctx, "value")
|
||||
if err != nil || string(value) != string([]byte{0, 1, 255}) {
|
||||
t.Fatalf("Get returned %v, %v", value, err)
|
||||
}
|
||||
if err := cache.Del(ctx, "value", "absent"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := cache.Get(ctx, "value"); !errors.Is(err, outbound.ErrCacheMiss) {
|
||||
t.Fatalf("deleted key: %v", err)
|
||||
}
|
||||
if err := cache.Del(ctx); err != nil {
|
||||
t.Fatalf("empty deletion: %v", err)
|
||||
}
|
||||
if err := cache.Set(ctx, "empty", []byte{}, time.Minute); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if value, err := cache.Get(ctx, "empty"); err != nil || len(value) != 0 {
|
||||
t.Fatalf("empty value: %v, %v", value, err)
|
||||
}
|
||||
})
|
||||
t.Run("expiration and rejected writes", func(t *testing.T) {
|
||||
if err := cache.Set(ctx, "expires", []byte("value"), 50*time.Millisecond); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
deadline := time.Now().Add(3 * time.Second)
|
||||
for {
|
||||
_, err := cache.Get(ctx, "expires")
|
||||
if errors.Is(err, outbound.ErrCacheMiss) {
|
||||
break
|
||||
}
|
||||
if err != nil || time.Now().After(deadline) {
|
||||
t.Fatalf("TTL did not expire: %v", err)
|
||||
}
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
}
|
||||
for _, ttl := range []time.Duration{0, -time.Second} {
|
||||
if err := cache.Set(ctx, "invalid", []byte("value"), ttl); !errors.Is(err, outbound.ErrInvalidCacheTTL) {
|
||||
t.Fatalf("invalid TTL: %v", err)
|
||||
}
|
||||
}
|
||||
if _, err := cache.Get(ctx, "invalid"); !errors.Is(err, outbound.ErrCacheMiss) {
|
||||
t.Fatalf("invalid write persisted: %v", err)
|
||||
}
|
||||
if err := cache.Set(ctx, "", []byte("value"), time.Minute); !errors.Is(err, outbound.ErrInvalidCacheKey) {
|
||||
t.Fatalf("empty key: %v", err)
|
||||
}
|
||||
})
|
||||
t.Run("database isolation", func(t *testing.T) {
|
||||
other := settings
|
||||
other.RedisDB = 1
|
||||
cache := env.newRedis(t, other)
|
||||
if _, err := cache.Get(ctx, "empty"); !errors.Is(err, outbound.ErrCacheMiss) {
|
||||
t.Fatalf("database isolation: %v", err)
|
||||
}
|
||||
})
|
||||
t.Run("authentication failure is not a miss", func(t *testing.T) {
|
||||
other := settings
|
||||
other.RedisPassword = "wrong-password"
|
||||
cache := env.newRedis(t, other)
|
||||
if err := cache.Check(ctx); err == nil {
|
||||
t.Fatal("wrong password passed readiness")
|
||||
}
|
||||
if _, err := cache.Get(ctx, "absent"); err == nil || errors.Is(err, outbound.ErrCacheMiss) {
|
||||
t.Fatalf("auth error classified as cache miss: %v", err)
|
||||
}
|
||||
})
|
||||
t.Run("caller cancellation", func(t *testing.T) {
|
||||
canceled, cancel := context.WithCancel(ctx)
|
||||
cancel()
|
||||
if _, err := cache.Get(canceled, "empty"); !errors.Is(err, context.Canceled) {
|
||||
t.Fatalf("Get ignored cancellation: %v", err)
|
||||
}
|
||||
if err := cache.Set(canceled, "canceled", []byte("value"), time.Minute); !errors.Is(err, context.Canceled) {
|
||||
t.Fatalf("Set ignored cancellation: %v", err)
|
||||
}
|
||||
if err := cache.Del(canceled, "empty"); !errors.Is(err, context.Canceled) {
|
||||
t.Fatalf("Del ignored cancellation: %v", err)
|
||||
}
|
||||
if err := cache.Check(canceled); !errors.Is(err, context.Canceled) {
|
||||
t.Fatalf("Ping ignored cancellation: %v", err)
|
||||
}
|
||||
})
|
||||
t.Run("outage is not a miss", func(t *testing.T) {
|
||||
env.stopRedis(ctx, t)
|
||||
start := time.Now()
|
||||
if _, err := cache.Get(ctx, "empty"); err == nil || errors.Is(err, outbound.ErrCacheMiss) {
|
||||
t.Fatalf("outage classified as cache miss: %v", err)
|
||||
}
|
||||
if elapsed := time.Since(start); elapsed > 2*time.Second {
|
||||
t.Fatalf("outage exceeded timeout budget: %s", elapsed)
|
||||
}
|
||||
if err := cache.Check(ctx); err == nil {
|
||||
t.Fatal("outage passed readiness")
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user