-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathredis3_test.go
120 lines (105 loc) · 2.5 KB
/
redis3_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
109
110
111
112
113
114
115
116
117
118
119
120
package redis3_test
import (
"testing"
"time"
"github.com/danfaizer/redis3"
)
func TestRediS3ClientNoBucket(t *testing.T) {
var err error
_, err = redis3.NewClient(
&redis3.Options{})
if err.Error() != "client: no bucket specified in options" {
t.Errorf("unexpected error: %s", err)
}
}
func TestRediS3ClientNoRegion(t *testing.T) {
var err error
_, err = redis3.NewClient(
&redis3.Options{
Bucket: "redis3-test",
})
if err.Error() != "client: no region specified in options" {
t.Errorf("unexpected error: %s", err)
}
}
func TestRediS3ClientUnexsistingBucket(t *testing.T) {
var err error
_, err = redis3.NewClient(
&redis3.Options{
Bucket: "redis3-test-wrong",
Region: "eu-west-1",
Endpoint: "http://127.0.0.1:5001",
})
if err.Error() != "client: specified bucket does not exist" {
t.Errorf("unexpected error: %s", err)
}
}
func TestRediS3Client(t *testing.T) {
client, err := redis3.NewClient(
&redis3.Options{
Bucket: "redis3-test",
AutoCreateBucket: true,
Region: "eu-west-1",
Timeout: 1,
Endpoint: "http://127.0.0.1:5001",
EnforceConsistency: true,
})
if err != nil {
t.Errorf("unexpected error: %s", err)
}
// Test Ping
err = client.Ping()
if err != nil {
t.Errorf("ping failed with error: %s", err)
}
// Test Get unexisting key
var unexistingValue string
_, err = client.Get("unexistingKey", &unexistingValue)
if err.Error() != "key not found" {
t.Errorf("unexpected error: %s", err)
}
// Test Set key1
err = client.Set("key1", "key1 value", 1)
if err != nil {
t.Errorf("unexpected error: %s", err)
}
// Sleep so key1 expires
time.Sleep(2 * time.Second)
var key1 string
_, err = client.Get("key1", &key1)
if err.Error() != "key not found" {
t.Errorf("unexpected error: %s", err)
}
// Test Set key2
err = client.Set("key2", "key2 value", 0)
if err != nil {
t.Errorf("unexpected error: %s", err)
}
var key2 string
_, err = client.Get("key2", &key2)
if err != nil {
t.Errorf("unexpected error: %s", err)
}
if key2 != "key2 value" {
t.Errorf("unexpected value for key2: %s", key2)
}
// Test lock
err = client.Lock("key2")
if err != nil {
t.Errorf("lock error: %s", err)
}
err = client.Set("key2", "key2 is locked", 0)
if err == nil {
t.Errorf("key locked error: %s", err)
}
// Test unlock
err = client.Unlock("key2")
if err != nil {
t.Errorf("unlock error: %s", err)
}
// Test delete
err = client.Del("key2")
if err != nil {
t.Errorf("del error: %s", err)
}
}