-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.go
154 lines (129 loc) · 3.55 KB
/
client_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
142
143
144
145
146
147
148
149
150
151
152
153
154
package redisclient
import (
"testing"
"os"
"strconv"
"time"
"sync"
"log"
"math/rand"
)
const performanceIterations = 1000
const stringKey = "sixthMonth"
const dictKey = "planets"
const listKey = "cars"
const nonExistentKey = "nonExistent"
const itemNotFoundMsg = "Cache item not found"
var client *Client
func TestMain(m *testing.M) {
client = New("http://localhost:8080/api/v1/")
code := m.Run()
os.Exit(code)
}
func TestPutAndGetString(t *testing.T) {
value := "June"
storedValue, err := client.PutWithExpire(stringKey, value, 10)
assertEquals(t, nil, err)
assertEquals(t, value, storedValue)
storedValue, err = client.Get(stringKey)
assertEquals(t, nil, err)
assertEquals(t, value, storedValue)
}
func TestPutAndGetDict(t *testing.T) {
value := map[string]string{"planet1": "Mercury", "planet2": "Venus", "planet3": "Earth"}
storedValue, err := client.Put(dictKey, value)
assertEquals(t, nil, err)
storedValue, err = client.GetDictValue(dictKey, "planet1")
assertEquals(t, nil, err)
assertEquals(t, "Mercury", storedValue)
}
func TestPutAndGetList(t *testing.T) {
value := [3]string{"Toyota", "Opel", "Ford"}
storedValue, err := client.Put(listKey, value)
assertEquals(t, nil, err)
storedValue, err = client.GetListElement(listKey, 1)
assertEquals(t, nil, err)
assertEquals(t, "Opel", storedValue)
}
func TestGetNonExistentItem(t *testing.T) {
storedValue, err := client.Get(nonExistentKey)
assertEquals(t, itemNotFoundMsg, err.Error())
assertEquals(t, nil, storedValue)
}
func TestKeys(t *testing.T) {
keys, err := client.Keys()
assertEquals(t, nil, err)
assertTrue(t, len(keys) > 0)
}
func TestDelete(t *testing.T) {
_, err := client.Delete(listKey)
assertEquals(t, nil, err)
storedValue, err := client.Get(listKey)
assertEquals(t, itemNotFoundMsg, err.Error())
assertEquals(t, nil, storedValue)
}
func TestExpireAndCheckTtl(t *testing.T) {
_, err := client.Expire(stringKey, 10)
assertEquals(t, nil, err)
ttl, err := client.GetTtl(stringKey)
assertEquals(t, nil, err)
assertTrue(t, ttl > 0)
}
func TestNonExistentExpire(t *testing.T) {
_, err := client.Expire(nonExistentKey, 10)
assertEquals(t, itemNotFoundMsg, err.Error())
}
func TestPersistItemTtl(t *testing.T) {
ttl, err := client.GetTtl(dictKey)
assertEquals(t, nil, err)
assertEquals(t, -1, ttl)
}
func TestNonExistentTtl(t *testing.T) {
_, err := client.GetTtl(nonExistentKey)
assertEquals(t, itemNotFoundMsg, err.Error())
}
func TestPutAndGetPerformance(t *testing.T) {
data := make([]string, performanceIterations)
for i := range data {
data[i] = stringKey + /*strconv.Itoa(i)//*/strconv.Itoa(rand.Intn(performanceIterations))
}
start := time.Now()
var wg sync.WaitGroup
wg.Add(performanceIterations)
for i := range data {
value := data[i]
go func() {
defer wg.Done()
client.PutWithExpire(value, value, rand.Intn(10))
_, err := client.Get(value)
if err != nil {
t.Fatalf("Error in process of get: '%v'", err)
}
}()
}
wg.Wait()
elapsed := time.Since(start)
log.Printf("%v times put and get took %v", performanceIterations, elapsed)
keys, err := client.Keys()
if err != nil {
t.Fatalf("Error in process of get keys: '%v'", err)
} else {
for i := range keys {
client.Delete(keys[i])
}
keys, _ = client.Keys()
assertEquals(t, 0, len(keys))
}
}
func assertEquals(t *testing.T, expected interface{}, actual interface{}) {
if expected == actual {
return
}
t.Fatalf("Expected: '%v'. Actual: '%v'", expected, actual)
}
func assertTrue(t *testing.T, condition bool) {
if condition {
return
}
t.Fatal("Expected 'true' but was 'false'")
}