34 lines
715 B
Go
34 lines
715 B
Go
package redispkg
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestDisabledIsSafe(t *testing.T) {
|
|
r := New("")
|
|
ctx := context.Background()
|
|
if _, ok := r.Get(ctx, "x"); ok {
|
|
t.Fatal("disabled Get must miss")
|
|
}
|
|
r.Set(ctx, "x", "y", time.Second) // 不 panic
|
|
if n := r.IncrWindow(ctx, "k", time.Second); n != 1 {
|
|
t.Fatal("disabled IncrWindow must allow")
|
|
}
|
|
un, ok := r.Lock(ctx, "lk", time.Second)
|
|
if !ok {
|
|
t.Fatal("disabled Lock must always acquire")
|
|
}
|
|
un()
|
|
}
|
|
|
|
func TestDeadRedisLockFailsOpen(t *testing.T) {
|
|
r := New("redis://127.0.0.1:16399") // 死端口
|
|
un, ok := r.Lock(context.Background(), "lk", time.Second)
|
|
if !ok {
|
|
t.Fatal("Lock on redis error must fail open (spec §9)")
|
|
}
|
|
un()
|
|
}
|