Skip to content
Snippets Groups Projects
Commit 5029e462 authored by scott.wang's avatar scott.wang
Browse files

Fix UT framework

parent 8d40bff3
No related branches found
No related tags found
No related merge requests found
package etcdv3 package etcdv3
import ( import (
"fmt"
"os" "os"
"os/exec" "os/exec"
"path" "path"
...@@ -10,19 +11,13 @@ import ( ...@@ -10,19 +11,13 @@ import (
"testing" "testing"
"time" "time"
"github.com/stretchr/testify/suite"
"github.com/coreos/etcd/mvcc/mvccpb" "github.com/coreos/etcd/mvcc/mvccpb"
"github.com/juju/errors" "github.com/juju/errors"
"google.golang.org/grpc/connectivity" "google.golang.org/grpc/connectivity"
) )
// etcd connect config
var (
name = "test"
timeout = time.Second
heartbeat = 1
endpoints = []string{"localhost:2379"}
)
// tests dataset // tests dataset
var tests = []struct { var tests = []struct {
input struct { input struct {
...@@ -51,69 +46,111 @@ var tests = []struct { ...@@ -51,69 +46,111 @@ var tests = []struct {
// test dataset prefix // test dataset prefix
const prefix = "name" const prefix = "name"
func initClient(t *testing.T) *Client { type ClientTestSuite struct {
suite.Suite
c, err := newClient(name, endpoints, timeout, heartbeat) etcdConfig struct {
if err != nil { name string
t.Fatal(err) endpoints []string
} timeout time.Duration
c.CleanKV() heartbeat int
return c
} }
func TestMain(m *testing.M) { client *Client
startETCDServer()
m.Run()
stopETCDServer()
} }
func startETCDServer() { // start etcd server
func (suite *ClientTestSuite) SetupSuite() {
cmd := exec.Command("./load.sh", "start") cmd := exec.Command("./load.sh", "start")
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
cmd.Stderr = os.Stdout cmd.Stderr = os.Stdout
cmd.Dir = "./single" cmd.Dir = "./single"
if err := cmd.Run(); err != nil { if err := cmd.Run(); err != nil {
panic(err) suite.T().Fatal(err)
} }
} }
func stopETCDServer() { // stop etcd server
func (suite *ClientTestSuite) TearDownSuite() {
cmd := exec.Command("./load.sh", "stop") cmd := exec.Command("./load.sh", "stop")
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
cmd.Stderr = os.Stdout cmd.Stderr = os.Stdout
cmd.Dir = "./single" cmd.Dir = "./single"
if err := cmd.Run(); err != nil { if err := cmd.Run(); err != nil {
panic(err) suite.T().Fatal(err)
}
} }
func (suite *ClientTestSuite) setUpClient() *Client {
c, err := newClient(suite.etcdConfig.name,
suite.etcdConfig.endpoints,
suite.etcdConfig.timeout,
suite.etcdConfig.heartbeat)
if err != nil {
suite.T().Fatal(err)
}
return c
} }
func Test_newClient(t *testing.T) { // set up a client for suite
func (suite *ClientTestSuite) SetupTest() {
c := suite.setUpClient()
c.CleanKV()
suite.client = c
return
}
c := initClient(t) func (suite *ClientTestSuite) TestClientClose() {
defer c.Close()
fmt.Println("called client close")
c := suite.client
t := suite.T()
defer c.Close()
if c.rawClient.ActiveConnection().GetState() != connectivity.Ready { if c.rawClient.ActiveConnection().GetState() != connectivity.Ready {
t.Fatal(c.rawClient.ActiveConnection().GetState()) t.Fatal(suite.client.rawClient.ActiveConnection().GetState())
}
}
func (suite *ClientTestSuite) TestClientValid() {
fmt.Println("called client valid")
c := suite.client
t := suite.T()
if c.Valid() != true {
t.Fatal("client is not valid")
} }
c.Close()
if suite.client.Valid() != false {
t.Fatal("client is valid")
} }
}
func (suite *ClientTestSuite) TestClientDone() {
func TestClient_Close(t *testing.T) { c := suite.client
c := initClient(t) go func() {
time.Sleep(2 * time.Second)
c.Close() c.Close()
}()
c.Wait.Wait()
} }
func TestClient_Create(t *testing.T) { func (suite *ClientTestSuite) TestClientCreateKV() {
tests := tests tests := tests
c := initClient(t) c := suite.client
defer c.Close() t := suite.T()
defer suite.client.Close()
for _, tc := range tests { for _, tc := range tests {
...@@ -131,19 +168,18 @@ func TestClient_Create(t *testing.T) { ...@@ -131,19 +168,18 @@ func TestClient_Create(t *testing.T) {
} }
if value != expect { if value != expect {
t.Fatalf("expect %v but get %v", expect, value) t.Fatalf("expect %v but get %v", expect, value)
} }
} }
} }
func TestClient_Delete(t *testing.T) { func (suite *ClientTestSuite) TestClientDeleteKV() {
tests := tests tests := tests
c := suite.client
t := suite.T()
c := initClient(t)
defer c.Close() defer c.Close()
for _, tc := range tests { for _, tc := range tests {
...@@ -172,12 +208,12 @@ func TestClient_Delete(t *testing.T) { ...@@ -172,12 +208,12 @@ func TestClient_Delete(t *testing.T) {
} }
func TestClient_GetChildrenKVList(t *testing.T) { func (suite *ClientTestSuite) TestClientGetChildrenKVList() {
tests := tests tests := tests
c := initClient(t) c := suite.client
defer c.Close() t := suite.T()
var expectKList []string var expectKList []string
var expectVList []string var expectVList []string
...@@ -210,12 +246,12 @@ func TestClient_GetChildrenKVList(t *testing.T) { ...@@ -210,12 +246,12 @@ func TestClient_GetChildrenKVList(t *testing.T) {
} }
func TestClient_Watch(t *testing.T) { func (suite *ClientTestSuite) TestClientWatch() {
tests := tests tests := tests
c := initClient(t) c := suite.client
defer c.Close() t := suite.T()
wg := sync.WaitGroup{} wg := sync.WaitGroup{}
wg.Add(1) wg.Add(1)
...@@ -231,10 +267,6 @@ func TestClient_Watch(t *testing.T) { ...@@ -231,10 +267,6 @@ func TestClient_Watch(t *testing.T) {
for e := range wc { for e := range wc {
if e.Err() != nil {
t.Fatal(err)
}
for _, event := range e.Events { for _, event := range e.Events {
t.Logf("type IsCreate %v k %s v %s", event.IsCreate(), event.Kv.Key, event.Kv.Value) t.Logf("type IsCreate %v k %s v %s", event.IsCreate(), event.Kv.Key, event.Kv.Value)
} }
...@@ -262,10 +294,11 @@ func TestClient_Watch(t *testing.T) { ...@@ -262,10 +294,11 @@ func TestClient_Watch(t *testing.T) {
} }
func TestClient_RegisterTemp(t *testing.T) { func (suite *ClientTestSuite) TestClientRegisterTemp() {
c := initClient(t) c := suite.client
observeC := initClient(t) observeC := suite.setUpClient()
t := suite.T()
wg := sync.WaitGroup{} wg := sync.WaitGroup{}
wg.Add(1) wg.Add(1)
...@@ -281,9 +314,6 @@ func TestClient_RegisterTemp(t *testing.T) { ...@@ -281,9 +314,6 @@ func TestClient_RegisterTemp(t *testing.T) {
for _, event := range e.Events { for _, event := range e.Events {
if e.Err() != nil {
t.Fatal(e.Err())
}
if event.Type == mvccpb.DELETE { if event.Type == mvccpb.DELETE {
t.Logf("complete key (%s) is delete", completePath) t.Logf("complete key (%s) is delete", completePath)
wg.Done() wg.Done()
...@@ -306,32 +336,18 @@ func TestClient_RegisterTemp(t *testing.T) { ...@@ -306,32 +336,18 @@ func TestClient_RegisterTemp(t *testing.T) {
wg.Wait() wg.Wait()
} }
func TestClient_Valid(t *testing.T) { func TestClientSuite(t *testing.T) {
suite.Run(t, &ClientTestSuite{
c := initClient(t) etcdConfig: struct {
name string
if c.Valid() != true { endpoints []string
t.Fatal("client is not valid") timeout time.Duration
} heartbeat int
}{
c.Close() name: "test",
endpoints: []string{"localhost:2379"},
if c.Valid() != false { timeout: time.Second,
heartbeat: 1,
t.Fatal("client is valid") },
})
}
}
func TestClient_Done(t *testing.T) {
c := initClient(t)
go func() {
time.Sleep(2 * time.Second)
c.Close()
}()
c.Wait.Wait()
} }
...@@ -3,7 +3,6 @@ package etcdv3 ...@@ -3,7 +3,6 @@ package etcdv3
import ( import (
"context" "context"
"sync" "sync"
"testing"
"time" "time"
"github.com/apache/dubbo-go/common" "github.com/apache/dubbo-go/common"
...@@ -53,9 +52,10 @@ func (r *mockFacade) IsAvailable() bool { ...@@ -53,9 +52,10 @@ func (r *mockFacade) IsAvailable() bool {
return true return true
} }
func Test_Fascade(t *testing.T) { func (suite *ClientTestSuite) TestFacade() {
c := initClient(t) c := suite.client
t := suite.T()
url, err := common.NewURL(context.Background(), "mock://127.0.0.1:2379") url, err := common.NewURL(context.Background(), "mock://127.0.0.1:2379")
if err != nil { if err != nil {
......
package etcdv3 package etcdv3
import ( import (
"testing"
"time" "time"
"github.com/apache/dubbo-go/remoting" "github.com/apache/dubbo-go/remoting"
...@@ -30,7 +29,7 @@ var changedData = ` ...@@ -30,7 +29,7 @@ var changedData = `
dubbo.service.com.ikurento.user.UserProvider.cluster=failover dubbo.service.com.ikurento.user.UserProvider.cluster=failover
` `
func TestListener(t *testing.T) { func (suite *ClientTestSuite) TestListener() {
var tests = []struct { var tests = []struct {
input struct { input struct {
...@@ -44,8 +43,8 @@ func TestListener(t *testing.T) { ...@@ -44,8 +43,8 @@ func TestListener(t *testing.T) {
}{k: "/dubbo", v: changedData}}, }{k: "/dubbo", v: changedData}},
} }
c := initClient(t) c := suite.client
defer c.Close() t := suite.T()
listener := NewEventListener(c) listener := NewEventListener(c)
dataListener := &mockDataListener{client: c, changedData: changedData, rc: make(chan remoting.Event)} dataListener := &mockDataListener{client: c, changedData: changedData, rc: make(chan remoting.Event)}
...@@ -64,7 +63,6 @@ func TestListener(t *testing.T) { ...@@ -64,7 +63,6 @@ func TestListener(t *testing.T) {
} }
msg := <-dataListener.rc msg := <-dataListener.rc
assert.Equal(t, changedData, msg.Content) assert.Equal(t, changedData, msg.Content)
} }
type mockDataListener struct { type mockDataListener struct {
......
...@@ -22,7 +22,7 @@ peer0_peer_port=2380 ...@@ -22,7 +22,7 @@ peer0_peer_port=2380
export ETCDCTL_API=3 export ETCDCTL_API=3
etcd_endpoints="http://${peer0_ip}:${peer0_client_port}" etcd_endpoints="http://${peer0_ip}:${peer0_client_port}"
ctl="./etcdctl --endpoints=$etcd_endpoints" ctl="etcdctl --endpoints=$etcd_endpoints"
usage() { usage() {
echo "Usage: $0 start" echo "Usage: $0 start"
...@@ -34,7 +34,7 @@ usage() { ...@@ -34,7 +34,7 @@ usage() {
} }
start() { start() {
./etcd --name=${name} \ etcd --name=${name} \
--initial-advertise-peer-urls http://${!ip}:${!peer_port} \ --initial-advertise-peer-urls http://${!ip}:${!peer_port} \
--listen-peer-urls http://${!ip}:${!peer_port} \ --listen-peer-urls http://${!ip}:${!peer_port} \
--listen-client-urls http://${!ip}:${!client_port},http://127.0.0.1:${!client_port} \ --listen-client-urls http://${!ip}:${!client_port},http://127.0.0.1:${!client_port} \
...@@ -43,7 +43,7 @@ start() { ...@@ -43,7 +43,7 @@ start() {
--initial-cluster etcd_node0=http://${peer0_ip}:${peer0_peer_port} \ --initial-cluster etcd_node0=http://${peer0_ip}:${peer0_peer_port} \
--initial-cluster-state new >> ${log_dir}/${name}.log 2>&1 & --initial-cluster-state new >> ${log_dir}/${name}.log 2>&1 &
sleep 2 sleep 5
PID=`ps aux | grep -w "name=${name}" | grep ${!client_port} | grep -v grep | awk '{print $2}'` PID=`ps aux | grep -w "name=${name}" | grep ${!client_port} | grep -v grep | awk '{print $2}'`
if [ "$PID" != "" ]; if [ "$PID" != "" ];
then then
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment