prysm-pulse/beacon-chain/chaintest/backend/simulated_backend_test.go
Nishant Das c48f9c96e0
Allow Simulated Backend to Advance Chain (#1445)
* initialize beacon state separately

* extracting out testcase into another func

* creating function for simulated objects

* creating simulated object struct

* final clean up

* documentation

* lint

* adding chain advancer

* review comments

* replacing lib

* adding initialization for chain and new vars in struct

* added func

* fixing tests and all other bugs

* fixing tests

* docs

* adding a new nil block function

* adding test

* remove check for nil blocks

* godoc

* fixing merge conflicts, tests

* fix test
2019-02-06 10:18:55 +08:00

72 lines
2.0 KiB
Go

package backend
import "testing"
func TestSimulatedBackendStartAndStop(t *testing.T) {
backend, err := NewSimulatedBackend()
if err != nil {
t.Fatalf("Could not create a new simulated backedn %v", err)
}
if err := backend.InitializeChain(); err != nil {
t.Fatalf("Could not initialize simulated backend %v", err)
}
if err := backend.Shutdown(); err != nil {
t.Errorf("Could not successfully shutdown simulated backend %v", err)
}
}
func TestGenerateBlocks(t *testing.T) {
backend, err := NewSimulatedBackend()
if err != nil {
t.Fatalf("Could not create a new simulated backedn %v", err)
}
if err := backend.InitializeChain(); err != nil {
t.Fatalf("Could not initialize simulated backend %v", err)
}
slotLimit := 250
for i := 0; i < slotLimit; i++ {
if err := backend.GenerateBlockAndAdvanceChain(&SimulatedObjects{}); err != nil {
t.Fatalf("Could not generate block and transition state successfully %v for slot %d", err, backend.state.Slot+1)
}
if backend.inMemoryBlocks[len(backend.inMemoryBlocks)-1].Slot != backend.state.Slot {
t.Errorf("In memory Blocks do not have the same last slot as the state, expected %d but got %d",
backend.state.Slot, backend.inMemoryBlocks[len(backend.inMemoryBlocks)-1])
}
}
if backend.state.Slot != uint64(slotLimit) {
t.Errorf("Unequal state slot and expected slot %d %d", backend.state.Slot, slotLimit)
}
}
func TestGenerateNilBlocks(t *testing.T) {
backend, err := NewSimulatedBackend()
if err != nil {
t.Fatalf("Could not create a new simulated backedn %v", err)
}
if err := backend.InitializeChain(); err != nil {
t.Fatalf("Could not initialize simulated backend %v", err)
}
slotLimit := 100
for i := 0; i < slotLimit; i++ {
if err := backend.GenerateNilBlockAndAdvanceChain(); err != nil {
t.Fatalf("Could not generate block and transition state successfully %v for slot %d", err, backend.state.Slot+1)
}
}
if backend.state.Slot != uint64(slotLimit) {
t.Errorf("Unequal state slot and expected slot %d %d", backend.state.Slot, slotLimit)
}
}