core: add ability to check whether blockchain is running
This commit is contained in:
parent
eb0494764c
commit
1dac45bbbb
2 changed files with 23 additions and 0 deletions
|
@ -151,6 +151,8 @@ type Blockchain struct {
|
|||
// Stop synchronization mechanisms.
|
||||
stopCh chan struct{}
|
||||
runToExitCh chan struct{}
|
||||
// isRunning denotes whether blockchain routines are currently running.
|
||||
isRunning atomic.Value
|
||||
|
||||
memPool *mempool.Pool
|
||||
|
||||
|
@ -302,6 +304,7 @@ func NewBlockchain(s storage.Store, cfg config.ProtocolConfiguration, log *zap.L
|
|||
return nil, err
|
||||
}
|
||||
|
||||
bc.isRunning.Store(false)
|
||||
return bc, nil
|
||||
}
|
||||
|
||||
|
@ -658,6 +661,7 @@ func (bc *Blockchain) initializeNativeCache(blockHeight uint32, d *dao.Simple) e
|
|||
// Run runs chain loop, it needs to be run as goroutine and executing it is
|
||||
// critical for correct Blockchain operation.
|
||||
func (bc *Blockchain) Run() {
|
||||
bc.isRunning.Store(true)
|
||||
persistTimer := time.NewTimer(persistInterval)
|
||||
defer func() {
|
||||
persistTimer.Stop()
|
||||
|
@ -667,6 +671,7 @@ func (bc *Blockchain) Run() {
|
|||
if err := bc.dao.Store.Close(); err != nil {
|
||||
bc.log.Warn("failed to close db", zap.Error(err))
|
||||
}
|
||||
bc.isRunning.Store(false)
|
||||
close(bc.runToExitCh)
|
||||
}()
|
||||
go bc.notificationDispatcher()
|
||||
|
|
|
@ -5,6 +5,7 @@ import (
|
|||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
|
@ -340,3 +341,20 @@ func TestBlockchain_BaseExecFeeBaseStoragePrice_Compat(t *testing.T) {
|
|||
check(t)
|
||||
})
|
||||
}
|
||||
|
||||
func TestBlockchain_IsRunning(t *testing.T) {
|
||||
chain := initTestChain(t, nil, nil)
|
||||
require.False(t, chain.isRunning.Load().(bool))
|
||||
oldPersisted := atomic.LoadUint32(&chain.persistedHeight)
|
||||
|
||||
go chain.Run()
|
||||
require.NoError(t, chain.AddBlock(chain.newBlock()))
|
||||
require.Eventually(t, func() bool {
|
||||
persisted := atomic.LoadUint32(&chain.persistedHeight)
|
||||
return persisted > oldPersisted
|
||||
}, 2*persistInterval, 100*time.Millisecond)
|
||||
require.True(t, chain.isRunning.Load().(bool))
|
||||
|
||||
chain.Close()
|
||||
require.False(t, chain.isRunning.Load().(bool))
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue