-
Notifications
You must be signed in to change notification settings - Fork 0
/
gomemcacheclient_test.go
124 lines (90 loc) · 2.59 KB
/
gomemcacheclient_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
121
122
123
124
package gomemcacheclient
/**
* @author jotaoverride
*/
import (
"errors"
"github.com/stretchr/testify/assert"
"os"
"testing"
"time"
)
func TestMiss(t *testing.T) {
var miss interface{}
err := client.Get("foo", &miss)
assert.Equal(t, errors.New("memcache: cache miss"), err)
assert.Equal(t, nil, miss)
}
func TestSetAndHit(t *testing.T) {
foo := map[string]interface{}{"key": "value"}
err := client.Set("foo", foo, 1)
assert.Nil(t, err)
empty := map[string]interface{}{}
// Hit
err = client.Get("foo", &empty)
assert.Nil(t, err)
assert.Equal(t, foo, empty)
assert.Equal(t, foo, empty)
err = client.Delete("foo")
assert.Nil(t, err)
// Miss
empty = map[string]interface{}{}
err = client.Get("foo", &empty)
assert.Equal(t, errors.New("memcache: cache miss"), err)
assert.Equal(t, map[string]interface{}{}, empty)
}
func TestSetTwiceAndHit(t *testing.T) {
err := client.Set("foo", "")
assert.Nil(t, err)
foo := map[string]interface{}{"key": "value"}
err = client.Set("foo", foo, 60)
assert.Nil(t, err)
empty := map[string]interface{}{}
err = client.Get("foo", &empty)
assert.Nil(t, err)
assert.Equal(t, foo, empty)
assert.Equal(t, foo, empty)
}
func TestGetDeleteAndMiss(t *testing.T) {
foo := map[string]interface{}{"key": "value"}
err := client.Set("foo", foo, 1)
assert.Nil(t, err)
err = client.Delete("foo")
assert.Nil(t, err)
// Miss
empty := map[string]interface{}{}
err = client.Get("foo", &empty)
assert.Equal(t, errors.New("memcache: cache miss"), err)
assert.Equal(t, map[string]interface{}{}, empty)
}
func TestGetExpiredItem(t *testing.T) {
client.SetDefaultExpiration(1)
foo := map[string]interface{}{"key": "value"}
err := client.Set("foo", foo)
assert.Nil(t, err)
time.Sleep(2 * time.Second)
// Miss
empty := map[string]interface{}{}
err = client.Get("foo", &empty)
assert.Equal(t, errors.New("memcache: cache miss"), err)
assert.Equal(t, map[string]interface{}{}, empty)
}
func TestNewClientWithExpiration(t *testing.T) {
otherClient, err := NewClient([]string{memcacheAddrs}, 1)
assert.Nil(t, err)
foo := map[string]interface{}{"key": "value"}
err = otherClient.Set("foo", foo)
assert.Nil(t, err)
time.Sleep(3 * time.Second)
// Miss
empty := map[string]interface{}{}
err = otherClient.Get("foo", &empty)
assert.Equal(t, errors.New("memcache: cache miss"), err)
assert.Equal(t, map[string]interface{}{}, empty)
}
func TestNewClientWithInvalidAddres(t *testing.T) {
if os.Getenv("GO_ENVIRONMENT") == "production" {
_, err := NewClient([]string{"192.168.6.6:666"}, 1)
assert.Equal(t, "Can't connect to the servers", err.Error())
}
}