From 8aaa310ba7d730016042cdbad96179a99657e76e Mon Sep 17 00:00:00 2001 From: Shihao Xia Date: Sat, 8 Jan 2022 00:25:07 -0500 Subject: [PATCH] fix blocking and non-blocking issues --- core/blockchain.go | 8 ++++++-- core/blockchain_repair_test.go | 20 +++++++++++--------- eth/fetcher/block_fetcher_test.go | 4 +++- graphql/graphql_test.go | 1 + internal/jsre/jsre_test.go | 8 ++++---- miner/miner_test.go | 5 +++++ miner/worker_test.go | 2 +- node/node_test.go | 2 +- rpc/client_test.go | 1 + signer/core/api_test.go | 3 +++ 10 files changed, 36 insertions(+), 18 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index 8da7cc22923f8..1a80d8cd3cdcd 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -800,8 +800,12 @@ func (bc *BlockChain) Stop() { for _, offset := range []uint64{0, 1, TriesInMemory - 1} { if number := bc.CurrentBlock().NumberU64(); number > offset { - recent := bc.GetBlockByNumber(number - offset) - + num := number - offset + recent := bc.GetBlockByNumber(num) + if recent == nil { + log.Error("Failed to get block", num) + continue + } log.Info("Writing cached state to disk", "block", recent.Number(), "hash", recent.Hash(), "root", recent.Root()) if err := triedb.Commit(recent.Root(), true, nil); err != nil { log.Error("Failed to commit recent state trie", "err", err) diff --git a/core/blockchain_repair_test.go b/core/blockchain_repair_test.go index eb5025ed55e78..f7ef0ba84ac40 100644 --- a/core/blockchain_repair_test.go +++ b/core/blockchain_repair_test.go @@ -1779,6 +1779,7 @@ func testRepair(t *testing.T, tt *rewindTest, snapshots bool) { SnapshotLimit: 0, // Disable snapshot by default } ) + defer engine.Close() if snapshots { config.SnapshotLimit = 256 config.SnapshotWait = true @@ -1787,6 +1788,7 @@ func testRepair(t *testing.T, tt *rewindTest, snapshots bool) { if err != nil { t.Fatalf("Failed to create chain: %v", err) } + defer chain.Stop() // If sidechain blocks are needed, make a light chain and import it var sideblocks types.Blocks if tt.sidechainBlocks > 0 { @@ -1836,25 +1838,25 @@ func testRepair(t *testing.T, tt *rewindTest, snapshots bool) { } defer db.Close() - chain, err = NewBlockChain(db, nil, params.AllEthashProtocolChanges, engine, vm.Config{}, nil, nil) + newChain, err := NewBlockChain(db, nil, params.AllEthashProtocolChanges, engine, vm.Config{}, nil, nil) if err != nil { t.Fatalf("Failed to recreate chain: %v", err) } - defer chain.Stop() + defer newChain.Stop() // Iterate over all the remaining blocks and ensure there are no gaps - verifyNoGaps(t, chain, true, canonblocks) - verifyNoGaps(t, chain, false, sideblocks) - verifyCutoff(t, chain, true, canonblocks, tt.expCanonicalBlocks) - verifyCutoff(t, chain, false, sideblocks, tt.expSidechainBlocks) + verifyNoGaps(t, newChain, true, canonblocks) + verifyNoGaps(t, newChain, false, sideblocks) + verifyCutoff(t, newChain, true, canonblocks, tt.expCanonicalBlocks) + verifyCutoff(t, newChain, false, sideblocks, tt.expSidechainBlocks) - if head := chain.CurrentHeader(); head.Number.Uint64() != tt.expHeadHeader { + if head := newChain.CurrentHeader(); head.Number.Uint64() != tt.expHeadHeader { t.Errorf("Head header mismatch: have %d, want %d", head.Number, tt.expHeadHeader) } - if head := chain.CurrentFastBlock(); head.NumberU64() != tt.expHeadFastBlock { + if head := newChain.CurrentFastBlock(); head.NumberU64() != tt.expHeadFastBlock { t.Errorf("Head fast block mismatch: have %d, want %d", head.NumberU64(), tt.expHeadFastBlock) } - if head := chain.CurrentBlock(); head.NumberU64() != tt.expHeadBlock { + if head := newChain.CurrentBlock(); head.NumberU64() != tt.expHeadBlock { t.Errorf("Head block mismatch: have %d, want %d", head.NumberU64(), tt.expHeadBlock) } if frozen, err := db.(freezer).Ancients(); err != nil { diff --git a/eth/fetcher/block_fetcher_test.go b/eth/fetcher/block_fetcher_test.go index 628a5650424d2..06c61ae55d205 100644 --- a/eth/fetcher/block_fetcher_test.go +++ b/eth/fetcher/block_fetcher_test.go @@ -364,6 +364,7 @@ func testSequentialAnnouncements(t *testing.T, light bool) { hashes, blocks := makeChain(targetBlocks, 0, genesis) tester := newTester(light) + defer tester.fetcher.Stop() headerFetcher := tester.makeHeaderFetcher("valid", blocks, -gatherSlack) bodyFetcher := tester.makeBodyFetcher("valid", blocks, 0) @@ -743,7 +744,7 @@ func testInvalidNumberAnnouncement(t *testing.T, light bool) { badBodyFetcher := tester.makeBodyFetcher("bad", blocks, 0) imported := make(chan interface{}) - announced := make(chan interface{}) + announced := make(chan interface{}, 2) tester.fetcher.importedHook = func(header *types.Header, block *types.Block) { if light { if header == nil { @@ -806,6 +807,7 @@ func TestEmptyBlockShortCircuit(t *testing.T) { hashes, blocks := makeChain(32, 0, genesis) tester := newTester(false) + defer tester.fetcher.Stop() headerFetcher := tester.makeHeaderFetcher("valid", blocks, -gatherSlack) bodyFetcher := tester.makeBodyFetcher("valid", blocks, 0) diff --git a/graphql/graphql_test.go b/graphql/graphql_test.go index 4e0f099e42089..a0b7979069278 100644 --- a/graphql/graphql_test.go +++ b/graphql/graphql_test.go @@ -48,6 +48,7 @@ func TestBuildSchema(t *testing.T) { conf := node.DefaultConfig conf.DataDir = ddir stack, err := node.New(&conf) + defer stack.Close() if err != nil { t.Fatalf("could not create new node: %v", err) } diff --git a/internal/jsre/jsre_test.go b/internal/jsre/jsre_test.go index bc38f7a44a86f..57acdaed90ae0 100644 --- a/internal/jsre/jsre_test.go +++ b/internal/jsre/jsre_test.go @@ -83,20 +83,20 @@ func TestNatto(t *testing.T) { err := jsre.Exec("test.js") if err != nil { - t.Errorf("expected no error, got %v", err) + t.Fatalf("expected no error, got %v", err) } time.Sleep(100 * time.Millisecond) val, err := jsre.Run("msg") if err != nil { - t.Errorf("expected no error, got %v", err) + t.Fatalf("expected no error, got %v", err) } if val.ExportType().Kind() != reflect.String { - t.Errorf("expected string value, got %v", val) + t.Fatalf("expected string value, got %v", val) } exp := "testMsg" got := val.ToString().String() if exp != got { - t.Errorf("expected '%v', got '%v'", exp, got) + t.Fatalf("expected '%v', got '%v'", exp, got) } jsre.Stop(false) } diff --git a/miner/miner_test.go b/miner/miner_test.go index de7ca73e260eb..6fbf700d72ed2 100644 --- a/miner/miner_test.go +++ b/miner/miner_test.go @@ -108,6 +108,7 @@ func TestMiner(t *testing.T) { // downloader StartEvent. func TestMinerDownloaderFirstFails(t *testing.T) { miner, mux := createMiner(t) + defer miner.Close() miner.Start(common.HexToAddress("0x12345")) waitForMiningState(t, miner, true) // Start the downloader @@ -175,15 +176,18 @@ func TestStartWhileDownload(t *testing.T) { func TestStartStopMiner(t *testing.T) { miner, _ := createMiner(t) + defer miner.Close() waitForMiningState(t, miner, false) miner.Start(common.HexToAddress("0x12345")) waitForMiningState(t, miner, true) miner.Stop() waitForMiningState(t, miner, false) + } func TestCloseMiner(t *testing.T) { miner, _ := createMiner(t) + defer miner.Close() waitForMiningState(t, miner, false) miner.Start(common.HexToAddress("0x12345")) waitForMiningState(t, miner, true) @@ -196,6 +200,7 @@ func TestCloseMiner(t *testing.T) { // possible at the moment func TestMinerSetEtherbase(t *testing.T) { miner, mux := createMiner(t) + defer miner.Close() // Start with a 'bad' mining address miner.Start(common.HexToAddress("0xdead")) waitForMiningState(t, miner, true) diff --git a/miner/worker_test.go b/miner/worker_test.go index c8ddd2c320b81..bbbff745bf3b9 100644 --- a/miner/worker_test.go +++ b/miner/worker_test.go @@ -382,7 +382,7 @@ func testRegenerateMiningBlock(t *testing.T, chainConfig *params.ChainConfig, en w, b := newTestWorker(t, chainConfig, engine, rawdb.NewMemoryDatabase(), 0) defer w.close() - var taskCh = make(chan struct{}) + var taskCh = make(chan struct{}, 3) taskIndex := 0 w.newTaskHook = func(task *task) { diff --git a/node/node_test.go b/node/node_test.go index e104630600042..25cfa9d38d78f 100644 --- a/node/node_test.go +++ b/node/node_test.go @@ -393,7 +393,7 @@ func TestLifecycleTerminationGuarantee(t *testing.T) { // on the given prefix func TestRegisterHandler_Successful(t *testing.T) { node := createNode(t, 7878, 7979) - + defer node.Close() // create and mount handler handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("success")) diff --git a/rpc/client_test.go b/rpc/client_test.go index 224eb0c5c828a..fa6010bb199c1 100644 --- a/rpc/client_test.go +++ b/rpc/client_test.go @@ -615,6 +615,7 @@ func TestClientReconnect(t *testing.T) { // Start a server and corresponding client. s1, l1 := startServer("127.0.0.1:0") client, err := DialContext(ctx, "ws://"+l1.Addr().String()) + defer client.Close() if err != nil { t.Fatal("can't dial", err) } diff --git a/signer/core/api_test.go b/signer/core/api_test.go index 36f12f71a52d1..9f44ca3195664 100644 --- a/signer/core/api_test.go +++ b/signer/core/api_test.go @@ -256,6 +256,9 @@ func TestSignTx(t *testing.T) { if err != nil { t.Fatal(err) } + if len(list) == 0 { + t.Fatal("Unexpected empty list") + } a := common.NewMixedcaseAddress(list[0]) methodSig := "test(uint)"