diff --git a/indexer/abci_test.go b/indexer/abci_test.go index 2dab573..b3895ea 100644 --- a/indexer/abci_test.go +++ b/indexer/abci_test.go @@ -67,10 +67,7 @@ func Test_ListenFinalizeBlock_Subscribe(t *testing.T) { indexer := app.EVMIndexer() defer app.Close() - blockChan, logsChan, pendChan := indexer.Subscribe() - defer close(blockChan) - defer close(logsChan) - defer close(pendChan) + blockChan, logsChan, _ := indexer.Subscribe() tx, evmTxHash := tests.GenerateCreateERC20Tx(t, app, privKeys[0]) diff --git a/indexer/indexer.go b/indexer/indexer.go index ee50fe4..0bc6d95 100644 --- a/indexer/indexer.go +++ b/indexer/indexer.go @@ -168,6 +168,7 @@ type blockEvents struct { // blockEventsEmitter emits block events to subscribers. func (e *EVMIndexerImpl) blockEventsEmitter(blockEvents *blockEvents, done chan struct{}) { defer close(done) + if blockEvents == nil { return } diff --git a/indexer/mempool_test.go b/indexer/mempool_test.go index c16c3ee..0b3bdf3 100644 --- a/indexer/mempool_test.go +++ b/indexer/mempool_test.go @@ -4,11 +4,15 @@ import ( "context" "sync" "testing" + "time" "github.com/initia-labs/minievm/tests" + "github.com/stretchr/testify/require" "github.com/cosmos/cosmos-sdk/types/mempool" + + coretypes "github.com/ethereum/go-ethereum/core/types" ) func Test_Mempool_Subscribe(t *testing.T) { @@ -17,9 +21,7 @@ func Test_Mempool_Subscribe(t *testing.T) { defer app.Close() blockChan, logsChan, pendChan := indexer.Subscribe() - defer close(blockChan) - defer close(logsChan) - defer close(pendChan) + go consumeBlockLogsChan(blockChan, logsChan, 5) tx, evmTxHash := tests.GenerateCreateERC20Tx(t, app, privKeys[0]) @@ -52,3 +54,16 @@ func Test_Mempool_Subscribe(t *testing.T) { wg.Wait() done() } + +func consumeBlockLogsChan(blockCh <-chan *coretypes.Header, logChan <-chan []*coretypes.Log, duration int) { + timer := time.NewTimer(time.Second * time.Duration(duration)) + + for { + select { + case <-blockCh: + case <-logChan: + case <-timer.C: + return + } + } +}