From 1f2b212842edefcae943b3d8e7ab3478db712f75 Mon Sep 17 00:00:00 2001 From: Rob Mulholand Date: Wed, 19 Jun 2019 15:41:35 -0500 Subject: [PATCH 1/3] Inline mock block repository assertions - facilitates making (async) assertions on individual fields from other packages --- integration_test/geth_blockchain_test.go | 3 +- pkg/fakes/mock_block_repository.go | 79 +++++++----------------- pkg/geth/cold_import/importer_test.go | 8 ++- pkg/history/block_validator_test.go | 2 +- pkg/history/populate_blocks_test.go | 8 ++- 5 files changed, 34 insertions(+), 66 deletions(-) diff --git a/integration_test/geth_blockchain_test.go b/integration_test/geth_blockchain_test.go index 69b15a821..67ef2451b 100644 --- a/integration_test/geth_blockchain_test.go +++ b/integration_test/geth_blockchain_test.go @@ -56,7 +56,8 @@ var _ = Describe("Reading from the Geth blockchain", func() { _, err = history.RetrieveAndUpdateBlocks(blockChain, blocks, queriedBlocks) Expect(err).NotTo(HaveOccurred()) - blocks.AssertCreateOrUpdateBlocksCallCountAndBlockNumbersEquals(2, []int64{lastBlock.Int64() - 5, lastBlock.Int64() - 6}) + Expect(blocks.CreateOrUpdateBlockCallCount).To(Equal(2)) + Expect(blocks.CreateOrUpdateBlockPassedBlockNumbers).To(Equal([]int64{lastBlock.Int64() - 5, lastBlock.Int64() - 6})) close(done) }, 30) diff --git a/pkg/fakes/mock_block_repository.go b/pkg/fakes/mock_block_repository.go index 70c89d02e..f22abde47 100644 --- a/pkg/fakes/mock_block_repository.go +++ b/pkg/fakes/mock_block_repository.go @@ -17,42 +17,34 @@ package fakes import ( - . "github.com/onsi/gomega" - "github.com/vulcanize/vulcanizedb/pkg/core" ) type MockBlockRepository struct { - createOrUpdateBlockCallCount int - createOrUpdateBlockCalled bool - createOrUpdateBlockPassedBlock core.Block - createOrUpdateBlockPassedBlockNumbers []int64 + CreateOrUpdateBlockCallCount int + CreateOrUpdateBlockPassedBlock core.Block + CreateOrUpdateBlockPassedBlockNumbers []int64 createOrUpdateBlockReturnErr error createOrUpdateBlockReturnInt int64 - missingBlockNumbersCalled bool - missingBlockNumbersPassedEndingBlockNumber int64 - missingBlockNumbersPassedNodeId string - missingBlockNumbersPassedStartingBlockNumber int64 + MissingBlockNumbersPassedEndingBlockNumber int64 + MissingBlockNumbersPassedNodeId string + MissingBlockNumbersPassedStartingBlockNumber int64 missingBlockNumbersReturnArray []int64 - setBlockStatusCalled bool - setBlockStatusPassedChainHead int64 + SetBlockStatusPassedChainHead int64 } func NewMockBlockRepository() *MockBlockRepository { return &MockBlockRepository{ - createOrUpdateBlockCallCount: 0, - createOrUpdateBlockCalled: false, - createOrUpdateBlockPassedBlock: core.Block{}, - createOrUpdateBlockPassedBlockNumbers: nil, + CreateOrUpdateBlockCallCount: 0, + CreateOrUpdateBlockPassedBlock: core.Block{}, + CreateOrUpdateBlockPassedBlockNumbers: nil, createOrUpdateBlockReturnErr: nil, createOrUpdateBlockReturnInt: 0, - missingBlockNumbersCalled: false, - missingBlockNumbersPassedEndingBlockNumber: 0, - missingBlockNumbersPassedNodeId: "", - missingBlockNumbersPassedStartingBlockNumber: 0, + MissingBlockNumbersPassedEndingBlockNumber: 0, + MissingBlockNumbersPassedNodeId: "", + MissingBlockNumbersPassedStartingBlockNumber: 0, missingBlockNumbersReturnArray: nil, - setBlockStatusCalled: false, - setBlockStatusPassedChainHead: 0, + SetBlockStatusPassedChainHead: 0, } } @@ -66,10 +58,9 @@ func (repository *MockBlockRepository) SetMissingBlockNumbersReturnArray(returnA } func (repository *MockBlockRepository) CreateOrUpdateBlock(block core.Block) (int64, error) { - repository.createOrUpdateBlockCallCount++ - repository.createOrUpdateBlockCalled = true - repository.createOrUpdateBlockPassedBlock = block - repository.createOrUpdateBlockPassedBlockNumbers = append(repository.createOrUpdateBlockPassedBlockNumbers, block.Number) + repository.CreateOrUpdateBlockCallCount++ + repository.CreateOrUpdateBlockPassedBlock = block + repository.CreateOrUpdateBlockPassedBlockNumbers = append(repository.CreateOrUpdateBlockPassedBlockNumbers, block.Number) return repository.createOrUpdateBlockReturnInt, repository.createOrUpdateBlockReturnErr } @@ -78,41 +69,13 @@ func (repository *MockBlockRepository) GetBlock(blockNumber int64) (core.Block, } func (repository *MockBlockRepository) MissingBlockNumbers(startingBlockNumber int64, endingBlockNumber int64, nodeId string) []int64 { - repository.missingBlockNumbersCalled = true - repository.missingBlockNumbersPassedStartingBlockNumber = startingBlockNumber - repository.missingBlockNumbersPassedEndingBlockNumber = endingBlockNumber - repository.missingBlockNumbersPassedNodeId = nodeId + repository.MissingBlockNumbersPassedStartingBlockNumber = startingBlockNumber + repository.MissingBlockNumbersPassedEndingBlockNumber = endingBlockNumber + repository.MissingBlockNumbersPassedNodeId = nodeId return repository.missingBlockNumbersReturnArray } func (repository *MockBlockRepository) SetBlocksStatus(chainHead int64) error { - repository.setBlockStatusCalled = true - repository.setBlockStatusPassedChainHead = chainHead + repository.SetBlockStatusPassedChainHead = chainHead return nil } - -func (repository *MockBlockRepository) AssertCreateOrUpdateBlockCallCountEquals(times int) { - Expect(repository.createOrUpdateBlockCallCount).To(Equal(times)) -} - -func (repository *MockBlockRepository) AssertCreateOrUpdateBlocksCallCountAndBlockNumbersEquals(times int, blockNumbers []int64) { - Expect(repository.createOrUpdateBlockCallCount).To(Equal(times)) - Expect(repository.createOrUpdateBlockPassedBlockNumbers).To(Equal(blockNumbers)) -} - -func (repository *MockBlockRepository) AssertCreateOrUpdateBlockCalledWith(block core.Block) { - Expect(repository.createOrUpdateBlockCalled).To(BeTrue()) - Expect(repository.createOrUpdateBlockPassedBlock).To(Equal(block)) -} - -func (repository *MockBlockRepository) AssertMissingBlockNumbersCalledWith(startingBlockNumber int64, endingBlockNumber int64, nodeId string) { - Expect(repository.missingBlockNumbersCalled).To(BeTrue()) - Expect(repository.missingBlockNumbersPassedStartingBlockNumber).To(Equal(startingBlockNumber)) - Expect(repository.missingBlockNumbersPassedEndingBlockNumber).To(Equal(endingBlockNumber)) - Expect(repository.missingBlockNumbersPassedNodeId).To(Equal(nodeId)) -} - -func (repository *MockBlockRepository) AssertSetBlockStatusCalledWith(chainHead int64) { - Expect(repository.setBlockStatusCalled).To(BeTrue()) - Expect(repository.setBlockStatusPassedChainHead).To(Equal(chainHead)) -} diff --git a/pkg/geth/cold_import/importer_test.go b/pkg/geth/cold_import/importer_test.go index ed435f5fa..babb33b79 100644 --- a/pkg/geth/cold_import/importer_test.go +++ b/pkg/geth/cold_import/importer_test.go @@ -57,7 +57,9 @@ var _ = Describe("Geth cold importer", func() { err := importer.Execute(startingBlockNumber, endingBlockNumber, nodeId) Expect(err).NotTo(HaveOccurred()) - mockBlockRepository.AssertMissingBlockNumbersCalledWith(startingBlockNumber, endingBlockNumber, nodeId) + Expect(mockBlockRepository.MissingBlockNumbersPassedStartingBlockNumber).To(Equal(startingBlockNumber)) + Expect(mockBlockRepository.MissingBlockNumbersPassedEndingBlockNumber).To(Equal(endingBlockNumber)) + Expect(mockBlockRepository.MissingBlockNumbersPassedNodeId).To(Equal(nodeId)) mockEthereumDatabase.AssertGetBlockHashCalledWith(missingBlockNumber) mockEthereumDatabase.AssertGetBlockCalledWith(fakeHash, missingBlockNumber) }) @@ -85,7 +87,7 @@ var _ = Describe("Geth cold importer", func() { Expect(mockTransactionConverter.ConvertBlockTransactionsToCorePassedBlock).To(Equal(fakeGethBlock)) convertedBlock, err := blockConverter.ToCoreBlock(fakeGethBlock) Expect(err).NotTo(HaveOccurred()) - mockBlockRepository.AssertCreateOrUpdateBlockCalledWith(convertedBlock) + Expect(mockBlockRepository.CreateOrUpdateBlockPassedBlock).To(Equal(convertedBlock)) }) It("sets is_final status on populated blocks", func() { @@ -105,7 +107,7 @@ var _ = Describe("Geth cold importer", func() { err := importer.Execute(startingBlockNumber, endingBlockNumber, "node_id") Expect(err).NotTo(HaveOccurred()) - mockBlockRepository.AssertSetBlockStatusCalledWith(endingBlockNumber) + Expect(mockBlockRepository.SetBlockStatusPassedChainHead).To(Equal(endingBlockNumber)) }) It("fetches receipts from level db and persists them to pg", func() { diff --git a/pkg/history/block_validator_test.go b/pkg/history/block_validator_test.go index 6548fc2d2..b4eb1154f 100644 --- a/pkg/history/block_validator_test.go +++ b/pkg/history/block_validator_test.go @@ -38,7 +38,7 @@ var _ = Describe("Blocks validator", func() { Expect(err).NotTo(HaveOccurred()) Expect(window).To(Equal(history.ValidationWindow{LowerBound: 5, UpperBound: 7})) - blocksRepository.AssertCreateOrUpdateBlockCallCountEquals(3) + Expect(blocksRepository.CreateOrUpdateBlockCallCount).To(Equal(3)) }) It("returns the number of largest block", func() { diff --git a/pkg/history/populate_blocks_test.go b/pkg/history/populate_blocks_test.go index 6a5138ed8..f6caecb32 100644 --- a/pkg/history/populate_blocks_test.go +++ b/pkg/history/populate_blocks_test.go @@ -53,7 +53,8 @@ var _ = Describe("Populating blocks", func() { Expect(err).NotTo(HaveOccurred()) Expect(blocksAdded).To(Equal(3)) - blockRepository.AssertCreateOrUpdateBlocksCallCountAndBlockNumbersEquals(3, []int64{5, 8, 10}) + Expect(blockRepository.CreateOrUpdateBlockCallCount).To(Equal(3)) + Expect(blockRepository.CreateOrUpdateBlockPassedBlockNumbers).To(Equal([]int64{5, 8, 10})) }) It("returns the number of blocks created", func() { @@ -73,7 +74,8 @@ var _ = Describe("Populating blocks", func() { _, err := history.RetrieveAndUpdateBlocks(blockChain, blockRepository, history.MakeRange(2, 5)) Expect(err).NotTo(HaveOccurred()) - blockRepository.AssertCreateOrUpdateBlocksCallCountAndBlockNumbersEquals(4, []int64{2, 3, 4, 5}) + Expect(blockRepository.CreateOrUpdateBlockCallCount).To(Equal(4)) + Expect(blockRepository.CreateOrUpdateBlockPassedBlockNumbers).To(Equal([]int64{2, 3, 4, 5})) }) It("does not call repository create block when there is an error", func() { @@ -84,6 +86,6 @@ var _ = Describe("Populating blocks", func() { _, err := history.RetrieveAndUpdateBlocks(blockChain, blockRepository, blocks) Expect(err).To(HaveOccurred()) - blockRepository.AssertCreateOrUpdateBlockCallCountEquals(0) + Expect(blockRepository.CreateOrUpdateBlockCallCount).To(Equal(0)) }) }) From b71d44009ce6a5c5287da598a9333082349735ed Mon Sep 17 00:00:00 2001 From: Rob Mulholand Date: Wed, 19 Jun 2019 15:45:25 -0500 Subject: [PATCH 2/3] Use eventual assertion in Geth blockchain test - Hoping to reduce flakiness by allowing multiple checks with timeout --- integration_test/geth_blockchain_test.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/integration_test/geth_blockchain_test.go b/integration_test/geth_blockchain_test.go index 67ef2451b..646996b39 100644 --- a/integration_test/geth_blockchain_test.go +++ b/integration_test/geth_blockchain_test.go @@ -56,8 +56,12 @@ var _ = Describe("Reading from the Geth blockchain", func() { _, err = history.RetrieveAndUpdateBlocks(blockChain, blocks, queriedBlocks) Expect(err).NotTo(HaveOccurred()) - Expect(blocks.CreateOrUpdateBlockCallCount).To(Equal(2)) - Expect(blocks.CreateOrUpdateBlockPassedBlockNumbers).To(Equal([]int64{lastBlock.Int64() - 5, lastBlock.Int64() - 6})) + Eventually(func() int { + return blocks.CreateOrUpdateBlockCallCount + }).Should(Equal(2)) + Eventually(func() []int64 { + return blocks.CreateOrUpdateBlockPassedBlockNumbers + }).Should(Equal([]int64{lastBlock.Int64() - 5, lastBlock.Int64() - 6})) close(done) }, 30) From 014b4be0cab689d777bfd7705b5d3514b03dfeed Mon Sep 17 00:00:00 2001 From: Rob Mulholand Date: Wed, 19 Jun 2019 15:50:27 -0500 Subject: [PATCH 3/3] Use eventual assertion in contract watcher test - Hoping to fix flakiness --- ...ct_watcher_header_sync_transformer_test.go | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/integration_test/contract_watcher_header_sync_transformer_test.go b/integration_test/contract_watcher_header_sync_transformer_test.go index 1c8507f2e..4a6c5d769 100644 --- a/integration_test/contract_watcher_header_sync_transformer_test.go +++ b/integration_test/contract_watcher_header_sync_transformer_test.go @@ -49,17 +49,18 @@ var _ = Describe("contractWatcher headerSync transformer", func() { c, ok := t.Contracts[tusdAddr] Expect(ok).To(Equal(true)) - // TODO: Fix this - // This test sometimes randomly fails because - // for some reason the starting block number is not updated from - // its original value (5197514) to the block number (6194632) - // of the earliest header (mocks.MockHeader1) in the repository - // It is not clear how this happens without one of the above insertErrs - // having been thrown and without any errors thrown during the Init() call - Expect(c.StartingBlock).To(Equal(int64(6194632))) - Expect(c.Abi).To(Equal(constants.TusdAbiString)) - Expect(c.Name).To(Equal("TrueUSD")) - Expect(c.Address).To(Equal(tusdAddr)) + Eventually(func() int64 { + return c.StartingBlock + }).Should(Equal(int64(6194632))) + Eventually(func() string { + return c.Abi + }).Should(Equal(constants.TusdAbiString)) + Eventually(func() string { + return c.Name + }).Should(Equal("TrueUSD")) + Eventually(func() string { + return c.Address + }).Should(Equal(tusdAddr)) }) It("Fails to initialize if first and block cannot be fetched from vDB headers table", func() {