-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathredis_test.go
108 lines (86 loc) · 2.2 KB
/
redis_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright (c) 2012-2016 The Revel Framework Authors, All rights reserved.
// Revel Framework source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.
package cache
import (
"net"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/bmizerany/assert"
)
// These tests require redis server running on localhost:6379 (the default)
const redisTestServer = "localhost:6379"
var newRedisCache = func(t *testing.T, defaultExpiration time.Duration) Cache {
c, err := net.Dial("tcp", redisTestServer)
if err == nil {
if _, err = c.Write([]byte("flush_all\r\n")); err != nil {
t.Errorf("Write failed: %s", err)
}
_ = c.Close()
redisCache := NewRedisCache(RedisOpts{
Host: redisTestServer,
Expiration: defaultExpiration,
})
if err = redisCache.Flush(); err != nil {
t.Errorf("Flush failed: %s", err)
}
return redisCache
}
t.Errorf("couldn't connect to redis on %s", redisTestServer)
t.FailNow()
panic("")
}
func TestRedisCache_TypicalGetSet(t *testing.T) {
typicalGetSet(t, newRedisCache)
}
func TestRedisCache_Expiration(t *testing.T) {
expiration(t, newRedisCache)
}
func TestRedisCache_EmptyCache(t *testing.T) {
emptyCache(t, newRedisCache)
}
func TestRedisCache_SetFields(t *testing.T) {
testSetFields(t, newRedisCache)
}
func TestRedisCache_Replace(t *testing.T) {
testReplace(t, newRedisCache)
}
func TestRedisCache_Add(t *testing.T) {
testAdd(t, newRedisCache)
}
func TestRedisCache_GetMulti(t *testing.T) {
testGetMulti(t, newRedisCache)
}
func TestRedisCache_Keys(t *testing.T) {
testKeys(t, newRedisCache)
}
func TestRedisCache_LockRetry(t *testing.T) {
cache := newRedisCache(t, testExpiryTime)
x, ok := cache.(*RedisCache)
if !ok {
t.Fatalf("Cannot convert racache")
}
x.lockRetries = 1
var counter int64
var errors int64
var wg sync.WaitGroup
for _, ix := range []int{1, 2} {
wg.Add(1)
go func(ix int) {
defer wg.Done()
if err := x.lockRetry("mohan", func() error {
time.Sleep(2 * time.Second)
return nil
}); err != nil {
atomic.AddInt64(&errors, 1)
} else {
atomic.AddInt64(&counter, 1)
}
}(ix)
}
wg.Wait()
assert.Equal(t, int64(1), counter)
assert.Equal(t, int64(1), errors)
}