tracker/cache/redis/redis_test.go

249 lines
5.1 KiB
Go
Raw Normal View History

2013-08-31 07:03:44 +02:00
// Copyright 2013 The Chihaya Authors. All rights reserved.
// Use of this source code is governed by the BSD 2-Clause license,
// which can be found in the LICENSE file.
package redis
import (
2013-08-31 21:06:42 +02:00
"math/rand"
2013-08-31 07:03:44 +02:00
"os"
2013-08-31 21:06:42 +02:00
"strconv"
2013-08-31 07:03:44 +02:00
"testing"
"time"
2013-08-31 21:06:42 +02:00
"github.com/garyburd/redigo/redis"
"github.com/pushrax/chihaya/cache"
"github.com/pushrax/chihaya/config"
2013-08-31 07:03:44 +02:00
)
2013-08-31 21:06:42 +02:00
// Maximum number of parallel retries; depends on system latency
const MAX_RETRIES = 9000
2013-08-31 07:03:44 +02:00
2013-08-31 21:06:42 +02:00
func CreateTestTxObj(t *testing.T) Tx {
2013-08-31 07:03:44 +02:00
testConfig, err := config.Open(os.Getenv("TESTCONFIGPATH"))
if err != nil {
t.Error(err)
}
testDialFunc := makeDialFunc(&testConfig.Cache)
testConn, err := testDialFunc()
if err != nil {
2013-08-31 21:14:21 +02:00
t.Error(err)
2013-08-31 07:03:44 +02:00
}
return Tx{&testConfig.Cache, false, false, testConn}
}
2013-08-31 21:06:42 +02:00
func SampleTransaction(testTx Tx, retries int, t *testing.T) {
2013-08-31 07:03:44 +02:00
defer func() {
2013-08-31 21:06:42 +02:00
if err := recover(); err != nil {
t.Errorf("initiateRead() failed: %s", err)
2013-08-31 07:03:44 +02:00
}
2013-08-31 21:06:42 +02:00
}()
2013-08-31 07:03:44 +02:00
err := testTx.initiateRead()
if err != nil {
2013-08-31 21:06:42 +02:00
t.Error(err)
2013-08-31 07:03:44 +02:00
}
_, err = testTx.Do("WATCH", "testKeyA")
if err != nil {
2013-08-31 21:06:42 +02:00
t.Error(err)
2013-08-31 07:03:44 +02:00
}
_, err = redis.String(testTx.Do("GET", "testKeyA"))
if err != nil {
if err == redis.ErrNil {
t.Log("redis.ErrNil")
} else {
2013-08-31 21:06:42 +02:00
t.Error(err)
2013-08-31 07:03:44 +02:00
}
}
_, err = testTx.Do("WATCH", "testKeyB")
if err != nil {
if err == redis.ErrNil {
t.Log("redis.ErrNil")
} else {
2013-08-31 21:06:42 +02:00
t.Error(err)
2013-08-31 07:03:44 +02:00
}
}
_, err = redis.String(testTx.Do("GET", "testKeyB"))
if err != nil {
if err == redis.ErrNil {
t.Log("redis.ErrNil")
} else {
2013-08-31 21:06:42 +02:00
t.Error(err)
2013-08-31 07:03:44 +02:00
}
}
err = testTx.initiateWrite()
if err != nil {
2013-08-31 21:06:42 +02:00
t.Error(err)
2013-08-31 07:03:44 +02:00
}
2013-08-31 21:06:42 +02:00
// Generate random data to set
2013-08-31 07:03:44 +02:00
randGen := rand.New(rand.NewSource(time.Now().UnixNano()))
err = testTx.Send("SET", "testKeyA", strconv.Itoa(randGen.Int()))
if err != nil {
2013-08-31 21:06:42 +02:00
t.Error(err)
2013-08-31 07:03:44 +02:00
}
err = testTx.Send("SET", "testKeyB", strconv.Itoa(randGen.Int()))
if err != nil {
2013-08-31 21:06:42 +02:00
t.Error(err)
2013-08-31 07:03:44 +02:00
}
err = testTx.Commit()
2013-08-31 21:06:42 +02:00
switch {
// For parallel runs, there may be conflicts, retry until successful
case err == cache.ErrTxConflict && retries > 0:
// t.Logf("Conflict, %d retries left",retries)
SampleTransaction(testTx, retries-1, t)
// Clear TxConflict, if retries max out, errors are already recorded
2013-08-31 07:03:44 +02:00
err = nil
2013-08-31 21:06:42 +02:00
case err == cache.ErrTxConflict:
t.Errorf("Conflict encountered, max retries reached: %s", err)
case err != nil:
t.Error(err)
default:
2013-08-31 07:03:44 +02:00
}
}
func TestRedisTransaction(t *testing.T) {
2013-08-31 21:06:42 +02:00
for i := 0; i < 10; i++ {
// No retries for serial transactions
SampleTransaction(CreateTestTxObj(t), 0, t)
2013-08-31 07:03:44 +02:00
}
}
func TestReadAfterWrite(t *testing.T) {
2013-08-31 21:06:42 +02:00
defer func() {
if err := recover(); err == nil {
t.Error("Read after write did not panic")
}
}()
2013-08-31 07:03:44 +02:00
testTx := CreateTestTxObj(t)
err := testTx.initiateWrite()
if err != nil {
2013-08-31 21:06:42 +02:00
t.Error(err)
2013-08-31 07:03:44 +02:00
}
2013-08-31 21:06:42 +02:00
2013-08-31 07:03:44 +02:00
err = testTx.initiateRead()
if err != nil {
2013-08-31 21:06:42 +02:00
t.Error(err)
2013-08-31 07:03:44 +02:00
}
}
2013-08-31 21:06:42 +02:00
func TestCloseClosedTransaction(t *testing.T) {
defer func() {
if err := recover(); err == nil {
t.Error("Closing a closed transaction did not panic")
}
}()
2013-08-31 07:03:44 +02:00
testTx := CreateTestTxObj(t)
testTx.close()
testTx.close()
}
func TestParallelTx0(t *testing.T) {
t.Parallel()
2013-08-31 21:06:42 +02:00
for i := 0; i < 20; i++ {
go SampleTransaction(CreateTestTxObj(t), MAX_RETRIES, t)
2013-08-31 07:03:44 +02:00
time.Sleep(1 * time.Millisecond)
}
}
func TestParallelTx1(t *testing.T) {
t.Parallel()
2013-08-31 21:06:42 +02:00
SampleTransaction(CreateTestTxObj(t), MAX_RETRIES, t)
for i := 0; i < 100; i++ {
go SampleTransaction(CreateTestTxObj(t), MAX_RETRIES, t)
2013-08-31 07:03:44 +02:00
}
}
func TestParallelTx2(t *testing.T) {
t.Parallel()
2013-08-31 21:06:42 +02:00
for i := 0; i < 100; i++ {
go SampleTransaction(CreateTestTxObj(t), MAX_RETRIES, t)
2013-08-31 07:03:44 +02:00
}
2013-08-31 21:06:42 +02:00
SampleTransaction(CreateTestTxObj(t), MAX_RETRIES, t)
2013-08-31 07:03:44 +02:00
}
2013-08-31 21:06:42 +02:00
// Just in case the above parallel tests didn't fail, force a failure here
2013-08-31 07:03:44 +02:00
func TestParallelInterrupted(t *testing.T) {
t.Parallel()
defer func() {
2013-08-31 21:06:42 +02:00
if err := recover(); err != nil {
t.Errorf("initiateRead() failed in parallel: %s", err)
2013-08-31 07:03:44 +02:00
}
2013-08-31 21:06:42 +02:00
}()
testTx := CreateTestTxObj(t)
2013-08-31 07:03:44 +02:00
err := testTx.initiateRead()
if err != nil {
2013-08-31 21:06:42 +02:00
t.Error(err)
2013-08-31 07:03:44 +02:00
}
_, err = testTx.Do("WATCH", "testKeyA")
if err != nil {
2013-08-31 21:06:42 +02:00
t.Error(err)
2013-08-31 07:03:44 +02:00
}
testValueA, err := redis.String(testTx.Do("GET", "testKeyA"))
if err != nil {
if err == redis.ErrNil {
t.Log("redis.ErrNil")
} else {
2013-08-31 21:06:42 +02:00
t.Error(err)
2013-08-31 07:03:44 +02:00
}
}
_, err = testTx.Do("WATCH", "testKeyB")
if err != nil {
if err == redis.ErrNil {
t.Log("redis.ErrNil")
} else {
2013-08-31 21:06:42 +02:00
t.Error(err)
2013-08-31 07:03:44 +02:00
}
}
testValueB, err := redis.String(testTx.Do("GET", "testKeyB"))
if err != nil {
if err == redis.ErrNil {
t.Log("redis.ErrNil")
} else {
2013-08-31 21:06:42 +02:00
t.Error(err)
2013-08-31 07:03:44 +02:00
}
}
2013-08-31 21:06:42 +02:00
// Stand in for what real updates would do
2013-08-31 07:03:44 +02:00
testValueB = testValueB + "+updates"
testValueA = testValueA + "+updates"
2013-08-31 21:06:42 +02:00
// Simulating another client interrupts transaction, causing exec to fail
SampleTransaction(CreateTestTxObj(t), MAX_RETRIES, t)
2013-08-31 07:03:44 +02:00
err = testTx.initiateWrite()
if err != nil {
2013-08-31 21:06:42 +02:00
t.Error(err)
2013-08-31 07:03:44 +02:00
}
err = testTx.Send("SET", "testKeyA", testValueA)
if err != nil {
2013-08-31 21:06:42 +02:00
t.Error(err)
2013-08-31 07:03:44 +02:00
}
err = testTx.Send("SET", "testKeyB", testValueB)
if err != nil {
2013-08-31 21:06:42 +02:00
t.Error(err)
2013-08-31 07:03:44 +02:00
}
keys, err := (testTx.Do("EXEC"))
if keys != nil {
2013-08-31 21:06:42 +02:00
t.Error("Keys not nil; exec should have been interrupted")
2013-08-31 07:03:44 +02:00
}
testTx.close()
if err != nil {
2013-08-31 21:06:42 +02:00
t.Error(err)
2013-08-31 07:03:44 +02:00
}
}