From 42a9b847ec81449b9c39d09c0533d1ad0b42b579 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Wed, 22 Feb 2017 20:01:32 +0100 Subject: [PATCH] Make all client tests safe to run in parallel --- Makefile | 4 ++-- rpc/client/http/rpc_test.go | 29 +++++++++++++++++++++-------- rpc/test/helpers.go | 37 ++++++++++++++++++++++++++++++++++++- 3 files changed, 59 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index 9985c39ad..bfdaf0077 100644 --- a/Makefile +++ b/Makefile @@ -22,11 +22,11 @@ dist: test: @echo "--> Running go test" - @go test -p 1 $(PACKAGES) + @go test $(PACKAGES) test_race: @echo "--> Running go test --race" - @go test -p 1 -v -race $(PACKAGES) + @go test -v -race $(PACKAGES) test_integrations: @bash ./test/test.sh diff --git a/rpc/client/http/rpc_test.go b/rpc/client/http/rpc_test.go index b2c0c6e99..4f48c7a27 100644 --- a/rpc/client/http/rpc_test.go +++ b/rpc/client/http/rpc_test.go @@ -38,7 +38,7 @@ func TestInfo(t *testing.T) { info, err := c.ABCIInfo() require.Nil(t, err, "%+v", err) assert.EqualValues(t, status.LatestBlockHeight, info.Response.LastBlockHeight) - assert.True(t, strings.HasPrefix(info.Response.Data, "size:")) + assert.True(t, strings.HasPrefix(info.Response.Data, "size")) } func TestNetInfo(t *testing.T) { @@ -83,8 +83,18 @@ func TestGenesisAndValidators(t *testing.T) { func TestAppCalls(t *testing.T) { assert, require := assert.New(t), require.New(t) c := GetClient() - _, err := c.Block(1) + + // get an offset of height to avoid racing and guessing + s, err := c.Status() + require.Nil(err) + // sh is start height or status height + sh := s.LatestBlockHeight + + // look for the future + _, err = c.Block(sh + 2) assert.NotNil(err) // no block yet + + // write something k, v, tx := MakeTxKV() _, err = c.BroadcastTxCommit(tx) require.Nil(err, "%+v", err) @@ -96,36 +106,39 @@ func TestAppCalls(t *testing.T) { // assert.Equal(k, data.GetKey()) // only returned for proofs assert.Equal(v, data.GetValue()) } + // +/- 1 making my head hurt + h := int(qres.Response.Height) - 1 + // and we can even check the block is added - block, err := c.Block(3) + block, err := c.Block(h) require.Nil(err, "%+v", err) appHash := block.BlockMeta.Header.AppHash assert.True(len(appHash) > 0) - assert.EqualValues(3, block.BlockMeta.Header.Height) + assert.EqualValues(h, block.BlockMeta.Header.Height) // check blockchain info, now that we know there is info // TODO: is this commented somewhere that they are returned // in order of descending height??? - info, err := c.BlockchainInfo(1, 3) + info, err := c.BlockchainInfo(h-2, h) require.Nil(err, "%+v", err) assert.True(info.LastHeight > 2) if assert.Equal(3, len(info.BlockMetas)) { lastMeta := info.BlockMetas[0] - assert.EqualValues(3, lastMeta.Header.Height) + assert.EqualValues(h, lastMeta.Header.Height) bMeta := block.BlockMeta assert.Equal(bMeta.Header.AppHash, lastMeta.Header.AppHash) assert.Equal(bMeta.BlockID, lastMeta.BlockID) } // and get the corresponding commit with the same apphash - commit, err := c.Commit(3) + commit, err := c.Commit(h) require.Nil(err, "%+v", err) cappHash := commit.Header.AppHash assert.Equal(appHash, cappHash) assert.NotNil(commit.Commit) // compare the commits (note Commit(2) has commit from Block(3)) - commit2, err := c.Commit(2) + commit2, err := c.Commit(h - 1) require.Nil(err, "%+v", err) assert.Equal(block.Block.LastCommit, commit2.Commit) diff --git a/rpc/test/helpers.go b/rpc/test/helpers.go index e835a3930..f51c83f9d 100644 --- a/rpc/test/helpers.go +++ b/rpc/test/helpers.go @@ -2,6 +2,10 @@ package rpctest import ( "fmt" + "math/rand" + "os" + "path/filepath" + "strings" "testing" "time" @@ -27,12 +31,43 @@ var ( const tmLogLevel = "error" +// f**ing long, but unique for each test +func makePathname() string { + // get path + p, err := os.Getwd() + if err != nil { + panic(err) + } + fmt.Println(p) + sep := string(filepath.Separator) + return strings.Replace(p, sep, "_", -1) +} + +func randPort() int { + // returns between base and base + spread + base, spread := 20000, 20000 + return base + rand.Intn(spread) +} + +func makeAddrs() (string, string, string) { + start := randPort() + return fmt.Sprintf("tcp://0.0.0.0:%d", start), + fmt.Sprintf("tcp://0.0.0.0:%d", start+1), + fmt.Sprintf("tcp://0.0.0.0:%d", start+2) +} + // GetConfig returns a config for the test cases as a singleton func GetConfig() cfg.Config { if config == nil { - config = tendermint_test.ResetConfig("rpc_test_client_test") + pathname := makePathname() + config = tendermint_test.ResetConfig(pathname) // Shut up the logging logger.SetLogLevel(tmLogLevel) + // and we use random ports to run in parallel + tm, rpc, grpc := makeAddrs() + config.Set("node_laddr", tm) + config.Set("rpc_laddr", rpc) + config.Set("grpc_laddr", grpc) } return config }