Files
zhangdeman 52205b42c8
Verify / verify (push) Canceled after 0s
Verify / verify (pull_request) Canceled after 0s
chore(module): 更新仓库地址及模块导入路径
2026-11-27 22:58:26 -08:00

77 lines
2.4 KiB
Go

package redis
import (
"context"
"errors"
"path/filepath"
"testing"
"time"
"git.zhangdeman.cn/zhangdeman/ginframework/fault"
"git.zhangdeman.cn/zhangdeman/ginframework/internal/testutil/redisserver"
)
func TestOpenNativeClientAndProbeFailure(t *testing.T) {
native := redisserver.New(t, 3)
disableRetries := -1
client, err := Open(context.Background(), Config{Name: "test", Network: "unix", Address: native.Options().Addr, MaxRetries: &disableRetries}, nil)
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
if err := client.Close(); err != nil {
t.Error(err)
}
})
if err := client.Set(context.Background(), "example", "kept", 0).Err(); err != nil {
t.Fatal(err)
}
if value, err := client.Get(context.Background(), "example").Result(); err != nil || value != "kept" {
t.Fatalf("native result changed: %q %v", value, err)
}
missing, err := Open(context.Background(), Config{Network: "unix", Address: filepath.Join(t.TempDir(), "missing.sock"), MaxRetries: &disableRetries, ProbeTimeoutMS: 50}, nil)
if missing != nil || !fault.Internal(err) {
t.Fatal("failed probe published a client")
}
cancelled, cancel := context.WithCancel(context.Background())
cancel()
missing, err = Open(cancelled, Config{Network: "unix", Address: native.Options().Addr, MaxRetries: &disableRetries}, nil)
if missing != nil || !errors.Is(err, context.Canceled) {
t.Fatal("startup cancellation lost")
}
}
func TestReadWriteTimeoutSentinelsKeepNativeSemantics(t *testing.T) {
native := redisserver.New(t, 3)
for _, test := range []struct {
name string
value int
expected time.Duration
}{
{name: "no timeout", value: -1, expected: 0},
{name: "skip deadline", value: -2, expected: -1},
{name: "milliseconds", value: 120, expected: 120 * time.Millisecond},
} {
t.Run(test.name, func(t *testing.T) {
client, err := Open(context.Background(), Config{
Network: "unix",
Address: native.Options().Addr,
ReadTimeoutMS: &test.value,
WriteTimeoutMS: &test.value,
}, nil)
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
if err := client.Close(); err != nil {
t.Error(err)
}
})
options := client.Options()
if options.ReadTimeout != test.expected || options.WriteTimeout != test.expected {
t.Fatalf("native timeout semantics lost: read=%v write=%v", options.ReadTimeout, options.WriteTimeout)
}
})
}
}