@@ -64,6 +64,8 @@ type testBackend struct {
6464 relHook func () // Hook is invoked when the requested state is released
6565}
6666
67+ // testBackend creates a new test backend. OBS: After test is done, teardown must be
68+ // invoked in order to release associated resources.
6769func newTestBackend (t * testing.T , n int , gspec * core.Genesis , generator func (i int , b * core.BlockGen )) * testBackend {
6870 backend := & testBackend {
6971 chainConfig : gspec .Config ,
@@ -141,6 +143,11 @@ func (b *testBackend) ChainDb() ethdb.Database {
141143 return b .chaindb
142144}
143145
146+ // teardown releases the associated resources.
147+ func (b * testBackend ) teardown () {
148+ b .chain .Stop ()
149+ }
150+
144151func (b * testBackend ) StateAtBlock (ctx context.Context , block * types.Block , reexec uint64 , base * state.StateDB , readOnly bool , preferDisk bool ) (* state.StateDB , StateReleaseFunc , error ) {
145152 statedb , err := b .chain .StateAt (block .Root ())
146153 if err != nil {
@@ -204,13 +211,15 @@ func TestTraceCall(t *testing.T) {
204211 }}
205212 genBlocks := 10
206213 signer := types.HomesteadSigner {}
207- api := NewAPI ( newTestBackend (t , genBlocks , genesis , func (i int , b * core.BlockGen ) {
214+ backend := newTestBackend (t , genBlocks , genesis , func (i int , b * core.BlockGen ) {
208215 // Transfer from account[0] to account[1]
209216 // value: 1000 wei
210217 // fee: 0 wei
211218 tx , _ := types .SignTx (types .NewTransaction (uint64 (i ), accounts [1 ].addr , big .NewInt (1000 ), params .TxGas , b .BaseFee (), nil ), signer , accounts [0 ].key )
212219 b .AddTx (tx )
213- }))
220+ })
221+ defer backend .teardown ()
222+ api := NewAPI (backend )
214223 var testSuite = []struct {
215224 blockNumber rpc.BlockNumber
216225 call ethapi.TransactionArgs
@@ -337,14 +346,16 @@ func TestTraceTransaction(t *testing.T) {
337346 }}
338347 target := common.Hash {}
339348 signer := types.HomesteadSigner {}
340- api := NewAPI ( newTestBackend (t , 1 , genesis , func (i int , b * core.BlockGen ) {
349+ backend := newTestBackend (t , 1 , genesis , func (i int , b * core.BlockGen ) {
341350 // Transfer from account[0] to account[1]
342351 // value: 1000 wei
343352 // fee: 0 wei
344353 tx , _ := types .SignTx (types .NewTransaction (uint64 (i ), accounts [1 ].addr , big .NewInt (1000 ), params .TxGas , b .BaseFee (), nil ), signer , accounts [0 ].key )
345354 b .AddTx (tx )
346355 target = tx .Hash ()
347- }))
356+ })
357+ defer backend .chain .Stop ()
358+ api := NewAPI (backend )
348359 result , err := api .TraceTransaction (context .Background (), target , nil )
349360 if err != nil {
350361 t .Errorf ("Failed to trace transaction %v" , err )
@@ -379,13 +390,15 @@ func TestTraceBlock(t *testing.T) {
379390 }}
380391 genBlocks := 10
381392 signer := types.HomesteadSigner {}
382- api := NewAPI ( newTestBackend (t , genBlocks , genesis , func (i int , b * core.BlockGen ) {
393+ backend := newTestBackend (t , genBlocks , genesis , func (i int , b * core.BlockGen ) {
383394 // Transfer from account[0] to account[1]
384395 // value: 1000 wei
385396 // fee: 0 wei
386397 tx , _ := types .SignTx (types .NewTransaction (uint64 (i ), accounts [1 ].addr , big .NewInt (1000 ), params .TxGas , b .BaseFee (), nil ), signer , accounts [0 ].key )
387398 b .AddTx (tx )
388- }))
399+ })
400+ defer backend .chain .Stop ()
401+ api := NewAPI (backend )
389402
390403 var testSuite = []struct {
391404 blockNumber rpc.BlockNumber
0 commit comments