From 82052d2791387ed24596d7f3eab44173057055ee Mon Sep 17 00:00:00 2001 From: Matheus Degiovani Date: Tue, 29 Nov 2022 11:31:44 -0300 Subject: [PATCH] rpctests: Switch to dcrtest/dcrdtest package This switches the tests in the rpctests package to use the recently introduced dcrtest/dcrdtest package. Given the rpctests package is inside the main dcrd module, the main dcrd module version is built for use in tests by default. --- go.mod | 3 +- go.sum | 3 ++ .../integration/rpctests/rpcserver_test.go | 40 ++++--------------- .../integration/rpctests/treasury_test.go | 12 +++--- 4 files changed, 18 insertions(+), 40 deletions(-) diff --git a/go.mod b/go.mod index e442f7116f..47aa3c2775 100644 --- a/go.mod +++ b/go.mod @@ -19,7 +19,7 @@ require ( github.com/decred/dcrd/crypto/ripemd160 v1.0.1 github.com/decred/dcrd/database/v3 v3.0.0 github.com/decred/dcrd/dcrec v1.0.0 - github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 + github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0 github.com/decred/dcrd/dcrjson/v4 v4.0.0 github.com/decred/dcrd/dcrutil/v4 v4.0.0 github.com/decred/dcrd/gcs/v4 v4.0.0 @@ -31,6 +31,7 @@ require ( github.com/decred/dcrd/rpcclient/v8 v8.0.0 github.com/decred/dcrd/txscript/v4 v4.0.0 github.com/decred/dcrd/wire v1.5.0 + github.com/decred/dcrtest/dcrdtest v0.0.0-20221125134442-85f922c0880e github.com/decred/go-socks v1.1.0 github.com/decred/slog v1.2.0 github.com/gorilla/websocket v1.5.0 diff --git a/go.sum b/go.sum index 939825e01a..d581d4db8c 100644 --- a/go.sum +++ b/go.sum @@ -7,8 +7,11 @@ github.com/dchest/siphash v1.2.2/go.mod h1:q+IRvb2gOSrUnYoPqHiyHXS0FOBBOdl6tONBl github.com/decred/base58 v1.0.3/go.mod h1:pXP9cXCfM2sFLb2viz2FNIdeMWmZDBKG3ZBYbiSM78E= github.com/decred/base58 v1.0.4 h1:QJC6B0E0rXOPA8U/kw2rP+qiRJsUaE2Er+pYb3siUeA= github.com/decred/base58 v1.0.4/go.mod h1:jJswKPEdvpFpvf7dsDvFZyLT22xZ9lWqEByX38oGd9E= +github.com/decred/dcrd v1.2.1-0.20221123192607-fc017ce3bb3b/go.mod h1:9MS9DQkbDZGj2yT6znvK4srKr3ZlAm4UTsJTFHgW7Ng= github.com/decred/dcrd/dcrec/edwards/v2 v2.0.2 h1:bX7rtGTMBDJxujZ29GNqtn7YCAdINjHKnA6J6tBBv6s= github.com/decred/dcrd/dcrec/edwards/v2 v2.0.2/go.mod h1:d0H8xGMWbiIQP7gN3v2rByWUcuZPm9YsgmnfoxgbINc= +github.com/decred/dcrtest/dcrdtest v0.0.0-20221125134442-85f922c0880e h1:QKtZ4/aNVRzMbW/dllhAiLJhXxswhaGGZgnu5Z9YtiM= +github.com/decred/dcrtest/dcrdtest v0.0.0-20221125134442-85f922c0880e/go.mod h1:9QDaNncc0bWuMeZcbbPhLqUIeE+IBYLAsHio8+2++JI= github.com/decred/go-socks v1.1.0 h1:dnENcc0KIqQo3HSXdgboXAHgqsCIutkqq6ntQjYtm2U= github.com/decred/go-socks v1.1.0/go.mod h1:sDhHqkZH0X4JjSa02oYOGhcGHYp12FsY1jQ/meV8md0= github.com/decred/slog v1.2.0 h1:soHAxV52B54Di3WtKLfPum9OFfWqwtf/ygf9njdfnPM= diff --git a/internal/integration/rpctests/rpcserver_test.go b/internal/integration/rpctests/rpcserver_test.go index 8fec5e1f78..6338e708bc 100644 --- a/internal/integration/rpctests/rpcserver_test.go +++ b/internal/integration/rpctests/rpcserver_test.go @@ -13,15 +13,13 @@ package rpctests import ( "bytes" "context" - "fmt" - "runtime/debug" "testing" "github.com/decred/dcrd/chaincfg/v3" - "github.com/decred/dcrd/rpctest" + "github.com/decred/dcrtest/dcrdtest" ) -func testGetBestBlock(ctx context.Context, r *rpctest.Harness, t *testing.T) { +func testGetBestBlock(ctx context.Context, r *dcrdtest.Harness, t *testing.T) { _, prevbestHeight, err := r.Node.GetBestBlock(ctx) if err != nil { t.Fatalf("Call to `getbestblock` failed: %v", err) @@ -51,7 +49,7 @@ func testGetBestBlock(ctx context.Context, r *rpctest.Harness, t *testing.T) { } } -func testGetBlockCount(ctx context.Context, r *rpctest.Harness, t *testing.T) { +func testGetBlockCount(ctx context.Context, r *dcrdtest.Harness, t *testing.T) { // Save the current count. currentCount, err := r.Node.GetBlockCount(ctx) if err != nil { @@ -73,7 +71,7 @@ func testGetBlockCount(ctx context.Context, r *rpctest.Harness, t *testing.T) { } } -func testGetBlockHash(ctx context.Context, r *rpctest.Harness, t *testing.T) { +func testGetBlockHash(ctx context.Context, r *dcrdtest.Harness, t *testing.T) { // Create a new block connecting to the current tip. generatedBlockHashes, err := r.Node.Generate(ctx, 1) if err != nil { @@ -102,7 +100,7 @@ func TestRpcServer(t *testing.T) { // ensure that non-standard transactions aren't accepted into the // mempool or relayed. args := []string{"--rejectnonstd"} - harness, err := rpctest.New(t, chaincfg.RegNetParams(), nil, args) + harness, err := dcrdtest.New(t, chaincfg.RegNetParams(), nil, args) if err != nil { t.Fatalf("unable to create primary harness: %v", err) } @@ -120,34 +118,12 @@ func TestRpcServer(t *testing.T) { _ = harness.TearDown() t.Fatalf("unable to setup test chain: %v", err) } - - defer func() { - // Clean up any active harnesses that are still currently - // running.This includes removing all temporary directories, - // and shutting down any created processes. - if err := rpctest.TearDownAll(); err != nil { - t.Fatalf("unable to tear down all harnesses: %v", err) - } - }() - - var currentTestNum int - defer func() { - // If one of the integration tests caused a panic within the - // main goroutine, then tear down all the harnesses in order to - // avoid any leaked dcrd processes. - if r := recover(); r != nil { - fmt.Println("recovering from test panic: ", r) - if err := rpctest.TearDownAll(); err != nil { - fmt.Println("unable to tear down all harnesses: ", err) - } - t.Fatalf("test #%v panicked: %s", currentTestNum, debug.Stack()) - } - }() + defer harness.TearDownInTest(t) // Test cases. tests := []struct { name string - f func(context.Context, *rpctest.Harness, *testing.T) + f func(context.Context, *dcrdtest.Harness, *testing.T) }{ { f: testGetBestBlock, @@ -166,7 +142,5 @@ func TestRpcServer(t *testing.T) { for _, test := range tests { test.f(ctx, harness, t) t.Logf("=== Running test: %v ===", test.name) - - currentTestNum++ } } diff --git a/internal/integration/rpctests/treasury_test.go b/internal/integration/rpctests/treasury_test.go index 640a2ab25d..85170d89c8 100644 --- a/internal/integration/rpctests/treasury_test.go +++ b/internal/integration/rpctests/treasury_test.go @@ -26,11 +26,11 @@ import ( "github.com/decred/dcrd/dcrec/secp256k1/v4" "github.com/decred/dcrd/dcrutil/v4" "github.com/decred/dcrd/rpcclient/v8" - "github.com/decred/dcrd/rpctest" "github.com/decred/dcrd/txscript/v4" "github.com/decred/dcrd/txscript/v4/sign" "github.com/decred/dcrd/txscript/v4/stdaddr" "github.com/decred/dcrd/wire" + "github.com/decred/dcrtest/dcrdtest" ) // timeoutCtx returns a context with the given timeout and automatically calls @@ -232,9 +232,9 @@ func TestTreasury(t *testing.T) { } } - // Create the rpctest harness and mine outputs for the voting wallet to + // Create the dcrdtest harness and mine outputs for the voting wallet to // use. - hn, err := rpctest.New(t, net, handlers, extraArgs) + hn, err := dcrdtest.New(t, net, handlers, extraArgs) if err != nil { t.Fatal(err) } @@ -247,13 +247,13 @@ func TestTreasury(t *testing.T) { t.Fatal(err) } defer hn.TearDown() - _, err = rpctest.AdjustedSimnetMiner(timeoutCtx(t, time.Minute), hn.Node, 64) + _, err = dcrdtest.AdjustedSimnetMiner(timeoutCtx(t, time.Minute), hn.Node, 64) if err != nil { t.Fatal(err) } // Create the voting wallet. - vw, err := rpctest.NewVotingWallet(ctx, hn) + vw, err := dcrdtest.NewVotingWallet(ctx, hn) if err != nil { t.Fatalf("unable to create voting wallet for test: %v", err) } @@ -265,7 +265,7 @@ func TestTreasury(t *testing.T) { t.Fatalf("voting wallet errored: %v", vwerr) }) vw.SetMiner(func(ctx context.Context, nb uint32) ([]*chainhash.Hash, error) { - return rpctest.AdjustedSimnetMiner(ctx, hn.Node, nb) + return dcrdtest.AdjustedSimnetMiner(ctx, hn.Node, nb) }) // Create a privkey and p2pkh addr we control for use in the tests.