-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
141 lines (111 loc) · 3.35 KB
/
main_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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package memcached
import (
"os"
"testing"
"time"
"github.com/bradfitz/gomemcache/memcache"
"github.com/stretchr/testify/assert"
)
var (
c *memcache.Client
s *Server
listen = ":11211"
)
func TestMain(m *testing.M) {
c = memcache.New(listen)
go func(s *Server) {
if err := ListenAndServe(listen); err != nil {
panic(err)
}
}(s)
time.Sleep(1 * time.Second)
os.Exit(m.Run())
}
func TestSet(t *testing.T) {
assert.NoError(t, c.Set(&memcache.Item{Key: "foo", Value: []byte("bar")}))
assert.NoError(t, c.Delete("foo"))
}
func TestMultiSet(t *testing.T) {
assert.NoError(t, c.Set(&memcache.Item{Key: "foo", Value: []byte("bar")}))
assert.NoError(t, c.Set(&memcache.Item{Key: "foo", Value: []byte("bar")}))
assert.NoError(t, c.Delete("foo"))
}
func TestGet(t *testing.T) {
assert.NoError(t, c.Set(&memcache.Item{Key: "foo", Value: []byte("bar")}))
itm, err := c.Get("foo")
assert.NoError(t, err)
assert.Equal(t, "bar", string(itm.Value))
assert.NoError(t, c.Delete("foo"))
}
func TestMultiGet(t *testing.T) {
assert.NoError(t, c.Set(&memcache.Item{Key: "foo", Value: []byte("foo")}))
assert.NoError(t, c.Set(&memcache.Item{Key: "bar", Value: []byte("bar")}))
items, err := c.GetMulti([]string{"foo", "bar"})
assert.NoError(t, err)
assert.Equal(t, 2, len(items))
assert.Equal(t, []byte("foo"), items["foo"].Value)
assert.Equal(t, []byte("bar"), items["bar"].Value)
assert.NoError(t, c.Delete("foo"))
assert.NoError(t, c.Delete("bar"))
}
func TestMiss(t *testing.T) {
itm, err := c.Get("foo")
assert.Nil(t, itm)
assert.Error(t, err)
}
func TestAdd(t *testing.T) {
assert.NoError(t, c.Add(&memcache.Item{Key: "foo", Value: []byte("bar")}))
}
func TestReplace(t *testing.T) {
itm := memcache.Item{Key: "foo", Value: []byte("bar")}
assert.NoError(t, c.Replace(&itm))
assert.NoError(t, c.Delete("foo"))
assert.Equal(t, memcache.ErrNotStored, c.Replace(&itm))
}
func TestAppend(t *testing.T) {
t.Skipf("Client doesn't support apppend")
// assert.NoError(t, c.Append(&memcache.Item{Key: "foo", Value: []byte("bar")}))
}
func TestPrepend(t *testing.T) {
t.Skipf("Client doesn't support prepend")
// assert.NoError(t, c.Prepend(&memcache.Item{Key: "foo", Value: []byte("bar")}))
}
func TestFlushAll(t *testing.T) {
t.Skipf("flush_all doesn't work yet")
// assert.NoError(t, c.FlushAll())
}
func TestDeleteAll(t *testing.T) {
t.Skipf("flush_all doesn't work yet")
// assert.NoError(t, c.DeleteAll())
}
func TestIncrement(t *testing.T) {
// Set initial value.
assert.NoError(t, c.Delete("foo"))
assert.NoError(t, c.Set(&memcache.Item{Key: "foo", Value: []byte("1")}))
// Check increment response.
delta, err := c.Increment("foo", 1)
assert.NoError(t, err)
assert.Equal(t, uint64(2), delta)
// Check get after increment
itm, err := c.Get("foo")
if assert.NoError(t, err) {
assert.NotNil(t, itm)
assert.Equal(t, []byte("2"), itm.Value)
}
}
func TestDecrement(t *testing.T) {
// Set up initial value.
assert.NoError(t, c.Delete("foo"))
assert.NoError(t, c.Set(&memcache.Item{Key: "foo", Value: []byte("100")}))
// Check the decrement response
delta, err := c.Decrement("foo", 1)
assert.NoError(t, err)
assert.Equal(t, uint64(99), delta)
// Check get matches the value just returned
itm, err := c.Get("foo")
assert.NoError(t, err)
assert.Equal(t, []byte("99"), itm.Value)
}
func TestCompareAndSwap(t *testing.T) {
t.Skipf("TODO")
}