Skip to content

Commit

Permalink
Export StartTestCluster to make it useable for other packages
Browse files Browse the repository at this point in the history
  • Loading branch information
samuel committed Oct 29, 2013
1 parent 4660345 commit ab53843
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 107 deletions.
4 changes: 2 additions & 2 deletions zk/lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

func TestLock(t *testing.T) {
ts, err := startTestCluster(1)
ts, err := StartTestCluster(1)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -64,7 +64,7 @@ func TestLock(t *testing.T) {
// This tests creating a lock with a path that's more than 1 node deep (e.g. "/test-multi-level/lock"),
// when a part of that path already exists (i.e. "/test-multi-level" node already exists).
func TestMultiLevelLock(t *testing.T) {
ts, err := startTestCluster(1)
ts, err := StartTestCluster(1)
if err != nil {
t.Fatal(err)
}
Expand Down
103 changes: 103 additions & 0 deletions zk/server_help.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package zk

import (
"fmt"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
"time"
)

type TestServer struct {
Port int
Path string
Srv *Server
}

type TestCluster struct {
Path string
Servers []TestServer
}

func StartTestCluster(size int) (*TestCluster, error) {
tmpPath, err := ioutil.TempDir("", "gozk")
if err != nil {
return nil, err
}
success := false
startPort := int(rand.Int31n(6000) + 10000)
cluster := &TestCluster{Path: tmpPath}
defer func() {
if !success {
cluster.stop()
}
}()
for serverN := 0; serverN < size; serverN++ {
srvPath := filepath.Join(tmpPath, fmt.Sprintf("srv%d", serverN))
if err := os.Mkdir(srvPath, 0700); err != nil {
return nil, err
}
port := startPort + serverN*3
cfg := ServerConfig{
ClientPort: port,
DataDir: srvPath,
}
for i := 0; i < size; i++ {
cfg.Servers = append(cfg.Servers, ServerConfigServer{
Id: i + 1,
Host: "127.0.0.1",
PeerPort: port + 1,
LeaderElectionPort: port + 2,
})
}
cfgPath := filepath.Join(srvPath, "zoo.cfg")
fi, err := os.Create(cfgPath)
if err != nil {
return nil, err
}
err = cfg.Marshall(fi)
fi.Close()
if err != nil {
return nil, err
}

fi, err = os.Create(filepath.Join(srvPath, "myid"))
if err != nil {
return nil, err
}
_, err = fmt.Fprintf(fi, "%d\n", serverN+1)
fi.Close()
if err != nil {
return nil, err
}

srv := &Server{
ConfigPath: cfgPath,
}
if err := srv.Start(); err != nil {
return nil, err
}
cluster.Servers = append(cluster.Servers, TestServer{
Path: srvPath,
Port: cfg.ClientPort,
Srv: srv,
})
}
success = true
time.Sleep(time.Second) // Give the server time to become active. Should probably actually attempt to connect to verify.
return cluster, nil
}

func (ts *TestCluster) connect(idx int) (*Conn, error) {
zk, _, err := Connect([]string{fmt.Sprintf("127.0.0.1:%d", ts.Servers[idx].Port)}, time.Second*15)
return zk, err
}

func (ts *TestCluster) stop() error {
for _, srv := range ts.Servers {
srv.Srv.Stop()
}
defer os.RemoveAll(ts.Path)
return nil
}
112 changes: 7 additions & 105 deletions zk/zk_test.go
Original file line number Diff line number Diff line change
@@ -1,110 +1,12 @@
package zk

import (
"fmt"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
"testing"
"time"
)

type testServer struct {
path string
port int
srv *Server
}

type testCluster struct {
path string
servers []testServer
}

func startTestCluster(size int) (*testCluster, error) {
tmpPath, err := ioutil.TempDir("", "gozk")
if err != nil {
return nil, err
}
success := false
startPort := int(rand.Int31n(6000) + 10000)
cluster := &testCluster{path: tmpPath}
defer func() {
if !success {
cluster.stop()
}
}()
for serverN := 0; serverN < size; serverN++ {
srvPath := filepath.Join(tmpPath, fmt.Sprintf("srv%d", serverN))
if err := os.Mkdir(srvPath, 0700); err != nil {
return nil, err
}
port := startPort + serverN*3
cfg := ServerConfig{
ClientPort: port,
DataDir: srvPath,
}
for i := 0; i < size; i++ {
cfg.Servers = append(cfg.Servers, ServerConfigServer{
Id: i + 1,
Host: "127.0.0.1",
PeerPort: port + 1,
LeaderElectionPort: port + 2,
})
}
cfgPath := filepath.Join(srvPath, "zoo.cfg")
fi, err := os.Create(cfgPath)
if err != nil {
return nil, err
}
err = cfg.Marshall(fi)
fi.Close()
if err != nil {
return nil, err
}

fi, err = os.Create(filepath.Join(srvPath, "myid"))
if err != nil {
return nil, err
}
_, err = fmt.Fprintf(fi, "%d\n", serverN+1)
fi.Close()
if err != nil {
return nil, err
}

srv := &Server{
ConfigPath: cfgPath,
}
if err := srv.Start(); err != nil {
return nil, err
}
cluster.servers = append(cluster.servers, testServer{
path: srvPath,
port: cfg.ClientPort,
srv: srv,
})
}
success = true
time.Sleep(time.Second) // Give the server time to become active. Should probably actually attempt to connect to verify.
return cluster, nil
}

func (ts *testCluster) connect(idx int) (*Conn, error) {
zk, _, err := Connect([]string{fmt.Sprintf("127.0.0.1:%d", ts.servers[idx].port)}, time.Second*15)
return zk, err
}

func (ts *testCluster) stop() error {
for _, srv := range ts.servers {
srv.srv.Stop()
}
defer os.RemoveAll(ts.path)
return nil
}

func TestCreate(t *testing.T) {
ts, err := startTestCluster(1)
ts, err := StartTestCluster(1)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -135,7 +37,7 @@ func TestCreate(t *testing.T) {
}

func TestMulti(t *testing.T) {
ts, err := startTestCluster(1)
ts, err := StartTestCluster(1)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -175,7 +77,7 @@ func TestMulti(t *testing.T) {
}

func TestGetSetACL(t *testing.T) {
ts, err := startTestCluster(1)
ts, err := StartTestCluster(1)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -229,7 +131,7 @@ func TestGetSetACL(t *testing.T) {
}

func TestAuth(t *testing.T) {
ts, err := startTestCluster(1)
ts, err := StartTestCluster(1)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -279,7 +181,7 @@ func TestAuth(t *testing.T) {
}

func TestChildWatch(t *testing.T) {
ts, err := startTestCluster(1)
ts, err := StartTestCluster(1)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -323,7 +225,7 @@ func TestChildWatch(t *testing.T) {
}

func TestSetWatchers(t *testing.T) {
ts, err := startTestCluster(1)
ts, err := StartTestCluster(1)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -379,7 +281,7 @@ func TestSetWatchers(t *testing.T) {
}

func TestExpiringWatch(t *testing.T) {
ts, err := startTestCluster(1)
ts, err := StartTestCluster(1)
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit ab53843

Please sign in to comment.