core: add Close() to blockchainer, implement it to properly close chain

Before it the deferred function in Run() was actually never able to properly
close the Store, so we weren't synching the latest state to the disk.
This commit is contained in:
Roman Khimov 2019-11-07 20:47:48 +03:00
parent d33083e1e1
commit b05754deac
8 changed files with 50 additions and 23 deletions

View file

@ -155,3 +155,23 @@ func TestGetTransaction(t *testing.T) {
assert.NoError(t, bc.persist())
}
}
func TestClose(t *testing.T) {
defer func() {
r := recover()
assert.NotNil(t, r)
}()
bc := newTestChain(t)
blocks := makeBlocks(10)
for i := 0; i < len(blocks); i++ {
require.NoError(t, bc.AddBlock(blocks[i]))
}
bc.Close()
// It's a hack, but we use internal knowledge of MemoryStore
// implementation which makes it completely unusable (up to panicing)
// after Close().
_ = bc.store.Put([]byte{0}, []byte{1})
// This should never be executed.
assert.Nil(t, t)
}