-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtera_test.go
161 lines (143 loc) · 3.81 KB
/
tera_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
155
156
157
158
159
160
161
package terago
import (
"fmt"
"testing"
"time"
)
func logExecTime(start time.Time, prefix string) {
elapsed_ms := time.Since(start) / time.Millisecond
fmt.Printf("Performance: %s cost %d ms.\n", prefix, elapsed_ms)
}
func TestTera(*testing.T) {
fmt.Println("Hello terago!")
start := time.Now()
client, c_err := NewClient("tera.flag", "terago")
defer client.Close()
if c_err != nil {
panic("tera.NewClient error: " + c_err.Error())
}
logExecTime(start, "NewClient")
start = time.Now()
kv, t_err := client.OpenKvStore("terago")
defer kv.Close()
if t_err != nil {
panic("tera.OpenKvStore error: " + t_err.Error())
}
logExecTime(start, "OpenKvStore")
start = time.Now()
p_err := kv.Put("hello", "terago", 10)
if p_err != nil {
panic("put key value error: " + p_err.Error())
}
logExecTime(start, "Put")
start = time.Now()
p_err = kv.PutAsync("helloasync", "terago", 10)
if p_err != nil {
panic("put key value async error: " + p_err.Error())
}
logExecTime(start, "PutAsync")
start = time.Now()
// get an exist key value, return value
value, g_err := kv.Get("hello")
if g_err != nil {
panic("get key value error: " + g_err.Error())
}
fmt.Printf("get key[%s] value[%s].\n", "hello", value)
logExecTime(start, "Get")
start = time.Now()
// get a not-exist key value, return "not found"
_, g_err = kv.Get("hell")
if g_err == nil {
panic("get key value should fail: " + g_err.Error())
}
logExecTime(start, "Get_NotExist")
start = time.Now()
d_err := kv.Delete("hello")
if d_err != nil {
panic("delete key value error: " + d_err.Error())
}
logExecTime(start, "Delete")
_, g_err = kv.Get("hello")
if g_err == nil {
panic("get key value should fail: " + g_err.Error())
}
}
func TestTeraBatch(*testing.T) {
fmt.Println("Hello terago batch!")
start := time.Now()
client, err := NewClient("tera.flag", "terago")
defer client.Close()
if err != nil {
panic("tera.NewClient error: " + err.Error())
}
logExecTime(start, "NewClient")
start = time.Now()
kv, err := client.OpenKvStore("terago")
defer kv.Close()
if err != nil {
panic("tera.OpenKvStore error: " + err.Error())
}
logExecTime(start, "OpenKvStore")
// for {
keys := []string{"t", "e", "r", "a", "go"}
values := []string{"tt", "ee", "rr", "aa", "gogo"}
start = time.Now()
var kvs []KeyValue
for i, k := range keys {
kvs = append(kvs, KeyValue{Key: k, Value: values[i], TTL: 10})
}
err = kv.BatchPut(kvs)
if err != nil {
panic("BatchPut error: " + err.Error())
}
logExecTime(start, "BatchPut")
start = time.Now()
// get an exist key value, return value
kvs, err = kv.BatchGet(keys)
if err != nil {
panic("BatchGet error: " + err.Error())
}
fmt.Printf("BatchGet KeyValues[%v]\n", kvs)
logExecTime(start, "BatchGet")
start = time.Now()
kvs, err = kv.RangeGet("a", "h", 10)
if err != nil {
panic("RangeGet error: " + err.Error())
}
fmt.Printf("RangeGet KeyValues[%v]\n", kvs)
if len(kvs) != 3 || kvs[0].Key != "a" || kvs[1].Key != "e" || kvs[2].Key != "go" {
e := fmt.Errorf("RangeGet err: %v", kvs)
panic(e)
}
logExecTime(start, "RangeGet")
start = time.Now()
kvs, err = kv.RangeGet("a", "h", 2)
if err != nil {
panic("RangeGet error: " + err.Error())
}
fmt.Printf("RangeGet2 KeyValues[%v]\n", kvs)
if len(kvs) != 2 || kvs[0].Key != "a" || kvs[1].Key != "e" {
e := fmt.Errorf("RangeGet2 err: %v", kvs)
panic(e)
}
logExecTime(start, "RangeGet2")
// }
}
func TestOpenFailed(*testing.T) {
fmt.Println("Hello terago!")
start := time.Now()
client, err := NewClient("tera.flag", "terago")
defer client.Close()
if err != nil {
panic("tera.NewClient error: " + err.Error())
}
logExecTime(start, "NewClient")
start = time.Now()
kv, err := client.OpenKvStore("teragooo")
defer kv.Close()
if err == nil {
panic("tera.OpenKvStore should failed: ")
}
fmt.Printf("%v\n", err)
logExecTime(start, "OpenKvStore")
}