ledisdb/server/app_test.go

97 lines
1.7 KiB
Go
Raw Normal View History

package server
2014-05-02 13:08:20 +04:00
import (
2014-05-05 07:37:44 +04:00
"os"
"sync"
2014-05-02 13:08:20 +04:00
"testing"
2015-05-04 17:42:28 +03:00
"github.com/siddontang/goredis"
"github.com/siddontang/ledisdb/config"
2014-05-02 13:08:20 +04:00
)
2014-05-05 07:37:44 +04:00
var testAppOnce sync.Once
2015-09-13 03:47:10 +03:00
var testAppAuthOnce sync.Once
2014-05-05 07:37:44 +04:00
var testApp *App
2015-03-11 06:54:02 +03:00
var testLedisClient *goredis.Client
2015-09-13 03:47:10 +03:00
var testLedisClientAuth *goredis.Client
func getTestConnAuth(password string) *goredis.PoolConn {
startTestAppAuth(password)
conn, _ := testLedisClientAuth.Get()
return conn
}
func newTestLedisClientAuth() {
testLedisClientAuth = goredis.NewClient("127.0.0.1:20000", "")
testLedisClientAuth.SetMaxIdleConns(4)
}
func startTestAppAuth(password string) {
f := func() {
newTestLedisClientAuth()
cfg := config.NewConfigDefault()
cfg.DataDir = "/tmp/testdb_auth"
os.RemoveAll(cfg.DataDir)
cfg.Addr = "127.0.0.1:20000"
cfg.HttpAddr = "127.0.0.1:20001"
cfg.AuthPassword = password
2015-09-13 03:47:10 +03:00
os.RemoveAll(cfg.DataDir)
var err error
testApp, err = NewApp(cfg)
if err != nil {
println(err.Error())
panic(err)
}
go testApp.Run()
}
testAppAuthOnce.Do(f)
}
2014-05-05 07:37:44 +04:00
2014-06-22 06:39:23 +04:00
func newTestLedisClient() {
2015-03-11 06:54:02 +03:00
testLedisClient = goredis.NewClient("127.0.0.1:16380", "")
testLedisClient.SetMaxIdleConns(4)
2014-05-05 07:37:44 +04:00
}
2015-03-11 06:54:02 +03:00
func getTestConn() *goredis.PoolConn {
2014-05-16 05:10:09 +04:00
startTestApp()
conn, _ := testLedisClient.Get()
return conn
2014-05-05 07:37:44 +04:00
}
func startTestApp() {
f := func() {
2014-06-22 06:39:23 +04:00
newTestLedisClient()
2014-05-02 13:08:20 +04:00
2014-10-11 12:00:59 +04:00
cfg := config.NewConfigDefault()
cfg.DataDir = "/tmp/testdb"
os.RemoveAll(cfg.DataDir)
2014-05-05 07:37:44 +04:00
cfg.Addr = "127.0.0.1:16380"
2014-12-26 05:08:37 +03:00
cfg.HttpAddr = "127.0.0.1:21181"
2014-05-05 07:37:44 +04:00
2015-09-13 03:47:10 +03:00
os.RemoveAll(cfg.DataDir)
2014-05-05 07:37:44 +04:00
var err error
2014-05-05 07:37:44 +04:00
testApp, err = NewApp(cfg)
if err != nil {
println(err.Error())
panic(err)
}
go testApp.Run()
}
testAppOnce.Do(f)
}
func TestApp(t *testing.T) {
startTestApp()
2014-05-02 13:08:20 +04:00
}