core, miner, rpc, eth: fix goroutine leaks in tests (#24211)
* fix blocking and non-blocking issues * core: revert change in blockchain.go Co-authored-by: Martin Holst Swende <martin@swende.se>
This commit is contained in:
@ -80,7 +80,8 @@ func (bc *testBlockChain) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent)
|
||||
}
|
||||
|
||||
func TestMiner(t *testing.T) {
|
||||
miner, mux := createMiner(t)
|
||||
miner, mux, cleanup := createMiner(t)
|
||||
defer cleanup(false)
|
||||
miner.Start(common.HexToAddress("0x12345"))
|
||||
waitForMiningState(t, miner, true)
|
||||
// Start the downloader
|
||||
@ -107,7 +108,8 @@ func TestMiner(t *testing.T) {
|
||||
// An initial FailedEvent should allow mining to stop on a subsequent
|
||||
// downloader StartEvent.
|
||||
func TestMinerDownloaderFirstFails(t *testing.T) {
|
||||
miner, mux := createMiner(t)
|
||||
miner, mux, cleanup := createMiner(t)
|
||||
defer cleanup(false)
|
||||
miner.Start(common.HexToAddress("0x12345"))
|
||||
waitForMiningState(t, miner, true)
|
||||
// Start the downloader
|
||||
@ -138,8 +140,8 @@ func TestMinerDownloaderFirstFails(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestMinerStartStopAfterDownloaderEvents(t *testing.T) {
|
||||
miner, mux := createMiner(t)
|
||||
|
||||
miner, mux, cleanup := createMiner(t)
|
||||
defer cleanup(false)
|
||||
miner.Start(common.HexToAddress("0x12345"))
|
||||
waitForMiningState(t, miner, true)
|
||||
// Start the downloader
|
||||
@ -161,7 +163,8 @@ func TestMinerStartStopAfterDownloaderEvents(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestStartWhileDownload(t *testing.T) {
|
||||
miner, mux := createMiner(t)
|
||||
miner, mux, cleanup := createMiner(t)
|
||||
defer cleanup(false)
|
||||
waitForMiningState(t, miner, false)
|
||||
miner.Start(common.HexToAddress("0x12345"))
|
||||
waitForMiningState(t, miner, true)
|
||||
@ -174,16 +177,19 @@ func TestStartWhileDownload(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestStartStopMiner(t *testing.T) {
|
||||
miner, _ := createMiner(t)
|
||||
miner, _, cleanup := createMiner(t)
|
||||
defer cleanup(false)
|
||||
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)
|
||||
miner, _, cleanup := createMiner(t)
|
||||
defer cleanup(true)
|
||||
waitForMiningState(t, miner, false)
|
||||
miner.Start(common.HexToAddress("0x12345"))
|
||||
waitForMiningState(t, miner, true)
|
||||
@ -195,7 +201,8 @@ func TestCloseMiner(t *testing.T) {
|
||||
// TestMinerSetEtherbase checks that etherbase becomes set even if mining isn't
|
||||
// possible at the moment
|
||||
func TestMinerSetEtherbase(t *testing.T) {
|
||||
miner, mux := createMiner(t)
|
||||
miner, mux, cleanup := createMiner(t)
|
||||
defer cleanup(false)
|
||||
// Start with a 'bad' mining address
|
||||
miner.Start(common.HexToAddress("0xdead"))
|
||||
waitForMiningState(t, miner, true)
|
||||
@ -230,7 +237,7 @@ func waitForMiningState(t *testing.T, m *Miner, mining bool) {
|
||||
t.Fatalf("Mining() == %t, want %t", state, mining)
|
||||
}
|
||||
|
||||
func createMiner(t *testing.T) (*Miner, *event.TypeMux) {
|
||||
func createMiner(t *testing.T) (*Miner, *event.TypeMux, func(skipMiner bool)) {
|
||||
// Create Ethash config
|
||||
config := Config{
|
||||
Etherbase: common.HexToAddress("123456789"),
|
||||
@ -259,5 +266,14 @@ func createMiner(t *testing.T) (*Miner, *event.TypeMux) {
|
||||
// Create event Mux
|
||||
mux := new(event.TypeMux)
|
||||
// Create Miner
|
||||
return New(backend, &config, chainConfig, mux, engine, nil, merger), mux
|
||||
miner := New(backend, &config, chainConfig, mux, engine, nil, merger)
|
||||
cleanup := func(skipMiner bool) {
|
||||
bc.Stop()
|
||||
engine.Close()
|
||||
pool.Stop()
|
||||
if !skipMiner {
|
||||
miner.Close()
|
||||
}
|
||||
}
|
||||
return miner, mux, cleanup
|
||||
}
|
||||
|
Reference in New Issue
Block a user