Skip to content

Commit

Permalink
Use a common quorum for most client tests. Tag tests as parallel wher…
Browse files Browse the repository at this point in the history
…ever possible. The drops total test time 70%.
  • Loading branch information
msolo committed Jan 27, 2019
1 parent 1a1ad7f commit b903a8a
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 113 deletions.
23 changes: 9 additions & 14 deletions zk/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

type logWriter struct {
t *testing.T
t tSimple
p string
}

Expand All @@ -17,6 +17,7 @@ func (lw logWriter) Write(b []byte) (int, error) {
}

func TestBasicCluster(t *testing.T) {
t.Parallel()
ts, err := StartTestCluster(t, 3, nil, logWriter{t: t, p: "[ZKERR] "})
if err != nil {
t.Fatal(err)
Expand All @@ -33,8 +34,6 @@ func TestBasicCluster(t *testing.T) {
}
defer zk2.Close()

time.Sleep(time.Second * 5)

if _, err := zk1.Create("/gozk-test", []byte("foo-cluster"), 0, WorldACL(PermAll)); err != nil {
t.Fatalf("Create failed on node 1: %+v", err)
}
Expand All @@ -48,6 +47,7 @@ func TestBasicCluster(t *testing.T) {

// If the current leader dies, then the session is reestablished with the new one.
func TestClientClusterFailover(t *testing.T) {
t.Parallel()
tc, err := StartTestCluster(t, 3, nil, logWriter{t: t, p: "[ZKERR] "})
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -87,9 +87,10 @@ func TestClientClusterFailover(t *testing.T) {
}
}

// If a ZooKeeper cluster looses quorum then a session is reconnected as soon
// If a ZooKeeper cluster loses quorum then a session is reconnected as soon
// as the quorum is restored.
func TestNoQuorum(t *testing.T) {
t.Parallel()
tc, err := StartTestCluster(t, 3, nil, logWriter{t: t, p: "[ZKERR] "})
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -186,11 +187,8 @@ func TestNoQuorum(t *testing.T) {
}

func TestWaitForClose(t *testing.T) {
ts, err := StartTestCluster(t, 1, nil, logWriter{t: t, p: "[ZKERR] "})
if err != nil {
t.Fatal(err)
}
defer ts.Stop()
t.Parallel()
ts := defaultTestCluster(t)
zk, _, err := ts.Connect(0)
if err != nil {
t.Fatalf("Connect returned error: %+v", err)
Expand Down Expand Up @@ -222,11 +220,8 @@ CONNECTED:
}

func TestBadSession(t *testing.T) {
ts, err := StartTestCluster(t, 1, nil, logWriter{t: t, p: "[ZKERR] "})
if err != nil {
t.Fatal(err)
}
defer ts.Stop()
t.Parallel()
ts := defaultTestCluster(t)
zk, _, err := ts.ConnectAll()
if err != nil {
t.Fatalf("Connect returned error: %+v", err)
Expand Down
9 changes: 3 additions & 6 deletions zk/dnshostprovider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@ func localhostLookupHost(host string) ([]string, error) {
// TestDNSHostProviderCreate is just like TestCreate, but with an
// overridden HostProvider that ignores the provided hostname.
func TestDNSHostProviderCreate(t *testing.T) {
ts, err := StartTestCluster(t, 1, nil, logWriter{t: t, p: "[ZKERR] "})
if err != nil {
t.Fatal(err)
}
defer ts.Stop()
t.Parallel()
ts := defaultTestCluster(t)

port := ts.Servers[0].Port
server := fmt.Sprintf("foo.example.com:%d", port)
Expand All @@ -31,7 +28,7 @@ func TestDNSHostProviderCreate(t *testing.T) {
}
defer zk.Close()

path := "/gozk-test"
path := "/TestDNSHostProviderCreate-gozk-test"

if err := zk.Delete(path, -1); err != nil && err != ErrNoNode {
t.Fatalf("Delete returned error: %+v", err)
Expand Down
12 changes: 2 additions & 10 deletions zk/lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ import (
)

func TestLock(t *testing.T) {
ts, err := StartTestCluster(t, 1, nil, logWriter{t: t, p: "[ZKERR] "})
if err != nil {
t.Fatal(err)
}
defer ts.Stop()
ts := defaultTestCluster(t)
zk, _, err := ts.ConnectAll()
if err != nil {
t.Fatalf("Connect returned error: %+v", err)
Expand Down Expand Up @@ -64,11 +60,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(t, 1, nil, logWriter{t: t, p: "[ZKERR] "})
if err != nil {
t.Fatal(err)
}
defer ts.Stop()
ts := defaultTestCluster(t)

zk, _, err := ts.ConnectAll()
if err != nil {
Expand Down
45 changes: 41 additions & 4 deletions zk/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"fmt"
"io"
"io/ioutil"
"log"
"math/rand"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"time"
Expand All @@ -17,7 +19,7 @@ const (
_testMyIDFileName = "myid"
)

func NewIntegrationTestServer(t *testing.T, configPath string, stdout, stderr io.Writer) (*server, error) {
func NewIntegrationTestServer(t tSimple, configPath string, stdout, stderr io.Writer) (*server, error) {
// allow external systems to configure this zk server bin path.
zkPath := os.Getenv("ZOOKEEPER_BIN_PATH")
if zkPath == "" {
Expand Down Expand Up @@ -53,9 +55,9 @@ type TestCluster struct {
Servers []TestServer
}

func StartTestCluster(t *testing.T, size int, stdout, stderr io.Writer) (*TestCluster, error) {
func StartTestCluster(t tSimple, size int, stdout, stderr io.Writer) (*TestCluster, error) {
if testing.Short() {
t.Skip("ZK clsuter tests skipped in short case.")
t.Skip("ZK cluster tests skipped in short case.")
}
tmpPath, err := ioutil.TempDir("", "gozk")
requireNoError(t, err, "failed to create tmp dir for test server setup")
Expand Down Expand Up @@ -258,9 +260,44 @@ func (tc *TestCluster) StopAllServers() error {
return nil
}

func requireNoError(t *testing.T, err error, msgAndArgs ...interface{}) {
func requireNoError(t tSimple, err error, msgAndArgs ...interface{}) {
t.Helper()
if err != nil {
t.Logf("received unexpected error: %v", err)
t.Fatal(msgAndArgs...)
}
}

// Create a simple interface to emulate just enough of the T struct
// to be used during setup when no T struct is available.
type tSimple interface {
Skip(args ...interface{})
Helper()
Logf(format string, args ...interface{})
Fatal(args ...interface{})
Fatalf(format string, args ...interface{})
}

type tSetup struct{}

func (ts *tSetup) Skip(args ...interface{}) {
log.Println(args...)
runtime.Goexit()
}

// Ignore stack labeling here as it is unnecessary.
func (ts *tSetup) Helper() {}

func (ts *tSetup) Logf(format string, args ...interface{}) {
log.Printf(format, args...)
}

func (ts *tSetup) Fatal(args ...interface{}) {
log.Println(args...)
os.Exit(1)
}

func (ts *tSetup) Fatalf(format string, args ...interface{}) {
log.Printf(format, args...)
os.Exit(1)
}
1 change: 1 addition & 0 deletions zk/simplednshostprovider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func TestSimpleDNSHostProviderRetryStart(t *testing.T) {
// remaps addresses to localhost:$PORT combinations corresponding to
// the test ZooKeeper instances.
func TestSimpleDNSHostProviderReconnect(t *testing.T) {
t.Parallel()
ts, err := StartTestCluster(t, 3, nil, logWriter{t: t, p: "[ZKERR] "})
if err != nil {
t.Fatal(err)
Expand Down
Loading

0 comments on commit b903a8a

Please sign in to comment.