forked from IceFireDB/IceFireDB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconn_test.go
72 lines (60 loc) · 1.45 KB
/
conn_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
// +build alltest
package main
import (
"context"
"fmt"
"log"
"os"
"path/filepath"
"sync"
"time"
"github.com/ledisdb/ledisdb/ledis"
"github.com/cenkalti/backoff/v4"
"github.com/go-redis/redis/v8"
lediscfg "github.com/ledisdb/ledisdb/config"
"github.com/tidwall/uhaha"
)
var (
testConnOnce sync.Once
testRedisClient *redis.Client
)
func getTestConn() *redis.Client {
log.SetOutput(os.Stderr)
f := func() {
conf.DataDir = "/tmp/icefiredb"
os.RemoveAll(conf.DataDir)
conf.DataDirReady = func(dir string) {
os.RemoveAll(filepath.Join(dir, "main.db"))
ldsCfg = lediscfg.NewConfigDefault()
ldsCfg.DataDir = filepath.Join(dir, "main.db")
ldsCfg.Databases = 1
ldsCfg.DBName = os.Getenv("DRIVER")
var err error
le, err = ledis.Open(ldsCfg)
if err != nil {
panic(err)
}
ldb, err = le.Select(0)
if err != nil {
panic(err)
}
}
conf.Snapshot = snapshot
conf.Restore = restore
fmt.Printf("start with Storage Engine: %s\n", os.Getenv("DRIVER"))
go uhaha.Main(conf)
testRedisClient = redis.NewClient(&redis.Options{
Addr: "127.0.0.1:11001",
})
log.Println("waiting for DB bootstrap")
// wait server starts
backoff.Retry(func() error {
_, err := testRedisClient.Set(context.Background(), "init", "1", 0).Result()
return err
}, backoff.NewConstantBackOff(1*time.Second))
// clean all data
testRedisClient.FlushAll(context.Background())
}
testConnOnce.Do(f)
return testRedisClient
}