-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
92 lines (79 loc) · 1.73 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
package redis
import (
"fmt"
"runtime"
"strconv"
"sync"
"testing"
)
func TestStart(t *testing.T) {
runtime.GOMAXPROCS(runtime.NumCPU())
t.Run("all", func(t *testing.T) {
t.Run("newclient", func(t *testing.T) {
testNew(t)
})
t.Run("ping", func(t *testing.T) {
testPing(t)
})
t.Run("stringSetAndGet", func(t *testing.T) {
testStringSetAndGet(t)
})
})
}
func testNew(t *testing.T) {
addr := "127.0.0.1:6379"
db := 4
client := NewRedisClient(addr, uint(db), "", 0)
if client == nil {
t.Fatalf("创建Redis实例错误, 地址:%s", addr)
}
t.Logf("创建Redis实例成功,地址:%s", addr)
}
func testPing(t *testing.T) {
addr := "127.0.0.1:6379"
db := 4
client := NewRedisClient(addr, uint(db), "", 0)
if client == nil {
t.Fatalf("创建Redis实例错误, 地址:%s", addr)
}
err := client.Ping()
if err != nil {
t.Fatalf("ping出错:%s", err)
}
t.Logf("ping成功,地址:%s", addr)
}
func testStringSetAndGet(t *testing.T) {
addr := "127.0.0.1:6379"
db := 4
client := NewRedisClient(addr, uint(db), "", 0)
if client == nil {
t.Fatalf("创建Redis实例错误, 地址:%s", addr)
}
var wg sync.WaitGroup
wg.Add(10)
for i := 0; i < 5; i++ {
go func(n int) {
defer wg.Done()
key := fmt.Sprintf("g%d", n)
val := strconv.FormatInt(int64(n*10), 10)
err := client.Set(key, []byte(val))
if err != nil {
t.Logf("Set error:%s", err)
}
t.Logf("setg%d, key:%s,val:%s", n, key, val)
}(i)
}
for i := 0; i < 5; i++ {
go func(n int) {
defer wg.Done()
//t.Logf("getg%d", n)
key := fmt.Sprintf("g%d", n)
getval, err := client.Get(key)
if err != nil {
t.Logf("get error:%s", err)
}
t.Logf("val is:%s", string(getval))
}(i)
}
wg.Wait()
}