Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

les: clean up resources during test #22707

Merged
merged 1 commit into from
Apr 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions les/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,17 +212,25 @@ func (s *LesServer) Stop() error {
close(s.closeCh)

s.clientPool.Stop()
s.serverset.close()
if s.serverset != nil {
s.serverset.close()
}
s.peers.close()
s.fcManager.Stop()
s.costTracker.stop()
s.handler.stop()
s.servingQueue.stop()
s.vfluxServer.Stop()
if s.vfluxServer != nil {
s.vfluxServer.Stop()
}

// Note, bloom trie indexer is closed by parent bloombits indexer.
s.chtIndexer.Close()
s.lesDb.Close()
if s.chtIndexer != nil {
s.chtIndexer.Close()
}
if s.lesDb != nil {
s.lesDb.Close()
}
s.wg.Wait()
log.Info("Les server stopped")

Expand Down
18 changes: 12 additions & 6 deletions les/test_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func testIndexers(db ethdb.Database, odr light.OdrBackend, config *light.Indexer
return indexers[:]
}

func newTestClientHandler(backend *backends.SimulatedBackend, odr *LesOdr, indexers []*core.ChainIndexer, db ethdb.Database, peers *serverPeerSet, ulcServers []string, ulcFraction int) *clientHandler {
func newTestClientHandler(backend *backends.SimulatedBackend, odr *LesOdr, indexers []*core.ChainIndexer, db ethdb.Database, peers *serverPeerSet, ulcServers []string, ulcFraction int) (*clientHandler, func()) {
var (
evmux = new(event.TypeMux)
engine = ethash.NewFaker()
Expand Down Expand Up @@ -245,10 +245,12 @@ func newTestClientHandler(backend *backends.SimulatedBackend, odr *LesOdr, index
client.oracle.Start(backend)
}
client.handler.start()
return client.handler
return client.handler, func() {
client.handler.stop()
}
}

func newTestServerHandler(blocks int, indexers []*core.ChainIndexer, db ethdb.Database, clock mclock.Clock) (*serverHandler, *backends.SimulatedBackend) {
func newTestServerHandler(blocks int, indexers []*core.ChainIndexer, db ethdb.Database, clock mclock.Clock) (*serverHandler, *backends.SimulatedBackend, func()) {
var (
gspec = core.Genesis{
Config: params.AllEthashProtocolChanges,
Expand Down Expand Up @@ -314,7 +316,8 @@ func newTestServerHandler(blocks int, indexers []*core.ChainIndexer, db ethdb.Da
}
server.servingQueue.setThreads(4)
server.handler.start()
return server.handler, simulation
closer := func() { server.Stop() }
return server.handler, simulation, closer
}

func alwaysTrueFn() bool {
Expand Down Expand Up @@ -600,8 +603,8 @@ func newClientServerEnv(t *testing.T, config testnetConfig) (*testServer, *testC
ccIndexer, cbIndexer, cbtIndexer := cIndexers[0], cIndexers[1], cIndexers[2]
odr.SetIndexers(ccIndexer, cbIndexer, cbtIndexer)

server, b := newTestServerHandler(config.blocks, sindexers, sdb, clock)
client := newTestClientHandler(b, odr, cIndexers, cdb, speers, config.ulcServers, config.ulcFraction)
server, b, serverClose := newTestServerHandler(config.blocks, sindexers, sdb, clock)
client, clientClose := newTestClientHandler(b, odr, cIndexers, cdb, speers, config.ulcServers, config.ulcFraction)

scIndexer.Start(server.blockchain)
sbIndexer.Start(server.blockchain)
Expand Down Expand Up @@ -658,7 +661,10 @@ func newClientServerEnv(t *testing.T, config testnetConfig) (*testServer, *testC
cbIndexer.Close()
scIndexer.Close()
sbIndexer.Close()
dist.close()
serverClose()
b.Close()
clientClose()
}
return s, c, teardown
}
Expand Down