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,68 @@
|
||||
package redis
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"net"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"lincodex/backend/config"
|
||||
"lincodex/backend/internal/port/outbound"
|
||||
)
|
||||
|
||||
func TestColdConnectionHonorsTimeout(t *testing.T) {
|
||||
for _, callerDeadline := range []bool{false, true} {
|
||||
name := "configured timeout"
|
||||
if callerDeadline {
|
||||
name = "shorter caller deadline"
|
||||
}
|
||||
t.Run(name, func(t *testing.T) {
|
||||
listener, err := (&net.ListenConfig{}).Listen(t.Context(), "tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
release := make(chan struct{})
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
defer close(done)
|
||||
connection, err := listener.Accept()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer connection.Close()
|
||||
<-release
|
||||
}()
|
||||
t.Cleanup(func() {
|
||||
close(release)
|
||||
_ = listener.Close()
|
||||
<-done
|
||||
})
|
||||
timeout := 100 * time.Millisecond
|
||||
if callerDeadline {
|
||||
timeout = time.Second
|
||||
}
|
||||
cache, cleanup, err := NewCache(config.Config{RedisAddr: listener.Addr().String(), RedisTimeout: timeout}, slog.New(slog.DiscardHandler))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(cleanup)
|
||||
ctx := t.Context()
|
||||
if callerDeadline {
|
||||
var cancel context.CancelFunc
|
||||
ctx, cancel = context.WithTimeout(ctx, 100*time.Millisecond)
|
||||
defer cancel()
|
||||
}
|
||||
started := time.Now()
|
||||
_, err = cache.Get(ctx, "absent")
|
||||
elapsed := time.Since(started)
|
||||
if err == nil || errors.Is(err, outbound.ErrCacheMiss) {
|
||||
t.Fatalf("unresponsive server: %v", err)
|
||||
}
|
||||
if elapsed > 500*time.Millisecond {
|
||||
t.Fatalf("cold connection ignored timeout: elapsed=%s error=%v", elapsed, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user