This repository has been archived by the owner on May 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathnodb_test.go
98 lines (76 loc) · 1.79 KB
/
nodb_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
package nodb
import (
"os"
"sync"
"testing"
"github.com/lunny/nodb/config"
)
var testLedis *Nodb
var testLedisOnce sync.Once
func getTestDB() *DB {
f := func() {
cfg := new(config.Config)
cfg.DataDir = "/tmp/test_ledis"
// cfg.BinLog.MaxFileSize = 1073741824
// cfg.BinLog.MaxFileNum = 3
os.RemoveAll(cfg.DataDir)
var err error
testLedis, err = Open(cfg)
if err != nil {
println(err.Error())
panic(err)
}
}
testLedisOnce.Do(f)
db, _ := testLedis.Select(0)
return db
}
func TestDB(t *testing.T) {
getTestDB()
}
func TestSelect(t *testing.T) {
db0, _ := testLedis.Select(0)
db1, _ := testLedis.Select(1)
key0 := []byte("db0_test_key")
key1 := []byte("db1_test_key")
db0.Set(key0, []byte("0"))
db1.Set(key1, []byte("1"))
if v, err := db0.Get(key0); err != nil {
t.Fatal(err)
} else if string(v) != "0" {
t.Fatal(string(v))
}
if v, err := db1.Get(key1); err != nil {
t.Fatal(err)
} else if string(v) != "1" {
t.Fatal(string(v))
}
}
func TestFlush(t *testing.T) {
db0, _ := testLedis.Select(0)
db1, _ := testLedis.Select(1)
db0.Set([]byte("a"), []byte("1"))
db0.ZAdd([]byte("zset_0"), ScorePair{int64(1), []byte("ma")})
db0.ZAdd([]byte("zset_0"), ScorePair{int64(2), []byte("mb")})
db1.Set([]byte("b"), []byte("2"))
db1.LPush([]byte("lst"), []byte("a1"), []byte("b2"))
db1.ZAdd([]byte("zset_0"), ScorePair{int64(3), []byte("mc")})
db1.FlushAll()
// 0 - existing
if exists, _ := db0.Exists([]byte("a")); exists <= 0 {
t.Fatal(false)
}
if zcnt, _ := db0.ZCard([]byte("zset_0")); zcnt != 2 {
t.Fatal(zcnt)
}
// 1 - deleted
if exists, _ := db1.Exists([]byte("b")); exists > 0 {
t.Fatal(false)
}
if llen, _ := db1.LLen([]byte("lst")); llen > 0 {
t.Fatal(llen)
}
if zcnt, _ := db1.ZCard([]byte("zset_1")); zcnt > 0 {
t.Fatal(zcnt)
}
}