Run tests in parallel

since some integration tests are quite long (minutes), run them in
parallel.

To avoid unreadable logging (due multiple tests running in parallel),
use only the `testing` pkg logging functions so the output will be shown
only on error or at the end of every test (since we pass `-v` to `go
test`).

By default 4 parallel tests will be run. The parallelism level can be
configured with the PARALLEL environment variable when invoking the test
script.
This commit is contained in:
Simone Gotti 2016-03-14 14:04:02 +01:00
parent e358037ad0
commit c6888387f8
6 changed files with 109 additions and 73 deletions

3
test
View File

@ -122,7 +122,8 @@ if [ -n "$INTEGRATION" ]; then
exit 1
fi
go test -timeout 20m $@ -v ${REPO_PATH}/tests/integration
[ -z "$PARALLEL" ] && PARALLEL=4
go test -timeout 20m $@ -v -parallel ${PARALLEL} ${REPO_PATH}/tests/integration
fi
echo "Success"

View File

@ -36,7 +36,7 @@ func TestServerParameters(t *testing.T) {
}
defer os.RemoveAll(dir)
tstore, err := NewTestStore(dir)
tstore, err := NewTestStore(t, dir)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
@ -71,7 +71,7 @@ func TestServerParameters(t *testing.T) {
t.Fatalf("unexpected err: %v", err)
}
tk, err := NewTestKeeper(dir, clusterName, tstore.storeBackend, storeEndpoints)
tk, err := NewTestKeeper(t, dir, clusterName, tstore.storeBackend, storeEndpoints)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}

View File

@ -30,7 +30,7 @@ import (
)
func setupStore(t *testing.T, dir string) *TestStore {
tstore, err := NewTestStore(dir)
tstore, err := NewTestStore(t, dir)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
@ -44,6 +44,7 @@ func setupStore(t *testing.T, dir string) *TestStore {
}
func TestInitWithMultipleKeepers(t *testing.T) {
t.Parallel()
dir, err := ioutil.TempDir("", "stolon")
if err != nil {
t.Fatalf("unexpected err: %v", err)
@ -82,7 +83,7 @@ func TestInitWithMultipleKeepers(t *testing.T) {
// Start 3 keepers
for i := uint8(0); i < 3; i++ {
tk, err := NewTestKeeper(dir, clusterName, tstore.storeBackend, storeEndpoints)
tk, err := NewTestKeeper(t, dir, clusterName, tstore.storeBackend, storeEndpoints)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
@ -97,7 +98,7 @@ func TestInitWithMultipleKeepers(t *testing.T) {
// Start 2 sentinels
for i := uint8(0); i < 2; i++ {
ts, err := NewTestSentinel(dir, clusterName, tstore.storeBackend, storeEndpoints)
ts, err := NewTestSentinel(t, dir, clusterName, tstore.storeBackend, storeEndpoints)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
@ -146,17 +147,17 @@ func setupServers(t *testing.T, dir string, numKeepers, numSentinels uint8, sync
tks := []*TestKeeper{}
tss := []*TestSentinel{}
tk, err := NewTestKeeper(dir, clusterName, tstore.storeBackend, storeEndpoints)
tk, err := NewTestKeeper(t, dir, clusterName, tstore.storeBackend, storeEndpoints)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
tks = append(tks, tk)
fmt.Printf("tk: %v\n", tk)
t.Logf("tk: %v\n", tk)
// Start sentinels
for i := uint8(0); i < numSentinels; i++ {
ts, err := NewTestSentinel(dir, clusterName, tstore.storeBackend, storeEndpoints)
ts, err := NewTestSentinel(t, dir, clusterName, tstore.storeBackend, storeEndpoints)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
@ -183,7 +184,7 @@ func setupServers(t *testing.T, dir string, numKeepers, numSentinels uint8, sync
// Start other keepers
for i := uint8(1); i < numKeepers; i++ {
tk, err := NewTestKeeper(dir, clusterName, tstore.storeBackend, storeEndpoints)
tk, err := NewTestKeeper(t, dir, clusterName, tstore.storeBackend, storeEndpoints)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
@ -314,10 +315,12 @@ func testMasterStandby(t *testing.T, syncRepl bool) {
}
func TestMasterStandby(t *testing.T) {
t.Parallel()
testMasterStandby(t, false)
}
func TestMasterStandbySyncRepl(t *testing.T) {
t.Parallel()
testMasterStandby(t, true)
}
@ -344,7 +347,7 @@ func testFailover(t *testing.T, syncRepl bool) {
}
// Stop the keeper process on master, should also stop the database
fmt.Printf("Stopping current master keeper: %s\n", master.id)
t.Logf("Stopping current master keeper: %s\n", master.id)
master.Stop()
if err := standbys[0].WaitRole(common.MasterRole, 30*time.Second); err != nil {
@ -361,9 +364,11 @@ func testFailover(t *testing.T, syncRepl bool) {
}
func TestFailover(t *testing.T) {
t.Parallel()
testFailover(t, false)
}
func TestFailoverSyncRepl(t *testing.T) {
t.Parallel()
testFailover(t, true)
}
@ -390,7 +395,7 @@ func testOldMasterRestart(t *testing.T, syncRepl bool) {
}
// Stop the keeper process on master, should also stop the database
fmt.Printf("Stopping current master keeper: %s\n", master.id)
t.Logf("Stopping current master keeper: %s\n", master.id)
master.Stop()
if err := standbys[0].WaitRole(common.MasterRole, 30*time.Second); err != nil {
@ -410,7 +415,7 @@ func testOldMasterRestart(t *testing.T, syncRepl bool) {
}
// Restart the old master
fmt.Printf("Restarting old master keeper: %s\n", master.id)
t.Logf("Restarting old master keeper: %s\n", master.id)
if err := master.Start(); err != nil {
t.Fatalf("unexpected err: %v", err)
}
@ -427,10 +432,12 @@ func testOldMasterRestart(t *testing.T, syncRepl bool) {
}
func TestOldMasterRestart(t *testing.T) {
t.Parallel()
testOldMasterRestart(t, false)
}
func TestOldMasterRestartSyncRepl(t *testing.T) {
t.Parallel()
testOldMasterRestart(t, true)
}
@ -457,11 +464,11 @@ func testPartition1(t *testing.T, syncRepl bool) {
}
// Freeze the keeper and postgres processes on the master
fmt.Printf("SIGSTOPping current master keeper: %s\n", master.id)
t.Logf("SIGSTOPping current master keeper: %s\n", master.id)
if err := master.Signal(syscall.SIGSTOP); err != nil {
t.Fatalf("unexpected err: %v", err)
}
fmt.Printf("SIGSTOPping current master postgres: %s\n", master.id)
t.Logf("SIGSTOPping current master postgres: %s\n", master.id)
if err := master.SignalPG(syscall.SIGSTOP); err != nil {
t.Fatalf("unexpected err: %v", err)
}
@ -482,11 +489,11 @@ func testPartition1(t *testing.T, syncRepl bool) {
}
// Make the master come back
fmt.Printf("Resuming old master keeper: %s\n", master.id)
t.Logf("Resuming old master keeper: %s\n", master.id)
if err := master.Signal(syscall.SIGCONT); err != nil {
t.Fatalf("unexpected err: %v", err)
}
fmt.Printf("Resuming old master postgres: %s\n", master.id)
t.Logf("Resuming old master postgres: %s\n", master.id)
if err := master.SignalPG(syscall.SIGCONT); err != nil {
t.Fatalf("unexpected err: %v", err)
}
@ -503,10 +510,12 @@ func testPartition1(t *testing.T, syncRepl bool) {
}
func TestPartition1(t *testing.T) {
t.Parallel()
testPartition1(t, false)
}
func TestPartition1SyncRepl(t *testing.T) {
t.Parallel()
testPartition1(t, true)
}
@ -533,7 +542,7 @@ func testTimelineFork(t *testing.T, syncRepl bool) {
}
// Stop one standby
fmt.Printf("Stopping standby[0]: %s\n", master.id)
t.Logf("Stopping standby[0]: %s\n", master.id)
standbys[0].Stop()
if err := standbys[0].WaitDBDown(60 * time.Second); err != nil {
t.Fatalf("unexpected err: %v", err)
@ -566,7 +575,7 @@ func testTimelineFork(t *testing.T, syncRepl bool) {
}
// Start standby[0]. It will be elected as master but it'll be behind (having only one line).
fmt.Printf("Starting standby[0]: %s\n", standbys[0].id)
t.Logf("Starting standby[0]: %s\n", standbys[0].id)
standbys[0].Start()
if err := standbys[0].WaitDBUp(60 * time.Second); err != nil {
t.Fatalf("unexpected err: %v", err)
@ -583,7 +592,7 @@ func testTimelineFork(t *testing.T, syncRepl bool) {
}
// Start the other stadby, it should be ahead of current on previous timeline and should full resync himself
fmt.Printf("Starting standby[1]: %s\n", standbys[1].id)
t.Logf("Starting standby[1]: %s\n", standbys[1].id)
standbys[1].Start()
// Standby[1] will start, then it'll detect it's in another timelinehistory,
// will stop, full resync and start. We have to avoid detecting it up
@ -598,9 +607,11 @@ func testTimelineFork(t *testing.T, syncRepl bool) {
}
func TestTimelineFork(t *testing.T) {
t.Parallel()
testTimelineFork(t, false)
}
func TestTimelineForkSyncRepl(t *testing.T) {
t.Parallel()
testTimelineFork(t, true)
}

View File

@ -29,13 +29,15 @@ import (
)
func TestInit(t *testing.T) {
t.Parallel()
dir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
defer os.RemoveAll(dir)
tstore, err := NewTestStore(dir)
tstore, err := NewTestStore(t, dir)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
@ -50,7 +52,7 @@ func TestInit(t *testing.T) {
clusterName := uuid.NewV4().String()
ts, err := NewTestSentinel(dir, clusterName, tstore.storeBackend, storeEndpoints)
ts, err := NewTestSentinel(t, dir, clusterName, tstore.storeBackend, storeEndpoints)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
@ -58,7 +60,7 @@ func TestInit(t *testing.T) {
t.Fatalf("unexpected err: %v", err)
}
defer ts.Stop()
tk, err := NewTestKeeper(dir, clusterName, tstore.storeBackend, storeEndpoints)
tk, err := NewTestKeeper(t, dir, clusterName, tstore.storeBackend, storeEndpoints)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
@ -76,13 +78,15 @@ func TestInit(t *testing.T) {
}
func TestInitialClusterConfig(t *testing.T) {
t.Parallel()
dir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
defer os.RemoveAll(dir)
tstore, err := NewTestStore(dir)
tstore, err := NewTestStore(t, dir)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
@ -113,7 +117,7 @@ func TestInitialClusterConfig(t *testing.T) {
defer tmpFile.Close()
tmpFile.WriteString(`{ "synchronous_replication": true }`)
ts, err := NewTestSentinel(dir, clusterName, tstore.storeBackend, storeEndpoints, fmt.Sprintf("--initial-cluster-config=%s", tmpFile.Name()))
ts, err := NewTestSentinel(t, dir, clusterName, tstore.storeBackend, storeEndpoints, fmt.Sprintf("--initial-cluster-config=%s", tmpFile.Name()))
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
@ -136,13 +140,15 @@ func TestInitialClusterConfig(t *testing.T) {
}
func TestExclusiveLock(t *testing.T) {
t.Parallel()
dir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
defer os.RemoveAll(dir)
tstore, err := NewTestStore(dir)
tstore, err := NewTestStore(t, dir)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
@ -160,7 +166,7 @@ func TestExclusiveLock(t *testing.T) {
u := uuid.NewV4()
id := fmt.Sprintf("%x", u[:4])
tk1, err := NewTestKeeperWithID(dir, id, clusterName, tstore.storeBackend, storeEndpoints)
tk1, err := NewTestKeeperWithID(t, dir, id, clusterName, tstore.storeBackend, storeEndpoints)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
@ -174,7 +180,7 @@ func TestExclusiveLock(t *testing.T) {
t.Fatalf("expecting tk1 up but it's down")
}
tk2, err := NewTestKeeperWithID(dir, id, clusterName, tstore.storeBackend, storeEndpoints)
tk2, err := NewTestKeeperWithID(t, dir, id, clusterName, tstore.storeBackend, storeEndpoints)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
@ -191,6 +197,8 @@ func TestExclusiveLock(t *testing.T) {
}
func TestKeeperPGConfDirBad(t *testing.T) {
t.Parallel()
dir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatalf("unexpected err: %v", err)
@ -199,14 +207,14 @@ func TestKeeperPGConfDirBad(t *testing.T) {
clusterName := uuid.NewV4().String()
tstore, err := NewTestStore(dir)
tstore, err := NewTestStore(t, dir)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
storeEndpoints := fmt.Sprintf("%s:%s", tstore.listenAddress, tstore.port)
// Test pgConfDir not absolute path
tk, err := NewTestKeeper(dir, clusterName, tstore.storeBackend, storeEndpoints, "--pg-conf-dir=not/absolute/path")
tk, err := NewTestKeeper(t, dir, clusterName, tstore.storeBackend, storeEndpoints, "--pg-conf-dir=not/absolute/path")
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
@ -219,7 +227,7 @@ func TestKeeperPGConfDirBad(t *testing.T) {
}
// Test unexistent pgConfDir
tk2, err := NewTestKeeper(dir, clusterName, tstore.storeBackend, storeEndpoints, "--pg-conf-dir=/unexistent-configuration-directory")
tk2, err := NewTestKeeper(t, dir, clusterName, tstore.storeBackend, storeEndpoints, "--pg-conf-dir=/unexistent-configuration-directory")
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
@ -240,7 +248,7 @@ func TestKeeperPGConfDirBad(t *testing.T) {
tmpFile.Close()
os.Remove(tmpFile.Name())
}()
tk3, err := NewTestKeeper(dir, clusterName, tstore.storeBackend, storeEndpoints, fmt.Sprintf("--pg-conf-dir=%s", tmpFile.Name()))
tk3, err := NewTestKeeper(t, dir, clusterName, tstore.storeBackend, storeEndpoints, fmt.Sprintf("--pg-conf-dir=%s", tmpFile.Name()))
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
@ -254,6 +262,8 @@ func TestKeeperPGConfDirBad(t *testing.T) {
}
func TestKeeperPGConfDirGood(t *testing.T) {
t.Parallel()
dir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatalf("unexpected err: %v", err)
@ -262,7 +272,7 @@ func TestKeeperPGConfDirGood(t *testing.T) {
clusterName := uuid.NewV4().String()
tstore, err := NewTestStore(dir)
tstore, err := NewTestStore(t, dir)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
@ -275,7 +285,7 @@ func TestKeeperPGConfDirGood(t *testing.T) {
storeEndpoints := fmt.Sprintf("%s:%s", tstore.listenAddress, tstore.port)
defer tstore.Stop()
ts, err := NewTestSentinel(dir, clusterName, tstore.storeBackend, storeEndpoints)
ts, err := NewTestSentinel(t, dir, clusterName, tstore.storeBackend, storeEndpoints)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
@ -291,7 +301,7 @@ func TestKeeperPGConfDirGood(t *testing.T) {
}
defer os.Remove(tmpDir)
tk, err := NewTestKeeper(dir, clusterName, tstore.storeBackend, storeEndpoints, fmt.Sprintf("--pg-conf-dir=%s", tmpDir))
tk, err := NewTestKeeper(t, dir, clusterName, tstore.storeBackend, storeEndpoints, fmt.Sprintf("--pg-conf-dir=%s", tmpDir))
if err != nil {
t.Fatalf("unexpected err: %v", err)
}

View File

@ -17,7 +17,6 @@ package integration
import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"testing"
@ -31,6 +30,8 @@ import (
)
func TestProxyListening(t *testing.T) {
t.Parallel()
dir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatalf("unexpected err: %v", err)
@ -39,13 +40,13 @@ func TestProxyListening(t *testing.T) {
clusterName := uuid.NewV4().String()
tstore, err := NewTestStore(dir)
tstore, err := NewTestStore(t, dir)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
storeEndpoints := fmt.Sprintf("%s:%s", tstore.listenAddress, tstore.port)
tp, err := NewTestProxy(dir, clusterName, tstore.storeBackend, storeEndpoints)
tp, err := NewTestProxy(t, dir, clusterName, tstore.storeBackend, storeEndpoints)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
@ -54,7 +55,7 @@ func TestProxyListening(t *testing.T) {
}
defer tp.Stop()
log.Printf("test proxy start with store down. Should not listen")
t.Logf("test proxy start with store down. Should not listen")
// tp should not listen because it cannot talk with store
if err := tp.WaitNotListening(10 * time.Second); err != nil {
t.Fatalf("expecting tp not listening due to failed store communication, but it's listening.")
@ -101,7 +102,7 @@ func TestProxyListening(t *testing.T) {
}
// test proxy start with the store up
log.Printf("test proxy start with the store up. Should listen")
t.Logf("test proxy start with the store up. Should listen")
if err := tp.Start(); err != nil {
t.Fatalf("unexpected err: %v", err)
}
@ -111,7 +112,7 @@ func TestProxyListening(t *testing.T) {
t.Fatalf("expecting tp listening, but it's not listening.")
}
log.Printf("test proxy error communicating with store. Should stop listening")
t.Logf("test proxy error communicating with store. Should stop listening")
// Stop store
tstore.Stop()
if err := tstore.WaitDown(10 * time.Second); err != nil {
@ -123,7 +124,7 @@ func TestProxyListening(t *testing.T) {
t.Fatalf("expecting tp not listening due to failed store communication, but it's listening.")
}
log.Printf("test proxy communication with store restored. Should start listening")
t.Logf("test proxy communication with store restored. Should start listening")
// Start store
if err := tstore.Start(); err != nil {
t.Fatalf("unexpected err: %v", err)
@ -136,7 +137,7 @@ func TestProxyListening(t *testing.T) {
t.Fatalf("expecting tp listening, but it's not listening.")
}
log.Printf("test proxyConf removed. Should continue listening")
t.Logf("test proxyConf removed. Should continue listening")
// remove proxyConf
pair, err = e.SetClusterData(cluster.KeepersState{},
&cluster.ClusterView{
@ -156,7 +157,7 @@ func TestProxyListening(t *testing.T) {
t.Fatalf("expecting tp listening, but it's not listening.")
}
log.Printf("test proxyConf restored. Should continue listening")
t.Logf("test proxyConf restored. Should continue listening")
// Set proxyConf again
pair, err = e.SetClusterData(cluster.KeepersState{},
&cluster.ClusterView{
@ -180,7 +181,7 @@ func TestProxyListening(t *testing.T) {
t.Fatalf("expecting tp listening, but it's not listening.")
}
log.Printf("test clusterView removed. Should continue listening")
t.Logf("test clusterView removed. Should continue listening")
// remove whole clusterview
_, err = e.SetClusterData(cluster.KeepersState{}, nil, pair)
if err != nil {

View File

@ -27,6 +27,7 @@ import (
"os/exec"
"path/filepath"
"strconv"
"testing"
"time"
"github.com/sorintlab/stolon/common"
@ -41,6 +42,7 @@ import (
)
type Process struct {
t *testing.T
id string
name string
args []string
@ -64,7 +66,7 @@ func (p *Process) start() error {
go func() {
scanner := bufio.NewScanner(pr)
for scanner.Scan() {
fmt.Printf("[%s]: %s\n", p.id, scanner.Text())
p.t.Logf("[%s]: %s\n", p.id, scanner.Text())
}
}()
@ -84,7 +86,7 @@ func (p *Process) StartExpect() error {
}
func (p *Process) Signal(sig os.Signal) error {
fmt.Printf("signalling %s %s with %s\n", p.name, p.id, sig)
p.t.Logf("signalling %s %s with %s\n", p.name, p.id, sig)
if p.cmd == nil {
panic(fmt.Errorf("p: %s, cmd is empty", p.id))
}
@ -92,7 +94,7 @@ func (p *Process) Signal(sig os.Signal) error {
}
func (p *Process) Kill() {
fmt.Printf("killing %s %s\n", p.name, p.id)
p.t.Logf("killing %s %s\n", p.name, p.id)
if p.cmd == nil {
panic(fmt.Errorf("p: %s, cmd is empty", p.id))
}
@ -102,7 +104,7 @@ func (p *Process) Kill() {
}
func (p *Process) Stop() {
fmt.Printf("stopping %s %s\n", p.name, p.id)
p.t.Logf("stopping %s %s\n", p.name, p.id)
if p.cmd == nil {
panic(fmt.Errorf("p: %s, cmd is empty", p.id))
}
@ -128,6 +130,7 @@ func (p *Process) Wait(timeout time.Duration) error {
}
type TestKeeper struct {
t *testing.T
Process
dataDir string
listenAddress string
@ -137,7 +140,7 @@ type TestKeeper struct {
db *sql.DB
}
func NewTestKeeperWithID(dir string, id string, clusterName string, storeBackend store.Backend, storeEndpoints string, a ...string) (*TestKeeper, error) {
func NewTestKeeperWithID(t *testing.T, dir string, id string, clusterName string, storeBackend store.Backend, storeEndpoints string, a ...string) (*TestKeeper, error) {
args := []string{}
dataDir := filepath.Join(dir, fmt.Sprintf("st%s", id))
@ -180,7 +183,9 @@ func NewTestKeeperWithID(dir string, id string, clusterName string, storeBackend
return nil, fmt.Errorf("missing STKEEPER_BIN env")
}
tk := &TestKeeper{
t: t,
Process: Process{
t: t,
id: id,
name: "keeper",
bin: bin,
@ -196,11 +201,11 @@ func NewTestKeeperWithID(dir string, id string, clusterName string, storeBackend
return tk, nil
}
func NewTestKeeper(dir string, clusterName string, storeBackend store.Backend, storeEndpoints string, a ...string) (*TestKeeper, error) {
func NewTestKeeper(t *testing.T, dir string, clusterName string, storeBackend store.Backend, storeEndpoints string, a ...string) (*TestKeeper, error) {
u := uuid.NewV4()
id := fmt.Sprintf("%x", u[:4])
return NewTestKeeperWithID(dir, id, clusterName, storeBackend, storeEndpoints, a...)
return NewTestKeeperWithID(t, dir, id, clusterName, storeBackend, storeEndpoints, a...)
}
func (tk *TestKeeper) Exec(query string, args ...interface{}) (sql.Result, error) {
@ -228,7 +233,7 @@ func (tk *TestKeeper) WaitDBUp(timeout time.Duration) error {
if err == nil {
return nil
}
fmt.Printf("tk: %v, error: %v\n", tk.id, err)
tk.t.Logf("tk: %v, error: %v\n", tk.id, err)
time.Sleep(2 * time.Second)
}
@ -255,7 +260,7 @@ func (tk *TestKeeper) WaitUp(timeout time.Duration) error {
if err == nil {
return nil
}
fmt.Printf("tk: %v, error: %v\n", tk.id, err)
tk.t.Logf("tk: %v, error: %v\n", tk.id, err)
time.Sleep(1 * time.Second)
}
@ -407,13 +412,13 @@ func waitChecks(timeout time.Duration, fns ...CheckFunc) error {
}
type TestSentinel struct {
t *testing.T
Process
listenAddress string
port string
}
func NewTestSentinel(dir string, clusterName string, storeBackend store.Backend, storeEndpoints string, a ...string) (*TestSentinel, error) {
func NewTestSentinel(t *testing.T, dir string, clusterName string, storeBackend store.Backend, storeEndpoints string, a ...string) (*TestSentinel, error) {
u := uuid.NewV4()
id := fmt.Sprintf("%x", u[:4])
@ -440,7 +445,9 @@ func NewTestSentinel(dir string, clusterName string, storeBackend store.Backend,
return nil, fmt.Errorf("missing STSENTINEL_BIN env")
}
ts := &TestSentinel{
t: t,
Process: Process{
t: t,
id: id,
name: "sentinel",
bin: bin,
@ -453,13 +460,13 @@ func NewTestSentinel(dir string, clusterName string, storeBackend store.Backend,
}
type TestProxy struct {
t *testing.T
Process
listenAddress string
port string
}
func NewTestProxy(dir string, clusterName string, storeBackend store.Backend, storeEndpoints string, a ...string) (*TestProxy, error) {
func NewTestProxy(t *testing.T, dir string, clusterName string, storeBackend store.Backend, storeEndpoints string, a ...string) (*TestProxy, error) {
u := uuid.NewV4()
id := fmt.Sprintf("%x", u[:4])
@ -486,7 +493,9 @@ func NewTestProxy(dir string, clusterName string, storeBackend store.Backend, st
return nil, fmt.Errorf("missing STPROXY_BIN env")
}
tp := &TestProxy{
t: t,
Process: Process{
t: t,
id: id,
name: "proxy",
bin: bin,
@ -505,7 +514,7 @@ func (tp *TestProxy) WaitListening(timeout time.Duration) error {
if err == nil {
return nil
}
fmt.Printf("tp: %v, error: %v\n", tp.id, err)
tp.t.Logf("tp: %v, error: %v\n", tp.id, err)
time.Sleep(2 * time.Second)
}
return fmt.Errorf("timeout")
@ -518,33 +527,33 @@ func (tp *TestProxy) WaitNotListening(timeout time.Duration) error {
if err != nil {
return nil
}
fmt.Printf("tp: %v, error: %v\n", tp.id, err)
tp.t.Logf("tp: %v, error: %v\n", tp.id, err)
time.Sleep(2 * time.Second)
}
return fmt.Errorf("timeout")
}
type TestStore struct {
t *testing.T
Process
listenAddress string
port string
store kvstore.Store
storeBackend store.Backend
}
func NewTestStore(dir string, a ...string) (*TestStore, error) {
func NewTestStore(t *testing.T, dir string, a ...string) (*TestStore, error) {
storeBackend := store.Backend(os.Getenv("STOLON_TEST_STORE_BACKEND"))
switch storeBackend {
case store.CONSUL:
return NewTestConsul(dir, a...)
return NewTestConsul(t, dir, a...)
case store.ETCD:
return NewTestEtcd(dir, a...)
return NewTestEtcd(t, dir, a...)
}
return nil, fmt.Errorf("wrong store backend")
}
func NewTestEtcd(dir string, a ...string) (*TestStore, error) {
func NewTestEtcd(t *testing.T, dir string, a ...string) (*TestStore, error) {
u := uuid.NewV4()
id := fmt.Sprintf("%x", u[:4])
@ -589,7 +598,9 @@ func NewTestEtcd(dir string, a ...string) (*TestStore, error) {
return nil, fmt.Errorf("missing ETCD_BIN env")
}
te := &TestStore{
t: t,
Process: Process{
t: t,
id: id,
name: "etcd",
bin: bin,
@ -603,7 +614,7 @@ func NewTestEtcd(dir string, a ...string) (*TestStore, error) {
return te, nil
}
func NewTestConsul(dir string, a ...string) (*TestStore, error) {
func NewTestConsul(t *testing.T, dir string, a ...string) (*TestStore, error) {
u := uuid.NewV4()
id := fmt.Sprintf("%x", u[:4])
@ -668,8 +679,10 @@ func NewTestConsul(dir string, a ...string) (*TestStore, error) {
if bin == "" {
return nil, fmt.Errorf("missing CONSUL_BIN env")
}
te := &TestStore{
ts := &TestStore{
t: t,
Process: Process{
t: t,
id: id,
name: "consul",
bin: bin,
@ -680,14 +693,14 @@ func NewTestConsul(dir string, a ...string) (*TestStore, error) {
store: kvstore,
storeBackend: store.CONSUL,
}
return te, nil
return ts, nil
}
func (te *TestStore) WaitUp(timeout time.Duration) error {
func (ts *TestStore) WaitUp(timeout time.Duration) error {
start := time.Now()
for time.Now().Add(-timeout).Before(start) {
_, err := te.store.Get("anykey")
fmt.Printf("err: %v\n", err)
_, err := ts.store.Get("anykey")
ts.t.Logf("err: %v\n", err)
if err != nil && err == kvstore.ErrKeyNotFound {
return nil
}
@ -700,10 +713,10 @@ func (te *TestStore) WaitUp(timeout time.Duration) error {
return fmt.Errorf("timeout")
}
func (te *TestStore) WaitDown(timeout time.Duration) error {
func (ts *TestStore) WaitDown(timeout time.Duration) error {
start := time.Now()
for time.Now().Add(-timeout).Before(start) {
_, err := te.store.Get("anykey")
_, err := ts.store.Get("anykey")
if err != nil && err != kvstore.ErrKeyNotFound {
return nil
}