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-09-01 21:59:46 +02:00
|
|
|
"encoding/json"
|
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-09-01 21:59:46 +02:00
|
|
|
"strings"
|
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-09-01 21:59:46 +02:00
|
|
|
"github.com/pushrax/chihaya/models"
|
2013-08-31 07:03:44 +02:00
|
|
|
)
|
|
|
|
|
2013-09-02 01:05:48 +02:00
|
|
|
// Maximum number of parallel retries; depends on system latency
|
2013-08-31 21:06:42 +02:00
|
|
|
const MAX_RETRIES = 9000
|
2013-09-02 01:05:48 +02:00
|
|
|
const sample_infohash = "58c290f4ea1efb3adcb8c1ed2643232117577bcd"
|
|
|
|
const sample_passkey = "32426b162be0bce5428e7e36afaf734ae5afb355"
|
2013-08-31 07:03:44 +02:00
|
|
|
|
2013-09-02 01:05:48 +02:00
|
|
|
// Common interface for benchmarks and test error reporting
|
2013-09-01 21:59:46 +02:00
|
|
|
type TestReporter interface {
|
|
|
|
Error(args ...interface{})
|
2013-09-02 01:05:48 +02:00
|
|
|
Errorf(format string, args ...interface{})
|
|
|
|
Log(args ...interface{})
|
|
|
|
Logf(format string, args ...interface{})
|
2013-09-01 21:59:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func verifyErrNil(err error, t TestReporter) {
|
2013-08-31 07:03:44 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2013-09-01 21:59:46 +02:00
|
|
|
}
|
2013-08-31 07:03:44 +02:00
|
|
|
|
2013-09-02 01:05:48 +02:00
|
|
|
func createTestTxObj(t TestReporter) *Tx {
|
2013-09-01 21:59:46 +02:00
|
|
|
testConfig, err := config.Open(os.Getenv("TESTCONFIGPATH"))
|
|
|
|
conf := &testConfig.Cache
|
|
|
|
verifyErrNil(err, t)
|
|
|
|
|
|
|
|
testPool := &Pool{
|
|
|
|
conf: conf,
|
|
|
|
pool: redis.Pool{
|
2013-09-07 00:39:14 +02:00
|
|
|
MaxIdle: conf.MaxIdleConns,
|
2013-09-01 21:59:46 +02:00
|
|
|
IdleTimeout: conf.IdleTimeout.Duration,
|
|
|
|
Dial: makeDialFunc(conf),
|
|
|
|
TestOnBorrow: testOnBorrow,
|
|
|
|
},
|
2013-08-31 07:03:44 +02:00
|
|
|
}
|
2013-09-01 21:59:46 +02:00
|
|
|
|
|
|
|
//testDialFunc := makeDialFunc(&testConfig.Cache)
|
|
|
|
//testConn, err := testDialFunc()
|
|
|
|
txObj := &Tx{
|
|
|
|
conf: testPool.conf,
|
|
|
|
done: false,
|
|
|
|
multi: false,
|
|
|
|
Conn: testPool.pool.Get(),
|
|
|
|
}
|
|
|
|
verifyErrNil(err, t)
|
|
|
|
|
|
|
|
// Test connection before returning
|
|
|
|
//txObj := Tx{&testConfig.Cache, false, false, testConn}
|
|
|
|
_, err = txObj.Do("PING")
|
|
|
|
verifyErrNil(err, t)
|
|
|
|
return txObj
|
2013-08-31 07:03:44 +02:00
|
|
|
}
|
|
|
|
|
2013-09-02 01:05:48 +02:00
|
|
|
func createUserFromValues(userVals []string, t TestReporter) *models.User {
|
|
|
|
ID, err := strconv.ParseUint(userVals[0], 10, 64)
|
|
|
|
verifyErrNil(err, t)
|
|
|
|
Passkey := userVals[1]
|
|
|
|
UpMultiplier, err := strconv.ParseFloat(userVals[2], 64)
|
|
|
|
verifyErrNil(err, t)
|
|
|
|
DownMultiplier, err := strconv.ParseFloat(userVals[3], 64)
|
|
|
|
Slots, err := strconv.ParseInt(userVals[4], 10, 64)
|
|
|
|
verifyErrNil(err, t)
|
|
|
|
SlotsUsed, err := strconv.ParseInt(userVals[5], 10, 64)
|
|
|
|
verifyErrNil(err, t)
|
|
|
|
return &models.User{ID, Passkey, UpMultiplier, DownMultiplier, Slots, SlotsUsed}
|
|
|
|
}
|
|
|
|
|
|
|
|
func createUser() models.User {
|
|
|
|
testUser := models.User{214, "32426b162be0bce5428e7e36afaf734ae5afb355", 0.0, 0.0, 4, 2}
|
|
|
|
return testUser
|
|
|
|
}
|
|
|
|
|
2013-09-04 05:58:13 +02:00
|
|
|
func createSeeders() []models.Peer {
|
2013-09-02 01:05:48 +02:00
|
|
|
testSeeders := make([]models.Peer, 4)
|
|
|
|
testSeeders[0] = models.Peer{"testPeerID0", 57005, 48879, "testIP", 6889, 1024, 3000, 4200, 6}
|
|
|
|
testSeeders[1] = models.Peer{"testPeerID1", 10101, 48879, "testIP", 6889, 1024, 3000, 4200, 6}
|
|
|
|
testSeeders[2] = models.Peer{"testPeerID2", 29890, 48879, "testIP", 6889, 1024, 3000, 4200, 6}
|
|
|
|
testSeeders[3] = models.Peer{"testPeerID3", 65261, 48879, "testIP", 6889, 1024, 3000, 4200, 6}
|
2013-09-04 05:58:13 +02:00
|
|
|
return testSeeders
|
|
|
|
}
|
|
|
|
|
|
|
|
func createLeechers() []models.Peer {
|
2013-09-02 01:05:48 +02:00
|
|
|
testLeechers := make([]models.Peer, 1)
|
|
|
|
testLeechers[0] = models.Peer{"testPeerID", 11111, 48879, "testIP", 6889, 1024, 3000, 4200, 6}
|
2013-09-04 05:58:13 +02:00
|
|
|
return testLeechers
|
|
|
|
}
|
|
|
|
|
|
|
|
func createTorrent() models.Torrent {
|
|
|
|
|
|
|
|
testSeeders := createSeeders()
|
|
|
|
testLeechers := createLeechers()
|
2013-09-02 01:05:48 +02:00
|
|
|
|
|
|
|
seeders := make(map[string]models.Peer)
|
|
|
|
for i := range testSeeders {
|
|
|
|
seeders[testSeeders[i].ID] = testSeeders[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
leechers := make(map[string]models.Peer)
|
|
|
|
for i := range testLeechers {
|
|
|
|
leechers[testLeechers[i].ID] = testLeechers[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
testTorrent := models.Torrent{48879, sample_infohash, true, seeders, leechers, 11, 0.0, 0.0, 0}
|
|
|
|
return testTorrent
|
|
|
|
}
|
|
|
|
|
|
|
|
func createTorrentJson(t TestReporter) []byte {
|
|
|
|
jsonTorrent, err := json.Marshal(createTorrent())
|
|
|
|
verifyErrNil(err, t)
|
|
|
|
return jsonTorrent
|
|
|
|
}
|
|
|
|
|
|
|
|
func createUserJson(t TestReporter) []byte {
|
|
|
|
jsonUser, err := json.Marshal(createUser())
|
|
|
|
verifyErrNil(err, t)
|
|
|
|
return jsonUser
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleJsonTransaction(testTx *Tx, retries int, t TestReporter) {
|
2013-08-31 07:03:44 +02:00
|
|
|
defer func() {
|
2013-09-02 01:05:48 +02:00
|
|
|
if err := recover(); err != nil {
|
|
|
|
t.Error(err)
|
2013-08-31 07:03:44 +02:00
|
|
|
}
|
2013-08-31 21:06:42 +02:00
|
|
|
}()
|
2013-09-01 21:59:46 +02:00
|
|
|
verifyErrNil(testTx.initiateRead(), t)
|
|
|
|
_, err := testTx.Do("WATCH", "testKeyA")
|
|
|
|
verifyErrNil(err, t)
|
|
|
|
|
2013-08-31 07:03:44 +02:00
|
|
|
_, err = redis.String(testTx.Do("GET", "testKeyA"))
|
|
|
|
if err != nil {
|
|
|
|
if err == redis.ErrNil {
|
2013-09-01 21:59:46 +02:00
|
|
|
t.Log("testKeyA does not exist yet")
|
2013-08-31 07:03:44 +02:00
|
|
|
} 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")
|
2013-09-02 01:05:48 +02:00
|
|
|
verifyErrNil(err, t)
|
2013-09-01 21:59:46 +02:00
|
|
|
|
2013-08-31 07:03:44 +02:00
|
|
|
_, err = redis.String(testTx.Do("GET", "testKeyB"))
|
|
|
|
if err != nil {
|
|
|
|
if err == redis.ErrNil {
|
2013-09-01 21:59:46 +02:00
|
|
|
t.Log("testKeyB does not exist yet")
|
2013-08-31 07:03:44 +02:00
|
|
|
} else {
|
2013-08-31 21:06:42 +02:00
|
|
|
t.Error(err)
|
2013-08-31 07:03:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-01 21:59:46 +02:00
|
|
|
verifyErrNil(testTx.initiateWrite(), t)
|
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()))
|
2013-09-01 21:59:46 +02:00
|
|
|
verifyErrNil(testTx.Send("SET", "testKeyA", strconv.Itoa(randGen.Int())), t)
|
|
|
|
verifyErrNil(testTx.Send("SET", "testKeyB", strconv.Itoa(randGen.Int())), t)
|
2013-08-31 07:03:44 +02:00
|
|
|
|
|
|
|
err = testTx.Commit()
|
2013-08-31 21:06:42 +02:00
|
|
|
// For parallel runs, there may be conflicts, retry until successful
|
2013-09-01 21:59:46 +02:00
|
|
|
if err == cache.ErrTxConflict && retries > 0 {
|
2013-09-02 01:05:48 +02:00
|
|
|
ExampleJsonTransaction(testTx, retries-1, t)
|
2013-08-31 21:06:42 +02:00
|
|
|
// Clear TxConflict, if retries max out, errors are already recorded
|
2013-08-31 07:03:44 +02:00
|
|
|
err = nil
|
2013-09-01 21:59:46 +02:00
|
|
|
} else if err == cache.ErrTxConflict {
|
|
|
|
t.Error("Conflict encountered, max retries reached")
|
2013-08-31 21:06:42 +02:00
|
|
|
t.Error(err)
|
2013-08-31 07:03:44 +02:00
|
|
|
}
|
2013-09-01 21:59:46 +02:00
|
|
|
verifyErrNil(err, t)
|
|
|
|
}
|
|
|
|
|
2013-09-04 05:58:13 +02:00
|
|
|
func ExampleJsonSchemaRemoveSeeder(torrent *models.Torrent, peer *models.Peer, t TestReporter) {
|
2013-09-02 01:05:48 +02:00
|
|
|
testTx := createTestTxObj(t)
|
|
|
|
|
|
|
|
verifyErrNil(testTx.initiateRead(), t)
|
|
|
|
|
2013-09-04 05:58:13 +02:00
|
|
|
key := testTx.conf.Prefix + "torrent:" + torrent.Infohash
|
2013-09-02 01:05:48 +02:00
|
|
|
_, err := testTx.Do("WATCH", key)
|
|
|
|
reply, err := redis.String(testTx.Do("GET", key))
|
|
|
|
if err != nil {
|
|
|
|
if err == redis.ErrNil {
|
2013-09-04 05:58:13 +02:00
|
|
|
t.Error("testTorrent does not exist")
|
2013-09-02 01:05:48 +02:00
|
|
|
} else {
|
|
|
|
t.Error(err)
|
2013-09-01 21:59:46 +02:00
|
|
|
}
|
2013-09-02 01:05:48 +02:00
|
|
|
}
|
2013-09-01 21:59:46 +02:00
|
|
|
|
2013-09-02 01:05:48 +02:00
|
|
|
verifyErrNil(json.NewDecoder(strings.NewReader(reply)).Decode(torrent), t)
|
2013-09-01 21:59:46 +02:00
|
|
|
|
2013-09-02 01:05:48 +02:00
|
|
|
delete(torrent.Seeders, "testPeerID2")
|
|
|
|
|
|
|
|
jsonTorrent, err := json.Marshal(torrent)
|
|
|
|
verifyErrNil(err, t)
|
|
|
|
verifyErrNil(testTx.initiateWrite(), t)
|
|
|
|
verifyErrNil(testTx.Send("SET", key, jsonTorrent), t)
|
|
|
|
verifyErrNil(testTx.Commit(), t)
|
2013-09-01 21:59:46 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-09-04 05:58:13 +02:00
|
|
|
func ExampleRedisTypeSchemaRemoveSeeder(torrent *models.Torrent, peer *models.Peer, t TestReporter) {
|
2013-09-02 01:05:48 +02:00
|
|
|
testTx := createTestTxObj(t)
|
2013-09-04 05:58:13 +02:00
|
|
|
setkey := testTx.conf.Prefix + "torrent:" + torrent.Infohash + ":seeders"
|
|
|
|
reply, err := redis.Int(testTx.Do("SREM", setkey, *peer))
|
2013-09-02 01:05:48 +02:00
|
|
|
if reply == 0 {
|
2013-09-04 05:58:13 +02:00
|
|
|
t.Errorf("remove %v failed", *peer)
|
2013-09-02 01:05:48 +02:00
|
|
|
}
|
|
|
|
verifyErrNil(err, t)
|
|
|
|
}
|
2013-09-01 21:59:46 +02:00
|
|
|
|
2013-09-04 05:58:13 +02:00
|
|
|
func ExampleJsonSchemaFindUser(passkey string, t TestReporter) (*models.User, bool) {
|
2013-09-02 01:05:48 +02:00
|
|
|
testTx := createTestTxObj(t)
|
2013-09-01 21:59:46 +02:00
|
|
|
|
2013-09-02 01:05:48 +02:00
|
|
|
verifyErrNil(testTx.initiateRead(), t)
|
2013-09-01 21:59:46 +02:00
|
|
|
|
2013-09-04 05:58:13 +02:00
|
|
|
key := testTx.conf.Prefix + "user:" + passkey
|
2013-09-02 01:05:48 +02:00
|
|
|
_, err := testTx.Do("WATCH", key)
|
|
|
|
verifyErrNil(err, t)
|
|
|
|
reply, err := redis.String(testTx.Do("GET", key))
|
|
|
|
if err != nil {
|
|
|
|
if err == redis.ErrNil {
|
|
|
|
return nil, false
|
|
|
|
} else {
|
|
|
|
t.Error(err)
|
2013-09-01 21:59:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-02 01:05:48 +02:00
|
|
|
user := &models.User{}
|
|
|
|
verifyErrNil(json.NewDecoder(strings.NewReader(reply)).Decode(user), t)
|
|
|
|
return user, true
|
2013-09-01 21:59:46 +02:00
|
|
|
}
|
|
|
|
|
2013-09-04 05:58:13 +02:00
|
|
|
func ExampleRedisTypesSchemaFindUser(passkey string, t TestReporter) (*models.User, bool) {
|
2013-09-02 01:05:48 +02:00
|
|
|
testTx := createTestTxObj(t)
|
|
|
|
hashkey := testTx.conf.Prefix + "user_hash:" + sample_passkey
|
|
|
|
userVals, err := redis.Strings(testTx.Do("HVALS", hashkey))
|
|
|
|
if userVals == nil {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
verifyErrNil(err, t)
|
|
|
|
compareUser := createUserFromValues(userVals, t)
|
|
|
|
return compareUser, true
|
|
|
|
}
|
2013-09-01 21:59:46 +02:00
|
|
|
|
2013-09-02 01:05:48 +02:00
|
|
|
func BenchmarkRedisJsonSchemaRemoveSeeder(b *testing.B) {
|
|
|
|
for bCount := 0; bCount < b.N; bCount++ {
|
2013-09-04 05:58:13 +02:00
|
|
|
b.StopTimer()
|
|
|
|
testTx := createTestTxObj(b)
|
|
|
|
testTorrent := createTorrent()
|
|
|
|
testSeeders := createSeeders()
|
|
|
|
key := testTx.conf.Prefix + "torrent:" + testTorrent.Infohash
|
|
|
|
// Benchmark setup not a transaction, not thread-safe
|
|
|
|
_, err := testTx.Do("SET", key, createTorrentJson(b))
|
|
|
|
verifyErrNil(err, b)
|
|
|
|
b.StartTimer()
|
|
|
|
|
|
|
|
ExampleJsonSchemaRemoveSeeder(&testTorrent, &testSeeders[2], b)
|
2013-09-01 21:59:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-02 01:05:48 +02:00
|
|
|
func BenchmarkRedisTypesSchemaRemoveSeeder(b *testing.B) {
|
2013-09-01 21:59:46 +02:00
|
|
|
for bCount := 0; bCount < b.N; bCount++ {
|
|
|
|
|
2013-09-04 05:58:13 +02:00
|
|
|
// Ensure that remove completes successfully,
|
|
|
|
// even if it doesn't impact the performance
|
2013-09-01 21:59:46 +02:00
|
|
|
b.StopTimer()
|
2013-09-02 01:05:48 +02:00
|
|
|
testTx := createTestTxObj(b)
|
2013-09-04 05:58:13 +02:00
|
|
|
testTorrent := createTorrent()
|
|
|
|
setkey := testTx.conf.Prefix + "torrent:" + testTorrent.Infohash + ":seeders"
|
|
|
|
testSeeders := createSeeders()
|
|
|
|
reply, err := redis.Int(testTx.Do("SADD", setkey,
|
|
|
|
testSeeders[0],
|
|
|
|
testSeeders[1],
|
|
|
|
testSeeders[2],
|
|
|
|
testSeeders[3]))
|
|
|
|
|
2013-09-02 01:05:48 +02:00
|
|
|
if reply == 0 {
|
2013-09-04 05:58:13 +02:00
|
|
|
b.Log("no keys added!")
|
2013-09-01 21:59:46 +02:00
|
|
|
}
|
|
|
|
verifyErrNil(err, b)
|
|
|
|
b.StartTimer()
|
|
|
|
|
2013-09-04 05:58:13 +02:00
|
|
|
ExampleRedisTypeSchemaRemoveSeeder(&testTorrent, &testSeeders[2], b)
|
2013-09-02 01:05:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkRedisJsonSchemaFindUser(b *testing.B) {
|
|
|
|
// Ensure successful user find ( a failed lookup may have different performance )
|
|
|
|
b.StopTimer()
|
|
|
|
testTx := createTestTxObj(b)
|
|
|
|
testUser := createUser()
|
|
|
|
userJson := string(createUserJson(b))
|
|
|
|
verifyErrNil(testTx.initiateWrite(), b)
|
|
|
|
key := testTx.conf.Prefix + "user:" + sample_passkey
|
|
|
|
verifyErrNil(testTx.Send("SET", key, userJson), b)
|
|
|
|
verifyErrNil(testTx.Commit(), b)
|
|
|
|
b.StartTimer()
|
|
|
|
for bCount := 0; bCount < b.N; bCount++ {
|
2013-09-04 05:58:13 +02:00
|
|
|
compareUser, exists := ExampleJsonSchemaFindUser(sample_passkey, b)
|
2013-09-02 01:05:48 +02:00
|
|
|
b.StopTimer()
|
|
|
|
if !exists {
|
|
|
|
b.Error("User not found!")
|
2013-09-01 21:59:46 +02:00
|
|
|
}
|
2013-09-02 01:05:48 +02:00
|
|
|
if testUser != *compareUser {
|
2013-09-01 21:59:46 +02:00
|
|
|
b.Errorf("user mismatch: %v vs. %v", compareUser, testUser)
|
|
|
|
}
|
2013-09-02 01:05:48 +02:00
|
|
|
b.StartTimer()
|
2013-09-01 21:59:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-02 01:05:48 +02:00
|
|
|
func BenchmarkRedisTypesSchemaFindUser(b *testing.B) {
|
2013-09-01 21:59:46 +02:00
|
|
|
|
2013-09-02 01:05:48 +02:00
|
|
|
// Ensure successful user find ( a failed lookup may have different performance )
|
|
|
|
b.StopTimer()
|
|
|
|
testUser := createUser()
|
|
|
|
testTx := createTestTxObj(b)
|
|
|
|
hashkey := testTx.conf.Prefix + "user_hash:" + sample_passkey
|
2013-09-04 05:58:13 +02:00
|
|
|
reply, err := testTx.Do("HMSET", hashkey,
|
|
|
|
"id", testUser.ID,
|
|
|
|
"passkey", testUser.Passkey,
|
|
|
|
"up_multiplier", testUser.UpMultiplier,
|
|
|
|
"down_multiplier", testUser.DownMultiplier,
|
|
|
|
"slots", testUser.Slots,
|
|
|
|
"slots_used", testUser.SlotsUsed)
|
|
|
|
|
2013-09-02 01:05:48 +02:00
|
|
|
if reply == nil {
|
|
|
|
b.Error("no hash fields added!")
|
|
|
|
}
|
|
|
|
verifyErrNil(err, b)
|
|
|
|
b.StartTimer()
|
2013-09-01 21:59:46 +02:00
|
|
|
|
2013-09-02 01:05:48 +02:00
|
|
|
for bCount := 0; bCount < b.N; bCount++ {
|
2013-09-01 21:59:46 +02:00
|
|
|
|
2013-09-04 05:58:13 +02:00
|
|
|
compareUser, exists := ExampleRedisTypesSchemaFindUser(sample_passkey, b)
|
2013-09-01 21:59:46 +02:00
|
|
|
|
2013-09-02 01:05:48 +02:00
|
|
|
b.StopTimer()
|
|
|
|
if !exists {
|
|
|
|
b.Error("User not found!")
|
|
|
|
}
|
|
|
|
if testUser != *compareUser {
|
|
|
|
b.Errorf("user mismatch: %v vs. %v", compareUser, testUser)
|
|
|
|
}
|
|
|
|
b.StartTimer()
|
2013-09-01 21:59:46 +02:00
|
|
|
}
|
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
|
2013-09-02 01:05:48 +02:00
|
|
|
ExampleJsonTransaction(createTestTxObj(t), 0, t)
|
2013-08-31 07:03:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReadAfterWrite(t *testing.T) {
|
2013-09-01 21:59:46 +02:00
|
|
|
// Test requires panic
|
2013-08-31 21:06:42 +02:00
|
|
|
defer func() {
|
2013-09-02 01:05:48 +02:00
|
|
|
if err := recover(); err == nil {
|
2013-08-31 21:06:42 +02:00
|
|
|
t.Error("Read after write did not panic")
|
|
|
|
}
|
|
|
|
}()
|
2013-08-31 07:03:44 +02:00
|
|
|
|
2013-09-01 21:59:46 +02:00
|
|
|
testTx := createTestTxObj(t)
|
2013-09-02 01:05:48 +02:00
|
|
|
verifyErrNil(testTx.initiateWrite(), t)
|
|
|
|
verifyErrNil(testTx.initiateRead(), t)
|
|
|
|
}
|
2013-09-01 21:59:46 +02:00
|
|
|
|
2013-09-02 01:05:48 +02:00
|
|
|
func TestCloseClosedTransaction(t *testing.T) {
|
2013-09-01 21:59:46 +02:00
|
|
|
//require panic
|
2013-08-31 21:06:42 +02:00
|
|
|
defer func() {
|
2013-09-02 01:05:48 +02:00
|
|
|
if err := recover(); err == nil {
|
|
|
|
t.Error("Closing a closed transaction did not panic")
|
2013-08-31 21:06:42 +02:00
|
|
|
}
|
|
|
|
}()
|
2013-09-02 01:05:48 +02:00
|
|
|
|
|
|
|
testTx := createTestTxObj(t)
|
|
|
|
testTx.close()
|
2013-08-31 07:03:44 +02:00
|
|
|
testTx.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParallelTx0(t *testing.T) {
|
2013-09-01 21:59:46 +02:00
|
|
|
if testing.Short() {
|
|
|
|
t.SkipNow()
|
|
|
|
}
|
2013-08-31 07:03:44 +02:00
|
|
|
t.Parallel()
|
2013-09-01 21:59:46 +02:00
|
|
|
|
2013-08-31 21:06:42 +02:00
|
|
|
for i := 0; i < 20; i++ {
|
2013-09-02 01:05:48 +02:00
|
|
|
go ExampleJsonTransaction(createTestTxObj(t), MAX_RETRIES, t)
|
2013-08-31 07:03:44 +02:00
|
|
|
time.Sleep(1 * time.Millisecond)
|
|
|
|
}
|
2013-09-01 21:59:46 +02:00
|
|
|
|
2013-08-31 07:03:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestParallelTx1(t *testing.T) {
|
2013-09-01 21:59:46 +02:00
|
|
|
if testing.Short() {
|
|
|
|
t.SkipNow()
|
|
|
|
}
|
2013-08-31 07:03:44 +02:00
|
|
|
t.Parallel()
|
2013-09-02 01:05:48 +02:00
|
|
|
ExampleJsonTransaction(createTestTxObj(t), MAX_RETRIES, t)
|
2013-08-31 21:06:42 +02:00
|
|
|
for i := 0; i < 100; i++ {
|
2013-09-02 01:05:48 +02:00
|
|
|
go ExampleJsonTransaction(createTestTxObj(t), MAX_RETRIES, t)
|
2013-08-31 07:03:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParallelTx2(t *testing.T) {
|
2013-09-01 21:59:46 +02:00
|
|
|
if testing.Short() {
|
|
|
|
t.SkipNow()
|
|
|
|
}
|
2013-08-31 07:03:44 +02:00
|
|
|
t.Parallel()
|
2013-08-31 21:06:42 +02:00
|
|
|
for i := 0; i < 100; i++ {
|
2013-09-02 01:05:48 +02:00
|
|
|
go ExampleJsonTransaction(createTestTxObj(t), MAX_RETRIES, t)
|
2013-08-31 07:03:44 +02:00
|
|
|
}
|
2013-09-02 01:05:48 +02:00
|
|
|
ExampleJsonTransaction(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()
|
|
|
|
|
2013-09-01 21:59:46 +02:00
|
|
|
testTx := createTestTxObj(t)
|
2013-08-31 07:03:44 +02:00
|
|
|
defer func() {
|
2013-09-02 01:05:48 +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
|
|
|
}()
|
2013-09-01 21:59:46 +02:00
|
|
|
verifyErrNil(testTx.initiateRead(), t)
|
2013-08-31 21:06:42 +02:00
|
|
|
|
2013-09-01 21:59:46 +02:00
|
|
|
_, err := testTx.Do("WATCH", "testKeyA")
|
|
|
|
verifyErrNil(err, t)
|
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
|
2013-09-02 01:05:48 +02:00
|
|
|
ExampleJsonTransaction(createTestTxObj(t), MAX_RETRIES, t)
|
2013-08-31 07:03:44 +02:00
|
|
|
|
2013-09-01 21:59:46 +02:00
|
|
|
verifyErrNil(testTx.initiateWrite(), t)
|
|
|
|
verifyErrNil(testTx.Send("SET", "testKeyA", testValueA), t)
|
|
|
|
verifyErrNil(testTx.Send("SET", "testKeyB", testValueB), t)
|
2013-08-31 07:03:44 +02:00
|
|
|
|
|
|
|
keys, err := (testTx.Do("EXEC"))
|
2013-09-01 21:59:46 +02:00
|
|
|
// Expect error
|
2013-08-31 07:03:44 +02:00
|
|
|
if keys != nil {
|
2013-09-02 01:05:48 +02:00
|
|
|
t.Error("Keys not nil; exec should have been interrupted")
|
2013-08-31 07:03:44 +02:00
|
|
|
}
|
2013-09-01 21:59:46 +02:00
|
|
|
verifyErrNil(err, t)
|
2013-08-31 07:03:44 +02:00
|
|
|
|
|
|
|
testTx.close()
|
|
|
|
}
|