From f25d727035f4615cbb4b43452191fd83218fc1e1 Mon Sep 17 00:00:00 2001 From: Anton Kaliaev Date: Thu, 7 Mar 2019 09:10:34 +0400 Subject: [PATCH] make dupl linter pass (#3385) Refs #3262 --- .golangci.yml | 1 - Makefile | 4 +- libs/db/db_test.go | 147 ++++++++++++++++++--------------------- rpc/client/event_test.go | 58 ++++++--------- 4 files changed, 90 insertions(+), 120 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 45cabe201..cf8bf165d 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -9,7 +9,6 @@ linters: - maligned - errcheck - staticcheck - - dupl - ineffassign - interfacer - unconvert diff --git a/Makefile b/Makefile index 08373644c..79ae6aaba 100644 --- a/Makefile +++ b/Makefile @@ -214,11 +214,11 @@ vagrant_test: ### go tests test: @echo "--> Running go test" - go test -p 1 $(PACKAGES) + @go test -p 1 $(PACKAGES) test_race: @echo "--> Running go test --race" - go test -p 1 -v -race $(PACKAGES) + @go test -p 1 -v -race $(PACKAGES) # uses https://github.com/sasha-s/go-deadlock/ to detect potential deadlocks test_with_deadlock: diff --git a/libs/db/db_test.go b/libs/db/db_test.go index ffa7bb6aa..7cb721b26 100644 --- a/libs/db/db_test.go +++ b/libs/db/db_test.go @@ -121,86 +121,75 @@ func TestDBIteratorNonemptyBeginAfter(t *testing.T) { } } -func TestDBBatchWrite1(t *testing.T) { - mdb := newMockDB() - ddb := NewDebugDB(t.Name(), mdb) - batch := ddb.NewBatch() - - batch.Set(bz("1"), bz("1")) - batch.Set(bz("2"), bz("2")) - batch.Delete(bz("3")) - batch.Set(bz("4"), bz("4")) - batch.Write() - - assert.Equal(t, 0, mdb.calls["Set"]) - assert.Equal(t, 0, mdb.calls["SetSync"]) - assert.Equal(t, 3, mdb.calls["SetNoLock"]) - assert.Equal(t, 0, mdb.calls["SetNoLockSync"]) - assert.Equal(t, 0, mdb.calls["Delete"]) - assert.Equal(t, 0, mdb.calls["DeleteSync"]) - assert.Equal(t, 1, mdb.calls["DeleteNoLock"]) - assert.Equal(t, 0, mdb.calls["DeleteNoLockSync"]) -} +func TestDBBatchWrite(t *testing.T) { + testCases := []struct { + modify func(batch Batch) + calls map[string]int + }{ + 0: { + func(batch Batch) { + batch.Set(bz("1"), bz("1")) + batch.Set(bz("2"), bz("2")) + batch.Delete(bz("3")) + batch.Set(bz("4"), bz("4")) + batch.Write() + }, + map[string]int{ + "Set": 0, "SetSync": 0, "SetNoLock": 3, "SetNoLockSync": 0, + "Delete": 0, "DeleteSync": 0, "DeleteNoLock": 1, "DeleteNoLockSync": 0, + }, + }, + 1: { + func(batch Batch) { + batch.Set(bz("1"), bz("1")) + batch.Set(bz("2"), bz("2")) + batch.Set(bz("4"), bz("4")) + batch.Delete(bz("3")) + batch.Write() + }, + map[string]int{ + "Set": 0, "SetSync": 0, "SetNoLock": 3, "SetNoLockSync": 0, + "Delete": 0, "DeleteSync": 0, "DeleteNoLock": 1, "DeleteNoLockSync": 0, + }, + }, + 2: { + func(batch Batch) { + batch.Set(bz("1"), bz("1")) + batch.Set(bz("2"), bz("2")) + batch.Delete(bz("3")) + batch.Set(bz("4"), bz("4")) + batch.WriteSync() + }, + map[string]int{ + "Set": 0, "SetSync": 0, "SetNoLock": 2, "SetNoLockSync": 1, + "Delete": 0, "DeleteSync": 0, "DeleteNoLock": 1, "DeleteNoLockSync": 0, + }, + }, + 3: { + func(batch Batch) { + batch.Set(bz("1"), bz("1")) + batch.Set(bz("2"), bz("2")) + batch.Set(bz("4"), bz("4")) + batch.Delete(bz("3")) + batch.WriteSync() + }, + map[string]int{ + "Set": 0, "SetSync": 0, "SetNoLock": 3, "SetNoLockSync": 0, + "Delete": 0, "DeleteSync": 0, "DeleteNoLock": 0, "DeleteNoLockSync": 1, + }, + }, + } -func TestDBBatchWrite2(t *testing.T) { - mdb := newMockDB() - ddb := NewDebugDB(t.Name(), mdb) - batch := ddb.NewBatch() - - batch.Set(bz("1"), bz("1")) - batch.Set(bz("2"), bz("2")) - batch.Set(bz("4"), bz("4")) - batch.Delete(bz("3")) - batch.Write() - - assert.Equal(t, 0, mdb.calls["Set"]) - assert.Equal(t, 0, mdb.calls["SetSync"]) - assert.Equal(t, 3, mdb.calls["SetNoLock"]) - assert.Equal(t, 0, mdb.calls["SetNoLockSync"]) - assert.Equal(t, 0, mdb.calls["Delete"]) - assert.Equal(t, 0, mdb.calls["DeleteSync"]) - assert.Equal(t, 1, mdb.calls["DeleteNoLock"]) - assert.Equal(t, 0, mdb.calls["DeleteNoLockSync"]) -} + for i, tc := range testCases { + mdb := newMockDB() + ddb := NewDebugDB(t.Name(), mdb) + batch := ddb.NewBatch() -func TestDBBatchWriteSync1(t *testing.T) { - mdb := newMockDB() - ddb := NewDebugDB(t.Name(), mdb) - batch := ddb.NewBatch() - - batch.Set(bz("1"), bz("1")) - batch.Set(bz("2"), bz("2")) - batch.Delete(bz("3")) - batch.Set(bz("4"), bz("4")) - batch.WriteSync() - - assert.Equal(t, 0, mdb.calls["Set"]) - assert.Equal(t, 0, mdb.calls["SetSync"]) - assert.Equal(t, 2, mdb.calls["SetNoLock"]) - assert.Equal(t, 1, mdb.calls["SetNoLockSync"]) - assert.Equal(t, 0, mdb.calls["Delete"]) - assert.Equal(t, 0, mdb.calls["DeleteSync"]) - assert.Equal(t, 1, mdb.calls["DeleteNoLock"]) - assert.Equal(t, 0, mdb.calls["DeleteNoLockSync"]) -} + tc.modify(batch) -func TestDBBatchWriteSync2(t *testing.T) { - mdb := newMockDB() - ddb := NewDebugDB(t.Name(), mdb) - batch := ddb.NewBatch() - - batch.Set(bz("1"), bz("1")) - batch.Set(bz("2"), bz("2")) - batch.Set(bz("4"), bz("4")) - batch.Delete(bz("3")) - batch.WriteSync() - - assert.Equal(t, 0, mdb.calls["Set"]) - assert.Equal(t, 0, mdb.calls["SetSync"]) - assert.Equal(t, 3, mdb.calls["SetNoLock"]) - assert.Equal(t, 0, mdb.calls["SetNoLockSync"]) - assert.Equal(t, 0, mdb.calls["Delete"]) - assert.Equal(t, 0, mdb.calls["DeleteSync"]) - assert.Equal(t, 0, mdb.calls["DeleteNoLock"]) - assert.Equal(t, 1, mdb.calls["DeleteNoLockSync"]) + for call, exp := range tc.calls { + got := mdb.calls[call] + assert.Equal(t, exp, got, "#%v - key: %s", i, call) + } + } } diff --git a/rpc/client/event_test.go b/rpc/client/event_test.go index da4625d51..7b00d6ead 100644 --- a/rpc/client/event_test.go +++ b/rpc/client/event_test.go @@ -1,6 +1,7 @@ package client_test import ( + "fmt" "reflect" "testing" "time" @@ -10,6 +11,7 @@ import ( abci "github.com/tendermint/tendermint/abci/types" cmn "github.com/tendermint/tendermint/libs/common" "github.com/tendermint/tendermint/rpc/client" + ctypes "github.com/tendermint/tendermint/rpc/core/types" "github.com/tendermint/tendermint/types" ) @@ -78,7 +80,10 @@ func TestBlockEvents(t *testing.T) { } } -func TestTxEventsSentWithBroadcastTxAsync(t *testing.T) { +func TestTxEventsSentWithBroadcastTxAsync(t *testing.T) { testTxEventsSent(t, "async") } +func TestTxEventsSentWithBroadcastTxSync(t *testing.T) { testTxEventsSent(t, "sync") } + +func testTxEventsSent(t *testing.T, broadcastMethod string) { for i, c := range GetClients() { i, c := i, c // capture params t.Run(reflect.TypeOf(c).String(), func(t *testing.T) { @@ -95,45 +100,22 @@ func TestTxEventsSentWithBroadcastTxAsync(t *testing.T) { _, _, tx := MakeTxKV() evtTyp := types.EventTx - // send async - txres, err := c.BroadcastTxAsync(tx) - require.Nil(t, err, "%+v", err) - require.Equal(t, txres.Code, abci.CodeTypeOK) // FIXME - - // and wait for confirmation - evt, err := client.WaitForOneEvent(c, evtTyp, waitForEventTimeout) - require.Nil(t, err, "%d: %+v", i, err) - // and make sure it has the proper info - txe, ok := evt.(types.EventDataTx) - require.True(t, ok, "%d: %#v", i, evt) - // make sure this is the proper tx - require.EqualValues(t, tx, txe.Tx) - require.True(t, txe.Result.IsOK()) - }) - } -} - -func TestTxEventsSentWithBroadcastTxSync(t *testing.T) { - for i, c := range GetClients() { - i, c := i, c // capture params - t.Run(reflect.TypeOf(c).String(), func(t *testing.T) { - - // start for this test it if it wasn't already running - if !c.IsRunning() { - // if so, then we start it, listen, and stop it. - err := c.Start() - require.Nil(t, err, "%d: %+v", i, err) - defer c.Stop() + // send + var ( + txres *ctypes.ResultBroadcastTx + err error + ) + switch broadcastMethod { + case "async": + txres, err = c.BroadcastTxAsync(tx) + case "sync": + txres, err = c.BroadcastTxSync(tx) + default: + panic(fmt.Sprintf("Unknown broadcastMethod %s", broadcastMethod)) } - // make the tx - _, _, tx := MakeTxKV() - evtTyp := types.EventTx - - // send sync - txres, err := c.BroadcastTxSync(tx) - require.Nil(t, err, "%+v", err) - require.Equal(t, txres.Code, abci.CodeTypeOK) // FIXME + require.NoError(t, err) + require.Equal(t, txres.Code, abci.CodeTypeOK) // and wait for confirmation evt, err := client.WaitForOneEvent(c, evtTyp, waitForEventTimeout)