98 lines
3.0 KiB
Go
98 lines
3.0 KiB
Go
package redis
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestRPushPipelineRejectsNonPositiveBatchSize(t *testing.T) {
|
|
client := &Client{}
|
|
if _, err := client.RPushPipeline("queue", []string{"video-1"}, 0); err == nil {
|
|
t.Fatal("RPushPipeline() should reject a non-positive batch size")
|
|
}
|
|
}
|
|
|
|
func TestRPushPipelineEmptyValuesDoesNotUseClient(t *testing.T) {
|
|
client := &Client{}
|
|
length, err := client.RPushPipeline("queue", nil, 1000)
|
|
if err != nil {
|
|
t.Fatalf("RPushPipeline() error = %v", err)
|
|
}
|
|
if length != 0 {
|
|
t.Fatalf("RPushPipeline() length = %d, want 0", length)
|
|
}
|
|
}
|
|
|
|
func TestRPushPipelineContextValidatesExpiration(t *testing.T) {
|
|
client := &Client{}
|
|
for _, expirations := range [][]time.Duration{
|
|
{0},
|
|
{-time.Second},
|
|
{time.Second, time.Minute},
|
|
} {
|
|
if _, err := client.RPushPipelineContext(
|
|
context.Background(),
|
|
"queue",
|
|
[]string{"video-1"},
|
|
1,
|
|
expirations...,
|
|
); err == nil {
|
|
t.Fatalf("expirations=%v should be rejected", expirations)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestScriptHashIsStable(t *testing.T) {
|
|
first := NewScript("return ARGV[1]")
|
|
second := NewScript("return ARGV[1]")
|
|
if first.Hash() == "" || first.Hash() != second.Hash() {
|
|
t.Fatalf("script hashes differ: first=%q second=%q", first.Hash(), second.Hash())
|
|
}
|
|
}
|
|
|
|
func TestRunScriptContextValidatesArguments(t *testing.T) {
|
|
client := &Client{}
|
|
if _, err := client.RunScriptContext(nil, NewScript("return 1"), nil); err == nil {
|
|
t.Fatal("RunScriptContext() should reject a nil context")
|
|
}
|
|
if _, err := client.RunScriptContext(context.Background(), nil, nil); err == nil {
|
|
t.Fatal("RunScriptContext() should reject a nil script")
|
|
}
|
|
if _, err := client.RunScriptContext(context.Background(), NewScript("return 1"), nil); err == nil {
|
|
t.Fatal("RunScriptContext() should reject an uninitialized client")
|
|
}
|
|
}
|
|
|
|
func TestContextRedisHelpersValidateContextAndClient(t *testing.T) {
|
|
var client *Client
|
|
if _, err := client.DelContext(context.Background(), "key"); err == nil {
|
|
t.Fatal("DelContext() should reject nil client")
|
|
}
|
|
if _, err := client.EvalContext(context.Background(), "return 1", nil); err == nil {
|
|
t.Fatal("EvalContext() should reject nil client")
|
|
}
|
|
if _, err := client.RPushPipelineContext(
|
|
context.Background(), "queue", []string{"video-1"}, 1,
|
|
); err == nil {
|
|
t.Fatal("RPushPipelineContext() should reject nil client")
|
|
}
|
|
if _, err := client.SetNXContext(context.Background(), "key", "value", 0); err == nil {
|
|
t.Fatal("SetNXContext() should reject nil client")
|
|
}
|
|
|
|
empty := &Client{}
|
|
if _, err := empty.DelContext(nil, "key"); err == nil {
|
|
t.Fatal("DelContext() should reject nil context")
|
|
}
|
|
if _, err := empty.EvalContext(nil, "return 1", nil); err == nil {
|
|
t.Fatal("EvalContext() should reject nil context")
|
|
}
|
|
if _, err := empty.RPushPipelineContext(nil, "queue", []string{"video-1"}, 1); err == nil {
|
|
t.Fatal("RPushPipelineContext() should reject nil context")
|
|
}
|
|
if _, err := empty.SetNXContext(nil, "key", "value", 0); err == nil {
|
|
t.Fatal("SetNXContext() should reject nil context")
|
|
}
|
|
}
|