From 5bd809f9c88ada458b821487e04069a67e905d0f Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Mon, 1 Mar 2021 11:13:52 +0300 Subject: [PATCH 1/4] *: drop go 1.13 support --- .circleci/config.yml | 19 ------------------- .github/workflows/run_tests.yml | 2 +- README.md | 2 +- go.mod | 2 +- 4 files changed, 3 insertions(+), 22 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index b6c61f5dd..29fd38ff1 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -3,11 +3,6 @@ orbs: codecov: codecov/codecov@1.0.5 executors: - go1_13: - docker: - - image: circleci/golang:1.13 - environment: - GO111MODULE: "on" go1_14: docker: - image: circleci/golang:1.14 @@ -54,16 +49,6 @@ jobs: name: go-vet command: go vet ./... - test_1_13: - working_directory: /go/src/github.com/nspcc-dev/neo-go - executor: go1_13 - steps: - - checkout - - run: git submodule sync - - run: git submodule update --init - - gomod - - run: go test -v -race ./... - test_1_14: working_directory: /go/src/github.com/nspcc-dev/neo-go executor: go1_14 @@ -132,10 +117,6 @@ workflows: filters: tags: only: v/[0-9]+\.[0-9]+\.[0-9]+/ - - test_1_13: - filters: - tags: - only: v/[0-9]+\.[0-9]+\.[0-9]+/ - test_1_14: filters: tags: diff --git a/.github/workflows/run_tests.yml b/.github/workflows/run_tests.yml index 64fae1f67..e3d56c0be 100644 --- a/.github/workflows/run_tests.yml +++ b/.github/workflows/run_tests.yml @@ -109,7 +109,7 @@ jobs: runs-on: ubuntu-18.04 strategy: matrix: - go_versions: [ '1.13', '1.14', '1.15' ] + go_versions: [ '1.14', '1.15' ] fail-fast: false steps: - uses: actions/checkout@v2 diff --git a/README.md b/README.md index 23bf6ddaf..808a97a40 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ Neo 3 code (0.90.0 being compatible with Neo 3 preview2). ## Installation -Go: 1.13+ +Go: 1.14+ Install dependencies. diff --git a/go.mod b/go.mod index 83bc560ee..f44763810 100644 --- a/go.mod +++ b/go.mod @@ -30,4 +30,4 @@ require ( gopkg.in/yaml.v2 v2.2.4 ) -go 1.13 +go 1.14 From 0cec99a3eac067b9728a93c3b82bf2949a085d42 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Mon, 1 Mar 2021 11:45:43 +0300 Subject: [PATCH 2/4] gomod: update dbft version --- go.mod | 2 +- go.sum | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index f44763810..ceac42631 100644 --- a/go.mod +++ b/go.mod @@ -12,7 +12,7 @@ require ( github.com/gorilla/websocket v1.4.2 github.com/hashicorp/golang-lru v0.5.4 github.com/mr-tron/base58 v1.1.2 - github.com/nspcc-dev/dbft v0.0.0-20210122071512-d9a728094f0d + github.com/nspcc-dev/dbft v0.0.0-20210301140418-d31e1e0c272f github.com/nspcc-dev/rfc6979 v0.2.0 github.com/pierrec/lz4 v2.5.2+incompatible github.com/prometheus/client_golang v1.2.1 diff --git a/go.sum b/go.sum index eefc4bb3a..ae5ed481e 100644 --- a/go.sum +++ b/go.sum @@ -173,6 +173,12 @@ github.com/nspcc-dev/dbft v0.0.0-20200219114139-199d286ed6c1 h1:yEx9WznS+rjE0jl0 github.com/nspcc-dev/dbft v0.0.0-20200219114139-199d286ed6c1/go.mod h1:O0qtn62prQSqizzoagHmuuKoz8QMkU3SzBoKdEvm3aQ= github.com/nspcc-dev/dbft v0.0.0-20210122071512-d9a728094f0d h1:uUaRysqa/9VtHETVARUlteqfbXAgwxR2nvUc4DzK4pI= github.com/nspcc-dev/dbft v0.0.0-20210122071512-d9a728094f0d/go.mod h1:I5D0W3tu3epdt2RMCTxS//HDr4S+OHRqajouQTOAHI8= +github.com/nspcc-dev/dbft v0.0.0-20210301082123-a8be68a63b6f h1:jKbF0lOzhkRGa0mndMWS+n/pxWLkuD+0IYBzSIPBZfo= +github.com/nspcc-dev/dbft v0.0.0-20210301082123-a8be68a63b6f/go.mod h1:U8MSnEShH+o5hexfWJdze6uMFJteP0ko7J2frO7Yu1Y= +github.com/nspcc-dev/dbft v0.0.0-20210301092823-72d7079ea827 h1:0/kXunWrz9a+4V1rcHMEcrKOJ/n/9VcFcAosEXTH7r4= +github.com/nspcc-dev/dbft v0.0.0-20210301092823-72d7079ea827/go.mod h1:U8MSnEShH+o5hexfWJdze6uMFJteP0ko7J2frO7Yu1Y= +github.com/nspcc-dev/dbft v0.0.0-20210301140418-d31e1e0c272f h1:d5vDIXpt9xFQeXg6LzCzCh5Lf8HS5EHLSBGLGwdWywQ= +github.com/nspcc-dev/dbft v0.0.0-20210301140418-d31e1e0c272f/go.mod h1:U8MSnEShH+o5hexfWJdze6uMFJteP0ko7J2frO7Yu1Y= github.com/nspcc-dev/neo-go v0.73.1-pre.0.20200303142215-f5a1b928ce09/go.mod h1:pPYwPZ2ks+uMnlRLUyXOpLieaDQSEaf4NM3zHVbRjmg= github.com/nspcc-dev/neofs-crypto v0.2.0 h1:ftN+59WqxSWz/RCgXYOfhmltOOqU+udsNQSvN6wkFck= github.com/nspcc-dev/neofs-crypto v0.2.0/go.mod h1:F/96fUzPM3wR+UGsPi3faVNmFlA9KAEAUQR7dMxZmNA= From 2c81fc8b8e107617fd1518db39da426095e32b03 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Mon, 1 Mar 2021 14:14:15 +0300 Subject: [PATCH 3/4] *: upgrade tests to use T.Cleanup() --- cli/candidate_test.go | 1 - cli/contract_test.go | 38 +++++++++------- cli/dump_test.go | 5 ++- cli/executor_test.go | 3 ++ cli/main_test.go | 1 - cli/multisig_test.go | 11 +++-- cli/nep17_test.go | 4 -- cli/options_test.go | 1 - cli/server/dump_test.go | 4 +- cli/server/server_test.go | 28 ++++++------ cli/smartcontract/smart_contract_test.go | 4 +- cli/wallet_test.go | 30 +++++++------ pkg/compiler/compiler_test.go | 4 +- pkg/consensus/consensus_test.go | 11 +---- pkg/consensus/payload_test.go | 1 - pkg/consensus/recovery_message_test.go | 1 - pkg/core/blockchain_test.go | 19 +------- pkg/core/helper_test.go | 7 +-- pkg/core/interop_neo_test.go | 1 - pkg/core/interop_system_test.go | 24 +++------- pkg/core/interops_test.go | 1 - pkg/core/mempool/subscriptions_test.go | 2 +- pkg/core/native_contract_test.go | 3 -- pkg/core/native_designate_test.go | 2 - pkg/core/native_ledger_test.go | 4 -- pkg/core/native_management_test.go | 9 ---- pkg/core/native_name_service_test.go | 10 ----- pkg/core/native_neo_test.go | 5 --- pkg/core/native_notary_test.go | 3 -- pkg/core/native_oracle_test.go | 1 - pkg/core/native_policy_test.go | 7 --- pkg/core/notary_test.go | 5 +-- pkg/core/oracle_test.go | 5 +-- pkg/core/storage/boltdb_store_test.go | 4 +- pkg/io/fileWriter_test.go | 8 +++- pkg/network/helper_test.go | 1 + pkg/network/server_test.go | 56 ++++++++++-------------- pkg/rpc/client/rpc_test.go | 10 ++--- pkg/rpc/client/wsclient_test.go | 7 --- pkg/vm/cli/cli_test.go | 8 +++- pkg/wallet/wallet_test.go | 8 +++- 41 files changed, 137 insertions(+), 220 deletions(-) diff --git a/cli/candidate_test.go b/cli/candidate_test.go index a85c68f4d..ff16ba0ba 100644 --- a/cli/candidate_test.go +++ b/cli/candidate_test.go @@ -13,7 +13,6 @@ import ( // stop working after validator will change. func TestRegisterCandidate(t *testing.T) { e := newExecutor(t, true) - defer e.Close(t) e.In.WriteString("one\r") e.Run(t, "neo-go", "wallet", "nep17", "multitransfer", diff --git a/cli/contract_test.go b/cli/contract_test.go index be5d7d24b..56daf0a03 100644 --- a/cli/contract_test.go +++ b/cli/contract_test.go @@ -27,7 +27,6 @@ import ( func TestCalcHash(t *testing.T) { e := newExecutor(t, false) - defer e.Close(t) nefPath := "./testdata/verify.nef" src, err := ioutil.ReadFile(nefPath) @@ -58,7 +57,9 @@ func TestCalcHash(t *testing.T) { }) t.Run("invalid file", func(t *testing.T) { p := path.Join(os.TempDir(), "neogo.calchash.verify.nef") - defer os.Remove(p) + t.Cleanup(func() { + os.Remove(p) + }) require.NoError(t, ioutil.WriteFile(p, src[:4], os.ModePerm)) e.RunWithError(t, append(cmd, "--sender", sender.StringLE(), "--in", p, "--manifest", manifestPath)...) }) @@ -82,10 +83,11 @@ func TestCalcHash(t *testing.T) { func TestContractInitAndCompile(t *testing.T) { tmpDir := path.Join(os.TempDir(), "neogo.inittest") require.NoError(t, os.Mkdir(tmpDir, os.ModePerm)) - defer os.RemoveAll(tmpDir) + t.Cleanup(func() { + os.RemoveAll(tmpDir) + }) e := newExecutor(t, false) - defer e.Close(t) t.Run("no path is provided", func(t *testing.T) { e.RunWithError(t, "neo-go", "contract", "init") @@ -140,14 +142,15 @@ func TestDeployBigContract(t *testing.T) { e := newExecutorWithConfig(t, true, func(c *config.Config) { c.ApplicationConfiguration.RPC.MaxGasInvoke = fixedn.Fixed8(1) }) - defer e.Close(t) // For proper nef generation. config.Version = "0.90.0-test" tmpDir := path.Join(os.TempDir(), "neogo.test.deployfail") require.NoError(t, os.Mkdir(tmpDir, os.ModePerm)) - defer os.RemoveAll(tmpDir) + t.Cleanup(func() { + os.RemoveAll(tmpDir) + }) nefName := path.Join(tmpDir, "deploy.nef") manifestName := path.Join(tmpDir, "deploy.manifest.json") @@ -165,14 +168,15 @@ func TestDeployBigContract(t *testing.T) { func TestComlileAndInvokeFunction(t *testing.T) { e := newExecutor(t, true) - defer e.Close(t) // For proper nef generation. config.Version = "0.90.0-test" tmpDir := path.Join(os.TempDir(), "neogo.test.compileandinvoke") require.NoError(t, os.Mkdir(tmpDir, os.ModePerm)) - defer os.RemoveAll(tmpDir) + t.Cleanup(func() { + os.RemoveAll(tmpDir) + }) nefName := path.Join(tmpDir, "deploy.nef") manifestName := path.Join(tmpDir, "deploy.manifest.json") @@ -314,10 +318,10 @@ func TestComlileAndInvokeFunction(t *testing.T) { "--in", "testdata/deploy/", // compile all files in dir "--out", nefName, "--manifest", manifestName) - defer func() { + t.Cleanup(func() { os.Remove(nefName) os.Remove(manifestName) - }() + }) rawNef, err := ioutil.ReadFile(nefName) require.NoError(t, err) @@ -349,7 +353,6 @@ func TestComlileAndInvokeFunction(t *testing.T) { func TestContractInspect(t *testing.T) { e := newExecutor(t, false) - defer e.Close(t) // For proper nef generation. config.Version = "0.90.0-test" @@ -357,7 +360,9 @@ func TestContractInspect(t *testing.T) { tmpDir := path.Join(os.TempDir(), "neogo.test.contract.inspect") require.NoError(t, os.Mkdir(tmpDir, os.ModePerm)) - defer os.RemoveAll(tmpDir) + t.Cleanup(func() { + os.RemoveAll(tmpDir) + }) nefName := path.Join(tmpDir, "deploy.nef") manifestName := path.Join(tmpDir, "deploy.manifest.json") @@ -394,7 +399,6 @@ func TestCompileExamples(t *testing.T) { tmpDir := os.TempDir() e := newExecutor(t, false) - defer e.Close(t) for _, info := range infos { t.Run(info.Name(), func(t *testing.T) { @@ -404,10 +408,10 @@ func TestCompileExamples(t *testing.T) { outPath := path.Join(tmpDir, info.Name()+".nef") manifestPath := path.Join(tmpDir, info.Name()+".manifest.json") - defer func() { + t.Cleanup(func() { os.Remove(outPath) os.Remove(manifestPath) - }() + }) cfgName := filterFilename(infos, ".yml") opts := []string{ @@ -426,10 +430,10 @@ func TestCompileExamples(t *testing.T) { for _, name := range []string{"invalid1", "invalid2", "invalid3"} { outPath := path.Join(tmpDir, name+".nef") manifestPath := path.Join(tmpDir, name+".manifest.json") - defer func() { + t.Cleanup(func() { os.Remove(outPath) os.Remove(manifestPath) - }() + }) e.RunWithError(t, "neo-go", "contract", "compile", "--in", path.Join(dir, name), "--out", outPath, diff --git a/cli/dump_test.go b/cli/dump_test.go index 64191e129..fb2f24389 100644 --- a/cli/dump_test.go +++ b/cli/dump_test.go @@ -14,7 +14,9 @@ import ( func TestDBRestore(t *testing.T) { tmpDir := path.Join(os.TempDir(), "neogo.restoretest") require.NoError(t, os.Mkdir(tmpDir, os.ModePerm)) - defer os.RemoveAll(tmpDir) + t.Cleanup(func() { + os.RemoveAll(tmpDir) + }) chainPath := path.Join(tmpDir, "neogotestchain") cfg, err := config.LoadFile("../config/protocol.unit_testnet.yml") @@ -31,7 +33,6 @@ func TestDBRestore(t *testing.T) { // generated via `go run ./scripts/gendump/main.go --out ./cli/testdata/chain50x2.acc --blocks 50 --txs 2` const inDump = "./testdata/chain50x2.acc" e := newExecutor(t, false) - defer e.Close(t) stateDump := path.Join(tmpDir, "neogo.teststate") baseArgs := []string{"neo-go", "db", "restore", "--unittest", "--config-path", tmpDir, "--in", inDump, "--dump", stateDump} diff --git a/cli/executor_test.go b/cli/executor_test.go index 7274e1301..3c75d4e23 100644 --- a/cli/executor_test.go +++ b/cli/executor_test.go @@ -102,6 +102,9 @@ func newExecutorWithConfig(t *testing.T, needChain bool, f func(*config.Config)) if needChain { e.Chain, e.RPC, e.NetSrv = newTestChain(t, f) } + t.Cleanup(func() { + e.Close(t) + }) return e } diff --git a/cli/main_test.go b/cli/main_test.go index c5ac7a622..3c70662d0 100644 --- a/cli/main_test.go +++ b/cli/main_test.go @@ -6,7 +6,6 @@ import ( func TestCLIVersion(t *testing.T) { e := newExecutor(t, false) - defer e.Close(t) e.Run(t, "neo-go", "--version") e.checkNextLine(t, "^neo-go version") e.checkEOF(t) diff --git a/cli/multisig_test.go b/cli/multisig_test.go index e3a9fd304..337f4d81e 100644 --- a/cli/multisig_test.go +++ b/cli/multisig_test.go @@ -20,7 +20,6 @@ import ( // 2. Transfer from multisig to another account. func TestSignMultisigTx(t *testing.T) { e := newExecutor(t, true) - defer e.Close(t) privs, pubs := generateKeys(t, 3) script, err := smartcontract.CreateMultiSigRedeemScript(2, pubs) @@ -31,9 +30,11 @@ func TestSignMultisigTx(t *testing.T) { // Create 2 wallets participating in multisig. tmpDir := os.TempDir() wallet1Path := path.Join(tmpDir, "multiWallet1.json") - defer os.Remove(wallet1Path) wallet2Path := path.Join(tmpDir, "multiWallet2.json") - defer os.Remove(wallet2Path) + t.Cleanup(func() { + os.Remove(wallet1Path) + os.Remove(wallet2Path) + }) addAccount := func(w string, wif string) { e.Run(t, "neo-go", "wallet", "init", "--wallet", w) @@ -64,7 +65,9 @@ func TestSignMultisigTx(t *testing.T) { require.NoError(t, err) txPath := path.Join(tmpDir, "multisigtx.json") - defer os.Remove(txPath) + t.Cleanup(func() { + os.Remove(txPath) + }) e.In.WriteString("pass\r") e.Run(t, "neo-go", "wallet", "nep17", "transfer", "--rpc-endpoint", "http://"+e.RPC.Addr, diff --git a/cli/nep17_test.go b/cli/nep17_test.go index 50d84baf2..8948176f1 100644 --- a/cli/nep17_test.go +++ b/cli/nep17_test.go @@ -18,7 +18,6 @@ import ( func TestNEP17Balance(t *testing.T) { e := newExecutor(t, true) - defer e.Close(t) cmdbalance := []string{"neo-go", "wallet", "nep17", "balance"} cmdbase := append(cmdbalance, "--rpc-endpoint", "http://"+e.RPC.Addr, @@ -106,7 +105,6 @@ func TestNEP17Transfer(t *testing.T) { defer w.Close() e := newExecutor(t, true) - defer e.Close(t) args := []string{ "neo-go", "wallet", "nep17", "transfer", "--rpc-endpoint", "http://" + e.RPC.Addr, @@ -164,7 +162,6 @@ func TestNEP17MultiTransfer(t *testing.T) { privs, _ := generateKeys(t, 3) e := newExecutor(t, true) - defer e.Close(t) neoContractHash, err := e.Chain.GetNativeContractScriptHash(nativenames.Neo) require.NoError(t, err) args := []string{ @@ -191,7 +188,6 @@ func TestNEP17MultiTransfer(t *testing.T) { func TestNEP17ImportToken(t *testing.T) { e := newExecutor(t, true) - defer e.Close(t) tmpDir := os.TempDir() walletPath := path.Join(tmpDir, "walletForImport.json") diff --git a/cli/options_test.go b/cli/options_test.go index 912501f89..5658f8179 100644 --- a/cli/options_test.go +++ b/cli/options_test.go @@ -11,7 +11,6 @@ import ( func TestGetRPCClient(t *testing.T) { e := newExecutor(t, true) - defer e.Close(t) t.Run("no endpoint", func(t *testing.T) { set := flag.NewFlagSet("flagSet", flag.ExitOnError) diff --git a/cli/server/dump_test.go b/cli/server/dump_test.go index d118125c7..f6d3cae67 100644 --- a/cli/server/dump_test.go +++ b/cli/server/dump_test.go @@ -11,10 +11,10 @@ import ( func TestGetPath(t *testing.T) { testPath, err := ioutil.TempDir("./", "") require.NoError(t, err) - defer func() { + t.Cleanup(func() { err := os.RemoveAll(testPath) require.NoError(t, err) - }() + }) path, err := getPath(testPath, 123) require.NoError(t, err) require.Equal(t, testPath+"/BlockStorage_100000/dump-block-1000.json", path) diff --git a/cli/server/server_test.go b/cli/server/server_test.go index d00959531..5f0184550 100644 --- a/cli/server/server_test.go +++ b/cli/server/server_test.go @@ -29,9 +29,9 @@ func TestGetConfigFromContext(t *testing.T) { func TestHandleLoggingParams(t *testing.T) { testLog, err := ioutil.TempFile("./", "*.log") require.NoError(t, err) - defer func() { + t.Cleanup(func() { require.NoError(t, os.Remove(testLog.Name())) - }() + }) t.Run("default", func(t *testing.T) { set := flag.NewFlagSet("flagSet", flag.ExitOnError) @@ -63,10 +63,10 @@ func TestInitBCWithMetrics(t *testing.T) { d, err := ioutil.TempDir("./", "") require.NoError(t, err) os.Chdir(d) - defer func() { + t.Cleanup(func() { os.Chdir("..") os.RemoveAll(d) - }() + }) set := flag.NewFlagSet("flagSet", flag.ExitOnError) set.String("config-path", "../../../config", "") @@ -79,9 +79,11 @@ func TestInitBCWithMetrics(t *testing.T) { require.NoError(t, err) chain, prometheus, pprof, err := initBCWithMetrics(cfg, logger) require.NoError(t, err) - defer chain.Close() - defer prometheus.ShutDown() - defer pprof.ShutDown() + t.Cleanup(func() { + chain.Close() + prometheus.ShutDown() + pprof.ShutDown() + }) require.Equal(t, netmode.TestNet, chain.GetConfig().Magic) } @@ -90,10 +92,10 @@ func TestDumpDB(t *testing.T) { d, err := ioutil.TempDir("./", "") require.NoError(t, err) os.Chdir(d) - defer func() { + t.Cleanup(func() { os.Chdir("..") os.RemoveAll(d) - }() + }) testDump := "file.acc" set := flag.NewFlagSet("flagSet", flag.ExitOnError) set.String("config-path", "../../../config", "") @@ -111,10 +113,10 @@ func TestDumpDB(t *testing.T) { d, err := ioutil.TempDir("./", "") require.NoError(t, err) os.Chdir(d) - defer func() { + t.Cleanup(func() { os.Chdir("..") os.RemoveAll(d) - }() + }) testDump := "file.acc" set := flag.NewFlagSet("flagSet", flag.ExitOnError) set.String("config-path", "../../../config", "") @@ -135,10 +137,10 @@ func TestRestoreDB(t *testing.T) { testDump := "file1.acc" saveDump := "file2.acc" os.Chdir(d) - defer func() { + t.Cleanup(func() { os.Chdir("..") os.RemoveAll(d) - }() + }) //dump first set := flag.NewFlagSet("flagSet", flag.ExitOnError) diff --git a/cli/smartcontract/smart_contract_test.go b/cli/smartcontract/smart_contract_test.go index eba92c021..64e8811fb 100644 --- a/cli/smartcontract/smart_contract_test.go +++ b/cli/smartcontract/smart_contract_test.go @@ -18,10 +18,10 @@ func TestInitSmartContract(t *testing.T) { d, err := ioutil.TempDir("./", "") require.NoError(t, err) os.Chdir(d) - defer func() { + t.Cleanup(func() { os.Chdir("..") os.RemoveAll(d) - }() + }) contractName := "testContract" set := flag.NewFlagSet("flagSet", flag.ExitOnError) diff --git a/cli/wallet_test.go b/cli/wallet_test.go index 923c5950e..259d8ed0f 100644 --- a/cli/wallet_test.go +++ b/cli/wallet_test.go @@ -22,10 +22,11 @@ import ( func TestWalletInit(t *testing.T) { tmpDir := path.Join(os.TempDir(), "neogo.test.walletinit") require.NoError(t, os.Mkdir(tmpDir, os.ModePerm)) - defer os.RemoveAll(tmpDir) + t.Cleanup(func() { + os.RemoveAll(tmpDir) + }) e := newExecutor(t, false) - defer e.Close(t) walletPath := path.Join(tmpDir, "wallet.json") e.Run(t, "neo-go", "wallet", "init", "--wallet", walletPath) @@ -90,7 +91,7 @@ func TestWalletInit(t *testing.T) { w, err := wallet.NewWalletFromFile(walletPath) require.NoError(t, err) - defer w.Close() + t.Cleanup(w.Close) acc := w.GetAccount(priv.GetScriptHash()) require.NotNil(t, acc) require.Equal(t, "test_account", acc.Label) @@ -121,7 +122,7 @@ func TestWalletInit(t *testing.T) { w, err := wallet.NewWalletFromFile(walletPath) require.NoError(t, err) - defer w.Close() + t.Cleanup(w.Close) actual := w.GetAccount(acc.PrivateKey().GetScriptHash()) require.NotNil(t, actual) require.NoError(t, actual.Decrypt("somepass")) @@ -157,7 +158,7 @@ func TestWalletInit(t *testing.T) { w, err := wallet.NewWalletFromFile(walletPath) require.NoError(t, err) - defer w.Close() + t.Cleanup(w.Close) actual := w.GetAccount(hash.Hash160(script)) require.NotNil(t, actual) require.NoError(t, actual.Decrypt("multipass")) @@ -168,7 +169,6 @@ func TestWalletInit(t *testing.T) { func TestWalletExport(t *testing.T) { e := newExecutor(t, false) - defer e.Close(t) t.Run("Encrypted", func(t *testing.T) { e.Run(t, "neo-go", "wallet", "export", @@ -195,12 +195,11 @@ func TestWalletExport(t *testing.T) { func TestClaimGas(t *testing.T) { e := newExecutor(t, true) - defer e.Close(t) const walletPath = "testdata/testwallet.json" w, err := wallet.NewWalletFromFile(walletPath) require.NoError(t, err) - defer w.Close() + t.Cleanup(w.Close) args := []string{ "neo-go", "wallet", "nep17", "multitransfer", @@ -243,7 +242,6 @@ func TestClaimGas(t *testing.T) { func TestImportDeployed(t *testing.T) { e := newExecutor(t, true) - defer e.Close(t) e.In.WriteString("one\r") e.Run(t, "neo-go", "contract", "deploy", @@ -262,7 +260,9 @@ func TestImportDeployed(t *testing.T) { tmpDir := os.TempDir() walletPath := path.Join(tmpDir, "wallet.json") e.Run(t, "neo-go", "wallet", "init", "--wallet", walletPath) - defer os.Remove(walletPath) + t.Cleanup(func() { + os.Remove(walletPath) + }) priv, err := keys.NewPrivateKey() require.NoError(t, err) @@ -274,7 +274,9 @@ func TestImportDeployed(t *testing.T) { "--contract", h.StringLE()) w, err := wallet.NewWalletFromFile(walletPath) - defer w.Close() + t.Cleanup(func() { + w.Close() + }) require.NoError(t, err) require.Equal(t, 1, len(w.Accounts)) contractAddr := w.Accounts[0].Address @@ -309,7 +311,6 @@ func TestImportDeployed(t *testing.T) { func TestWalletDump(t *testing.T) { e := newExecutor(t, false) - defer e.Close(t) cmd := []string{"neo-go", "wallet", "dump", "--wallet", "testdata/testwallet.json"} e.Run(t, cmd...) @@ -340,10 +341,11 @@ func TestWalletDump(t *testing.T) { func TestWalletConvert(t *testing.T) { tmpDir := path.Join(os.TempDir(), "neogo.test.convert") require.NoError(t, os.Mkdir(tmpDir, os.ModePerm)) - defer os.RemoveAll(tmpDir) + t.Cleanup(func() { + os.RemoveAll(tmpDir) + }) e := newExecutor(t, false) - defer e.Close(t) outPath := path.Join(tmpDir, "wallet.json") cmd := []string{"neo-go", "wallet", "convert"} diff --git a/pkg/compiler/compiler_test.go b/pkg/compiler/compiler_test.go index 5ab8e614f..7ea0e8f65 100644 --- a/pkg/compiler/compiler_test.go +++ b/pkg/compiler/compiler_test.go @@ -62,10 +62,10 @@ func TestCompiler(t *testing.T) { require.NoError(t, err) err = os.MkdirAll(exampleSavePath, os.ModePerm) require.NoError(t, err) - defer func() { + t.Cleanup(func() { err := os.RemoveAll(exampleSavePath) require.NoError(t, err) - }() + }) outfile := exampleSavePath + "/test.nef" _, err = compiler.CompileAndSave(exampleCompilePath+"/"+infos[0].Name(), &compiler.Options{Outfile: outfile}) require.NoError(t, err) diff --git a/pkg/consensus/consensus_test.go b/pkg/consensus/consensus_test.go index 541033b4a..7c08940e4 100644 --- a/pkg/consensus/consensus_test.go +++ b/pkg/consensus/consensus_test.go @@ -44,7 +44,6 @@ func TestNewService(t *testing.T) { require.NotPanics(t, func() { txx = srv.getVerifiedTx() }) require.Len(t, txx, 1) require.Equal(t, tx, txx[0]) - srv.Chain.Close() } func initServiceNextConsensus(t *testing.T, newAcc *wallet.Account, offset uint32) (*service, *wallet.Account) { @@ -129,7 +128,6 @@ func TestService_NextConsensus(t *testing.T) { t.Run("vote 1 block before update", func(t *testing.T) { // voting occurs every block in SingleTestChain srv, acc := initServiceNextConsensus(t, newAcc, 1) bc := srv.Chain.(*core.Blockchain) - defer bc.Close() height := bc.BlockHeight() checkNextConsensus(t, bc, height, acc.Contract.ScriptHash()) @@ -219,7 +217,6 @@ func TestService_GetVerified(t *testing.T) { require.Contains(t, txx, txs[1]) require.NotContains(t, txx, txs[2]) }) - srv.Chain.Close() } func TestService_ValidatePayload(t *testing.T) { @@ -257,7 +254,6 @@ func TestService_ValidatePayload(t *testing.T) { require.NoError(t, p.Sign(priv)) require.True(t, srv.validatePayload(p)) }) - srv.Chain.Close() } func TestService_getTx(t *testing.T) { @@ -294,13 +290,12 @@ func TestService_getTx(t *testing.T) { require.NotNil(t, got) require.Equal(t, h, got.Hash()) }) - srv.Chain.Close() } func TestService_PrepareRequest(t *testing.T) { srv := newTestServiceWithState(t, true) srv.dbft.Start() - defer srv.dbft.Timer.Stop() + t.Cleanup(srv.dbft.Timer.Stop) priv, _ := getTestValidator(1) p := new(Payload) @@ -359,12 +354,10 @@ func TestService_OnPayload(t *testing.T) { require.NoError(t, p.Sign(priv)) srv.OnPayload(&p.Extensible) shouldReceive(t, srv.messages) - srv.Chain.Close() } func TestVerifyBlock(t *testing.T) { srv := newTestService(t) - defer srv.Chain.Close() srv.lastTimestamp = 1 t.Run("good empty", func(t *testing.T) { @@ -513,7 +506,7 @@ func newTestChain(t *testing.T, stateRootInHeader bool) *core.Blockchain { require.NoError(t, err) go chain.Run() - + t.Cleanup(chain.Close) return chain } diff --git a/pkg/consensus/payload_test.go b/pkg/consensus/payload_test.go index 1a0f243a3..2aad8dab5 100644 --- a/pkg/consensus/payload_test.go +++ b/pkg/consensus/payload_test.go @@ -262,7 +262,6 @@ func TestPayload_Sign(t *testing.T) { p := randomPayload(t, prepareRequestType) h := priv.PublicKey().GetScriptHash() bc := newTestChain(t, false) - defer bc.Close() require.Error(t, bc.VerifyWitness(h, p, &p.Witness, payloadGasLimit)) require.NoError(t, p.Sign(priv)) require.NoError(t, bc.VerifyWitness(h, p, &p.Witness, payloadGasLimit)) diff --git a/pkg/consensus/recovery_message_test.go b/pkg/consensus/recovery_message_test.go index d0ae10fef..d70f0bd97 100644 --- a/pkg/consensus/recovery_message_test.go +++ b/pkg/consensus/recovery_message_test.go @@ -23,7 +23,6 @@ func TestRecoveryMessageSetters(t *testing.T) { func testRecoveryMessageSetters(t *testing.T, enableStateRoot bool) { srv := newTestServiceWithState(t, enableStateRoot) - defer srv.Chain.Close() privs := make([]*privateKey, testchain.Size()) pubs := make([]crypto.PublicKey, testchain.Size()) for i := 0; i < testchain.Size(); i++ { diff --git a/pkg/core/blockchain_test.go b/pkg/core/blockchain_test.go index 5faf3a9d6..6c5ca3166 100644 --- a/pkg/core/blockchain_test.go +++ b/pkg/core/blockchain_test.go @@ -41,7 +41,6 @@ import ( func TestVerifyHeader(t *testing.T) { bc := newTestChain(t) - defer bc.Close() prev := bc.topBlock.Load().(*block.Block).Header() t.Run("Invalid", func(t *testing.T) { t.Run("Hash", func(t *testing.T) { @@ -68,7 +67,6 @@ func TestVerifyHeader(t *testing.T) { func TestAddHeaders(t *testing.T) { bc := newTestChain(t) - defer bc.Close() lastBlock := bc.topBlock.Load().(*block.Block) h1 := newBlock(bc.config, 1, lastBlock.Hash()).Header() h2 := newBlock(bc.config, 2, h1.Hash()).Header() @@ -132,7 +130,6 @@ func TestAddBlockStateRoot(t *testing.T) { bc := newTestChainWithCustomCfg(t, func(c *config.Config) { c.ProtocolConfiguration.StateRootInHeader = true }) - defer bc.Close() sr, err := bc.GetStateRoot(bc.BlockHeight()) require.NoError(t, err) @@ -159,7 +156,6 @@ func TestAddBlockStateRoot(t *testing.T) { func TestAddBadBlock(t *testing.T) { bc := newTestChain(t) - defer bc.Close() // It has ValidUntilBlock == 0, which is wrong tx := transaction.New(netmode.UnitTestNet, []byte{byte(opcode.PUSH1)}, 0) tx.Signers = []transaction.Signer{{ @@ -250,7 +246,6 @@ func (bc *Blockchain) newTestTx(h util.Uint160, script []byte) *transaction.Tran func TestVerifyTx(t *testing.T) { bc := newTestChain(t) - defer bc.Close() accs := make([]*wallet.Account, 5) for i := range accs { @@ -1063,7 +1058,6 @@ func TestVerifyTx(t *testing.T) { func TestVerifyHashAgainstScript(t *testing.T) { bc := newTestChain(t) - defer bc.Close() cs, csInvalid := getTestContractState(bc) ic := bc.newInteropContext(trigger.Verification, bc.dao, nil, nil) @@ -1137,7 +1131,6 @@ func TestVerifyHashAgainstScript(t *testing.T) { func TestIsTxStillRelevant(t *testing.T) { bc := newTestChain(t) - defer bc.Close() mp := bc.GetMemPool() newTx := func(t *testing.T) *transaction.Transaction { @@ -1233,7 +1226,6 @@ func TestMemPoolRemoval(t *testing.T) { const added = 16 const notAdded = 32 bc := newTestChain(t) - defer bc.Close() addedTxes := make([]*transaction.Transaction, added) notAddedTxes := make([]*transaction.Transaction, notAdded) for i := range addedTxes { @@ -1310,7 +1302,6 @@ func TestGetTransaction(t *testing.T) { func TestGetClaimable(t *testing.T) { bc := newTestChain(t) - defer bc.Close() _, err := bc.genBlocks(10) require.NoError(t, err) @@ -1327,7 +1318,8 @@ func TestClose(t *testing.T) { r := recover() assert.NotNil(t, r) }() - bc := newTestChain(t) + bc := initTestChain(t, nil, nil) + go bc.Run() _, err := bc.genBlocks(10) require.NoError(t, err) bc.Close() @@ -1350,7 +1342,6 @@ func TestSubscriptions(t *testing.T) { executionCh := make(chan *state.AppExecResult, chBufSize) bc := newTestChain(t) - defer bc.Close() bc.SubscribeForBlocks(blockCh) bc.SubscribeForTransactions(txCh) bc.SubscribeForNotifications(notificationCh) @@ -1473,7 +1464,6 @@ func testDumpAndRestore(t *testing.T, dumpF, restoreF func(c *config.Config)) { } bc := newTestChainWithCustomCfg(t, dumpF) - defer bc.Close() initBasicChain(t, bc) require.True(t, bc.BlockHeight() > 5) // ensure that test is valid @@ -1485,14 +1475,12 @@ func testDumpAndRestore(t *testing.T, dumpF, restoreF func(c *config.Config)) { buf := w.Bytes() t.Run("invalid start", func(t *testing.T) { bc2 := newTestChainWithCustomCfg(t, restoreF) - defer bc2.Close() r := io.NewBinReaderFromBuf(buf) require.Error(t, chaindump.Restore(bc2, r, 2, 1, nil)) }) t.Run("good", func(t *testing.T) { bc2 := newTestChainWithCustomCfg(t, restoreF) - defer bc2.Close() r := io.NewBinReaderFromBuf(buf) require.NoError(t, chaindump.Restore(bc2, r, 0, 2, nil)) @@ -1547,7 +1535,6 @@ func TestRemoveUntraceable(t *testing.T) { c.ProtocolConfiguration.MaxTraceableBlocks = 2 c.ProtocolConfiguration.RemoveUntraceableBlocks = true }) - defer bc.Close() tx1, err := testchain.NewTransferFromOwner(bc, bc.contracts.NEO.Hash, util.Uint160{}, 1, 0, bc.BlockHeight()+1) require.NoError(t, err) @@ -1576,7 +1563,6 @@ func TestRemoveUntraceable(t *testing.T) { func TestInvalidNotification(t *testing.T) { bc := newTestChain(t) - defer bc.Close() cs, _ := getTestContractState(bc) require.NoError(t, bc.contracts.Management.PutContractState(bc.dao, cs)) @@ -1591,7 +1577,6 @@ func TestInvalidNotification(t *testing.T) { // Test that deletion of non-existent doesn't result in error in tx or block addition. func TestMPTDeleteNoKey(t *testing.T) { bc := newTestChain(t) - defer bc.Close() cs, _ := getTestContractState(bc) require.NoError(t, bc.contracts.Management.PutContractState(bc.dao, cs)) diff --git a/pkg/core/helper_test.go b/pkg/core/helper_test.go index 733ecf969..40b64f9b5 100644 --- a/pkg/core/helper_test.go +++ b/pkg/core/helper_test.go @@ -59,6 +59,7 @@ func newTestChainWithCustomCfg(t *testing.T, f func(*config.Config)) *Blockchain func newTestChainWithCustomCfgAndStore(t *testing.T, st storage.Store, f func(*config.Config)) *Blockchain { chain := initTestChain(t, st, f) go chain.Run() + t.Cleanup(chain.Close) return chain } @@ -174,7 +175,6 @@ func TestBug1728(t *testing.T) { require.NoError(t, err) bc := newTestChain(t) - defer bc.Close() aer, err := invokeContractMethod(bc, 10000000000, bc.contracts.Management.Hash, "deploy", rawNef, rawManifest) @@ -242,13 +242,14 @@ func TestCreateBasicChain(t *testing.T) { const prefix = "../rpc/server/testdata/" bc := newTestChain(t) - defer bc.Close() initBasicChain(t, bc) if saveChain { outStream, err := os.Create(prefix + "testblocks.acc") require.NoError(t, err) - defer outStream.Close() + t.Cleanup(func() { + outStream.Close() + }) writer := io.NewBinWriterFromIO(outStream) writer.WriteU32LE(bc.BlockHeight()) diff --git a/pkg/core/interop_neo_test.go b/pkg/core/interop_neo_test.go index dc095cfe7..613dae823 100644 --- a/pkg/core/interop_neo_test.go +++ b/pkg/core/interop_neo_test.go @@ -36,7 +36,6 @@ import ( func TestStorageFind(t *testing.T) { v, contractState, context, chain := createVMAndContractState(t) - defer chain.Close() arr := []stackitem.Item{ stackitem.NewBigInteger(big.NewInt(42)), diff --git a/pkg/core/interop_system_test.go b/pkg/core/interop_system_test.go index 5d3ff67f7..ff9e936df 100644 --- a/pkg/core/interop_system_test.go +++ b/pkg/core/interop_system_test.go @@ -32,7 +32,6 @@ import ( func TestContractIsStandard(t *testing.T) { v, ic, chain := createVM(t) - defer chain.Close() t.Run("contract not stored", func(t *testing.T) { priv, err := keys.NewPrivateKey() @@ -91,8 +90,7 @@ func TestContractIsStandard(t *testing.T) { } func TestContractCreateAccount(t *testing.T) { - v, ic, chain := createVM(t) - defer chain.Close() + v, ic, _ := createVM(t) t.Run("Good", func(t *testing.T) { priv, err := keys.NewPrivateKey() require.NoError(t, err) @@ -112,8 +110,7 @@ func TestContractCreateAccount(t *testing.T) { } func TestContractCreateMultisigAccount(t *testing.T) { - v, ic, chain := createVM(t) - defer chain.Close() + v, ic, _ := createVM(t) t.Run("Good", func(t *testing.T) { m, n := 3, 5 pubs := make(keys.PublicKeys, n) @@ -159,8 +156,7 @@ func TestContractCreateMultisigAccount(t *testing.T) { } func TestRuntimeGasLeft(t *testing.T) { - v, ic, chain := createVM(t) - defer chain.Close() + v, ic, _ := createVM(t) v.GasLimit = 100 v.AddGas(58) @@ -169,8 +165,7 @@ func TestRuntimeGasLeft(t *testing.T) { } func TestRuntimeGetNotifications(t *testing.T) { - v, ic, chain := createVM(t) - defer chain.Close() + v, ic, _ := createVM(t) ic.Notifications = []state.NotificationEvent{ {ScriptHash: util.Uint160{1}, Name: "Event1", Item: stackitem.NewArray([]stackitem.Item{stackitem.NewByteArray([]byte{11})})}, @@ -211,8 +206,7 @@ func TestRuntimeGetNotifications(t *testing.T) { } func TestRuntimeGetInvocationCounter(t *testing.T) { - v, ic, chain := createVM(t) - defer chain.Close() + v, ic, _ := createVM(t) ic.VM.Invocations[hash.Hash160([]byte{2})] = 42 @@ -231,7 +225,6 @@ func TestRuntimeGetInvocationCounter(t *testing.T) { func TestStoragePut(t *testing.T) { _, cs, ic, bc := createVMAndContractState(t) - defer bc.Close() require.NoError(t, bc.contracts.Management.PutContractState(ic.DAO, cs)) @@ -304,7 +297,6 @@ func TestStoragePut(t *testing.T) { func TestStorageDelete(t *testing.T) { v, cs, ic, bc := createVMAndContractState(t) - defer bc.Close() require.NoError(t, bc.contracts.Management.PutContractState(ic.DAO, cs)) v.LoadScriptWithHash(cs.NEF.Script, cs.Hash, callflag.All) @@ -675,7 +667,6 @@ func loadScriptWithHashAndFlags(ic *interop.Context, script []byte, hash util.Ui func TestContractCall(t *testing.T) { _, ic, bc := createVM(t) - defer bc.Close() cs, currCs := getTestContractState(bc) require.NoError(t, bc.contracts.Management.PutContractState(ic.DAO, cs)) @@ -805,8 +796,7 @@ func TestContractCall(t *testing.T) { } func TestContractGetCallFlags(t *testing.T) { - v, ic, bc := createVM(t) - defer bc.Close() + v, ic, _ := createVM(t) v.LoadScriptWithHash([]byte{byte(opcode.RET)}, util.Uint160{1, 2, 3}, callflag.All) require.NoError(t, contractGetCallFlags(ic)) @@ -815,7 +805,6 @@ func TestContractGetCallFlags(t *testing.T) { func TestRuntimeCheckWitness(t *testing.T) { _, ic, bc := createVM(t) - defer bc.Close() script := []byte{byte(opcode.RET)} scriptHash := hash.Hash160(script) @@ -1008,7 +997,6 @@ func TestRuntimeCheckWitness(t *testing.T) { func TestLoadToken(t *testing.T) { bc := newTestChain(t) - defer bc.Close() cs, _ := getTestContractState(bc) require.NoError(t, bc.contracts.Management.PutContractState(bc.dao, cs)) diff --git a/pkg/core/interops_test.go b/pkg/core/interops_test.go index a8391ef9d..8f793bd5c 100644 --- a/pkg/core/interops_test.go +++ b/pkg/core/interops_test.go @@ -18,7 +18,6 @@ func testNonInterop(t *testing.T, value interface{}, f func(*interop.Context) er v := vm.New() v.Estack().PushVal(value) chain := newTestChain(t) - defer chain.Close() d := dao.NewSimple(storage.NewMemoryStore(), netmode.UnitTestNet, chain.config.StateRootInHeader) context := chain.newInteropContext(trigger.Application, d, nil, nil) context.VM = v diff --git a/pkg/core/mempool/subscriptions_test.go b/pkg/core/mempool/subscriptions_test.go index 5c123ec0e..77b18141e 100644 --- a/pkg/core/mempool/subscriptions_test.go +++ b/pkg/core/mempool/subscriptions_test.go @@ -29,7 +29,7 @@ func TestSubscriptions(t *testing.T) { subChan1 := make(chan Event, 3) subChan2 := make(chan Event, 3) mp.SubscribeForTransactions(subChan1) - defer mp.StopSubscriptions() + t.Cleanup(mp.StopSubscriptions) txs := make([]*transaction.Transaction, 4) for i := range txs { diff --git a/pkg/core/native_contract_test.go b/pkg/core/native_contract_test.go index 5a5e85493..eea43082d 100644 --- a/pkg/core/native_contract_test.go +++ b/pkg/core/native_contract_test.go @@ -166,7 +166,6 @@ func (tn *testNative) callOtherContractWithReturn(ic *interop.Context, args []st func TestNativeContract_Invoke(t *testing.T) { chain := newTestChain(t) - defer chain.Close() tn := newTestNative() chain.registerNative(tn) @@ -206,7 +205,6 @@ func TestNativeContract_Invoke(t *testing.T) { func TestNativeContract_InvokeInternal(t *testing.T) { chain := newTestChain(t) - defer chain.Close() tn := newTestNative() chain.registerNative(tn) @@ -251,7 +249,6 @@ func TestNativeContract_InvokeInternal(t *testing.T) { func TestNativeContract_InvokeOtherContract(t *testing.T) { chain := newTestChain(t) - defer chain.Close() tn := newTestNative() chain.registerNative(tn) diff --git a/pkg/core/native_designate_test.go b/pkg/core/native_designate_test.go index 94542ad20..aabce2a99 100644 --- a/pkg/core/native_designate_test.go +++ b/pkg/core/native_designate_test.go @@ -80,7 +80,6 @@ func (bc *Blockchain) getNodesByRole(t *testing.T, ok bool, r native.Role, index func TestDesignate_DesignateAsRoleTx(t *testing.T) { bc := newTestChain(t) - defer bc.Close() priv, err := keys.NewPrivateKey() require.NoError(t, err) @@ -104,7 +103,6 @@ func TestDesignate_DesignateAsRoleTx(t *testing.T) { func TestDesignate_DesignateAsRole(t *testing.T) { bc := newTestChain(t) - defer bc.Close() des := bc.contracts.Designate tx := transaction.New(netmode.UnitTestNet, []byte{}, 0) diff --git a/pkg/core/native_ledger_test.go b/pkg/core/native_ledger_test.go index cb25ccf14..71c600a7d 100644 --- a/pkg/core/native_ledger_test.go +++ b/pkg/core/native_ledger_test.go @@ -12,7 +12,6 @@ import ( func TestLedgerGetTransactionHeight(t *testing.T) { _, tx, _, chain := createVMAndTX(t) - defer chain.Close() ledger := chain.contracts.ByName(nativenames.Ledger).Metadata().Hash @@ -39,7 +38,6 @@ func TestLedgerGetTransactionHeight(t *testing.T) { func TestLedgerGetTransaction(t *testing.T) { _, tx, _, chain := createVMAndTX(t) - defer chain.Close() ledger := chain.contracts.ByName(nativenames.Ledger).Metadata().Hash t.Run("success", func(t *testing.T) { @@ -80,7 +78,6 @@ func TestLedgerGetTransaction(t *testing.T) { func TestLedgerGetTransactionFromBlock(t *testing.T) { chain := newTestChain(t) - defer chain.Close() ledger := chain.contracts.ByName(nativenames.Ledger).Metadata().Hash res, err := invokeContractMethod(chain, 100000000, ledger, "currentIndex") // adds a block @@ -132,7 +129,6 @@ func TestLedgerGetTransactionFromBlock(t *testing.T) { func TestLedgerGetBlock(t *testing.T) { chain := newTestChain(t) - defer chain.Close() ledger := chain.contracts.ByName(nativenames.Ledger).Metadata().Hash bhash := chain.GetHeaderHash(0) diff --git a/pkg/core/native_management_test.go b/pkg/core/native_management_test.go index 1445fd901..294a94154 100644 --- a/pkg/core/native_management_test.go +++ b/pkg/core/native_management_test.go @@ -30,7 +30,6 @@ import ( // leads to tx deserialization failure. func TestRestoreAfterDeploy(t *testing.T) { bc := newTestChain(t) - defer bc.Close() // nef.NewFile() cares about version a lot. config.Version = "0.90.0-test" @@ -61,7 +60,6 @@ func TestStartFromHeight(t *testing.T) { bc := newTestChainWithCustomCfgAndStore(t, st, nil) cs1, _ := getTestContractState(bc) func() { - defer bc.Close() require.NoError(t, bc.contracts.Management.PutContractState(bc.dao, cs1)) checkContractState(t, bc, cs1.Hash, cs1) _, err := bc.dao.Store.Persist() @@ -74,7 +72,6 @@ func TestStartFromHeight(t *testing.T) { func TestContractDeployAndUpdateWithParameter(t *testing.T) { bc := newTestChain(t) - defer bc.Close() // nef.NewFile() cares about version a lot. config.Version = "0.90.0-test" @@ -125,7 +122,6 @@ func TestContractDeployAndUpdateWithParameter(t *testing.T) { func TestContractDeploy(t *testing.T) { bc := newTestChain(t) - defer bc.Close() // nef.NewFile() cares about version a lot. config.Version = "0.90.0-test" @@ -277,7 +273,6 @@ func TestContractDeploy(t *testing.T) { r := io.NewBinReaderFromBuf(w.Bytes()) bc2 := newTestChain(t) - defer bc2.Close() require.NoError(t, chaindump.Restore(bc2, r, 0, bc.BlockHeight()+1, nil)) require.NoError(t, r.Err) @@ -364,7 +359,6 @@ func checkContractState(t *testing.T, bc *Blockchain, h util.Uint160, cs *state. func TestContractUpdate(t *testing.T) { bc := newTestChain(t) - defer bc.Close() // nef.NewFile() cares about version a lot. config.Version = "0.90.0-test" @@ -526,7 +520,6 @@ func TestContractUpdate(t *testing.T) { func TestGetContract(t *testing.T) { bc := newTestChain(t) - defer bc.Close() mgmtHash := bc.ManagementContractHash() cs1, _ := getTestContractState(bc) @@ -553,7 +546,6 @@ func TestGetContract(t *testing.T) { func TestContractDestroy(t *testing.T) { bc := newTestChain(t) - defer bc.Close() mgmtHash := bc.ManagementContractHash() cs1, _ := getTestContractState(bc) @@ -607,7 +599,6 @@ func compareContractStates(t *testing.T, expected *state.Contract, actual stacki func TestMinimumDeploymentFee(t *testing.T) { chain := newTestChain(t) - defer chain.Close() t.Run("get, internal method", func(t *testing.T) { n := chain.contracts.Management.GetMinimumDeploymentFee(chain.dao) diff --git a/pkg/core/native_name_service_test.go b/pkg/core/native_name_service_test.go index 905d22c3c..dc519aaa2 100644 --- a/pkg/core/native_name_service_test.go +++ b/pkg/core/native_name_service_test.go @@ -21,7 +21,6 @@ import ( func TestNameService_Price(t *testing.T) { bc := newTestChain(t) - defer bc.Close() testGetSet(t, bc, bc.contracts.NameService.Hash, "Price", native.DefaultDomainPrice, 1, 10000_00000000) @@ -29,7 +28,6 @@ func TestNameService_Price(t *testing.T) { func TestNonfungible(t *testing.T) { bc := newTestChain(t) - defer bc.Close() acc := newAccountWithGAS(t, bc) testNameServiceInvokeAux(t, bc, defaultNameServiceSysfee, acc, "symbol", "NNS") @@ -39,7 +37,6 @@ func TestNonfungible(t *testing.T) { func TestAddRoot(t *testing.T) { bc := newTestChain(t) - defer bc.Close() transferFundsToCommittee(t, bc) nsHash := bc.contracts.NameService.Hash @@ -61,7 +58,6 @@ func TestAddRoot(t *testing.T) { func TestExpiration(t *testing.T) { bc := newTestChain(t) - defer bc.Close() transferFundsToCommittee(t, bc) acc := newAccountWithGAS(t, bc) @@ -130,7 +126,6 @@ const secondsInYear = 365 * 24 * 3600 func TestRegisterAndRenew(t *testing.T) { bc := newTestChain(t) - defer bc.Close() transferFundsToCommittee(t, bc) @@ -181,7 +176,6 @@ func TestRegisterAndRenew(t *testing.T) { func TestSetGetRecord(t *testing.T) { bc := newTestChain(t) - defer bc.Close() transferFundsToCommittee(t, bc) acc := newAccountWithGAS(t, bc) @@ -222,7 +216,6 @@ func TestSetGetRecord(t *testing.T) { func TestSetAdmin(t *testing.T) { bc := newTestChain(t) - defer bc.Close() transferFundsToCommittee(t, bc) owner := newAccountWithGAS(t, bc) @@ -265,7 +258,6 @@ func TestSetAdmin(t *testing.T) { func TestTransfer(t *testing.T) { bc := newTestChain(t) - defer bc.Close() transferFundsToCommittee(t, bc) from := newAccountWithGAS(t, bc) @@ -299,7 +291,6 @@ func TestTransfer(t *testing.T) { func TestTokensOf(t *testing.T) { bc := newTestChain(t) - defer bc.Close() transferFundsToCommittee(t, bc) acc1 := newAccountWithGAS(t, bc) @@ -356,7 +347,6 @@ func testTokensOf(t *testing.T, bc *Blockchain, signer *wallet.Account, result [ func TestResolve(t *testing.T) { bc := newTestChain(t) - defer bc.Close() transferFundsToCommittee(t, bc) acc := newAccountWithGAS(t, bc) diff --git a/pkg/core/native_neo_test.go b/pkg/core/native_neo_test.go index ea3d45410..02508d471 100644 --- a/pkg/core/native_neo_test.go +++ b/pkg/core/native_neo_test.go @@ -38,7 +38,6 @@ func checkTxHalt(t *testing.T, bc *Blockchain, h util.Uint256) { func TestNEO_Vote(t *testing.T) { bc := newTestChain(t) - defer bc.Close() neo := bc.contracts.NEO tx := transaction.New(netmode.UnitTestNet, []byte{byte(opcode.PUSH1)}, 0) @@ -195,7 +194,6 @@ func TestNEO_Vote(t *testing.T) { func TestNEO_SetGasPerBlock(t *testing.T) { bc := newTestChain(t) - defer bc.Close() testGetSet(t, bc, bc.contracts.NEO.Hash, "GasPerBlock", 5*native.GASFactor, 0, 10*native.GASFactor) @@ -203,7 +201,6 @@ func TestNEO_SetGasPerBlock(t *testing.T) { func TestNEO_CalculateBonus(t *testing.T) { bc := newTestChain(t) - defer bc.Close() neo := bc.contracts.NEO tx := transaction.New(netmode.UnitTestNet, []byte{}, 0) @@ -233,7 +230,6 @@ func TestNEO_CalculateBonus(t *testing.T) { func TestNEO_CommitteeBountyOnPersist(t *testing.T) { bc := newTestChain(t) - defer bc.Close() hs := make([]util.Uint160, testchain.CommitteeSize()) for i := range hs { @@ -256,7 +252,6 @@ func TestNEO_CommitteeBountyOnPersist(t *testing.T) { func TestNEO_TransferOnPayment(t *testing.T) { bc := newTestChain(t) - defer bc.Close() cs, _ := getTestContractState(bc) require.NoError(t, bc.contracts.Management.PutContractState(bc.dao, cs)) diff --git a/pkg/core/native_notary_test.go b/pkg/core/native_notary_test.go index d72c305ce..85ab22a8d 100644 --- a/pkg/core/native_notary_test.go +++ b/pkg/core/native_notary_test.go @@ -22,7 +22,6 @@ import ( func TestNotaryContractPipeline(t *testing.T) { chain := newTestChain(t) - defer chain.Close() notaryHash := chain.contracts.Notary.Hash gasHash := chain.contracts.GAS.Hash @@ -249,7 +248,6 @@ func TestNotaryContractPipeline(t *testing.T) { func TestNotaryNodesReward(t *testing.T) { checkReward := func(nKeys int, nNotaryNodes int, spendFullDeposit bool) { chain := newTestChain(t) - defer chain.Close() notaryHash := chain.contracts.Notary.Hash gasHash := chain.contracts.GAS.Hash signer := testchain.MultisigScriptHash() @@ -326,7 +324,6 @@ func TestNotaryNodesReward(t *testing.T) { func TestMaxNotValidBeforeDelta(t *testing.T) { chain := newTestChain(t) - defer chain.Close() testGetSet(t, chain, chain.contracts.Notary.Hash, "MaxNotValidBeforeDelta", 140, int64(chain.GetConfig().ValidatorsCount), transaction.MaxValidUntilBlockIncrement/2) diff --git a/pkg/core/native_oracle_test.go b/pkg/core/native_oracle_test.go index 38671ad52..d1564451d 100644 --- a/pkg/core/native_oracle_test.go +++ b/pkg/core/native_oracle_test.go @@ -115,7 +115,6 @@ func putOracleRequest(t *testing.T, h util.Uint160, bc *Blockchain, func TestOracle_Request(t *testing.T) { bc := newTestChain(t) - defer bc.Close() orc := bc.contracts.Oracle cs := getOracleContractState(orc.Hash) diff --git a/pkg/core/native_policy_test.go b/pkg/core/native_policy_test.go index 3ef5c4828..c00d6583c 100644 --- a/pkg/core/native_policy_test.go +++ b/pkg/core/native_policy_test.go @@ -78,7 +78,6 @@ func testGetSet(t *testing.T, chain *Blockchain, hash util.Uint160, name string, func TestMaxTransactionsPerBlock(t *testing.T) { chain := newTestChain(t) - defer chain.Close() t.Run("get, internal method", func(t *testing.T) { n := chain.contracts.Policy.GetMaxTransactionsPerBlockInternal(chain.dao) @@ -90,7 +89,6 @@ func TestMaxTransactionsPerBlock(t *testing.T) { func TestMaxBlockSize(t *testing.T) { chain := newTestChain(t) - defer chain.Close() t.Run("get, internal method", func(t *testing.T) { n := chain.contracts.Policy.GetMaxBlockSizeInternal(chain.dao) @@ -102,7 +100,6 @@ func TestMaxBlockSize(t *testing.T) { func TestFeePerByte(t *testing.T) { chain := newTestChain(t) - defer chain.Close() t.Run("get, internal method", func(t *testing.T) { n := chain.contracts.Policy.GetFeePerByteInternal(chain.dao) @@ -114,7 +111,6 @@ func TestFeePerByte(t *testing.T) { func TestExecFeeFactor(t *testing.T) { chain := newTestChain(t) - defer chain.Close() t.Run("get, internal method", func(t *testing.T) { n := chain.contracts.Policy.GetExecFeeFactorInternal(chain.dao) @@ -126,7 +122,6 @@ func TestExecFeeFactor(t *testing.T) { func TestBlockSystemFee(t *testing.T) { chain := newTestChain(t) - defer chain.Close() t.Run("get, internal method", func(t *testing.T) { n := chain.contracts.Policy.GetMaxBlockSystemFeeInternal(chain.dao) @@ -138,7 +133,6 @@ func TestBlockSystemFee(t *testing.T) { func TestStoragePrice(t *testing.T) { chain := newTestChain(t) - defer chain.Close() t.Run("get, internal method", func(t *testing.T) { n := chain.contracts.Policy.GetStoragePriceInternal(chain.dao) @@ -150,7 +144,6 @@ func TestStoragePrice(t *testing.T) { func TestBlockedAccounts(t *testing.T) { chain := newTestChain(t) - defer chain.Close() account := util.Uint160{1, 2, 3} policyHash := chain.contracts.Policy.Metadata().Hash diff --git a/pkg/core/notary_test.go b/pkg/core/notary_test.go index d3ba93ec8..42efcdcdf 100644 --- a/pkg/core/notary_test.go +++ b/pkg/core/notary_test.go @@ -59,7 +59,6 @@ func getTestNotary(t *testing.T, bc *Blockchain, walletPath, pass string, onTx f func TestNotary(t *testing.T) { bc := newTestChain(t) - defer bc.Close() var ( nonce uint32 nvbDiffFallback uint32 = 20 @@ -641,10 +640,10 @@ func TestNotary(t *testing.T) { // Subscriptions test mp1.RunSubscriptions() go ntr1.Run() - defer func() { + t.Cleanup(func() { ntr1.Stop() mp1.StopSubscriptions() - }() + }) finalizeWithError = false requester1, _ := wallet.NewAccount() requester2, _ := wallet.NewAccount() diff --git a/pkg/core/oracle_test.go b/pkg/core/oracle_test.go index efdf749f1..d7afc1f06 100644 --- a/pkg/core/oracle_test.go +++ b/pkg/core/oracle_test.go @@ -76,7 +76,6 @@ func getTestOracle(t *testing.T, bc *Blockchain, walletPath, pass string) ( // https://github.com/neo-project/neo-modules/blob/master/tests/Neo.Plugins.OracleService.Tests/UT_OracleService.cs#L61 func TestCreateResponseTx(t *testing.T) { bc := newTestChain(t) - defer bc.Close() require.Equal(t, int64(30), bc.GetBaseExecFee()) require.Equal(t, int64(1000), bc.FeePerByte()) @@ -116,7 +115,6 @@ func TestOracle_InvalidWallet(t *testing.T) { func TestOracle(t *testing.T) { bc := newTestChain(t) - defer bc.Close() oracleCtr := bc.contracts.Oracle acc1, orc1, m1, ch1 := getTestOracle(t, bc, "./testdata/oracle1.json", "one") @@ -272,9 +270,8 @@ func TestOracleFull(t *testing.T) { require.NoError(t, bc.contracts.Management.PutContractState(bc.dao, cs)) go bc.Run() - defer bc.Close() go orc.Run() - defer orc.Shutdown() + t.Cleanup(orc.Shutdown) bc.setNodesByRole(t, true, native.RoleOracle, keys.PublicKeys{acc.PrivateKey().PublicKey()}) putOracleRequest(t, cs.Hash, bc, "http://get.1234", new(string), "handle", []byte{}, 10_000_000) diff --git a/pkg/core/storage/boltdb_store_test.go b/pkg/core/storage/boltdb_store_test.go index e9759b299..e36df6fda 100644 --- a/pkg/core/storage/boltdb_store_test.go +++ b/pkg/core/storage/boltdb_store_test.go @@ -11,10 +11,10 @@ import ( func newBoltStoreForTesting(t *testing.T) Store { testFileName := "test_bolt_db" file, err := ioutil.TempFile("", testFileName) - defer func() { + t.Cleanup(func() { err := os.RemoveAll(testFileName) require.NoError(t, err) - }() + }) require.NoError(t, err) require.NoError(t, file.Close()) boltDBStore, err := NewBoltDBStore(BoltDBOptions{FilePath: testFileName}) diff --git a/pkg/io/fileWriter_test.go b/pkg/io/fileWriter_test.go index a2c8f7e2d..d93296b96 100644 --- a/pkg/io/fileWriter_test.go +++ b/pkg/io/fileWriter_test.go @@ -15,7 +15,9 @@ func TestMakeDirForFile_HappyPath(t *testing.T) { filePath := tempDir + "/testDir/testFile.test" err = MakeDirForFile(filePath, "test") - defer removeDir(t, tempDir) + t.Cleanup(func() { + removeDir(t, tempDir) + }) require.NoError(t, err) _, errChDir := os.Create(filePath) @@ -34,6 +36,8 @@ func TestMakeDirForFile_Negative(t *testing.T) { filePath := file.Name() + "/error" dir := path.Dir(filePath) err = MakeDirForFile(filePath, "test") - defer removeDir(t, dir) + t.Cleanup(func() { + removeDir(t, dir) + }) require.Errorf(t, err, "could not create dir for test: mkdir %s : not a directory", filePath) } diff --git a/pkg/network/helper_test.go b/pkg/network/helper_test.go index e084a3252..5787655df 100644 --- a/pkg/network/helper_test.go +++ b/pkg/network/helper_test.go @@ -192,5 +192,6 @@ func newTestServer(t *testing.T, serverConfig ServerConfig) *Server { s, err := newServerFromConstructors(serverConfig, fakechain.NewFakeChain(), zaptest.NewLogger(t), newFakeTransp, newFakeConsensus, newTestDiscovery) require.NoError(t, err) + t.Cleanup(s.discovery.Close) return s } diff --git a/pkg/network/server_test.go b/pkg/network/server_test.go index 3e3e79292..ef066d7f9 100644 --- a/pkg/network/server_test.go +++ b/pkg/network/server_test.go @@ -53,7 +53,6 @@ func TestNewServer(t *testing.T) { t.Run("set defaults", func(t *testing.T) { s = newTestServer(t, ServerConfig{MinPeers: -1}) - defer s.discovery.Close() require.True(t, s.ID() != 0) require.Equal(t, defaultMinPeers, s.ServerConfig.MinPeers) @@ -67,7 +66,6 @@ func TestNewServer(t *testing.T) { AttemptConnPeers: 3, } s = newTestServer(t, cfg) - defer s.discovery.Close() require.True(t, s.ID() != 0) require.Equal(t, 1, s.ServerConfig.MinPeers) @@ -140,10 +138,10 @@ func TestServerRegisterPeer(t *testing.T) { } ch := startWithChannel(s) - defer func() { + t.Cleanup(func() { s.Shutdown() <-ch - }() + }) s.register <- ps[0] require.Eventually(t, func() bool { return 1 == s.PeerCount() }, time.Second, time.Millisecond*10) @@ -317,11 +315,11 @@ func TestServerNotSendsVerack(t *testing.T) { s.run() close(finished) }() - defer func() { + t.Cleanup(func() { // close via quit as server was started via `run()`, not `Start()` close(s.quit) <-finished - }() + }) na, _ := net.ResolveTCPAddr("tcp", "0.0.0.0:3000") p.netaddr = *na @@ -379,18 +377,18 @@ func (s *Server) testHandleMessage(t *testing.T, p Peer, cmd CommandType, pl pay return s } -func startTestServer(t *testing.T) (*Server, func()) { +func startTestServer(t *testing.T) *Server { s := newTestServer(t, ServerConfig{Port: 0, UserAgent: "/test/"}) ch := startWithChannel(s) - return s, func() { + t.Cleanup(func() { s.Shutdown() <-ch - } + }) + return s } func TestBlock(t *testing.T) { - s, shutdown := startTestServer(t) - defer shutdown() + s := startTestServer(t) atomic2.StoreUint32(&s.chain.(*fakechain.FakeChain).Blockheight, 12344) require.Equal(t, uint32(12344), s.chain.BlockHeight()) @@ -402,8 +400,7 @@ func TestBlock(t *testing.T) { } func TestConsensus(t *testing.T) { - s, shutdown := startTestServer(t) - defer shutdown() + s := startTestServer(t) atomic2.StoreUint32(&s.chain.(*fakechain.FakeChain).Blockheight, 4) p := newLocalPeer(t, s) @@ -450,8 +447,7 @@ func TestConsensus(t *testing.T) { } func TestTransaction(t *testing.T) { - s, shutdown := startTestServer(t) - defer shutdown() + s := startTestServer(t) t.Run("good", func(t *testing.T) { tx := newDummyTx() @@ -503,8 +499,7 @@ func (s *Server) testHandleGetData(t *testing.T, invType payload.InventoryType, } func TestGetData(t *testing.T) { - s, shutdown := startTestServer(t) - defer shutdown() + s := startTestServer(t) s.chain.(*fakechain.FakeChain).UtilityTokenBalance = big.NewInt(1000000) t.Run("block", func(t *testing.T) { @@ -563,8 +558,8 @@ func TestGetData(t *testing.T) { }) } -func initGetBlocksTest(t *testing.T) (*Server, func(), []*block.Block) { - s, shutdown := startTestServer(t) +func initGetBlocksTest(t *testing.T) (*Server, []*block.Block) { + s := startTestServer(t) var blocks []*block.Block for i := uint32(12); i <= 15; i++ { @@ -572,12 +567,11 @@ func initGetBlocksTest(t *testing.T) (*Server, func(), []*block.Block) { s.chain.(*fakechain.FakeChain).PutBlock(b) blocks = append(blocks, b) } - return s, shutdown, blocks + return s, blocks } func TestGetBlocks(t *testing.T) { - s, shutdown, blocks := initGetBlocksTest(t) - defer shutdown() + s, blocks := initGetBlocksTest(t) expected := make([]util.Uint256, len(blocks)) for i := range blocks { @@ -608,8 +602,7 @@ func TestGetBlocks(t *testing.T) { } func TestGetBlockByIndex(t *testing.T) { - s, shutdown, blocks := initGetBlocksTest(t) - defer shutdown() + s, blocks := initGetBlocksTest(t) var expected []*block.Block var actual []*block.Block @@ -643,8 +636,7 @@ func TestGetBlockByIndex(t *testing.T) { } func TestGetHeaders(t *testing.T) { - s, shutdown, blocks := initGetBlocksTest(t) - defer shutdown() + s, blocks := initGetBlocksTest(t) expected := make([]*block.Header, len(blocks)) for i := range blocks { @@ -683,8 +675,7 @@ func TestGetHeaders(t *testing.T) { } func TestInv(t *testing.T) { - s, shutdown := startTestServer(t) - defer shutdown() + s := startTestServer(t) s.chain.(*fakechain.FakeChain).UtilityTokenBalance = big.NewInt(10000000) var actual []util.Uint256 @@ -749,8 +740,7 @@ func TestInv(t *testing.T) { } func TestRequestTx(t *testing.T) { - s, shutdown := startTestServer(t) - defer shutdown() + s := startTestServer(t) var actual []util.Uint256 p := newLocalPeer(t, s) @@ -795,8 +785,7 @@ func TestRequestTx(t *testing.T) { } func TestAddrs(t *testing.T) { - s, shutdown := startTestServer(t) - defer shutdown() + s := startTestServer(t) ips := make([][16]byte, 4) copy(ips[0][:], net.IPv4(1, 2, 3, 4)) @@ -855,8 +844,7 @@ func (f feerStub) P2PSigExtensionsEnabled() bool { return false } func (f feerStub) GetBaseExecFee() int64 { return interop.DefaultBaseExecFee } func TestMemPool(t *testing.T) { - s, shutdown := startTestServer(t) - defer shutdown() + s := startTestServer(t) var actual []util.Uint256 p := newLocalPeer(t, s) diff --git a/pkg/rpc/client/rpc_test.go b/pkg/rpc/client/rpc_test.go index 2e9674420..9c8f46565 100644 --- a/pkg/rpc/client/rpc_test.go +++ b/pkg/rpc/client/rpc_test.go @@ -1439,7 +1439,6 @@ func testRPCClient(t *testing.T, newClient func(context.Context, string, Options for _, testCase := range testBatch { t.Run(testCase.name, func(t *testing.T) { srv := initTestServer(t, testCase.serverResponse) - defer srv.Close() endpoint := srv.URL opts := Options{} @@ -1467,7 +1466,6 @@ func testRPCClient(t *testing.T, newClient func(context.Context, string, Options } for serverResponse, testBatch := range rpcClientErrorCases { srv := initTestServer(t, serverResponse) - defer srv.Close() endpoint := srv.URL opts := Options{} @@ -1524,6 +1522,8 @@ func initTestServer(t *testing.T, resp string) *httptest.Server { requestHandler(t, r.In, w, resp) })) + t.Cleanup(srv.Close) + return srv } @@ -1582,7 +1582,7 @@ func TestCalculateValidUntilBlock(t *testing.T) { } requestHandler(t, r.In, w, response) })) - defer srv.Close() + t.Cleanup(srv.Close) endpoint := srv.URL opts := Options{} @@ -1616,7 +1616,7 @@ func TestGetNetwork(t *testing.T) { // request handler already have `getversion` response wrapper requestHandler(t, r.In, w, "") })) - defer srv.Close() + t.Cleanup(srv.Close) endpoint := srv.URL opts := Options{} @@ -1648,7 +1648,7 @@ func TestUninitedClient(t *testing.T) { // request handler already have `getversion` response wrapper requestHandler(t, r.In, w, "") })) - defer srv.Close() + t.Cleanup(srv.Close) endpoint := srv.URL opts := Options{} diff --git a/pkg/rpc/client/wsclient_test.go b/pkg/rpc/client/wsclient_test.go index c198842d0..81c1291aa 100644 --- a/pkg/rpc/client/wsclient_test.go +++ b/pkg/rpc/client/wsclient_test.go @@ -17,7 +17,6 @@ import ( func TestWSClientClose(t *testing.T) { srv := initTestServer(t, "") - defer srv.Close() wsc, err := NewWS(context.TODO(), httpURLtoWS(srv.URL), Options{}) require.NoError(t, err) wsc.Close() @@ -42,7 +41,6 @@ func TestWSClientSubscription(t *testing.T) { for name, f := range cases { t.Run(name, func(t *testing.T) { srv := initTestServer(t, `{"jsonrpc": "2.0", "id": 1, "result": "55aaff00"}`) - defer srv.Close() wsc, err := NewWS(context.TODO(), httpURLtoWS(srv.URL), Options{}) require.NoError(t, err) require.NoError(t, wsc.Init()) @@ -56,7 +54,6 @@ func TestWSClientSubscription(t *testing.T) { for name, f := range cases { t.Run(name, func(t *testing.T) { srv := initTestServer(t, `{"jsonrpc": "2.0", "id": 1, "error":{"code":-32602,"message":"Invalid Params"}}`) - defer srv.Close() wsc, err := NewWS(context.TODO(), httpURLtoWS(srv.URL), Options{}) require.NoError(t, err) require.NoError(t, wsc.Init()) @@ -106,7 +103,6 @@ func TestWSClientUnsubscription(t *testing.T) { for name, rc := range cases { t.Run(name, func(t *testing.T) { srv := initTestServer(t, rc.response) - defer srv.Close() wsc, err := NewWS(context.TODO(), httpURLtoWS(srv.URL), Options{}) require.NoError(t, err) require.NoError(t, wsc.Init()) @@ -165,7 +161,6 @@ func TestWSClientEvents(t *testing.T) { func TestWSExecutionVMStateCheck(t *testing.T) { // Will answer successfully if request slips through. srv := initTestServer(t, `{"jsonrpc": "2.0", "id": 1, "result": "55aaff00"}`) - defer srv.Close() wsc, err := NewWS(context.TODO(), httpURLtoWS(srv.URL), Options{}) require.NoError(t, err) require.NoError(t, wsc.Init()) @@ -330,7 +325,6 @@ func TestWSFilteredSubscriptions(t *testing.T) { ws.Close() } })) - defer srv.Close() wsc, err := NewWS(context.TODO(), httpURLtoWS(srv.URL), Options{}) require.NoError(t, err) wsc.network = netmode.UnitTestNet @@ -342,7 +336,6 @@ func TestWSFilteredSubscriptions(t *testing.T) { func TestNewWS(t *testing.T) { srv := initTestServer(t, "") - defer srv.Close() t.Run("good", func(t *testing.T) { c, err := NewWS(context.TODO(), httpURLtoWS(srv.URL), Options{}) diff --git a/pkg/vm/cli/cli_test.go b/pkg/vm/cli/cli_test.go index 0e89b4e60..b32ce15a0 100644 --- a/pkg/vm/cli/cli_test.go +++ b/pkg/vm/cli/cli_test.go @@ -164,7 +164,9 @@ func TestLoad(t *testing.T) { }` tmpDir := path.Join(os.TempDir(), "vmcliloadtest") require.NoError(t, os.Mkdir(tmpDir, os.ModePerm)) - defer os.RemoveAll(tmpDir) + t.Cleanup(func() { + os.RemoveAll(tmpDir) + }) t.Run("loadgo", func(t *testing.T) { filename := path.Join(tmpDir, "vmtestcontract.go") @@ -244,7 +246,9 @@ func TestRunWithDifferentArguments(t *testing.T) { filename := path.Join(os.TempDir(), "run_vmtestcontract.go") require.NoError(t, ioutil.WriteFile(filename, []byte(src), os.ModePerm)) - defer os.Remove(filename) + t.Cleanup(func() { + os.Remove(filename) + }) e := newTestVMCLI(t) e.runProg(t, diff --git a/pkg/wallet/wallet_test.go b/pkg/wallet/wallet_test.go index 1ce1302ce..1811062df 100644 --- a/pkg/wallet/wallet_test.go +++ b/pkg/wallet/wallet_test.go @@ -89,7 +89,9 @@ func TestSave(t *testing.T) { Default: false, }) - defer removeWallet(t, file.Name()) + t.Cleanup(func() { + removeWallet(t, file.Name()) + }) errForSave := wallet.Save() require.NoError(t, errForSave) @@ -129,7 +131,9 @@ func checkWalletConstructor(t *testing.T) *Wallet { file, err := ioutil.TempFile("", walletTemplate) require.NoError(t, err) wallet, err := NewWallet(file.Name()) - defer removeWallet(t, file.Name()) + t.Cleanup(func() { + removeWallet(t, file.Name()) + }) require.NoError(t, err) return wallet } From ffb2d78bbd312f9918ea5b493758cd188fa8221e Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Mon, 1 Mar 2021 15:08:49 +0300 Subject: [PATCH 4/4] core: update Notary test Let's allow to wait a bit more. Hardware resources of the Github workflow tests aren't so fluent. --- pkg/core/notary_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/core/notary_test.go b/pkg/core/notary_test.go index 42efcdcdf..2f18333b6 100644 --- a/pkg/core/notary_test.go +++ b/pkg/core/notary_test.go @@ -661,6 +661,6 @@ func TestNotary(t *testing.T) { mtx.RLock() defer mtx.RUnlock() return completedTxes[requests[0].MainTransaction.Hash()] != nil - }, time.Second, time.Millisecond) + }, 2*time.Second, 10*time.Millisecond) checkFallbackTxs(t, requests, false) }