From 48e067aeab9f4bcbb6a3be16206b3742f00827f7 Mon Sep 17 00:00:00 2001 From: akucukoduk16 Date: Fri, 19 Aug 2022 13:17:57 +0300 Subject: [PATCH 01/13] validated single and two blocks with BlocksMapDb implemented --- .../protocol/engines/IngestEngineTest.java | 117 ++++++++++++++---- 1 file changed, 90 insertions(+), 27 deletions(-) diff --git a/src/test/java/protocol/engines/IngestEngineTest.java b/src/test/java/protocol/engines/IngestEngineTest.java index 5723c6b2..b9fbdc37 100644 --- a/src/test/java/protocol/engines/IngestEngineTest.java +++ b/src/test/java/protocol/engines/IngestEngineTest.java @@ -1,5 +1,10 @@ package protocol.engines; +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -9,6 +14,7 @@ import static org.mockito.Mockito.*; +import org.mockito.internal.util.MockUtil; import model.Entity; import model.codec.EntityType; import model.crypto.PublicKey; @@ -16,6 +22,7 @@ import model.lightchain.*; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import org.testcontainers.shaded.org.apache.commons.io.FileUtils; import protocol.Parameters; import protocol.assigner.AssignerInf; import state.Snapshot; @@ -23,6 +30,7 @@ import storage.Blocks; import storage.Identifiers; import storage.Transactions; +import storage.mapdb.BlocksMapDb; import unittest.fixtures.AccountFixture; import unittest.fixtures.BlockFixture; import unittest.fixtures.EntityFixture; @@ -32,46 +40,71 @@ * Encapsulates tests for ingest engine implementation. */ public class IngestEngineTest { + private static final String TEMP_DIR = "tempdir"; + private static final String TEMP_FILE_ID = "tempfileID.db"; + private static final String TEMP_FILE_HEIGHT = "tempfileHEIGHT.db"; + private Path tempdir; + private BlocksMapDb db; /** * Evaluates that when a new validated block arrives at ingest engine, - * the engine adds the block to its block storage database. + * the engine adds the block to its mocked block storage database. * The engine also adds hash of all the transactions of block into its "transactions" database. */ @Test - public void testValidatedSingleBlock() { + public void testValidatedSingleBlockMockBlocks() { Blocks blocks = mock(Blocks.class); - Identifiers seenEntities = mock(Identifiers.class); - Identifiers transactionIds = mock(Identifiers.class); - Transactions pendingTransactions = mock(Transactions.class); - - Block block = BlockFixture.newBlock(); - - // mocks block as new to ingest engine. - when(seenEntities.has(block.id())).thenReturn(false); - when(blocks.has(block.id())).thenReturn(false); - - IngestEngine ingestEngine = this.mockIngestEngineForEntities( - new ArrayList<>(List.of(block)), - seenEntities, - transactionIds, - pendingTransactions, - blocks); - - // action - ingestEngine.process(block); + runTestValidatedSingleBlock(blocks); + } - // verify - verifyBlockHappyPathCalled(block, blocks, pendingTransactions, transactionIds, seenEntities); + /** + * Evaluates that when a new validated block arrives at ingest engine, + * the engine adds the block to its real block storage database. + * The engine also adds hash of all the transactions of block into its "transactions" database. + */ + @Test + public void testValidatedSingleBlockRealBlocks() throws IOException { + Path currentRelativePath = Paths.get(""); + tempdir = Files.createTempDirectory(currentRelativePath, TEMP_DIR); + db = new BlocksMapDb(tempdir.toAbsolutePath() + "/" + TEMP_FILE_ID, + tempdir.toAbsolutePath() + "/" + TEMP_FILE_HEIGHT); + Blocks blocks = db; + runTestValidatedSingleBlock(blocks); + db.closeDb(); + FileUtils.deleteDirectory(new File(tempdir.toString())); } /** * Evaluates that when two validated blocks arrive at ingest engine SEQUENTIALLY, - * the engine adds the blocks to its block storage database. + * the engine adds the blocks to its mocked block storage database. * The engine also adds hash of all the transactions of blocks into its "transactions" database. */ @Test - public void testValidatedTwoBlocks() { + public void testValidatedTwoBlocksMockBlocks() { Blocks blocks = mock(Blocks.class); + runTestValidatedTwoBlocks(blocks); + } + + /** + * Evaluates that when two validated blocks arrive at ingest engine SEQUENTIALLY, + * the engine adds the blocks to its real block storage database. + * The engine also adds hash of all the transactions of blocks into its "transactions" database. + */ + @Test + public void testValidatedTwoBlocksRealBlocks() throws IOException { + Path currentRelativePath = Paths.get(""); + tempdir = Files.createTempDirectory(currentRelativePath, TEMP_DIR); + db = new BlocksMapDb(tempdir.toAbsolutePath() + "/" + TEMP_FILE_ID, + tempdir.toAbsolutePath() + "/" + TEMP_FILE_HEIGHT); + Blocks blocks = db; + runTestValidatedTwoBlocks(blocks); + db.closeDb(); + FileUtils.deleteDirectory(new File(tempdir.toString())); + } + /** + * The method called by test validated single block for mocked and real versions. + * @param blocks mocked or real block. + */ + public void runTestValidatedTwoBlocks(Blocks blocks){ Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); @@ -95,6 +128,7 @@ public void testValidatedTwoBlocks() { // verification for block 2 verifyBlockHappyPathCalled(block2, blocks, pendingTransactions, transactionIds, seenEntities); + } /** @@ -663,8 +697,10 @@ private void verifyBlockHappyPathCalled( Transactions pendingTx, Identifiers txIds, Identifiers seenEntities) { + if (MockUtil.isMock(blocks)){ + verify(blocks, times(1)).add(block); + } - verify(blocks, times(1)).add(block); verify(seenEntities, times(1)).add(block.id()); for (Transaction tx : block.getTransactions()) { verify(pendingTx, times(1)).has(tx.id()); @@ -731,7 +767,9 @@ private IngestEngine mockIngestEngineForEntities( Block block = (Block) e; when(state.atBlockId(block.getPreviousBlockId())).thenReturn(snapshot); - when(blocks.has(block.id())).thenReturn(false); + if (MockUtil.isMock(blocks)){ + when(blocks.has(block.id())).thenReturn(false); + } for (Transaction tx : block.getTransactions()) { when(pendingTx.has(tx.id())).thenReturn(false); } @@ -810,4 +848,29 @@ private void mockAssignment(AssignerInf assigner, Entity e, Snapshot snapshot) { // returns the mock account for all identifiers when(snapshot.getAccount(any(Identifier.class))).thenReturn(account); } + + /** + * The method called by test validated single block for mocked and real versions. + * @param blocks mocked or real block. + */ + private void runTestValidatedSingleBlock(Blocks blocks){ + Identifiers seenEntities = mock(Identifiers.class); + Identifiers transactionIds = mock(Identifiers.class); + Transactions pendingTransactions = mock(Transactions.class); + Block block = BlockFixture.newBlock(); + when(seenEntities.has(block.id())).thenReturn(false); + if (MockUtil.isMock(blocks)){ + when(blocks.has(block.id())).thenReturn(false); + } + IngestEngine ingestEngine = this.mockIngestEngineForEntities( + new ArrayList<>(List.of(block)), + seenEntities, + transactionIds, + pendingTransactions, + blocks); + // action + ingestEngine.process(block); + // verify + verifyBlockHappyPathCalled(block, blocks, pendingTransactions, transactionIds, seenEntities); + } } \ No newline at end of file From e9c404b53abb56e0a6971ce27c8c547cc31536ad Mon Sep 17 00:00:00 2001 From: akucukoduk16 Date: Fri, 19 Aug 2022 13:35:46 +0300 Subject: [PATCH 02/13] validated two blocks concurrently with BlocksMapDb implemented --- .../protocol/engines/IngestEngineTest.java | 69 ++++++++++++++++--- 1 file changed, 59 insertions(+), 10 deletions(-) diff --git a/src/test/java/protocol/engines/IngestEngineTest.java b/src/test/java/protocol/engines/IngestEngineTest.java index b9fbdc37..b91d45bb 100644 --- a/src/test/java/protocol/engines/IngestEngineTest.java +++ b/src/test/java/protocol/engines/IngestEngineTest.java @@ -51,7 +51,7 @@ public class IngestEngineTest { * The engine also adds hash of all the transactions of block into its "transactions" database. */ @Test - public void testValidatedSingleBlockMockBlocks() { + public void testValidatedSingleBlockMockedStorage() { Blocks blocks = mock(Blocks.class); runTestValidatedSingleBlock(blocks); } @@ -62,7 +62,7 @@ public void testValidatedSingleBlockMockBlocks() { * The engine also adds hash of all the transactions of block into its "transactions" database. */ @Test - public void testValidatedSingleBlockRealBlocks() throws IOException { + public void testValidatedSingleBlockRealStorage() throws IOException { Path currentRelativePath = Paths.get(""); tempdir = Files.createTempDirectory(currentRelativePath, TEMP_DIR); db = new BlocksMapDb(tempdir.toAbsolutePath() + "/" + TEMP_FILE_ID, @@ -79,7 +79,7 @@ public void testValidatedSingleBlockRealBlocks() throws IOException { * The engine also adds hash of all the transactions of blocks into its "transactions" database. */ @Test - public void testValidatedTwoBlocksMockBlocks() { + public void testValidatedTwoBlocksMockedStorage() { Blocks blocks = mock(Blocks.class); runTestValidatedTwoBlocks(blocks); } @@ -90,7 +90,7 @@ public void testValidatedTwoBlocksMockBlocks() { * The engine also adds hash of all the transactions of blocks into its "transactions" database. */ @Test - public void testValidatedTwoBlocksRealBlocks() throws IOException { + public void testValidatedTwoBlocksRealStorage() throws IOException { Path currentRelativePath = Paths.get(""); tempdir = Files.createTempDirectory(currentRelativePath, TEMP_DIR); db = new BlocksMapDb(tempdir.toAbsolutePath() + "/" + TEMP_FILE_ID, @@ -133,12 +133,36 @@ public void runTestValidatedTwoBlocks(Blocks blocks){ /** * Evaluates that when two validated blocks arrive at ingest engine concurrently, - * the engine adds the blocks to its block storage database. + * the engine adds the blocks to its mocked block storage database. * The engine also adds hash of all the transactions of blocks into its "transactions" database. */ @Test - public void testValidatedTwoBlocksConcurrently() { + public void testValidatedTwoBlocksConcurrentlyMockedBlocks() { Blocks blocks = mock(Blocks.class); + runTestValidatedTwoBlocksConcurrently(blocks); + } + /** + * Evaluates that when two validated blocks arrive at ingest engine concurrently, + * the engine adds the blocks to its real block storage database. + * The engine also adds hash of all the transactions of blocks into its "transactions" database. + */ + @Test + public void testValidatedTwoBlocksConcurrentlyRealBlocks() throws IOException { + Path currentRelativePath = Paths.get(""); + tempdir = Files.createTempDirectory(currentRelativePath, TEMP_DIR); + db = new BlocksMapDb(tempdir.toAbsolutePath() + "/" + TEMP_FILE_ID, + tempdir.toAbsolutePath() + "/" + TEMP_FILE_HEIGHT); + Blocks blocks = db; + runTestValidatedTwoBlocksConcurrently(blocks); + db.closeDb(); + FileUtils.deleteDirectory(new File(tempdir.toString())); + } + + /** + * The method called by test validated two blocks for mocked and real versions concurrently. + * @param blocks mocked or real block. + */ + public void runTestValidatedTwoBlocksConcurrently(Blocks blocks){ Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); @@ -166,12 +190,36 @@ public void testValidatedTwoBlocksConcurrently() { /** * Evaluates that when two same validated blocks arrive at ingest engine (second one should be ignored), - * the engine adds the blocks to its block storage database. + * the engine adds the blocks to its mocked block storage database. * The engine also adds hash of all the transactions of blocks into its "transactions" database. */ @Test - public void testValidatedSameTwoBlocks() { + public void testValidatedSameTwoBlocksMockedStorage() { Blocks blocks = mock(Blocks.class); + runTestValidatedSameTwoBlocks(blocks); + } + /** + * Evaluates that when two same validated blocks arrive at ingest engine (second one should be ignored), + * the engine adds the blocks to its real block storage database. + * The engine also adds hash of all the transactions of blocks into its "transactions" database. + */ + @Test + public void testValidatedSameTwoBlocksRealStorage() throws IOException { + Path currentRelativePath = Paths.get(""); + tempdir = Files.createTempDirectory(currentRelativePath, TEMP_DIR); + db = new BlocksMapDb(tempdir.toAbsolutePath() + "/" + TEMP_FILE_ID, + tempdir.toAbsolutePath() + "/" + TEMP_FILE_HEIGHT); + Blocks blocks = db; + runTestValidatedSameTwoBlocks(blocks); + db.closeDb(); + FileUtils.deleteDirectory(new File(tempdir.toString())); + } + + /** + * The method called by test validated two blocks for mocked and real versions concurrently. + * @param blocks mocked or real block. + */ + public void runTestValidatedSameTwoBlocks(Blocks blocks){ Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); @@ -179,8 +227,9 @@ public void testValidatedSameTwoBlocks() { Block block = BlockFixture.newBlock(); when(seenEntities.has(block.id())).thenReturn(false); - when(blocks.has(block.id())).thenReturn(false); - + if (MockUtil.isMock(blocks)){ + when(blocks.has(block.id())).thenReturn(false); + } IngestEngine ingestEngine = this.mockIngestEngineForEntities( new ArrayList<>(List.of(block)), seenEntities, From 80764a9adde7cdd52c96864c77678494fc0ee334 Mon Sep 17 00:00:00 2001 From: akucukoduk16 Date: Fri, 19 Aug 2022 15:21:08 +0300 Subject: [PATCH 03/13] all ingest engine test seperated for mocked and real blocks storage --- .../protocol/engines/IngestEngineTest.java | 519 ++++++++++++++---- 1 file changed, 411 insertions(+), 108 deletions(-) diff --git a/src/test/java/protocol/engines/IngestEngineTest.java b/src/test/java/protocol/engines/IngestEngineTest.java index b91d45bb..54c085fd 100644 --- a/src/test/java/protocol/engines/IngestEngineTest.java +++ b/src/test/java/protocol/engines/IngestEngineTest.java @@ -217,7 +217,7 @@ public void testValidatedSameTwoBlocksRealStorage() throws IOException { /** * The method called by test validated two blocks for mocked and real versions concurrently. - * @param blocks mocked or real block. + * @param blocks mocked or real blocks. */ public void runTestValidatedSameTwoBlocks(Blocks blocks){ Identifiers seenEntities = mock(Identifiers.class); @@ -227,9 +227,7 @@ public void runTestValidatedSameTwoBlocks(Blocks blocks){ Block block = BlockFixture.newBlock(); when(seenEntities.has(block.id())).thenReturn(false); - if (MockUtil.isMock(blocks)){ - when(blocks.has(block.id())).thenReturn(false); - } + IngestEngine ingestEngine = this.mockIngestEngineForEntities( new ArrayList<>(List.of(block)), seenEntities, @@ -249,12 +247,37 @@ public void runTestValidatedSameTwoBlocks(Blocks blocks){ /** * Evaluates that when a new validated block (with shared transactions in pendingTx) arrive at ingest engine, - * the engine adds the blocks to its block storage database. + * the engine adds the blocks to its real block storage database. * The engine also removes hash of the transactions of blocks from pendingTransactions. */ @Test - public void testValidatedBlockContainingPendingTransaction() { + public void testValidatedBlockContainingPendingTransactionMockedStorage() throws IOException { Blocks blocks = mock(Blocks.class); + runTestValidatedBlockContainingPendingTransaction(blocks); + } + + /** + * Evaluates that when a new validated block (with shared transactions in pendingTx) arrive at ingest engine, + * the engine adds the blocks to its real block storage database. + * The engine also removes hash of the transactions of blocks from pendingTransactions. + */ + @Test + public void testValidatedBlockContainingPendingTransactionRealStorage() throws IOException { + Path currentRelativePath = Paths.get(""); + tempdir = Files.createTempDirectory(currentRelativePath, TEMP_DIR); + db = new BlocksMapDb(tempdir.toAbsolutePath() + "/" + TEMP_FILE_ID, + tempdir.toAbsolutePath() + "/" + TEMP_FILE_HEIGHT); + Blocks blocks = db; + runTestValidatedBlockContainingPendingTransaction(blocks); + db.closeDb(); + FileUtils.deleteDirectory(new File(tempdir.toString())); + } + + /** + * The method called by test validated blocks containing pending transactions for mocked and real versions concurrently. + * @param blocks mocked or real block. + */ + public void runTestValidatedBlockContainingPendingTransaction(Blocks blocks){ Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); @@ -281,16 +304,41 @@ public void testValidatedBlockContainingPendingTransaction() { verify(pendingTransactions, times(1)).remove(tx.id()); } } - /** * Evaluates that when two new validated blocks (with a shared transactions in pendingTx, disjoint set) - * arrive at ingest engine, the engine adds the blocks to its block storage database. + * arrive at ingest engine, the engine adds the blocks to its real block storage database. * The engine also removes the hash of the single shared transaction among blocks from pending transactions. */ @Test - public void testConcurrentBlockIngestionContainingSeenTransactionDisjointSet() { + public void testConcurrentBlockIngestionContainingSeenTransactionDisjointSetMockedStorage() throws IOException { // R Blocks blocks = mock(Blocks.class); + runTestConcurrentBlockIngestionContainingSeenTransactionDisjointSet(blocks); + } + /** + * Evaluates that when two new validated blocks (with a shared transactions in pendingTx, disjoint set) + * arrive at ingest engine, the engine adds the blocks to its real block storage database. + * The engine also removes the hash of the single shared transaction among blocks from pending transactions. + */ + @Test + public void testConcurrentBlockIngestionContainingSeenTransactionDisjointSetRealStorage() throws IOException { + // R + Path currentRelativePath = Paths.get(""); + tempdir = Files.createTempDirectory(currentRelativePath, TEMP_DIR); + db = new BlocksMapDb(tempdir.toAbsolutePath() + "/" + TEMP_FILE_ID, + tempdir.toAbsolutePath() + "/" + TEMP_FILE_HEIGHT); + Blocks blocks = db; + runTestConcurrentBlockIngestionContainingSeenTransactionDisjointSet(blocks); + db.closeDb(); + FileUtils.deleteDirectory(new File(tempdir.toString())); + + } + + /** + * The method called by test concurrent block ingestion containing seen transaction disjoint set mocked and real versions. + * @param blocks mocked or real block. + */ + private void runTestConcurrentBlockIngestionContainingSeenTransactionDisjointSet(Blocks blocks){ Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); @@ -326,13 +374,10 @@ public void testConcurrentBlockIngestionContainingSeenTransactionDisjointSet() { } /** - * Evaluates that when two new validated blocks (with shared transactions in pendingTx, overlapping set) - * arrive at ingest engine, the engine adds the blocks to its block storage database. - * The engine also removes hash of the transactions of blocks from pendingTransactions. + * The method called by test concurrent block ingestion containing seen transaction overlapping set mocked and real versions. + * @param blocks mocked or real block. */ - @Test - public void testConcurrentBlockIngestionContainingSeenTransactionOverlappingSet() { - Blocks blocks = mock(Blocks.class); + private void runTestConcurrentBlockIngestionContainingSeenTransactionOverlappingSet(Blocks blocks){ Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); @@ -365,54 +410,100 @@ public void testConcurrentBlockIngestionContainingSeenTransactionOverlappingSet( verify(pendingTransactions, times(1)).remove(tx.id()); } } + /** + * Evaluates that when two new validated blocks (with shared transactions in pendingTx, overlapping set) + * arrive at ingest engine, the engine adds the blocks to its mocked block storage database. + * The engine also removes hash of the transactions of blocks from pendingTransactions. + */ + @Test + public void testConcurrentBlockIngestionContainingSeenTransactionOverlappingSetMockedStorage() { + Blocks blocks = mock(Blocks.class); + runTestConcurrentBlockIngestionContainingSeenTransactionOverlappingSet(blocks); + } + /** + * Evaluates that when two new validated blocks (with shared transactions in pendingTx, overlapping set) + * arrive at ingest engine, the engine adds the blocks to its real block storage database. + * The engine also removes hash of the transactions of blocks from pendingTransactions. + */ + @Test + public void testConcurrentBlockIngestionContainingSeenTransactionOverlappingSetRealStorage() throws IOException { + Path currentRelativePath = Paths.get(""); + tempdir = Files.createTempDirectory(currentRelativePath, TEMP_DIR); + db = new BlocksMapDb(tempdir.toAbsolutePath() + "/" + TEMP_FILE_ID, + tempdir.toAbsolutePath() + "/" + TEMP_FILE_HEIGHT); + Blocks blocks = db; + runTestConcurrentBlockIngestionContainingSeenTransactionOverlappingSet(blocks); + db.closeDb(); + FileUtils.deleteDirectory(new File(tempdir.toString())); + } +private void runTestValidatedAlreadyIngestedBlock(Blocks blocks){ + Identifiers seenEntities = mock(Identifiers.class); + Identifiers transactionIds = mock(Identifiers.class); + Transactions pendingTransactions = mock(Transactions.class); + + Block block = BlockFixture.newBlock(); + + IngestEngine ingestEngine = this.mockIngestEngineForEntities( + new ArrayList<>(List.of(block)), + seenEntities, + transactionIds, + pendingTransactions, + blocks); + + when(seenEntities.has(block.id())).thenReturn(true); // block is already ingested + if (MockUtil.isMock(blocks)){ + when(blocks.has(block.id())).thenReturn(false); + } // block is already ingested + + // action + ingestEngine.process(block); + + // verification + + verify(seenEntities, times(0)).add(block.id()); + verify(seenEntities, times(1)).has(block.id()); + for (Transaction tx : block.getTransactions()) { + verify(pendingTransactions, times(0)).has(tx.id()); + verify(transactionIds, times(0)).add(tx.id()); + } +} /** * Evaluates that when an already ingested validated block arrives at ingest engine, * the engine discards the block right away. */ @Test - public void testValidatedAlreadyIngestedBlock() { + public void testValidatedAlreadyIngestedBlockMockedStorage() { Blocks blocks = mock(Blocks.class); - Identifiers seenEntities = mock(Identifiers.class); - Identifiers transactionIds = mock(Identifiers.class); - Transactions pendingTransactions = mock(Transactions.class); - - Block block = BlockFixture.newBlock(); - - IngestEngine ingestEngine = this.mockIngestEngineForEntities( - new ArrayList<>(List.of(block)), - seenEntities, - transactionIds, - pendingTransactions, - blocks); - - when(seenEntities.has(block.id())).thenReturn(true); // block is already ingested - when(blocks.has(block.id())).thenReturn(true); // block is already ingested + runTestValidatedAlreadyIngestedBlock(blocks); + } - // action - ingestEngine.process(block); + /** + * Evaluates that when an already ingested validated block arrives at ingest engine, + * the engine discards the block right away. + */ + @Test + public void testValidatedAlreadyIngestedBlockRealStorage() throws IOException { + Path currentRelativePath = Paths.get(""); + tempdir = Files.createTempDirectory(currentRelativePath, TEMP_DIR); + db = new BlocksMapDb(tempdir.toAbsolutePath() + "/" + TEMP_FILE_ID, + tempdir.toAbsolutePath() + "/" + TEMP_FILE_HEIGHT); + Blocks blocks = db; + runTestValidatedAlreadyIngestedBlock(blocks); + db.closeDb(); + FileUtils.deleteDirectory(new File(tempdir.toString())); - // verification - verify(blocks, times(0)).add(block); - verify(seenEntities, times(0)).add(block.id()); - verify(seenEntities, times(1)).has(block.id()); - for (Transaction tx : block.getTransactions()) { - verify(pendingTransactions, times(0)).has(tx.id()); - verify(transactionIds, times(0)).add(tx.id()); - } } /** - * Evaluates that when a new validated transaction arrives at ingest engine, - * the engine adds hash of the transaction into its pending transactions' database. + * The method called by test validated transaction for mocked and real blokcs storage. + * @param blocks mocked or real block. */ - @Test - public void testValidatedTransaction() { - // R + private void runTestValidatedTransaction(Blocks blocks){ Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); - Blocks blocks = mock(Blocks.class); + ValidatedTransaction tx = ValidatedTransactionFixture.newValidatedTransaction(); @@ -430,53 +521,102 @@ public void testValidatedTransaction() { verifyTransactionHappyPathCalled(tx, seenEntities, transactionIds, pendingTransactions); } + /** + * Evaluates that when a new validated transaction arrives at ingest engine, with mocked blocks storage, + * the engine adds hash of the transaction into its pending transactions' database. + */ + @Test + public void testValidatedTransactionMockedStorage() { + // R + Blocks blocks = mock(Blocks.class); + runTestValidatedTransaction(blocks); + } + /** + * Evaluates that when a new validated transaction arrives at ingest engine, with real blocks storage, + * the engine adds hash of the transaction into its pending transactions' database. + */ + @Test + public void testValidatedTransactionRealStorage() throws IOException { + // R + Path currentRelativePath = Paths.get(""); + tempdir = Files.createTempDirectory(currentRelativePath, TEMP_DIR); + db = new BlocksMapDb(tempdir.toAbsolutePath() + "/" + TEMP_FILE_ID, + tempdir.toAbsolutePath() + "/" + TEMP_FILE_HEIGHT); + Blocks blocks = db; + runTestValidatedTransaction(blocks); + db.closeDb(); + FileUtils.deleteDirectory(new File(tempdir.toString())); + } + + /** + * The method called by test validated two transactions for mocked and real blocks storage. + * @param blocks mocked or real block. + */ +private void runTestValidatedTwoTransactions(Blocks blocks){ + Identifiers seenEntities = mock(Identifiers.class); + Identifiers transactionIds = mock(Identifiers.class); + Transactions pendingTransactions = mock(Transactions.class); + + + ValidatedTransaction tx1 = ValidatedTransactionFixture.newValidatedTransaction(); + ValidatedTransaction tx2 = ValidatedTransactionFixture.newValidatedTransaction(); + when(seenEntities.has(any(Identifier.class))).thenReturn(false); + when(transactionIds.has(any(Identifier.class))).thenReturn(false); + when(pendingTransactions.has(any(Identifier.class))).thenReturn(false); + + IngestEngine ingestEngine = this.mockIngestEngineForEntities( + new ArrayList<>(Arrays.asList(tx1, tx2)), + seenEntities, + transactionIds, + pendingTransactions, + blocks); + + // action + ingestEngine.process(tx1); + ingestEngine.process(tx2); + + // verification of tx1 + verifyTransactionHappyPathCalled(tx1, seenEntities, transactionIds, pendingTransactions); + + // verification of tx2 + verifyTransactionHappyPathCalled(tx2, seenEntities, transactionIds, pendingTransactions); +} /** * Evaluates that when two validated transactions arrives at ingest engine sequentially, * the engine adds the hashes of the transactions into its pending transactions' database. */ @Test - public void testValidatedTwoTransactions() { + public void testValidatedTwoTransactionsMockedBlocksStorage() { // R - Identifiers seenEntities = mock(Identifiers.class); - Identifiers transactionIds = mock(Identifiers.class); - Transactions pendingTransactions = mock(Transactions.class); Blocks blocks = mock(Blocks.class); - - ValidatedTransaction tx1 = ValidatedTransactionFixture.newValidatedTransaction(); - ValidatedTransaction tx2 = ValidatedTransactionFixture.newValidatedTransaction(); - when(seenEntities.has(any(Identifier.class))).thenReturn(false); - when(transactionIds.has(any(Identifier.class))).thenReturn(false); - when(pendingTransactions.has(any(Identifier.class))).thenReturn(false); - - IngestEngine ingestEngine = this.mockIngestEngineForEntities( - new ArrayList<>(Arrays.asList(tx1, tx2)), - seenEntities, - transactionIds, - pendingTransactions, - blocks); - - // action - ingestEngine.process(tx1); - ingestEngine.process(tx2); - - // verification of tx1 - verifyTransactionHappyPathCalled(tx1, seenEntities, transactionIds, pendingTransactions); - - // verification of tx2 - verifyTransactionHappyPathCalled(tx2, seenEntities, transactionIds, pendingTransactions); + runTestValidatedTwoTransactions(blocks); } - /** - * Evaluates that when two validated transactions arrive at ingest engine concurrently, + * Evaluates that when two validated transactions arrives at ingest engine sequentially, * the engine adds the hashes of the transactions into its pending transactions' database. */ @Test - public void testConcurrentValidatedTwoTransactions() { + public void testValidatedTwoTransactionsRealBlocksStorage() throws IOException { // R + Path currentRelativePath = Paths.get(""); + tempdir = Files.createTempDirectory(currentRelativePath, TEMP_DIR); + db = new BlocksMapDb(tempdir.toAbsolutePath() + "/" + TEMP_FILE_ID, + tempdir.toAbsolutePath() + "/" + TEMP_FILE_HEIGHT); + Blocks blocks = db; + runTestValidatedTwoTransactions(blocks); + db.closeDb(); + FileUtils.deleteDirectory(new File(tempdir.toString())); + } + + /** + * The method called by test validated two transactions concurrently for mocked and real blocks storage. + * @param blocks mocked or real block. + */ + private void runTestConcurrentValidatedTwoTransactions(Blocks blocks){ Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); - Blocks blocks = mock(Blocks.class); + ValidatedTransaction tx1 = ValidatedTransactionFixture.newValidatedTransaction(); ValidatedTransaction tx2 = ValidatedTransactionFixture.newValidatedTransaction(); @@ -498,19 +638,43 @@ public void testConcurrentValidatedTwoTransactions() { // verification of tx2 verifyTransactionHappyPathCalled(tx2, seenEntities, transactionIds, pendingTransactions); } + /** + * Evaluates that when two validated transactions arrive at ingest engine concurrently, with mocked blocks storage, + * the engine adds the hashes of the transactions into its pending transactions' database. + */ + @Test + public void testConcurrentValidatedTwoTransactionsMockedBlockStorage() { + // R + Blocks blocks = mock(Blocks.class); + runTestConcurrentValidatedTwoTransactions(blocks); + + } /** - * Evaluates that when two same validated transactions arrive at ingest engine sequentially, - * the engine adds the hash of the first transaction into its pending transactions' database. - * The engine also discards the second transaction right away. + * Evaluates that when two validated transactions arrive at ingest engine concurrently, with real blocks storage, + * the engine adds the hashes of the transactions into its pending transactions' database. */ @Test - public void testValidatedSameTwoTransactions() { + public void testConcurrentValidatedTwoTransactionsRealBlockStorage() throws IOException { // R + Path currentRelativePath = Paths.get(""); + tempdir = Files.createTempDirectory(currentRelativePath, TEMP_DIR); + db = new BlocksMapDb(tempdir.toAbsolutePath() + "/" + TEMP_FILE_ID, + tempdir.toAbsolutePath() + "/" + TEMP_FILE_HEIGHT); + Blocks blocks = db; + runTestConcurrentValidatedTwoTransactions(blocks); + db.closeDb(); + FileUtils.deleteDirectory(new File(tempdir.toString())); + } + + /** + * The method called by test validated same two transactions for mocked and real blocks storage. + * @param blocks mocked or real blocks + */ + private void runTestValidatedSameTwoTransactions(Blocks blocks){ Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); - Blocks blocks = mock(Blocks.class); ValidatedTransaction tx = ValidatedTransactionFixture.newValidatedTransaction(); when(seenEntities.has(tx.id())).thenReturn(false); @@ -535,18 +699,44 @@ public void testValidatedSameTwoTransactions() { verifyTransactionHappyPathCalled(tx, seenEntities, transactionIds, pendingTransactions); verify(seenEntities, times(2)).has(tx.id()); } + /** + * Evaluates that when two same validated transactions arrive at ingest engine sequentially, with mocked blocks, + * the engine adds the hash of the first transaction into its pending transactions' database. + * The engine also discards the second transaction right away. + */ + @Test + public void testValidatedSameTwoTransactionsMockedBlocks() { + // R + Blocks blocks = mock(Blocks.class); + runTestValidatedSameTwoTransactions(blocks); + } /** - * Evaluates that when a validated transaction which is already in transaction identifiers arrives at ingest engine, - * the engine discards the transaction. + * Evaluates that when two same validated transactions arrive at ingest engine sequentially, with real blocks, + * the engine adds the hash of the first transaction into its pending transactions' database. + * The engine also discards the second transaction right away. */ @Test - public void testValidatedTransactionAlreadyInTransactionIdStorage() { + public void testValidatedSameTwoTransactionsRealBlocks() throws IOException { // R + Path currentRelativePath = Paths.get(""); + tempdir = Files.createTempDirectory(currentRelativePath, TEMP_DIR); + db = new BlocksMapDb(tempdir.toAbsolutePath() + "/" + TEMP_FILE_ID, + tempdir.toAbsolutePath() + "/" + TEMP_FILE_HEIGHT); + Blocks blocks = db; + runTestValidatedSameTwoTransactions(blocks); + db.closeDb(); + FileUtils.deleteDirectory(new File(tempdir.toString())); + } + + /** + * The method called by test validated transactions already in transaction id storage for mocked and real blocks + * @param blocks mocked or real blocks + */ + private void runTestValidatedTransactionAlreadyInTransactionIdStorage(Blocks blocks){ Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); - Blocks blocks = mock(Blocks.class); ValidatedTransaction tx = ValidatedTransactionFixture.newValidatedTransaction(); @@ -572,6 +762,34 @@ public void testValidatedTransactionAlreadyInTransactionIdStorage() { verify(pendingTransactions, times(0)).add(tx); } + /** + * Evaluates that when a validated transaction which is already in transaction identifiers arrives at ingest engine, + * with mocked blocks storage, the engine discards the transaction. + */ + @Test + public void testValidatedTransactionAlreadyInTransactionIdStorageMockedBlocks() { + // R + Blocks blocks = mock(Blocks.class); + runTestValidatedTransactionAlreadyInTransactionIdStorage(blocks); + } + + /** + * Evaluates that when a validated transaction which is already in transaction identifiers arrives at ingest engine, + * with real blocks storage, the engine discards the transaction. + */ + @Test + public void testValidatedTransactionAlreadyInTransactionIdStorageRealBlocks() throws IOException { + // R + Path currentRelativePath = Paths.get(""); + tempdir = Files.createTempDirectory(currentRelativePath, TEMP_DIR); + db = new BlocksMapDb(tempdir.toAbsolutePath() + "/" + TEMP_FILE_ID, + tempdir.toAbsolutePath() + "/" + TEMP_FILE_HEIGHT); + Blocks blocks = db; + runTestValidatedTransactionAlreadyInTransactionIdStorage(blocks); + db.closeDb(); + FileUtils.deleteDirectory(new File(tempdir.toString())); + } + /** * Evaluates that when an entity that is neither a validated block nor a validated transaction * arrives at ingest engine, the engine throws IllegalArgumentException. @@ -598,18 +816,13 @@ public void testNeitherBlockNorTransaction() { } /** - * Evaluates that when a validated block and a validated transaction arrives at ingest engine concurrently, - * the engine adds the block to its block storage database. - * The engine also adds hash of all the transactions of block - * and the hash of the validated transaction into its "transactions" database. + * the method called by test concurrent validated transaction and block nonoverlapping for mocked and real versions. + * @param blocks mocked or real blocks */ - @Test - public void testConcurrentValidatedTransactionAndBlockNonOverlapping() { - // R + private void runTestConcurrentValidatedTransactionAndBlockNonOverlapping(Blocks blocks){ Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); - Blocks blocks = mock(Blocks.class); ValidatedTransaction validatedTx = ValidatedTransactionFixture.newValidatedTransaction(); Block block = BlockFixture.newBlock(); @@ -633,20 +846,47 @@ public void testConcurrentValidatedTransactionAndBlockNonOverlapping() { // no transaction should be removed from pending ones verify(pendingTransactions, times(0)).remove(any(Identifier.class)); } + /** + * Evaluates that when a validated block and a validated transaction arrives at ingest engine concurrently, + * the engine adds the block to its block storage database. + * The engine also adds hash of all the transactions of block + * and the hash of the validated transaction into its "transactions" database. + */ + @Test + public void testConcurrentValidatedTransactionAndBlockNonOverlappingMockedBlocks() { + // R + Blocks blocks = mock(Blocks.class); + runTestConcurrentValidatedTransactionAndBlockNonOverlapping(blocks); + } /** - * Evaluates that when a validated block and a validated transaction (which the block contains) - * arrive at ingest engine (block first), the engine adds the block to its block storage database. - * The engine also adds hash of all the transactions of block into its "transactions" database. - * Hence, when the transaction that also included in the block comes next, it is never added to pending - * transactions' database. + * Evaluates that when a validated block and a validated transaction arrives at ingest engine concurrently, + * the engine adds the block to its block storage database. + * The engine also adds hash of all the transactions of block + * and the hash of the validated transaction into its "transactions" database. */ @Test - public void testProcessBlockAndIncludedTransaction_BlockFirst() { + public void testConcurrentValidatedTransactionAndBlockNonOverlappingRealBlocks() throws IOException { + // R + Path currentRelativePath = Paths.get(""); + tempdir = Files.createTempDirectory(currentRelativePath, TEMP_DIR); + db = new BlocksMapDb(tempdir.toAbsolutePath() + "/" + TEMP_FILE_ID, + tempdir.toAbsolutePath() + "/" + TEMP_FILE_HEIGHT); + Blocks blocks = db; + runTestConcurrentValidatedTransactionAndBlockNonOverlapping(blocks); + db.closeDb(); + FileUtils.deleteDirectory(new File(tempdir.toString())); + } + + /** + * The method called by tests processBlockAndIncludedTransaction_BlockFirst for mocked and real block storages. + * @param blocks mocked or real blocks. + */ + private void runTestProcessBlockAndIncludedTransaction_BlockFirst(Blocks blocks){ Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); - Blocks blocks = mock(Blocks.class); + Block block = BlockFixture.newBlock(); ValidatedTransaction validatedTx = block.getTransactions()[0]; // the transaction is in the block @@ -667,7 +907,9 @@ public void testProcessBlockAndIncludedTransaction_BlockFirst() { ingestEngine.process(validatedTx); // verification for block - verify(blocks, times(1)).add(block); + if (MockUtil.isMock(blocks)){ + verify(blocks, times(1)).add(block); + } verify(seenEntities, times(1)).add(block.id()); for (Transaction tx : block.getTransactions()) { verify(transactionIds, times(1)).add(tx.id()); @@ -678,21 +920,46 @@ public void testProcessBlockAndIncludedTransaction_BlockFirst() { verify(transactionIds, times(1)).has(validatedTx.id()); verify(pendingTransactions, times(0)).add(validatedTx); } + /** + * Evaluates that when a validated block and a validated transaction (which the block contains) + * arrive at ingest engine (block first), the engine adds the block to its mocked block storage database. + * The engine also adds hash of all the transactions of block into its "transactions" database. + * Hence, when the transaction that also included in the block comes next, it is never added to pending + * transactions' database. + */ + @Test + public void testProcessBlockAndIncludedTransaction_BlockFirstMockedBlocks() { + Blocks blocks = mock(Blocks.class); + runTestProcessBlockAndIncludedTransaction_BlockFirst(blocks); + } /** * Evaluates that when a validated block and a validated transaction (which the block contains) - * arrive at ingest engine (transaction first), the engine adds the block to its block storage database. + * arrive at ingest engine (block first), the engine adds the block to its real block storage database. * The engine also adds hash of all the transactions of block into its "transactions" database. * Hence, when the transaction that also included in the block comes next, it is never added to pending * transactions' database. */ @Test - public void testProcessBlockAndIncludedTransaction_TransactionFirst() { - // R + public void testProcessBlockAndIncludedTransaction_BlockFirstRealBlocks() throws IOException { + Path currentRelativePath = Paths.get(""); + tempdir = Files.createTempDirectory(currentRelativePath, TEMP_DIR); + db = new BlocksMapDb(tempdir.toAbsolutePath() + "/" + TEMP_FILE_ID, + tempdir.toAbsolutePath() + "/" + TEMP_FILE_HEIGHT); + Blocks blocks = db; + runTestProcessBlockAndIncludedTransaction_BlockFirst(blocks); + db.closeDb(); + FileUtils.deleteDirectory(new File(tempdir.toString())); + } + + /** + * The method called by tests process block and included transaction and transaction first for mocked and real blocks. + * @param blocks real or mocked blocks. + */ + private void runTestProcessBlockAndIncludedTransaction_TransactionFirst(Blocks blocks){ Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); - Blocks blocks = mock(Blocks.class); Block block = BlockFixture.newBlock(); ValidatedTransaction validatedTx = block.getTransactions()[0]; // the transaction is in the block @@ -714,7 +981,9 @@ public void testProcessBlockAndIncludedTransaction_TransactionFirst() { ingestEngine.process(block); // verification for block - verify(blocks, times(1)).add(block); + if (MockUtil.isMock(blocks)){ + verify(blocks, times(1)).add(block); + } verify(seenEntities, times(1)).add(block.id()); for (Transaction tx : block.getTransactions()) { verify(transactionIds, times(1)).add(tx.id()); @@ -728,6 +997,40 @@ public void testProcessBlockAndIncludedTransaction_TransactionFirst() { verify(pendingTransactions, times(1)).remove(validatedTx.id()); } + /** + * Evaluates that when a validated block and a validated transaction (which the block contains) + * arrive at ingest engine (transaction first), the engine adds the block to its mocked block storage database. + * The engine also adds hash of all the transactions of block into its "transactions" database. + * Hence, when the transaction that also included in the block comes next, it is never added to pending + * transactions' database. + */ + @Test + public void testProcessBlockAndIncludedTransaction_TransactionFirstMockedBlocks() { + // R + Blocks blocks = mock(Blocks.class); + runTestProcessBlockAndIncludedTransaction_TransactionFirst(blocks); + } + + /** + * Evaluates that when a validated block and a validated transaction (which the block contains) + * arrive at ingest engine (transaction first), the engine adds the block to its real block storage database. + * The engine also adds hash of all the transactions of block into its "transactions" database. + * Hence, when the transaction that also included in the block comes next, it is never added to pending + * transactions' database. + */ + @Test + public void testProcessBlockAndIncludedTransaction_TransactionFirstRealBlocks() throws IOException { + // R + Path currentRelativePath = Paths.get(""); + tempdir = Files.createTempDirectory(currentRelativePath, TEMP_DIR); + db = new BlocksMapDb(tempdir.toAbsolutePath() + "/" + TEMP_FILE_ID, + tempdir.toAbsolutePath() + "/" + TEMP_FILE_HEIGHT); + Blocks blocks = db; + runTestProcessBlockAndIncludedTransaction_TransactionFirst(blocks); + db.closeDb(); + FileUtils.deleteDirectory(new File(tempdir.toString())); + } + /** * Verifies mocked storage components have been called with expected parameters on an * expected number of times for block happy path, i.e., block is added to the blocks storage, and its id is From ddae10e9bb9db586397617b828dc0785ba098197 Mon Sep 17 00:00:00 2001 From: akucukoduk16 Date: Fri, 19 Aug 2022 15:28:26 +0300 Subject: [PATCH 04/13] checkstyle fix --- .../protocol/engines/IngestEngineTest.java | 189 ++++++++++-------- 1 file changed, 110 insertions(+), 79 deletions(-) diff --git a/src/test/java/protocol/engines/IngestEngineTest.java b/src/test/java/protocol/engines/IngestEngineTest.java index 54c085fd..4944392c 100644 --- a/src/test/java/protocol/engines/IngestEngineTest.java +++ b/src/test/java/protocol/engines/IngestEngineTest.java @@ -14,12 +14,12 @@ import static org.mockito.Mockito.*; -import org.mockito.internal.util.MockUtil; import model.Entity; import model.codec.EntityType; import model.crypto.PublicKey; import model.crypto.Signature; import model.lightchain.*; +import org.mockito.internal.util.MockUtil; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.testcontainers.shaded.org.apache.commons.io.FileUtils; @@ -45,6 +45,7 @@ public class IngestEngineTest { private static final String TEMP_FILE_HEIGHT = "tempfileHEIGHT.db"; private Path tempdir; private BlocksMapDb db; + /** * Evaluates that when a new validated block arrives at ingest engine, * the engine adds the block to its mocked block storage database. @@ -100,11 +101,13 @@ public void testValidatedTwoBlocksRealStorage() throws IOException { db.closeDb(); FileUtils.deleteDirectory(new File(tempdir.toString())); } + /** * The method called by test validated single block for mocked and real versions. + * * @param blocks mocked or real block. */ - public void runTestValidatedTwoBlocks(Blocks blocks){ + public void runTestValidatedTwoBlocks(Blocks blocks) { Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); @@ -141,6 +144,7 @@ public void testValidatedTwoBlocksConcurrentlyMockedBlocks() { Blocks blocks = mock(Blocks.class); runTestValidatedTwoBlocksConcurrently(blocks); } + /** * Evaluates that when two validated blocks arrive at ingest engine concurrently, * the engine adds the blocks to its real block storage database. @@ -160,9 +164,10 @@ public void testValidatedTwoBlocksConcurrentlyRealBlocks() throws IOException { /** * The method called by test validated two blocks for mocked and real versions concurrently. + * * @param blocks mocked or real block. */ - public void runTestValidatedTwoBlocksConcurrently(Blocks blocks){ + public void runTestValidatedTwoBlocksConcurrently(Blocks blocks) { Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); @@ -198,6 +203,7 @@ public void testValidatedSameTwoBlocksMockedStorage() { Blocks blocks = mock(Blocks.class); runTestValidatedSameTwoBlocks(blocks); } + /** * Evaluates that when two same validated blocks arrive at ingest engine (second one should be ignored), * the engine adds the blocks to its real block storage database. @@ -217,9 +223,10 @@ public void testValidatedSameTwoBlocksRealStorage() throws IOException { /** * The method called by test validated two blocks for mocked and real versions concurrently. + * * @param blocks mocked or real blocks. */ - public void runTestValidatedSameTwoBlocks(Blocks blocks){ + public void runTestValidatedSameTwoBlocks(Blocks blocks) { Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); @@ -275,9 +282,10 @@ public void testValidatedBlockContainingPendingTransactionRealStorage() throws I /** * The method called by test validated blocks containing pending transactions for mocked and real versions concurrently. + * * @param blocks mocked or real block. */ - public void runTestValidatedBlockContainingPendingTransaction(Blocks blocks){ + public void runTestValidatedBlockContainingPendingTransaction(Blocks blocks) { Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); @@ -304,6 +312,7 @@ public void runTestValidatedBlockContainingPendingTransaction(Blocks blocks){ verify(pendingTransactions, times(1)).remove(tx.id()); } } + /** * Evaluates that when two new validated blocks (with a shared transactions in pendingTx, disjoint set) * arrive at ingest engine, the engine adds the blocks to its real block storage database. @@ -315,6 +324,7 @@ public void testConcurrentBlockIngestionContainingSeenTransactionDisjointSetMock Blocks blocks = mock(Blocks.class); runTestConcurrentBlockIngestionContainingSeenTransactionDisjointSet(blocks); } + /** * Evaluates that when two new validated blocks (with a shared transactions in pendingTx, disjoint set) * arrive at ingest engine, the engine adds the blocks to its real block storage database. @@ -336,9 +346,10 @@ public void testConcurrentBlockIngestionContainingSeenTransactionDisjointSetReal /** * The method called by test concurrent block ingestion containing seen transaction disjoint set mocked and real versions. + * * @param blocks mocked or real block. */ - private void runTestConcurrentBlockIngestionContainingSeenTransactionDisjointSet(Blocks blocks){ + private void runTestConcurrentBlockIngestionContainingSeenTransactionDisjointSet(Blocks blocks) { Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); @@ -375,9 +386,10 @@ private void runTestConcurrentBlockIngestionContainingSeenTransactionDisjointSet /** * The method called by test concurrent block ingestion containing seen transaction overlapping set mocked and real versions. + * * @param blocks mocked or real block. */ - private void runTestConcurrentBlockIngestionContainingSeenTransactionOverlappingSet(Blocks blocks){ + private void runTestConcurrentBlockIngestionContainingSeenTransactionOverlappingSet(Blocks blocks) { Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); @@ -410,6 +422,7 @@ private void runTestConcurrentBlockIngestionContainingSeenTransactionOverlapping verify(pendingTransactions, times(1)).remove(tx.id()); } } + /** * Evaluates that when two new validated blocks (with shared transactions in pendingTx, overlapping set) * arrive at ingest engine, the engine adds the blocks to its mocked block storage database. @@ -437,37 +450,39 @@ public void testConcurrentBlockIngestionContainingSeenTransactionOverlappingSetR db.closeDb(); FileUtils.deleteDirectory(new File(tempdir.toString())); } -private void runTestValidatedAlreadyIngestedBlock(Blocks blocks){ - Identifiers seenEntities = mock(Identifiers.class); - Identifiers transactionIds = mock(Identifiers.class); - Transactions pendingTransactions = mock(Transactions.class); - - Block block = BlockFixture.newBlock(); - - IngestEngine ingestEngine = this.mockIngestEngineForEntities( - new ArrayList<>(List.of(block)), - seenEntities, - transactionIds, - pendingTransactions, - blocks); - - when(seenEntities.has(block.id())).thenReturn(true); // block is already ingested - if (MockUtil.isMock(blocks)){ - when(blocks.has(block.id())).thenReturn(false); - } // block is already ingested - - // action - ingestEngine.process(block); - - // verification - - verify(seenEntities, times(0)).add(block.id()); - verify(seenEntities, times(1)).has(block.id()); - for (Transaction tx : block.getTransactions()) { - verify(pendingTransactions, times(0)).has(tx.id()); - verify(transactionIds, times(0)).add(tx.id()); + + private void runTestValidatedAlreadyIngestedBlock(Blocks blocks) { + Identifiers seenEntities = mock(Identifiers.class); + Identifiers transactionIds = mock(Identifiers.class); + Transactions pendingTransactions = mock(Transactions.class); + + Block block = BlockFixture.newBlock(); + + IngestEngine ingestEngine = this.mockIngestEngineForEntities( + new ArrayList<>(List.of(block)), + seenEntities, + transactionIds, + pendingTransactions, + blocks); + + when(seenEntities.has(block.id())).thenReturn(true); // block is already ingested + if (MockUtil.isMock(blocks)) { + when(blocks.has(block.id())).thenReturn(false); + } // block is already ingested + + // action + ingestEngine.process(block); + + // verification + + verify(seenEntities, times(0)).add(block.id()); + verify(seenEntities, times(1)).has(block.id()); + for (Transaction tx : block.getTransactions()) { + verify(pendingTransactions, times(0)).has(tx.id()); + verify(transactionIds, times(0)).add(tx.id()); + } } -} + /** * Evaluates that when an already ingested validated block arrives at ingest engine, * the engine discards the block right away. @@ -497,9 +512,10 @@ public void testValidatedAlreadyIngestedBlockRealStorage() throws IOException { /** * The method called by test validated transaction for mocked and real blokcs storage. + * * @param blocks mocked or real block. */ - private void runTestValidatedTransaction(Blocks blocks){ + private void runTestValidatedTransaction(Blocks blocks) { Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); @@ -531,6 +547,7 @@ public void testValidatedTransactionMockedStorage() { Blocks blocks = mock(Blocks.class); runTestValidatedTransaction(blocks); } + /** * Evaluates that when a new validated transaction arrives at ingest engine, with real blocks storage, * the engine adds hash of the transaction into its pending transactions' database. @@ -550,37 +567,39 @@ public void testValidatedTransactionRealStorage() throws IOException { /** * The method called by test validated two transactions for mocked and real blocks storage. + * * @param blocks mocked or real block. */ -private void runTestValidatedTwoTransactions(Blocks blocks){ - Identifiers seenEntities = mock(Identifiers.class); - Identifiers transactionIds = mock(Identifiers.class); - Transactions pendingTransactions = mock(Transactions.class); - - - ValidatedTransaction tx1 = ValidatedTransactionFixture.newValidatedTransaction(); - ValidatedTransaction tx2 = ValidatedTransactionFixture.newValidatedTransaction(); - when(seenEntities.has(any(Identifier.class))).thenReturn(false); - when(transactionIds.has(any(Identifier.class))).thenReturn(false); - when(pendingTransactions.has(any(Identifier.class))).thenReturn(false); - - IngestEngine ingestEngine = this.mockIngestEngineForEntities( - new ArrayList<>(Arrays.asList(tx1, tx2)), - seenEntities, - transactionIds, - pendingTransactions, - blocks); - - // action - ingestEngine.process(tx1); - ingestEngine.process(tx2); - - // verification of tx1 - verifyTransactionHappyPathCalled(tx1, seenEntities, transactionIds, pendingTransactions); - - // verification of tx2 - verifyTransactionHappyPathCalled(tx2, seenEntities, transactionIds, pendingTransactions); -} + private void runTestValidatedTwoTransactions(Blocks blocks) { + Identifiers seenEntities = mock(Identifiers.class); + Identifiers transactionIds = mock(Identifiers.class); + Transactions pendingTransactions = mock(Transactions.class); + + + ValidatedTransaction tx1 = ValidatedTransactionFixture.newValidatedTransaction(); + ValidatedTransaction tx2 = ValidatedTransactionFixture.newValidatedTransaction(); + when(seenEntities.has(any(Identifier.class))).thenReturn(false); + when(transactionIds.has(any(Identifier.class))).thenReturn(false); + when(pendingTransactions.has(any(Identifier.class))).thenReturn(false); + + IngestEngine ingestEngine = this.mockIngestEngineForEntities( + new ArrayList<>(Arrays.asList(tx1, tx2)), + seenEntities, + transactionIds, + pendingTransactions, + blocks); + + // action + ingestEngine.process(tx1); + ingestEngine.process(tx2); + + // verification of tx1 + verifyTransactionHappyPathCalled(tx1, seenEntities, transactionIds, pendingTransactions); + + // verification of tx2 + verifyTransactionHappyPathCalled(tx2, seenEntities, transactionIds, pendingTransactions); + } + /** * Evaluates that when two validated transactions arrives at ingest engine sequentially, * the engine adds the hashes of the transactions into its pending transactions' database. @@ -591,6 +610,7 @@ public void testValidatedTwoTransactionsMockedBlocksStorage() { Blocks blocks = mock(Blocks.class); runTestValidatedTwoTransactions(blocks); } + /** * Evaluates that when two validated transactions arrives at ingest engine sequentially, * the engine adds the hashes of the transactions into its pending transactions' database. @@ -610,9 +630,10 @@ public void testValidatedTwoTransactionsRealBlocksStorage() throws IOException { /** * The method called by test validated two transactions concurrently for mocked and real blocks storage. + * * @param blocks mocked or real block. */ - private void runTestConcurrentValidatedTwoTransactions(Blocks blocks){ + private void runTestConcurrentValidatedTwoTransactions(Blocks blocks) { Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); @@ -638,6 +659,7 @@ private void runTestConcurrentValidatedTwoTransactions(Blocks blocks){ // verification of tx2 verifyTransactionHappyPathCalled(tx2, seenEntities, transactionIds, pendingTransactions); } + /** * Evaluates that when two validated transactions arrive at ingest engine concurrently, with mocked blocks storage, * the engine adds the hashes of the transactions into its pending transactions' database. @@ -669,9 +691,10 @@ public void testConcurrentValidatedTwoTransactionsRealBlockStorage() throws IOEx /** * The method called by test validated same two transactions for mocked and real blocks storage. + * * @param blocks mocked or real blocks */ - private void runTestValidatedSameTwoTransactions(Blocks blocks){ + private void runTestValidatedSameTwoTransactions(Blocks blocks) { Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); @@ -699,6 +722,7 @@ private void runTestValidatedSameTwoTransactions(Blocks blocks){ verifyTransactionHappyPathCalled(tx, seenEntities, transactionIds, pendingTransactions); verify(seenEntities, times(2)).has(tx.id()); } + /** * Evaluates that when two same validated transactions arrive at ingest engine sequentially, with mocked blocks, * the engine adds the hash of the first transaction into its pending transactions' database. @@ -731,9 +755,10 @@ public void testValidatedSameTwoTransactionsRealBlocks() throws IOException { /** * The method called by test validated transactions already in transaction id storage for mocked and real blocks + * * @param blocks mocked or real blocks */ - private void runTestValidatedTransactionAlreadyInTransactionIdStorage(Blocks blocks){ + private void runTestValidatedTransactionAlreadyInTransactionIdStorage(Blocks blocks) { Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); @@ -817,9 +842,10 @@ public void testNeitherBlockNorTransaction() { /** * the method called by test concurrent validated transaction and block nonoverlapping for mocked and real versions. + * * @param blocks mocked or real blocks */ - private void runTestConcurrentValidatedTransactionAndBlockNonOverlapping(Blocks blocks){ + private void runTestConcurrentValidatedTransactionAndBlockNonOverlapping(Blocks blocks) { Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); @@ -846,6 +872,7 @@ private void runTestConcurrentValidatedTransactionAndBlockNonOverlapping(Blocks // no transaction should be removed from pending ones verify(pendingTransactions, times(0)).remove(any(Identifier.class)); } + /** * Evaluates that when a validated block and a validated transaction arrives at ingest engine concurrently, * the engine adds the block to its block storage database. @@ -880,9 +907,10 @@ public void testConcurrentValidatedTransactionAndBlockNonOverlappingRealBlocks() /** * The method called by tests processBlockAndIncludedTransaction_BlockFirst for mocked and real block storages. + * * @param blocks mocked or real blocks. */ - private void runTestProcessBlockAndIncludedTransaction_BlockFirst(Blocks blocks){ + private void runTestProcessBlockAndIncludedTransaction_BlockFirst(Blocks blocks) { Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); @@ -907,7 +935,7 @@ private void runTestProcessBlockAndIncludedTransaction_BlockFirst(Blocks blocks) ingestEngine.process(validatedTx); // verification for block - if (MockUtil.isMock(blocks)){ + if (MockUtil.isMock(blocks)) { verify(blocks, times(1)).add(block); } verify(seenEntities, times(1)).add(block.id()); @@ -920,6 +948,7 @@ private void runTestProcessBlockAndIncludedTransaction_BlockFirst(Blocks blocks) verify(transactionIds, times(1)).has(validatedTx.id()); verify(pendingTransactions, times(0)).add(validatedTx); } + /** * Evaluates that when a validated block and a validated transaction (which the block contains) * arrive at ingest engine (block first), the engine adds the block to its mocked block storage database. @@ -954,9 +983,10 @@ public void testProcessBlockAndIncludedTransaction_BlockFirstRealBlocks() throws /** * The method called by tests process block and included transaction and transaction first for mocked and real blocks. + * * @param blocks real or mocked blocks. */ - private void runTestProcessBlockAndIncludedTransaction_TransactionFirst(Blocks blocks){ + private void runTestProcessBlockAndIncludedTransaction_TransactionFirst(Blocks blocks) { Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); @@ -981,7 +1011,7 @@ private void runTestProcessBlockAndIncludedTransaction_TransactionFirst(Blocks b ingestEngine.process(block); // verification for block - if (MockUtil.isMock(blocks)){ + if (MockUtil.isMock(blocks)) { verify(blocks, times(1)).add(block); } verify(seenEntities, times(1)).add(block.id()); @@ -1049,7 +1079,7 @@ private void verifyBlockHappyPathCalled( Transactions pendingTx, Identifiers txIds, Identifiers seenEntities) { - if (MockUtil.isMock(blocks)){ + if (MockUtil.isMock(blocks)) { verify(blocks, times(1)).add(block); } @@ -1119,7 +1149,7 @@ private IngestEngine mockIngestEngineForEntities( Block block = (Block) e; when(state.atBlockId(block.getPreviousBlockId())).thenReturn(snapshot); - if (MockUtil.isMock(blocks)){ + if (MockUtil.isMock(blocks)) { when(blocks.has(block.id())).thenReturn(false); } for (Transaction tx : block.getTransactions()) { @@ -1203,15 +1233,16 @@ private void mockAssignment(AssignerInf assigner, Entity e, Snapshot snapshot) { /** * The method called by test validated single block for mocked and real versions. + * * @param blocks mocked or real block. */ - private void runTestValidatedSingleBlock(Blocks blocks){ + private void runTestValidatedSingleBlock(Blocks blocks) { Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); Block block = BlockFixture.newBlock(); when(seenEntities.has(block.id())).thenReturn(false); - if (MockUtil.isMock(blocks)){ + if (MockUtil.isMock(blocks)) { when(blocks.has(block.id())).thenReturn(false); } IngestEngine ingestEngine = this.mockIngestEngineForEntities( From 003007c954c4fc5c8571ecb266bd7d11f3425fdf Mon Sep 17 00:00:00 2001 From: akucukoduk16 Date: Fri, 19 Aug 2022 15:31:40 +0300 Subject: [PATCH 05/13] checkstyle fix --- src/test/java/protocol/engines/IngestEngineTest.java | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/test/java/protocol/engines/IngestEngineTest.java b/src/test/java/protocol/engines/IngestEngineTest.java index 4944392c..fe72e1ec 100644 --- a/src/test/java/protocol/engines/IngestEngineTest.java +++ b/src/test/java/protocol/engines/IngestEngineTest.java @@ -19,9 +19,9 @@ import model.crypto.PublicKey; import model.crypto.Signature; import model.lightchain.*; -import org.mockito.internal.util.MockUtil; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import org.mockito.internal.util.MockUtil; import org.testcontainers.shaded.org.apache.commons.io.FileUtils; import protocol.Parameters; import protocol.assigner.AssignerInf; @@ -520,7 +520,6 @@ private void runTestValidatedTransaction(Blocks blocks) { Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); - ValidatedTransaction tx = ValidatedTransactionFixture.newValidatedTransaction(); IngestEngine ingestEngine = this.mockIngestEngineForEntities( @@ -575,7 +574,6 @@ private void runTestValidatedTwoTransactions(Blocks blocks) { Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); - ValidatedTransaction tx1 = ValidatedTransactionFixture.newValidatedTransaction(); ValidatedTransaction tx2 = ValidatedTransactionFixture.newValidatedTransaction(); when(seenEntities.has(any(Identifier.class))).thenReturn(false); @@ -638,7 +636,6 @@ private void runTestConcurrentValidatedTwoTransactions(Blocks blocks) { Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); - ValidatedTransaction tx1 = ValidatedTransactionFixture.newValidatedTransaction(); ValidatedTransaction tx2 = ValidatedTransactionFixture.newValidatedTransaction(); @@ -756,7 +753,7 @@ public void testValidatedSameTwoTransactionsRealBlocks() throws IOException { /** * The method called by test validated transactions already in transaction id storage for mocked and real blocks * - * @param blocks mocked or real blocks + * @param blocks mocked or real blocks. */ private void runTestValidatedTransactionAlreadyInTransactionIdStorage(Blocks blocks) { Identifiers seenEntities = mock(Identifiers.class); @@ -841,7 +838,7 @@ public void testNeitherBlockNorTransaction() { } /** - * the method called by test concurrent validated transaction and block nonoverlapping for mocked and real versions. + * The method called by test concurrent validated transaction and block nonoverlapping for mocked and real versions. * * @param blocks mocked or real blocks */ @@ -915,7 +912,6 @@ private void runTestProcessBlockAndIncludedTransaction_BlockFirst(Blocks blocks) Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); - Block block = BlockFixture.newBlock(); ValidatedTransaction validatedTx = block.getTransactions()[0]; // the transaction is in the block From 2f92a4ecc614c68ff4ef82fd026f36493c3b248b Mon Sep 17 00:00:00 2001 From: akucukoduk16 Date: Fri, 19 Aug 2022 15:33:20 +0300 Subject: [PATCH 06/13] checkstyle fix --- src/test/java/protocol/engines/IngestEngineTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/protocol/engines/IngestEngineTest.java b/src/test/java/protocol/engines/IngestEngineTest.java index fe72e1ec..bd36ab27 100644 --- a/src/test/java/protocol/engines/IngestEngineTest.java +++ b/src/test/java/protocol/engines/IngestEngineTest.java @@ -751,7 +751,7 @@ public void testValidatedSameTwoTransactionsRealBlocks() throws IOException { } /** - * The method called by test validated transactions already in transaction id storage for mocked and real blocks + * The method called by test validated transactions already in transaction id storage for mocked and real blocks. * * @param blocks mocked or real blocks. */ From 558fb51e4b4787e68ad2c64a5e610f8903f171fc Mon Sep 17 00:00:00 2001 From: akucukoduk16 Date: Thu, 25 Aug 2022 10:42:05 +0300 Subject: [PATCH 07/13] isolated helper methods from the cases whether block storage mocked or real. --- .../protocol/engines/IngestEngineTest.java | 181 +++++++++++------- 1 file changed, 108 insertions(+), 73 deletions(-) diff --git a/src/test/java/protocol/engines/IngestEngineTest.java b/src/test/java/protocol/engines/IngestEngineTest.java index bd36ab27..bad592e8 100644 --- a/src/test/java/protocol/engines/IngestEngineTest.java +++ b/src/test/java/protocol/engines/IngestEngineTest.java @@ -21,7 +21,6 @@ import model.lightchain.*; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -import org.mockito.internal.util.MockUtil; import org.testcontainers.shaded.org.apache.commons.io.FileUtils; import protocol.Parameters; import protocol.assigner.AssignerInf; @@ -54,7 +53,10 @@ public class IngestEngineTest { @Test public void testValidatedSingleBlockMockedStorage() { Blocks blocks = mock(Blocks.class); - runTestValidatedSingleBlock(blocks); + Block block = BlockFixture.newBlock(); + when(blocks.has(block.id())).thenReturn(false); + runTestValidatedSingleBlock(blocks,block); + verify(blocks, times(1)).add(block); } /** @@ -69,7 +71,9 @@ public void testValidatedSingleBlockRealStorage() throws IOException { db = new BlocksMapDb(tempdir.toAbsolutePath() + "/" + TEMP_FILE_ID, tempdir.toAbsolutePath() + "/" + TEMP_FILE_HEIGHT); Blocks blocks = db; - runTestValidatedSingleBlock(blocks); + Block block = BlockFixture.newBlock(); + runTestValidatedSingleBlock(blocks,block); + Assertions.assertTrue(blocks.has(block.id())); db.closeDb(); FileUtils.deleteDirectory(new File(tempdir.toString())); } @@ -82,7 +86,17 @@ public void testValidatedSingleBlockRealStorage() throws IOException { @Test public void testValidatedTwoBlocksMockedStorage() { Blocks blocks = mock(Blocks.class); - runTestValidatedTwoBlocks(blocks); + + Block block1 = BlockFixture.newBlock(); + Block block2 = BlockFixture.newBlock(); + + when(blocks.has(block1.id())).thenReturn(false); + when(blocks.has(block2.id())).thenReturn(false); + + runTestValidatedTwoBlocks(blocks,block1,block2); + + verify(blocks, times(1)).add(block1); + verify(blocks, times(1)).add(block2); } /** @@ -97,7 +111,12 @@ public void testValidatedTwoBlocksRealStorage() throws IOException { db = new BlocksMapDb(tempdir.toAbsolutePath() + "/" + TEMP_FILE_ID, tempdir.toAbsolutePath() + "/" + TEMP_FILE_HEIGHT); Blocks blocks = db; - runTestValidatedTwoBlocks(blocks); + Block block1 = BlockFixture.newBlock(); + Block block2 = BlockFixture.newBlock(); + runTestValidatedTwoBlocks(blocks,block1,block2); + + Assertions.assertTrue(blocks.has(block1.id())); + Assertions.assertTrue(blocks.has(block2.id())); db.closeDb(); FileUtils.deleteDirectory(new File(tempdir.toString())); } @@ -107,14 +126,11 @@ public void testValidatedTwoBlocksRealStorage() throws IOException { * * @param blocks mocked or real block. */ - public void runTestValidatedTwoBlocks(Blocks blocks) { + public void runTestValidatedTwoBlocks(Blocks blocks,Block block1,Block block2) { Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); - Block block1 = BlockFixture.newBlock(); - Block block2 = BlockFixture.newBlock(); - IngestEngine ingestEngine = this.mockIngestEngineForEntities( new ArrayList<>(Arrays.asList(block1, block2)), seenEntities, @@ -142,7 +158,15 @@ public void runTestValidatedTwoBlocks(Blocks blocks) { @Test public void testValidatedTwoBlocksConcurrentlyMockedBlocks() { Blocks blocks = mock(Blocks.class); - runTestValidatedTwoBlocksConcurrently(blocks); + Block block1 = BlockFixture.newBlock(); + Block block2 = BlockFixture.newBlock(); + when(blocks.has(block1.id())).thenReturn(false); + when(blocks.has(block2.id())).thenReturn(false); + runTestValidatedTwoBlocksConcurrently(blocks,block1,block2); + + verify(blocks, times(1)).add(block1); + verify(blocks, times(1)).add(block2); + } /** @@ -157,7 +181,11 @@ public void testValidatedTwoBlocksConcurrentlyRealBlocks() throws IOException { db = new BlocksMapDb(tempdir.toAbsolutePath() + "/" + TEMP_FILE_ID, tempdir.toAbsolutePath() + "/" + TEMP_FILE_HEIGHT); Blocks blocks = db; - runTestValidatedTwoBlocksConcurrently(blocks); + Block block1 = BlockFixture.newBlock(); + Block block2 = BlockFixture.newBlock(); + runTestValidatedTwoBlocksConcurrently(blocks,block1,block2); + Assertions.assertTrue(blocks.has(block1.id())); + Assertions.assertTrue(blocks.has(block2.id())); db.closeDb(); FileUtils.deleteDirectory(new File(tempdir.toString())); } @@ -167,14 +195,11 @@ public void testValidatedTwoBlocksConcurrentlyRealBlocks() throws IOException { * * @param blocks mocked or real block. */ - public void runTestValidatedTwoBlocksConcurrently(Blocks blocks) { + public void runTestValidatedTwoBlocksConcurrently(Blocks blocks,Block block1, Block block2) { Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); - Block block1 = BlockFixture.newBlock(); - Block block2 = BlockFixture.newBlock(); - IngestEngine ingestEngine = this.mockIngestEngineForEntities( new ArrayList<>(Arrays.asList(block1, block2)), seenEntities, @@ -201,7 +226,11 @@ public void runTestValidatedTwoBlocksConcurrently(Blocks blocks) { @Test public void testValidatedSameTwoBlocksMockedStorage() { Blocks blocks = mock(Blocks.class); - runTestValidatedSameTwoBlocks(blocks); + Block block = BlockFixture.newBlock(); + when(blocks.has(block.id())).thenReturn(false); + runTestValidatedSameTwoBlocks(blocks,block); + verify(blocks, times(1)).add(block); + } /** @@ -216,7 +245,9 @@ public void testValidatedSameTwoBlocksRealStorage() throws IOException { db = new BlocksMapDb(tempdir.toAbsolutePath() + "/" + TEMP_FILE_ID, tempdir.toAbsolutePath() + "/" + TEMP_FILE_HEIGHT); Blocks blocks = db; - runTestValidatedSameTwoBlocks(blocks); + Block block = BlockFixture.newBlock(); + runTestValidatedSameTwoBlocks(blocks,block); + Assertions.assertTrue(blocks.has(block.id())); db.closeDb(); FileUtils.deleteDirectory(new File(tempdir.toString())); } @@ -226,13 +257,11 @@ public void testValidatedSameTwoBlocksRealStorage() throws IOException { * * @param blocks mocked or real blocks. */ - public void runTestValidatedSameTwoBlocks(Blocks blocks) { + public void runTestValidatedSameTwoBlocks(Blocks blocks,Block block) { Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); - Block block = BlockFixture.newBlock(); - when(seenEntities.has(block.id())).thenReturn(false); IngestEngine ingestEngine = this.mockIngestEngineForEntities( @@ -260,7 +289,10 @@ public void runTestValidatedSameTwoBlocks(Blocks blocks) { @Test public void testValidatedBlockContainingPendingTransactionMockedStorage() throws IOException { Blocks blocks = mock(Blocks.class); - runTestValidatedBlockContainingPendingTransaction(blocks); + Block block = BlockFixture.newBlock(); + when(blocks.has(block.id())).thenReturn(false); + runTestValidatedBlockContainingPendingTransaction(blocks,block); + verify(blocks, times(1)).add(block); } /** @@ -275,7 +307,9 @@ public void testValidatedBlockContainingPendingTransactionRealStorage() throws I db = new BlocksMapDb(tempdir.toAbsolutePath() + "/" + TEMP_FILE_ID, tempdir.toAbsolutePath() + "/" + TEMP_FILE_HEIGHT); Blocks blocks = db; - runTestValidatedBlockContainingPendingTransaction(blocks); + Block block = BlockFixture.newBlock(); + runTestValidatedBlockContainingPendingTransaction(blocks,block); + Assertions.assertTrue(blocks.has(block.id())); db.closeDb(); FileUtils.deleteDirectory(new File(tempdir.toString())); } @@ -285,11 +319,10 @@ public void testValidatedBlockContainingPendingTransactionRealStorage() throws I * * @param blocks mocked or real block. */ - public void runTestValidatedBlockContainingPendingTransaction(Blocks blocks) { + public void runTestValidatedBlockContainingPendingTransaction(Blocks blocks,Block block) { Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); - Block block = BlockFixture.newBlock(); IngestEngine ingestEngine = this.mockIngestEngineForEntities( new ArrayList<>(List.of(block)), @@ -322,7 +355,12 @@ public void runTestValidatedBlockContainingPendingTransaction(Blocks blocks) { public void testConcurrentBlockIngestionContainingSeenTransactionDisjointSetMockedStorage() throws IOException { // R Blocks blocks = mock(Blocks.class); - runTestConcurrentBlockIngestionContainingSeenTransactionDisjointSet(blocks); + Block block1 = BlockFixture.newBlock(); + Block block2 = BlockFixture.newBlock(); + runTestConcurrentBlockIngestionContainingSeenTransactionDisjointSet(blocks,block1,block2); + verify(blocks, times(1)).add(block1); + verify(blocks, times(1)).add(block2); + } /** @@ -338,10 +376,13 @@ public void testConcurrentBlockIngestionContainingSeenTransactionDisjointSetReal db = new BlocksMapDb(tempdir.toAbsolutePath() + "/" + TEMP_FILE_ID, tempdir.toAbsolutePath() + "/" + TEMP_FILE_HEIGHT); Blocks blocks = db; - runTestConcurrentBlockIngestionContainingSeenTransactionDisjointSet(blocks); + Block block1 = BlockFixture.newBlock(); + Block block2 = BlockFixture.newBlock(); + runTestConcurrentBlockIngestionContainingSeenTransactionDisjointSet(blocks,block1,block2); + Assertions.assertTrue(blocks.has(block1.id())); + Assertions.assertTrue(blocks.has(block2.id())); db.closeDb(); FileUtils.deleteDirectory(new File(tempdir.toString())); - } /** @@ -349,14 +390,12 @@ public void testConcurrentBlockIngestionContainingSeenTransactionDisjointSetReal * * @param blocks mocked or real block. */ - private void runTestConcurrentBlockIngestionContainingSeenTransactionDisjointSet(Blocks blocks) { + private void runTestConcurrentBlockIngestionContainingSeenTransactionDisjointSet(Blocks blocks,Block block1,Block block2) { Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); ArrayList accounts = new ArrayList<>(AccountFixture.newAccounts(10, 10).values()); - Block block1 = BlockFixture.newBlock(); - Block block2 = BlockFixture.newBlock(); IngestEngine ingestEngine = this.mockIngestEngineForEntities( new ArrayList<>(Arrays.asList(block1, block2)), @@ -389,12 +428,11 @@ private void runTestConcurrentBlockIngestionContainingSeenTransactionDisjointSet * * @param blocks mocked or real block. */ - private void runTestConcurrentBlockIngestionContainingSeenTransactionOverlappingSet(Blocks blocks) { + private void runTestConcurrentBlockIngestionContainingSeenTransactionOverlappingSet(Blocks blocks,Block block1,Block block2) { Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); - Block block1 = BlockFixture.newBlock(); - Block block2 = BlockFixture.newBlock(); + IngestEngine ingestEngine = this.mockIngestEngineForEntities( new ArrayList<>(Arrays.asList(block1, block2)), @@ -431,7 +469,13 @@ private void runTestConcurrentBlockIngestionContainingSeenTransactionOverlapping @Test public void testConcurrentBlockIngestionContainingSeenTransactionOverlappingSetMockedStorage() { Blocks blocks = mock(Blocks.class); - runTestConcurrentBlockIngestionContainingSeenTransactionOverlappingSet(blocks); + Block block1 = BlockFixture.newBlock(); + Block block2 = BlockFixture.newBlock(); + when(blocks.has(block1.id())).thenReturn(false); + when(blocks.has(block2.id())).thenReturn(false); + runTestConcurrentBlockIngestionContainingSeenTransactionOverlappingSet(blocks,block1,block2); + verify(blocks, times(1)).add(block1); + verify(blocks, times(1)).add(block2); } /** @@ -446,18 +490,20 @@ public void testConcurrentBlockIngestionContainingSeenTransactionOverlappingSetR db = new BlocksMapDb(tempdir.toAbsolutePath() + "/" + TEMP_FILE_ID, tempdir.toAbsolutePath() + "/" + TEMP_FILE_HEIGHT); Blocks blocks = db; - runTestConcurrentBlockIngestionContainingSeenTransactionOverlappingSet(blocks); + Block block1 = BlockFixture.newBlock(); + Block block2 = BlockFixture.newBlock(); + runTestConcurrentBlockIngestionContainingSeenTransactionOverlappingSet(blocks,block1,block2); + Assertions.assertTrue(blocks.has(block1.id())); + Assertions.assertTrue(blocks.has(block2.id())); db.closeDb(); FileUtils.deleteDirectory(new File(tempdir.toString())); } - private void runTestValidatedAlreadyIngestedBlock(Blocks blocks) { + private void runTestValidatedAlreadyIngestedBlock(Blocks blocks,Block block) { Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); - Block block = BlockFixture.newBlock(); - IngestEngine ingestEngine = this.mockIngestEngineForEntities( new ArrayList<>(List.of(block)), seenEntities, @@ -466,9 +512,6 @@ private void runTestValidatedAlreadyIngestedBlock(Blocks blocks) { blocks); when(seenEntities.has(block.id())).thenReturn(true); // block is already ingested - if (MockUtil.isMock(blocks)) { - when(blocks.has(block.id())).thenReturn(false); - } // block is already ingested // action ingestEngine.process(block); @@ -490,7 +533,9 @@ private void runTestValidatedAlreadyIngestedBlock(Blocks blocks) { @Test public void testValidatedAlreadyIngestedBlockMockedStorage() { Blocks blocks = mock(Blocks.class); - runTestValidatedAlreadyIngestedBlock(blocks); + Block block = BlockFixture.newBlock(); + when(blocks.has(block.id())).thenReturn(false); + runTestValidatedAlreadyIngestedBlock(blocks,block); } /** @@ -504,10 +549,10 @@ public void testValidatedAlreadyIngestedBlockRealStorage() throws IOException { db = new BlocksMapDb(tempdir.toAbsolutePath() + "/" + TEMP_FILE_ID, tempdir.toAbsolutePath() + "/" + TEMP_FILE_HEIGHT); Blocks blocks = db; - runTestValidatedAlreadyIngestedBlock(blocks); + Block block = BlockFixture.newBlock(); + runTestValidatedAlreadyIngestedBlock(blocks,block); db.closeDb(); FileUtils.deleteDirectory(new File(tempdir.toString())); - } /** @@ -907,12 +952,10 @@ public void testConcurrentValidatedTransactionAndBlockNonOverlappingRealBlocks() * * @param blocks mocked or real blocks. */ - private void runTestProcessBlockAndIncludedTransaction_BlockFirst(Blocks blocks) { + private void runTestProcessBlockAndIncludedTransaction_BlockFirst(Blocks blocks,Block block) { Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); - - Block block = BlockFixture.newBlock(); ValidatedTransaction validatedTx = block.getTransactions()[0]; // the transaction is in the block IngestEngine ingestEngine = this.mockIngestEngineForEntities( @@ -930,10 +973,6 @@ private void runTestProcessBlockAndIncludedTransaction_BlockFirst(Blocks blocks) // process transaction ingestEngine.process(validatedTx); - // verification for block - if (MockUtil.isMock(blocks)) { - verify(blocks, times(1)).add(block); - } verify(seenEntities, times(1)).add(block.id()); for (Transaction tx : block.getTransactions()) { verify(transactionIds, times(1)).add(tx.id()); @@ -955,7 +994,10 @@ private void runTestProcessBlockAndIncludedTransaction_BlockFirst(Blocks blocks) @Test public void testProcessBlockAndIncludedTransaction_BlockFirstMockedBlocks() { Blocks blocks = mock(Blocks.class); - runTestProcessBlockAndIncludedTransaction_BlockFirst(blocks); + Block block = BlockFixture.newBlock(); + when(blocks.has(block.id())).thenReturn(false); + runTestProcessBlockAndIncludedTransaction_BlockFirst(blocks,block); + verify(blocks, times(1)).add(block); } /** @@ -972,7 +1014,9 @@ public void testProcessBlockAndIncludedTransaction_BlockFirstRealBlocks() throws db = new BlocksMapDb(tempdir.toAbsolutePath() + "/" + TEMP_FILE_ID, tempdir.toAbsolutePath() + "/" + TEMP_FILE_HEIGHT); Blocks blocks = db; - runTestProcessBlockAndIncludedTransaction_BlockFirst(blocks); + Block block = BlockFixture.newBlock(); + runTestProcessBlockAndIncludedTransaction_BlockFirst(blocks,block); + Assertions.assertTrue(blocks.has(block.id())); db.closeDb(); FileUtils.deleteDirectory(new File(tempdir.toString())); } @@ -982,12 +1026,11 @@ public void testProcessBlockAndIncludedTransaction_BlockFirstRealBlocks() throws * * @param blocks real or mocked blocks. */ - private void runTestProcessBlockAndIncludedTransaction_TransactionFirst(Blocks blocks) { + private void runTestProcessBlockAndIncludedTransaction_TransactionFirst(Blocks blocks,Block block) { Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); - Block block = BlockFixture.newBlock(); ValidatedTransaction validatedTx = block.getTransactions()[0]; // the transaction is in the block IngestEngine ingestEngine = this.mockIngestEngineForEntities( @@ -1006,10 +1049,6 @@ private void runTestProcessBlockAndIncludedTransaction_TransactionFirst(Blocks b // process block next. ingestEngine.process(block); - // verification for block - if (MockUtil.isMock(blocks)) { - verify(blocks, times(1)).add(block); - } verify(seenEntities, times(1)).add(block.id()); for (Transaction tx : block.getTransactions()) { verify(transactionIds, times(1)).add(tx.id()); @@ -1034,7 +1073,10 @@ private void runTestProcessBlockAndIncludedTransaction_TransactionFirst(Blocks b public void testProcessBlockAndIncludedTransaction_TransactionFirstMockedBlocks() { // R Blocks blocks = mock(Blocks.class); - runTestProcessBlockAndIncludedTransaction_TransactionFirst(blocks); + Block block = BlockFixture.newBlock(); + when(blocks.has(block.id())).thenReturn(false); + runTestProcessBlockAndIncludedTransaction_TransactionFirst(blocks,block); + verify(blocks, times(1)).add(block); } /** @@ -1052,7 +1094,9 @@ public void testProcessBlockAndIncludedTransaction_TransactionFirstRealBlocks() db = new BlocksMapDb(tempdir.toAbsolutePath() + "/" + TEMP_FILE_ID, tempdir.toAbsolutePath() + "/" + TEMP_FILE_HEIGHT); Blocks blocks = db; - runTestProcessBlockAndIncludedTransaction_TransactionFirst(blocks); + Block block = BlockFixture.newBlock(); + runTestProcessBlockAndIncludedTransaction_TransactionFirst(blocks,block); + Assertions.assertTrue(blocks.has(block.id())); db.closeDb(); FileUtils.deleteDirectory(new File(tempdir.toString())); } @@ -1075,10 +1119,6 @@ private void verifyBlockHappyPathCalled( Transactions pendingTx, Identifiers txIds, Identifiers seenEntities) { - if (MockUtil.isMock(blocks)) { - verify(blocks, times(1)).add(block); - } - verify(seenEntities, times(1)).add(block.id()); for (Transaction tx : block.getTransactions()) { verify(pendingTx, times(1)).has(tx.id()); @@ -1145,9 +1185,6 @@ private IngestEngine mockIngestEngineForEntities( Block block = (Block) e; when(state.atBlockId(block.getPreviousBlockId())).thenReturn(snapshot); - if (MockUtil.isMock(blocks)) { - when(blocks.has(block.id())).thenReturn(false); - } for (Transaction tx : block.getTransactions()) { when(pendingTx.has(tx.id())).thenReturn(false); } @@ -1232,15 +1269,13 @@ private void mockAssignment(AssignerInf assigner, Entity e, Snapshot snapshot) { * * @param blocks mocked or real block. */ - private void runTestValidatedSingleBlock(Blocks blocks) { + private void runTestValidatedSingleBlock(Blocks blocks,Block block) { Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); - Block block = BlockFixture.newBlock(); + when(seenEntities.has(block.id())).thenReturn(false); - if (MockUtil.isMock(blocks)) { - when(blocks.has(block.id())).thenReturn(false); - } + IngestEngine ingestEngine = this.mockIngestEngineForEntities( new ArrayList<>(List.of(block)), seenEntities, From 59055ca07e1b224f33eb8b75ef3c5844ee79f241 Mon Sep 17 00:00:00 2001 From: akucukoduk16 Date: Thu, 25 Aug 2022 11:01:01 +0300 Subject: [PATCH 08/13] shortened the method names --- .../protocol/engines/IngestEngineTest.java | 105 +++++++++--------- 1 file changed, 52 insertions(+), 53 deletions(-) diff --git a/src/test/java/protocol/engines/IngestEngineTest.java b/src/test/java/protocol/engines/IngestEngineTest.java index bad592e8..bf112988 100644 --- a/src/test/java/protocol/engines/IngestEngineTest.java +++ b/src/test/java/protocol/engines/IngestEngineTest.java @@ -55,7 +55,7 @@ public void testValidatedSingleBlockMockedStorage() { Blocks blocks = mock(Blocks.class); Block block = BlockFixture.newBlock(); when(blocks.has(block.id())).thenReturn(false); - runTestValidatedSingleBlock(blocks,block); + runTestValidatedSingleBlock(blocks, block); verify(blocks, times(1)).add(block); } @@ -72,7 +72,7 @@ public void testValidatedSingleBlockRealStorage() throws IOException { tempdir.toAbsolutePath() + "/" + TEMP_FILE_HEIGHT); Blocks blocks = db; Block block = BlockFixture.newBlock(); - runTestValidatedSingleBlock(blocks,block); + runTestValidatedSingleBlock(blocks, block); Assertions.assertTrue(blocks.has(block.id())); db.closeDb(); FileUtils.deleteDirectory(new File(tempdir.toString())); @@ -93,7 +93,7 @@ public void testValidatedTwoBlocksMockedStorage() { when(blocks.has(block1.id())).thenReturn(false); when(blocks.has(block2.id())).thenReturn(false); - runTestValidatedTwoBlocks(blocks,block1,block2); + runTestValidatedTwoBlocks(blocks, block1, block2); verify(blocks, times(1)).add(block1); verify(blocks, times(1)).add(block2); @@ -113,7 +113,7 @@ public void testValidatedTwoBlocksRealStorage() throws IOException { Blocks blocks = db; Block block1 = BlockFixture.newBlock(); Block block2 = BlockFixture.newBlock(); - runTestValidatedTwoBlocks(blocks,block1,block2); + runTestValidatedTwoBlocks(blocks, block1, block2); Assertions.assertTrue(blocks.has(block1.id())); Assertions.assertTrue(blocks.has(block2.id())); @@ -126,7 +126,7 @@ public void testValidatedTwoBlocksRealStorage() throws IOException { * * @param blocks mocked or real block. */ - public void runTestValidatedTwoBlocks(Blocks blocks,Block block1,Block block2) { + public void runTestValidatedTwoBlocks(Blocks blocks, Block block1, Block block2) { Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); @@ -162,7 +162,7 @@ public void testValidatedTwoBlocksConcurrentlyMockedBlocks() { Block block2 = BlockFixture.newBlock(); when(blocks.has(block1.id())).thenReturn(false); when(blocks.has(block2.id())).thenReturn(false); - runTestValidatedTwoBlocksConcurrently(blocks,block1,block2); + runTestValidatedTwoBlocksConcurrently(blocks, block1, block2); verify(blocks, times(1)).add(block1); verify(blocks, times(1)).add(block2); @@ -183,7 +183,7 @@ public void testValidatedTwoBlocksConcurrentlyRealBlocks() throws IOException { Blocks blocks = db; Block block1 = BlockFixture.newBlock(); Block block2 = BlockFixture.newBlock(); - runTestValidatedTwoBlocksConcurrently(blocks,block1,block2); + runTestValidatedTwoBlocksConcurrently(blocks, block1, block2); Assertions.assertTrue(blocks.has(block1.id())); Assertions.assertTrue(blocks.has(block2.id())); db.closeDb(); @@ -195,7 +195,7 @@ public void testValidatedTwoBlocksConcurrentlyRealBlocks() throws IOException { * * @param blocks mocked or real block. */ - public void runTestValidatedTwoBlocksConcurrently(Blocks blocks,Block block1, Block block2) { + public void runTestValidatedTwoBlocksConcurrently(Blocks blocks, Block block1, Block block2) { Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); @@ -228,7 +228,7 @@ public void testValidatedSameTwoBlocksMockedStorage() { Blocks blocks = mock(Blocks.class); Block block = BlockFixture.newBlock(); when(blocks.has(block.id())).thenReturn(false); - runTestValidatedSameTwoBlocks(blocks,block); + runTestValidatedSameTwoBlocks(blocks, block); verify(blocks, times(1)).add(block); } @@ -246,7 +246,7 @@ public void testValidatedSameTwoBlocksRealStorage() throws IOException { tempdir.toAbsolutePath() + "/" + TEMP_FILE_HEIGHT); Blocks blocks = db; Block block = BlockFixture.newBlock(); - runTestValidatedSameTwoBlocks(blocks,block); + runTestValidatedSameTwoBlocks(blocks, block); Assertions.assertTrue(blocks.has(block.id())); db.closeDb(); FileUtils.deleteDirectory(new File(tempdir.toString())); @@ -257,7 +257,7 @@ public void testValidatedSameTwoBlocksRealStorage() throws IOException { * * @param blocks mocked or real blocks. */ - public void runTestValidatedSameTwoBlocks(Blocks blocks,Block block) { + public void runTestValidatedSameTwoBlocks(Blocks blocks, Block block) { Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); @@ -287,11 +287,11 @@ public void runTestValidatedSameTwoBlocks(Blocks blocks,Block block) { * The engine also removes hash of the transactions of blocks from pendingTransactions. */ @Test - public void testValidatedBlockContainingPendingTransactionMockedStorage() throws IOException { + public void testValidatedBlockPendingTransactionMockedStorage() throws IOException { Blocks blocks = mock(Blocks.class); Block block = BlockFixture.newBlock(); when(blocks.has(block.id())).thenReturn(false); - runTestValidatedBlockContainingPendingTransaction(blocks,block); + runTestValidatedBlockPendingTransaction(blocks, block); verify(blocks, times(1)).add(block); } @@ -301,14 +301,14 @@ public void testValidatedBlockContainingPendingTransactionMockedStorage() throws * The engine also removes hash of the transactions of blocks from pendingTransactions. */ @Test - public void testValidatedBlockContainingPendingTransactionRealStorage() throws IOException { + public void testValidatedBlockPendingTransactionRealStorage() throws IOException { Path currentRelativePath = Paths.get(""); tempdir = Files.createTempDirectory(currentRelativePath, TEMP_DIR); db = new BlocksMapDb(tempdir.toAbsolutePath() + "/" + TEMP_FILE_ID, tempdir.toAbsolutePath() + "/" + TEMP_FILE_HEIGHT); Blocks blocks = db; Block block = BlockFixture.newBlock(); - runTestValidatedBlockContainingPendingTransaction(blocks,block); + runTestValidatedBlockPendingTransaction(blocks, block); Assertions.assertTrue(blocks.has(block.id())); db.closeDb(); FileUtils.deleteDirectory(new File(tempdir.toString())); @@ -319,7 +319,7 @@ public void testValidatedBlockContainingPendingTransactionRealStorage() throws I * * @param blocks mocked or real block. */ - public void runTestValidatedBlockContainingPendingTransaction(Blocks blocks,Block block) { + public void runTestValidatedBlockPendingTransaction(Blocks blocks, Block block) { Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); @@ -352,12 +352,12 @@ public void runTestValidatedBlockContainingPendingTransaction(Blocks blocks,Bloc * The engine also removes the hash of the single shared transaction among blocks from pending transactions. */ @Test - public void testConcurrentBlockIngestionContainingSeenTransactionDisjointSetMockedStorage() throws IOException { + public void testConcurrentBlockDisjointSetMockedStorage() throws IOException { // R Blocks blocks = mock(Blocks.class); Block block1 = BlockFixture.newBlock(); Block block2 = BlockFixture.newBlock(); - runTestConcurrentBlockIngestionContainingSeenTransactionDisjointSet(blocks,block1,block2); + runTestConcurrentBlockSeenTransactionDisjointSet(blocks, block1, block2); verify(blocks, times(1)).add(block1); verify(blocks, times(1)).add(block2); @@ -369,7 +369,7 @@ public void testConcurrentBlockIngestionContainingSeenTransactionDisjointSetMock * The engine also removes the hash of the single shared transaction among blocks from pending transactions. */ @Test - public void testConcurrentBlockIngestionContainingSeenTransactionDisjointSetRealStorage() throws IOException { + public void testConcurrentBlockDisjointSetRealStorage() throws IOException { // R Path currentRelativePath = Paths.get(""); tempdir = Files.createTempDirectory(currentRelativePath, TEMP_DIR); @@ -378,7 +378,7 @@ public void testConcurrentBlockIngestionContainingSeenTransactionDisjointSetReal Blocks blocks = db; Block block1 = BlockFixture.newBlock(); Block block2 = BlockFixture.newBlock(); - runTestConcurrentBlockIngestionContainingSeenTransactionDisjointSet(blocks,block1,block2); + runTestConcurrentBlockSeenTransactionDisjointSet(blocks, block1, block2); Assertions.assertTrue(blocks.has(block1.id())); Assertions.assertTrue(blocks.has(block2.id())); db.closeDb(); @@ -390,7 +390,7 @@ public void testConcurrentBlockIngestionContainingSeenTransactionDisjointSetReal * * @param blocks mocked or real block. */ - private void runTestConcurrentBlockIngestionContainingSeenTransactionDisjointSet(Blocks blocks,Block block1,Block block2) { + private void runTestConcurrentBlockSeenTransactionDisjointSet(Blocks blocks, Block block1, Block block2) { Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); @@ -428,12 +428,11 @@ private void runTestConcurrentBlockIngestionContainingSeenTransactionDisjointSet * * @param blocks mocked or real block. */ - private void runTestConcurrentBlockIngestionContainingSeenTransactionOverlappingSet(Blocks blocks,Block block1,Block block2) { + private void runTestConcurrentBlockOverlappingSet(Blocks blocks, Block block1, Block block2) { Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); - IngestEngine ingestEngine = this.mockIngestEngineForEntities( new ArrayList<>(Arrays.asList(block1, block2)), seenEntities, @@ -467,13 +466,13 @@ private void runTestConcurrentBlockIngestionContainingSeenTransactionOverlapping * The engine also removes hash of the transactions of blocks from pendingTransactions. */ @Test - public void testConcurrentBlockIngestionContainingSeenTransactionOverlappingSetMockedStorage() { + public void testConcurrentBlockOverlappingSetMockedStorage() { Blocks blocks = mock(Blocks.class); Block block1 = BlockFixture.newBlock(); Block block2 = BlockFixture.newBlock(); when(blocks.has(block1.id())).thenReturn(false); when(blocks.has(block2.id())).thenReturn(false); - runTestConcurrentBlockIngestionContainingSeenTransactionOverlappingSet(blocks,block1,block2); + runTestConcurrentBlockOverlappingSet(blocks, block1, block2); verify(blocks, times(1)).add(block1); verify(blocks, times(1)).add(block2); } @@ -484,7 +483,7 @@ public void testConcurrentBlockIngestionContainingSeenTransactionOverlappingSetM * The engine also removes hash of the transactions of blocks from pendingTransactions. */ @Test - public void testConcurrentBlockIngestionContainingSeenTransactionOverlappingSetRealStorage() throws IOException { + public void testConcurrentBlockOverlappingSetRealStorage() throws IOException { Path currentRelativePath = Paths.get(""); tempdir = Files.createTempDirectory(currentRelativePath, TEMP_DIR); db = new BlocksMapDb(tempdir.toAbsolutePath() + "/" + TEMP_FILE_ID, @@ -492,14 +491,14 @@ public void testConcurrentBlockIngestionContainingSeenTransactionOverlappingSetR Blocks blocks = db; Block block1 = BlockFixture.newBlock(); Block block2 = BlockFixture.newBlock(); - runTestConcurrentBlockIngestionContainingSeenTransactionOverlappingSet(blocks,block1,block2); + runTestConcurrentBlockOverlappingSet(blocks, block1, block2); Assertions.assertTrue(blocks.has(block1.id())); Assertions.assertTrue(blocks.has(block2.id())); db.closeDb(); FileUtils.deleteDirectory(new File(tempdir.toString())); } - private void runTestValidatedAlreadyIngestedBlock(Blocks blocks,Block block) { + private void runTestValidatedAlreadyIngestedBlock(Blocks blocks, Block block) { Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); @@ -535,7 +534,7 @@ public void testValidatedAlreadyIngestedBlockMockedStorage() { Blocks blocks = mock(Blocks.class); Block block = BlockFixture.newBlock(); when(blocks.has(block.id())).thenReturn(false); - runTestValidatedAlreadyIngestedBlock(blocks,block); + runTestValidatedAlreadyIngestedBlock(blocks, block); } /** @@ -550,7 +549,7 @@ public void testValidatedAlreadyIngestedBlockRealStorage() throws IOException { tempdir.toAbsolutePath() + "/" + TEMP_FILE_HEIGHT); Blocks blocks = db; Block block = BlockFixture.newBlock(); - runTestValidatedAlreadyIngestedBlock(blocks,block); + runTestValidatedAlreadyIngestedBlock(blocks, block); db.closeDb(); FileUtils.deleteDirectory(new File(tempdir.toString())); } @@ -707,7 +706,7 @@ private void runTestConcurrentValidatedTwoTransactions(Blocks blocks) { * the engine adds the hashes of the transactions into its pending transactions' database. */ @Test - public void testConcurrentValidatedTwoTransactionsMockedBlockStorage() { + public void testConcurrentTwoTransactionsMockedBlockStorage() { // R Blocks blocks = mock(Blocks.class); runTestConcurrentValidatedTwoTransactions(blocks); @@ -719,7 +718,7 @@ public void testConcurrentValidatedTwoTransactionsMockedBlockStorage() { * the engine adds the hashes of the transactions into its pending transactions' database. */ @Test - public void testConcurrentValidatedTwoTransactionsRealBlockStorage() throws IOException { + public void testConcurrentTwoTransactionsRealBlockStorage() throws IOException { // R Path currentRelativePath = Paths.get(""); tempdir = Files.createTempDirectory(currentRelativePath, TEMP_DIR); @@ -800,7 +799,7 @@ public void testValidatedSameTwoTransactionsRealBlocks() throws IOException { * * @param blocks mocked or real blocks. */ - private void runTestValidatedTransactionAlreadyInTransactionIdStorage(Blocks blocks) { + private void runTestAlreadyInTransactionIdStorage(Blocks blocks) { Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); @@ -834,10 +833,10 @@ private void runTestValidatedTransactionAlreadyInTransactionIdStorage(Blocks blo * with mocked blocks storage, the engine discards the transaction. */ @Test - public void testValidatedTransactionAlreadyInTransactionIdStorageMockedBlocks() { + public void testAlreadyInTransactionIdStorageMockedBlocks() { // R Blocks blocks = mock(Blocks.class); - runTestValidatedTransactionAlreadyInTransactionIdStorage(blocks); + runTestAlreadyInTransactionIdStorage(blocks); } /** @@ -845,14 +844,14 @@ public void testValidatedTransactionAlreadyInTransactionIdStorageMockedBlocks() * with real blocks storage, the engine discards the transaction. */ @Test - public void testValidatedTransactionAlreadyInTransactionIdStorageRealBlocks() throws IOException { + public void testAlreadyInTransactionIdStorageRealBlocks() throws IOException { // R Path currentRelativePath = Paths.get(""); tempdir = Files.createTempDirectory(currentRelativePath, TEMP_DIR); db = new BlocksMapDb(tempdir.toAbsolutePath() + "/" + TEMP_FILE_ID, tempdir.toAbsolutePath() + "/" + TEMP_FILE_HEIGHT); Blocks blocks = db; - runTestValidatedTransactionAlreadyInTransactionIdStorage(blocks); + runTestAlreadyInTransactionIdStorage(blocks); db.closeDb(); FileUtils.deleteDirectory(new File(tempdir.toString())); } @@ -887,7 +886,7 @@ public void testNeitherBlockNorTransaction() { * * @param blocks mocked or real blocks */ - private void runTestConcurrentValidatedTransactionAndBlockNonOverlapping(Blocks blocks) { + private void runTestConcurrentTransactionAndBlockNonOverlapping(Blocks blocks) { Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); @@ -922,10 +921,10 @@ private void runTestConcurrentValidatedTransactionAndBlockNonOverlapping(Blocks * and the hash of the validated transaction into its "transactions" database. */ @Test - public void testConcurrentValidatedTransactionAndBlockNonOverlappingMockedBlocks() { + public void testConcurrentTransactionAndBlockNonOverlappingMockedBlocks() { // R Blocks blocks = mock(Blocks.class); - runTestConcurrentValidatedTransactionAndBlockNonOverlapping(blocks); + runTestConcurrentTransactionAndBlockNonOverlapping(blocks); } /** @@ -935,14 +934,14 @@ public void testConcurrentValidatedTransactionAndBlockNonOverlappingMockedBlocks * and the hash of the validated transaction into its "transactions" database. */ @Test - public void testConcurrentValidatedTransactionAndBlockNonOverlappingRealBlocks() throws IOException { + public void testConcurrentTransactionAndBlockNonOverlappingRealBlocks() throws IOException { // R Path currentRelativePath = Paths.get(""); tempdir = Files.createTempDirectory(currentRelativePath, TEMP_DIR); db = new BlocksMapDb(tempdir.toAbsolutePath() + "/" + TEMP_FILE_ID, tempdir.toAbsolutePath() + "/" + TEMP_FILE_HEIGHT); Blocks blocks = db; - runTestConcurrentValidatedTransactionAndBlockNonOverlapping(blocks); + runTestConcurrentTransactionAndBlockNonOverlapping(blocks); db.closeDb(); FileUtils.deleteDirectory(new File(tempdir.toString())); } @@ -952,7 +951,7 @@ public void testConcurrentValidatedTransactionAndBlockNonOverlappingRealBlocks() * * @param blocks mocked or real blocks. */ - private void runTestProcessBlockAndIncludedTransaction_BlockFirst(Blocks blocks,Block block) { + private void runTestIncludedTransactionBlockFirst(Blocks blocks, Block block) { Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); @@ -992,11 +991,11 @@ private void runTestProcessBlockAndIncludedTransaction_BlockFirst(Blocks blocks, * transactions' database. */ @Test - public void testProcessBlockAndIncludedTransaction_BlockFirstMockedBlocks() { + public void testIncludedTransactionBlockFirstMockedBlocks() { Blocks blocks = mock(Blocks.class); Block block = BlockFixture.newBlock(); when(blocks.has(block.id())).thenReturn(false); - runTestProcessBlockAndIncludedTransaction_BlockFirst(blocks,block); + runTestIncludedTransactionBlockFirst(blocks, block); verify(blocks, times(1)).add(block); } @@ -1008,14 +1007,14 @@ public void testProcessBlockAndIncludedTransaction_BlockFirstMockedBlocks() { * transactions' database. */ @Test - public void testProcessBlockAndIncludedTransaction_BlockFirstRealBlocks() throws IOException { + public void testIncludedTransactionBlockFirstRealBlocks() throws IOException { Path currentRelativePath = Paths.get(""); tempdir = Files.createTempDirectory(currentRelativePath, TEMP_DIR); db = new BlocksMapDb(tempdir.toAbsolutePath() + "/" + TEMP_FILE_ID, tempdir.toAbsolutePath() + "/" + TEMP_FILE_HEIGHT); Blocks blocks = db; Block block = BlockFixture.newBlock(); - runTestProcessBlockAndIncludedTransaction_BlockFirst(blocks,block); + runTestIncludedTransactionBlockFirst(blocks, block); Assertions.assertTrue(blocks.has(block.id())); db.closeDb(); FileUtils.deleteDirectory(new File(tempdir.toString())); @@ -1026,7 +1025,7 @@ public void testProcessBlockAndIncludedTransaction_BlockFirstRealBlocks() throws * * @param blocks real or mocked blocks. */ - private void runTestProcessBlockAndIncludedTransaction_TransactionFirst(Blocks blocks,Block block) { + private void runIncludedTransactionTransactionFirst(Blocks blocks, Block block) { Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); @@ -1070,12 +1069,12 @@ private void runTestProcessBlockAndIncludedTransaction_TransactionFirst(Blocks b * transactions' database. */ @Test - public void testProcessBlockAndIncludedTransaction_TransactionFirstMockedBlocks() { + public void testIncludedTransactionTransactionFirstMockedBlocks() { // R Blocks blocks = mock(Blocks.class); Block block = BlockFixture.newBlock(); when(blocks.has(block.id())).thenReturn(false); - runTestProcessBlockAndIncludedTransaction_TransactionFirst(blocks,block); + runIncludedTransactionTransactionFirst(blocks, block); verify(blocks, times(1)).add(block); } @@ -1087,7 +1086,7 @@ public void testProcessBlockAndIncludedTransaction_TransactionFirstMockedBlocks( * transactions' database. */ @Test - public void testProcessBlockAndIncludedTransaction_TransactionFirstRealBlocks() throws IOException { + public void testIncludedTransactionTransactionFirstRealBlocks() throws IOException { // R Path currentRelativePath = Paths.get(""); tempdir = Files.createTempDirectory(currentRelativePath, TEMP_DIR); @@ -1095,7 +1094,7 @@ public void testProcessBlockAndIncludedTransaction_TransactionFirstRealBlocks() tempdir.toAbsolutePath() + "/" + TEMP_FILE_HEIGHT); Blocks blocks = db; Block block = BlockFixture.newBlock(); - runTestProcessBlockAndIncludedTransaction_TransactionFirst(blocks,block); + runIncludedTransactionTransactionFirst(blocks, block); Assertions.assertTrue(blocks.has(block.id())); db.closeDb(); FileUtils.deleteDirectory(new File(tempdir.toString())); @@ -1269,7 +1268,7 @@ private void mockAssignment(AssignerInf assigner, Entity e, Snapshot snapshot) { * * @param blocks mocked or real block. */ - private void runTestValidatedSingleBlock(Blocks blocks,Block block) { + private void runTestValidatedSingleBlock(Blocks blocks, Block block) { Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); From cfd235d5ea57a677819e47ffdfd378c33a123113 Mon Sep 17 00:00:00 2001 From: akucukoduk16 Date: Thu, 25 Aug 2022 11:11:11 +0300 Subject: [PATCH 09/13] added neither block nor transaction separation. --- .../protocol/engines/IngestEngineTest.java | 32 +++++++++++++++++-- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/src/test/java/protocol/engines/IngestEngineTest.java b/src/test/java/protocol/engines/IngestEngineTest.java index bf112988..b27b4b20 100644 --- a/src/test/java/protocol/engines/IngestEngineTest.java +++ b/src/test/java/protocol/engines/IngestEngineTest.java @@ -858,15 +858,41 @@ public void testAlreadyInTransactionIdStorageRealBlocks() throws IOException { /** * Evaluates that when an entity that is neither a validated block nor a validated transaction - * arrives at ingest engine, the engine throws IllegalArgumentException. + * arrives at ingest engine, with mocked block storage, the engine throws IllegalArgumentException. */ @Test - public void testNeitherBlockNorTransaction() { + public void testNeitherBlockNorTransactionMockedBlocks() { // R + Blocks blocks = mock(Blocks.class); + runTestNeitherBlockNorTransaction(blocks); + } + + /** + * Evaluates that when an entity that is neither a validated block nor a validated transaction + * arrives at ingest engine, with real block storage, the engine throws IllegalArgumentException. + */ + @Test + public void testNeitherBlockNorTransactionRealBlocks() throws IOException { + // R + Path currentRelativePath = Paths.get(""); + tempdir = Files.createTempDirectory(currentRelativePath, TEMP_DIR); + db = new BlocksMapDb(tempdir.toAbsolutePath() + "/" + TEMP_FILE_ID, + tempdir.toAbsolutePath() + "/" + TEMP_FILE_HEIGHT); + Blocks blocks = db; + runTestNeitherBlockNorTransaction(blocks); + db.closeDb(); + FileUtils.deleteDirectory(new File(tempdir.toString())); + } + + /** + * The method called by testNeitherBlockNorTransaction for mocked and real versions. + * + * @param blocks mocked or real blocks + */ + public void runTestNeitherBlockNorTransaction(Blocks blocks) { Transactions pendingTransactions = mock(Transactions.class); AssignerInf assigner = mock(AssignerInf.class); Identifiers transactionIds = mock(Identifiers.class); - Blocks blocks = mock(Blocks.class); State state = mock(State.class); Identifiers seenEntities = mock(Identifiers.class); From ad666d34464abf71fbe7f5f067563ed345356cf6 Mon Sep 17 00:00:00 2001 From: akucukoduk16 Date: Fri, 16 Sep 2022 09:57:38 +0300 Subject: [PATCH 10/13] style fix --- .../protocol/engines/IngestEngineTest.java | 230 ++++++++---------- 1 file changed, 102 insertions(+), 128 deletions(-) diff --git a/src/test/java/protocol/engines/IngestEngineTest.java b/src/test/java/protocol/engines/IngestEngineTest.java index b27b4b20..02410a17 100644 --- a/src/test/java/protocol/engines/IngestEngineTest.java +++ b/src/test/java/protocol/engines/IngestEngineTest.java @@ -55,7 +55,7 @@ public void testValidatedSingleBlockMockedStorage() { Blocks blocks = mock(Blocks.class); Block block = BlockFixture.newBlock(); when(blocks.has(block.id())).thenReturn(false); - runTestValidatedSingleBlock(blocks, block); + testValidatedSingleBlock(blocks, block); verify(blocks, times(1)).add(block); } @@ -66,16 +66,20 @@ public void testValidatedSingleBlockMockedStorage() { */ @Test public void testValidatedSingleBlockRealStorage() throws IOException { - Path currentRelativePath = Paths.get(""); - tempdir = Files.createTempDirectory(currentRelativePath, TEMP_DIR); - db = new BlocksMapDb(tempdir.toAbsolutePath() + "/" + TEMP_FILE_ID, - tempdir.toAbsolutePath() + "/" + TEMP_FILE_HEIGHT); - Blocks blocks = db; - Block block = BlockFixture.newBlock(); - runTestValidatedSingleBlock(blocks, block); - Assertions.assertTrue(blocks.has(block.id())); - db.closeDb(); - FileUtils.deleteDirectory(new File(tempdir.toString())); + try { + Path currentRelativePath = Paths.get(""); + tempdir = Files.createTempDirectory(currentRelativePath, TEMP_DIR); + db = new BlocksMapDb(tempdir.toAbsolutePath() + "/" + TEMP_FILE_ID, + tempdir.toAbsolutePath() + "/" + TEMP_FILE_HEIGHT); + Blocks blocks = db; + Block block = BlockFixture.newBlock(); + testValidatedSingleBlock(blocks, block); + Assertions.assertTrue(blocks.has(block.id())); + } finally { + db.closeDb(); + FileUtils.deleteDirectory(new File(tempdir.toString())); + } + } /** @@ -93,7 +97,7 @@ public void testValidatedTwoBlocksMockedStorage() { when(blocks.has(block1.id())).thenReturn(false); when(blocks.has(block2.id())).thenReturn(false); - runTestValidatedTwoBlocks(blocks, block1, block2); + testValidatedTwoBlocks(blocks, block1, block2); verify(blocks, times(1)).add(block1); verify(blocks, times(1)).add(block2); @@ -106,27 +110,33 @@ public void testValidatedTwoBlocksMockedStorage() { */ @Test public void testValidatedTwoBlocksRealStorage() throws IOException { - Path currentRelativePath = Paths.get(""); - tempdir = Files.createTempDirectory(currentRelativePath, TEMP_DIR); - db = new BlocksMapDb(tempdir.toAbsolutePath() + "/" + TEMP_FILE_ID, - tempdir.toAbsolutePath() + "/" + TEMP_FILE_HEIGHT); - Blocks blocks = db; - Block block1 = BlockFixture.newBlock(); - Block block2 = BlockFixture.newBlock(); - runTestValidatedTwoBlocks(blocks, block1, block2); + try { + Path currentRelativePath = Paths.get(""); + tempdir = Files.createTempDirectory(currentRelativePath, TEMP_DIR); + db = new BlocksMapDb(tempdir.toAbsolutePath() + "/" + TEMP_FILE_ID, + tempdir.toAbsolutePath() + "/" + TEMP_FILE_HEIGHT); + Blocks blocks = db; + Block block1 = BlockFixture.newBlock(); + Block block2 = BlockFixture.newBlock(); + testValidatedTwoBlocks(blocks, block1, block2); + + Assertions.assertTrue(blocks.has(block1.id())); + Assertions.assertTrue(blocks.has(block2.id())); + } catch (IOException e) { + Assertions.fail(); + } finally { + db.closeDb(); + FileUtils.deleteDirectory(new File(tempdir.toString())); + } - Assertions.assertTrue(blocks.has(block1.id())); - Assertions.assertTrue(blocks.has(block2.id())); - db.closeDb(); - FileUtils.deleteDirectory(new File(tempdir.toString())); } /** - * The method called by test validated single block for mocked and real versions. + * Test validated single block for mocked and real versions. * * @param blocks mocked or real block. */ - public void runTestValidatedTwoBlocks(Blocks blocks, Block block1, Block block2) { + public void testValidatedTwoBlocks(Blocks blocks, Block block1, Block block2) { Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); @@ -138,14 +148,11 @@ public void runTestValidatedTwoBlocks(Blocks blocks, Block block1, Block block2) pendingTransactions, blocks); - // action ingestEngine.process(block1); ingestEngine.process(block2); - // verification for block 1 verifyBlockHappyPathCalled(block1, blocks, pendingTransactions, transactionIds, seenEntities); - // verification for block 2 verifyBlockHappyPathCalled(block2, blocks, pendingTransactions, transactionIds, seenEntities); } @@ -162,7 +169,7 @@ public void testValidatedTwoBlocksConcurrentlyMockedBlocks() { Block block2 = BlockFixture.newBlock(); when(blocks.has(block1.id())).thenReturn(false); when(blocks.has(block2.id())).thenReturn(false); - runTestValidatedTwoBlocksConcurrently(blocks, block1, block2); + testValidatedTwoBlocksConcurrently(blocks, block1, block2); verify(blocks, times(1)).add(block1); verify(blocks, times(1)).add(block2); @@ -183,7 +190,7 @@ public void testValidatedTwoBlocksConcurrentlyRealBlocks() throws IOException { Blocks blocks = db; Block block1 = BlockFixture.newBlock(); Block block2 = BlockFixture.newBlock(); - runTestValidatedTwoBlocksConcurrently(blocks, block1, block2); + testValidatedTwoBlocksConcurrently(blocks, block1, block2); Assertions.assertTrue(blocks.has(block1.id())); Assertions.assertTrue(blocks.has(block2.id())); db.closeDb(); @@ -191,11 +198,11 @@ public void testValidatedTwoBlocksConcurrentlyRealBlocks() throws IOException { } /** - * The method called by test validated two blocks for mocked and real versions concurrently. + * Test validated two blocks for mocked and real versions concurrently. * * @param blocks mocked or real block. */ - public void runTestValidatedTwoBlocksConcurrently(Blocks blocks, Block block1, Block block2) { + public void testValidatedTwoBlocksConcurrently(Blocks blocks, Block block1, Block block2) { Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); @@ -228,7 +235,7 @@ public void testValidatedSameTwoBlocksMockedStorage() { Blocks blocks = mock(Blocks.class); Block block = BlockFixture.newBlock(); when(blocks.has(block.id())).thenReturn(false); - runTestValidatedSameTwoBlocks(blocks, block); + testValidatedSameTwoBlocks(blocks, block); verify(blocks, times(1)).add(block); } @@ -246,18 +253,18 @@ public void testValidatedSameTwoBlocksRealStorage() throws IOException { tempdir.toAbsolutePath() + "/" + TEMP_FILE_HEIGHT); Blocks blocks = db; Block block = BlockFixture.newBlock(); - runTestValidatedSameTwoBlocks(blocks, block); + testValidatedSameTwoBlocks(blocks, block); Assertions.assertTrue(blocks.has(block.id())); db.closeDb(); FileUtils.deleteDirectory(new File(tempdir.toString())); } /** - * The method called by test validated two blocks for mocked and real versions concurrently. + * Test validated two blocks for mocked and real versions concurrently. * * @param blocks mocked or real blocks. */ - public void runTestValidatedSameTwoBlocks(Blocks blocks, Block block) { + public void testValidatedSameTwoBlocks(Blocks blocks, Block block) { Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); @@ -271,12 +278,10 @@ public void runTestValidatedSameTwoBlocks(Blocks blocks, Block block) { pendingTransactions, blocks); - // action ingestEngine.process(block); when(seenEntities.has(block.id())).thenReturn(true); // block is already seen ingestEngine.process(block); - // verification verifyBlockHappyPathCalled(block, blocks, pendingTransactions, transactionIds, seenEntities); verify(seenEntities, times(2)).has(block.id()); } @@ -291,7 +296,7 @@ public void testValidatedBlockPendingTransactionMockedStorage() throws IOExcepti Blocks blocks = mock(Blocks.class); Block block = BlockFixture.newBlock(); when(blocks.has(block.id())).thenReturn(false); - runTestValidatedBlockPendingTransaction(blocks, block); + testValidatedBlockPendingTransaction(blocks, block); verify(blocks, times(1)).add(block); } @@ -308,18 +313,18 @@ public void testValidatedBlockPendingTransactionRealStorage() throws IOException tempdir.toAbsolutePath() + "/" + TEMP_FILE_HEIGHT); Blocks blocks = db; Block block = BlockFixture.newBlock(); - runTestValidatedBlockPendingTransaction(blocks, block); + testValidatedBlockPendingTransaction(blocks, block); Assertions.assertTrue(blocks.has(block.id())); db.closeDb(); FileUtils.deleteDirectory(new File(tempdir.toString())); } /** - * The method called by test validated blocks containing pending transactions for mocked and real versions concurrently. + * Test validated blocks containing pending transactions for mocked and real versions concurrently. * * @param blocks mocked or real block. */ - public void runTestValidatedBlockPendingTransaction(Blocks blocks, Block block) { + public void testValidatedBlockPendingTransaction(Blocks blocks, Block block) { Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); @@ -335,7 +340,7 @@ public void runTestValidatedBlockPendingTransaction(Blocks blocks, Block block) when(pendingTransactions.has(tx.id())).thenReturn(true); // pendingTx contains all txs of block } - // action + ingestEngine.process(block); // verification @@ -353,11 +358,10 @@ public void runTestValidatedBlockPendingTransaction(Blocks blocks, Block block) */ @Test public void testConcurrentBlockDisjointSetMockedStorage() throws IOException { - // R Blocks blocks = mock(Blocks.class); Block block1 = BlockFixture.newBlock(); Block block2 = BlockFixture.newBlock(); - runTestConcurrentBlockSeenTransactionDisjointSet(blocks, block1, block2); + testConcurrentBlockSeenTransactionDisjointSet(blocks, block1, block2); verify(blocks, times(1)).add(block1); verify(blocks, times(1)).add(block2); @@ -370,7 +374,6 @@ public void testConcurrentBlockDisjointSetMockedStorage() throws IOException { */ @Test public void testConcurrentBlockDisjointSetRealStorage() throws IOException { - // R Path currentRelativePath = Paths.get(""); tempdir = Files.createTempDirectory(currentRelativePath, TEMP_DIR); db = new BlocksMapDb(tempdir.toAbsolutePath() + "/" + TEMP_FILE_ID, @@ -378,7 +381,7 @@ public void testConcurrentBlockDisjointSetRealStorage() throws IOException { Blocks blocks = db; Block block1 = BlockFixture.newBlock(); Block block2 = BlockFixture.newBlock(); - runTestConcurrentBlockSeenTransactionDisjointSet(blocks, block1, block2); + testConcurrentBlockSeenTransactionDisjointSet(blocks, block1, block2); Assertions.assertTrue(blocks.has(block1.id())); Assertions.assertTrue(blocks.has(block2.id())); db.closeDb(); @@ -386,11 +389,11 @@ public void testConcurrentBlockDisjointSetRealStorage() throws IOException { } /** - * The method called by test concurrent block ingestion containing seen transaction disjoint set mocked and real versions. + * Test concurrent block ingestion containing seen transaction disjoint set mocked and real versions. * * @param blocks mocked or real block. */ - private void runTestConcurrentBlockSeenTransactionDisjointSet(Blocks blocks, Block block1, Block block2) { + private void testConcurrentBlockSeenTransactionDisjointSet(Blocks blocks, Block block1, Block block2) { Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); @@ -424,11 +427,11 @@ private void runTestConcurrentBlockSeenTransactionDisjointSet(Blocks blocks, Blo } /** - * The method called by test concurrent block ingestion containing seen transaction overlapping set mocked and real versions. + * Test concurrent block ingestion containing seen transaction overlapping set mocked and real versions. * * @param blocks mocked or real block. */ - private void runTestConcurrentBlockOverlappingSet(Blocks blocks, Block block1, Block block2) { + private void testConcurrentBlockOverlappingSet(Blocks blocks, Block block1, Block block2) { Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); @@ -472,7 +475,7 @@ public void testConcurrentBlockOverlappingSetMockedStorage() { Block block2 = BlockFixture.newBlock(); when(blocks.has(block1.id())).thenReturn(false); when(blocks.has(block2.id())).thenReturn(false); - runTestConcurrentBlockOverlappingSet(blocks, block1, block2); + testConcurrentBlockOverlappingSet(blocks, block1, block2); verify(blocks, times(1)).add(block1); verify(blocks, times(1)).add(block2); } @@ -491,14 +494,14 @@ public void testConcurrentBlockOverlappingSetRealStorage() throws IOException { Blocks blocks = db; Block block1 = BlockFixture.newBlock(); Block block2 = BlockFixture.newBlock(); - runTestConcurrentBlockOverlappingSet(blocks, block1, block2); + testConcurrentBlockOverlappingSet(blocks, block1, block2); Assertions.assertTrue(blocks.has(block1.id())); Assertions.assertTrue(blocks.has(block2.id())); db.closeDb(); FileUtils.deleteDirectory(new File(tempdir.toString())); } - private void runTestValidatedAlreadyIngestedBlock(Blocks blocks, Block block) { + private void testValidatedAlreadyIngestedBlock(Blocks blocks, Block block) { Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); @@ -512,11 +515,8 @@ private void runTestValidatedAlreadyIngestedBlock(Blocks blocks, Block block) { when(seenEntities.has(block.id())).thenReturn(true); // block is already ingested - // action ingestEngine.process(block); - // verification - verify(seenEntities, times(0)).add(block.id()); verify(seenEntities, times(1)).has(block.id()); for (Transaction tx : block.getTransactions()) { @@ -534,7 +534,7 @@ public void testValidatedAlreadyIngestedBlockMockedStorage() { Blocks blocks = mock(Blocks.class); Block block = BlockFixture.newBlock(); when(blocks.has(block.id())).thenReturn(false); - runTestValidatedAlreadyIngestedBlock(blocks, block); + testValidatedAlreadyIngestedBlock(blocks, block); } /** @@ -549,17 +549,17 @@ public void testValidatedAlreadyIngestedBlockRealStorage() throws IOException { tempdir.toAbsolutePath() + "/" + TEMP_FILE_HEIGHT); Blocks blocks = db; Block block = BlockFixture.newBlock(); - runTestValidatedAlreadyIngestedBlock(blocks, block); + testValidatedAlreadyIngestedBlock(blocks, block); db.closeDb(); FileUtils.deleteDirectory(new File(tempdir.toString())); } /** - * The method called by test validated transaction for mocked and real blokcs storage. + * Test validated transaction for mocked and real blokcs storage. * * @param blocks mocked or real block. */ - private void runTestValidatedTransaction(Blocks blocks) { + private void testValidatedTransaction(Blocks blocks) { Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); @@ -573,10 +573,8 @@ private void runTestValidatedTransaction(Blocks blocks) { pendingTransactions, blocks); - // action ingestEngine.process(tx); - // verification verifyTransactionHappyPathCalled(tx, seenEntities, transactionIds, pendingTransactions); } @@ -586,9 +584,8 @@ private void runTestValidatedTransaction(Blocks blocks) { */ @Test public void testValidatedTransactionMockedStorage() { - // R Blocks blocks = mock(Blocks.class); - runTestValidatedTransaction(blocks); + testValidatedTransaction(blocks); } /** @@ -597,23 +594,22 @@ public void testValidatedTransactionMockedStorage() { */ @Test public void testValidatedTransactionRealStorage() throws IOException { - // R Path currentRelativePath = Paths.get(""); tempdir = Files.createTempDirectory(currentRelativePath, TEMP_DIR); db = new BlocksMapDb(tempdir.toAbsolutePath() + "/" + TEMP_FILE_ID, tempdir.toAbsolutePath() + "/" + TEMP_FILE_HEIGHT); Blocks blocks = db; - runTestValidatedTransaction(blocks); + testValidatedTransaction(blocks); db.closeDb(); FileUtils.deleteDirectory(new File(tempdir.toString())); } /** - * The method called by test validated two transactions for mocked and real blocks storage. + * Test validated two transactions for mocked and real blocks storage. * * @param blocks mocked or real block. */ - private void runTestValidatedTwoTransactions(Blocks blocks) { + private void testValidatedTwoTransactions(Blocks blocks) { Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); @@ -631,14 +627,11 @@ private void runTestValidatedTwoTransactions(Blocks blocks) { pendingTransactions, blocks); - // action ingestEngine.process(tx1); ingestEngine.process(tx2); - // verification of tx1 verifyTransactionHappyPathCalled(tx1, seenEntities, transactionIds, pendingTransactions); - // verification of tx2 verifyTransactionHappyPathCalled(tx2, seenEntities, transactionIds, pendingTransactions); } @@ -648,9 +641,8 @@ private void runTestValidatedTwoTransactions(Blocks blocks) { */ @Test public void testValidatedTwoTransactionsMockedBlocksStorage() { - // R Blocks blocks = mock(Blocks.class); - runTestValidatedTwoTransactions(blocks); + testValidatedTwoTransactions(blocks); } /** @@ -659,23 +651,22 @@ public void testValidatedTwoTransactionsMockedBlocksStorage() { */ @Test public void testValidatedTwoTransactionsRealBlocksStorage() throws IOException { - // R Path currentRelativePath = Paths.get(""); tempdir = Files.createTempDirectory(currentRelativePath, TEMP_DIR); db = new BlocksMapDb(tempdir.toAbsolutePath() + "/" + TEMP_FILE_ID, tempdir.toAbsolutePath() + "/" + TEMP_FILE_HEIGHT); Blocks blocks = db; - runTestValidatedTwoTransactions(blocks); + testValidatedTwoTransactions(blocks); db.closeDb(); FileUtils.deleteDirectory(new File(tempdir.toString())); } /** - * The method called by test validated two transactions concurrently for mocked and real blocks storage. + * Test validated two transactions concurrently for mocked and real blocks storage. * * @param blocks mocked or real block. */ - private void runTestConcurrentValidatedTwoTransactions(Blocks blocks) { + private void testConcurrentValidatedTwoTransactions(Blocks blocks) { Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); @@ -707,9 +698,8 @@ private void runTestConcurrentValidatedTwoTransactions(Blocks blocks) { */ @Test public void testConcurrentTwoTransactionsMockedBlockStorage() { - // R Blocks blocks = mock(Blocks.class); - runTestConcurrentValidatedTwoTransactions(blocks); + testConcurrentValidatedTwoTransactions(blocks); } @@ -719,23 +709,22 @@ public void testConcurrentTwoTransactionsMockedBlockStorage() { */ @Test public void testConcurrentTwoTransactionsRealBlockStorage() throws IOException { - // R Path currentRelativePath = Paths.get(""); tempdir = Files.createTempDirectory(currentRelativePath, TEMP_DIR); db = new BlocksMapDb(tempdir.toAbsolutePath() + "/" + TEMP_FILE_ID, tempdir.toAbsolutePath() + "/" + TEMP_FILE_HEIGHT); Blocks blocks = db; - runTestConcurrentValidatedTwoTransactions(blocks); + testConcurrentValidatedTwoTransactions(blocks); db.closeDb(); FileUtils.deleteDirectory(new File(tempdir.toString())); } /** - * The method called by test validated same two transactions for mocked and real blocks storage. + * Test validated same two transactions for mocked and real blocks storage. * * @param blocks mocked or real blocks */ - private void runTestValidatedSameTwoTransactions(Blocks blocks) { + private void testValidatedSameTwoTransactions(Blocks blocks) { Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); @@ -752,14 +741,12 @@ private void runTestValidatedSameTwoTransactions(Blocks blocks) { pendingTransactions, blocks); - // action ingestEngine.process(tx); when(seenEntities.has(tx.id())).thenReturn(true); when(pendingTransactions.has(tx.id())).thenReturn(true); - // process the same transaction again. + ingestEngine.process(tx); - // verification verifyTransactionHappyPathCalled(tx, seenEntities, transactionIds, pendingTransactions); verify(seenEntities, times(2)).has(tx.id()); } @@ -771,9 +758,8 @@ private void runTestValidatedSameTwoTransactions(Blocks blocks) { */ @Test public void testValidatedSameTwoTransactionsMockedBlocks() { - // R Blocks blocks = mock(Blocks.class); - runTestValidatedSameTwoTransactions(blocks); + testValidatedSameTwoTransactions(blocks); } /** @@ -783,23 +769,22 @@ public void testValidatedSameTwoTransactionsMockedBlocks() { */ @Test public void testValidatedSameTwoTransactionsRealBlocks() throws IOException { - // R Path currentRelativePath = Paths.get(""); tempdir = Files.createTempDirectory(currentRelativePath, TEMP_DIR); db = new BlocksMapDb(tempdir.toAbsolutePath() + "/" + TEMP_FILE_ID, tempdir.toAbsolutePath() + "/" + TEMP_FILE_HEIGHT); Blocks blocks = db; - runTestValidatedSameTwoTransactions(blocks); + testValidatedSameTwoTransactions(blocks); db.closeDb(); FileUtils.deleteDirectory(new File(tempdir.toString())); } /** - * The method called by test validated transactions already in transaction id storage for mocked and real blocks. + * Test validated transactions already in transaction id storage for mocked and real blocks. * * @param blocks mocked or real blocks. */ - private void runTestAlreadyInTransactionIdStorage(Blocks blocks) { + private void testAlreadyInTransactionIdStorage(Blocks blocks) { Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); @@ -818,10 +803,8 @@ private void runTestAlreadyInTransactionIdStorage(Blocks blocks) { when(transactionIds.has(tx.id())).thenReturn(true); when(pendingTransactions.has(tx.id())).thenReturn(false); - // action ingestEngine.process(tx); - // verification verify(seenEntities, times(1)).add(tx.id()); verify(pendingTransactions, times(1)).has(tx.id()); verify(transactionIds, times(1)).has(tx.id()); @@ -834,9 +817,8 @@ private void runTestAlreadyInTransactionIdStorage(Blocks blocks) { */ @Test public void testAlreadyInTransactionIdStorageMockedBlocks() { - // R Blocks blocks = mock(Blocks.class); - runTestAlreadyInTransactionIdStorage(blocks); + testAlreadyInTransactionIdStorage(blocks); } /** @@ -845,13 +827,12 @@ public void testAlreadyInTransactionIdStorageMockedBlocks() { */ @Test public void testAlreadyInTransactionIdStorageRealBlocks() throws IOException { - // R Path currentRelativePath = Paths.get(""); tempdir = Files.createTempDirectory(currentRelativePath, TEMP_DIR); db = new BlocksMapDb(tempdir.toAbsolutePath() + "/" + TEMP_FILE_ID, tempdir.toAbsolutePath() + "/" + TEMP_FILE_HEIGHT); Blocks blocks = db; - runTestAlreadyInTransactionIdStorage(blocks); + testAlreadyInTransactionIdStorage(blocks); db.closeDb(); FileUtils.deleteDirectory(new File(tempdir.toString())); } @@ -862,9 +843,8 @@ public void testAlreadyInTransactionIdStorageRealBlocks() throws IOException { */ @Test public void testNeitherBlockNorTransactionMockedBlocks() { - // R Blocks blocks = mock(Blocks.class); - runTestNeitherBlockNorTransaction(blocks); + testNeitherBlockNorTransaction(blocks); } /** @@ -873,23 +853,22 @@ public void testNeitherBlockNorTransactionMockedBlocks() { */ @Test public void testNeitherBlockNorTransactionRealBlocks() throws IOException { - // R Path currentRelativePath = Paths.get(""); tempdir = Files.createTempDirectory(currentRelativePath, TEMP_DIR); db = new BlocksMapDb(tempdir.toAbsolutePath() + "/" + TEMP_FILE_ID, tempdir.toAbsolutePath() + "/" + TEMP_FILE_HEIGHT); Blocks blocks = db; - runTestNeitherBlockNorTransaction(blocks); + testNeitherBlockNorTransaction(blocks); db.closeDb(); FileUtils.deleteDirectory(new File(tempdir.toString())); } /** - * The method called by testNeitherBlockNorTransaction for mocked and real versions. + * TestNeitherBlockNorTransaction for mocked and real versions. * * @param blocks mocked or real blocks */ - public void runTestNeitherBlockNorTransaction(Blocks blocks) { + public void testNeitherBlockNorTransaction(Blocks blocks) { Transactions pendingTransactions = mock(Transactions.class); AssignerInf assigner = mock(AssignerInf.class); Identifiers transactionIds = mock(Identifiers.class); @@ -908,11 +887,11 @@ public void runTestNeitherBlockNorTransaction(Blocks blocks) { } /** - * The method called by test concurrent validated transaction and block nonoverlapping for mocked and real versions. + * Test concurrent validated transaction and block nonoverlapping for mocked and real versions. * * @param blocks mocked or real blocks */ - private void runTestConcurrentTransactionAndBlockNonOverlapping(Blocks blocks) { + private void testConcurrentTransactionAndBlockNonOverlapping(Blocks blocks) { Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); @@ -948,9 +927,8 @@ private void runTestConcurrentTransactionAndBlockNonOverlapping(Blocks blocks) { */ @Test public void testConcurrentTransactionAndBlockNonOverlappingMockedBlocks() { - // R Blocks blocks = mock(Blocks.class); - runTestConcurrentTransactionAndBlockNonOverlapping(blocks); + testConcurrentTransactionAndBlockNonOverlapping(blocks); } /** @@ -961,23 +939,22 @@ public void testConcurrentTransactionAndBlockNonOverlappingMockedBlocks() { */ @Test public void testConcurrentTransactionAndBlockNonOverlappingRealBlocks() throws IOException { - // R Path currentRelativePath = Paths.get(""); tempdir = Files.createTempDirectory(currentRelativePath, TEMP_DIR); db = new BlocksMapDb(tempdir.toAbsolutePath() + "/" + TEMP_FILE_ID, tempdir.toAbsolutePath() + "/" + TEMP_FILE_HEIGHT); Blocks blocks = db; - runTestConcurrentTransactionAndBlockNonOverlapping(blocks); + testConcurrentTransactionAndBlockNonOverlapping(blocks); db.closeDb(); FileUtils.deleteDirectory(new File(tempdir.toString())); } /** - * The method called by tests processBlockAndIncludedTransaction_BlockFirst for mocked and real block storages. + * Tests processBlockAndIncludedTransaction_BlockFirst for mocked and real block storages. * * @param blocks mocked or real blocks. */ - private void runTestIncludedTransactionBlockFirst(Blocks blocks, Block block) { + private void testIncludedTransactionBlockFirst(Blocks blocks, Block block) { Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); @@ -1021,7 +998,7 @@ public void testIncludedTransactionBlockFirstMockedBlocks() { Blocks blocks = mock(Blocks.class); Block block = BlockFixture.newBlock(); when(blocks.has(block.id())).thenReturn(false); - runTestIncludedTransactionBlockFirst(blocks, block); + testIncludedTransactionBlockFirst(blocks, block); verify(blocks, times(1)).add(block); } @@ -1040,18 +1017,18 @@ public void testIncludedTransactionBlockFirstRealBlocks() throws IOException { tempdir.toAbsolutePath() + "/" + TEMP_FILE_HEIGHT); Blocks blocks = db; Block block = BlockFixture.newBlock(); - runTestIncludedTransactionBlockFirst(blocks, block); + testIncludedTransactionBlockFirst(blocks, block); Assertions.assertTrue(blocks.has(block.id())); db.closeDb(); FileUtils.deleteDirectory(new File(tempdir.toString())); } /** - * The method called by tests process block and included transaction and transaction first for mocked and real blocks. + * Tests process block and included transaction and transaction first for mocked and real blocks. * * @param blocks real or mocked blocks. */ - private void runIncludedTransactionTransactionFirst(Blocks blocks, Block block) { + private void testIncludedTransactionTransactionFirst(Blocks blocks, Block block) { Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); @@ -1096,11 +1073,10 @@ private void runIncludedTransactionTransactionFirst(Blocks blocks, Block block) */ @Test public void testIncludedTransactionTransactionFirstMockedBlocks() { - // R Blocks blocks = mock(Blocks.class); Block block = BlockFixture.newBlock(); when(blocks.has(block.id())).thenReturn(false); - runIncludedTransactionTransactionFirst(blocks, block); + testIncludedTransactionTransactionFirst(blocks, block); verify(blocks, times(1)).add(block); } @@ -1113,14 +1089,13 @@ public void testIncludedTransactionTransactionFirstMockedBlocks() { */ @Test public void testIncludedTransactionTransactionFirstRealBlocks() throws IOException { - // R Path currentRelativePath = Paths.get(""); tempdir = Files.createTempDirectory(currentRelativePath, TEMP_DIR); db = new BlocksMapDb(tempdir.toAbsolutePath() + "/" + TEMP_FILE_ID, tempdir.toAbsolutePath() + "/" + TEMP_FILE_HEIGHT); Blocks blocks = db; Block block = BlockFixture.newBlock(); - runIncludedTransactionTransactionFirst(blocks, block); + testIncludedTransactionTransactionFirst(blocks, block); Assertions.assertTrue(blocks.has(block.id())); db.closeDb(); FileUtils.deleteDirectory(new File(tempdir.toString())); @@ -1128,7 +1103,7 @@ public void testIncludedTransactionTransactionFirstRealBlocks() throws IOExcepti /** * Verifies mocked storage components have been called with expected parameters on an - * expected number of times for block happy path, i.e., block is added to the blocks storage, and its id is + * expected number of times for block happy path, i.e., block is added to the block's storage, and its id is * added to seenEntities storage. Also, all its transactions ids are added to the pendingTx and * txIds. * @@ -1290,11 +1265,12 @@ private void mockAssignment(AssignerInf assigner, Entity e, Snapshot snapshot) { } /** - * The method called by test validated single block for mocked and real versions. + * Test validated single block for mocked and real versions. * - * @param blocks mocked or real block. + * @param blocks mocked or real block storage. + * @param block the real block */ - private void runTestValidatedSingleBlock(Blocks blocks, Block block) { + private void testValidatedSingleBlock(Blocks blocks, Block block) { Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); @@ -1307,9 +1283,7 @@ private void runTestValidatedSingleBlock(Blocks blocks, Block block) { transactionIds, pendingTransactions, blocks); - // action ingestEngine.process(block); - // verify verifyBlockHappyPathCalled(block, blocks, pendingTransactions, transactionIds, seenEntities); } } \ No newline at end of file From a6556c1f9d1cfd73f9c713e2f0ef94a1583b3241 Mon Sep 17 00:00:00 2001 From: akucukoduk16 Date: Fri, 23 Sep 2022 17:56:35 +0300 Subject: [PATCH 11/13] Merge branch 'master' into abdullah/ingest-engine-test-real-blocks --- .../java/protocol/engines/IngestEngineTest.java | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/test/java/protocol/engines/IngestEngineTest.java b/src/test/java/protocol/engines/IngestEngineTest.java index 1ffb507a..9b0d3b28 100644 --- a/src/test/java/protocol/engines/IngestEngineTest.java +++ b/src/test/java/protocol/engines/IngestEngineTest.java @@ -44,7 +44,6 @@ public class IngestEngineTest { private static final String TEMP_FILE_HEIGHT = "tempfileHEIGHT.db"; - /** * Evaluates that when a new validated block arrives at ingest engine, * the engine adds the block to its mocked block storage database. @@ -291,8 +290,14 @@ public void testValidated_SameTwoBlocks_RealStorage() throws IOException { db.closeDb(); FileUtils.deleteDirectory(new File(tempdir.toString())); } - } + + /** + * Get the temporary path. + * + * @param label dir name, height or id. + * @return temporary path. + */ private static String getTemporaryPath(String label) { Path temporaryDir = null; Path currentRelativePath = Paths.get(""); @@ -393,10 +398,9 @@ public void testValidatedBlock_PendingTransaction(Blocks blocks, Block block) { blocks); for (Transaction tx : block.getTransactions()) { - when(pendingTransactions.has(tx.id())).thenReturn(true); // pendingTx contains all txs of block + when(pendingTransactions.has(tx.id())).thenReturn(true); } - ingestEngine.process(block); // verification @@ -1481,9 +1485,4 @@ private void testValidatedSingleBlock(Blocks blocks, Block block) { verifyBlockHappyPathCalled(block, blocks, pendingTransactions, transactionIds, seenEntities); } - /** - * Get the temporary path. - * @param label temp file id. - * @return temporary path. - */ } \ No newline at end of file From b44772b579e621715703d7fc68041e1c65d0278e Mon Sep 17 00:00:00 2001 From: Yahya Date: Wed, 28 Sep 2022 09:38:52 -0700 Subject: [PATCH 12/13] adds temp block storage --- .gitignore | 2 +- src/main/java/storage/Blocks.java | 5 ++ src/main/java/storage/mapdb/BlocksMapDb.java | 2 +- src/test/java/storage/BlocksTest.java | 2 +- .../unittest/storage/TempBlocksMapDB.java | 81 +++++++++++++++++++ 5 files changed, 89 insertions(+), 3 deletions(-) create mode 100644 src/test/java/unittest/storage/TempBlocksMapDB.java diff --git a/.gitignore b/.gitignore index 30bc3a22..92be858e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -"# Compiled class file +# Compiled class file *.class # Log file diff --git a/src/main/java/storage/Blocks.java b/src/main/java/storage/Blocks.java index bd645a59..81748635 100644 --- a/src/main/java/storage/Blocks.java +++ b/src/main/java/storage/Blocks.java @@ -57,4 +57,9 @@ public interface Blocks { * @return all stored blocks in database. */ ArrayList all(); + + /** + * Closes the database. + */ + void close(); } diff --git a/src/main/java/storage/mapdb/BlocksMapDb.java b/src/main/java/storage/mapdb/BlocksMapDb.java index c44f6d47..f33690b7 100644 --- a/src/main/java/storage/mapdb/BlocksMapDb.java +++ b/src/main/java/storage/mapdb/BlocksMapDb.java @@ -175,7 +175,7 @@ public ArrayList all() { /** * Close the db. */ - public void closeDb() { + public void close() { dbId.close(); dbHeight.close(); } diff --git a/src/test/java/storage/BlocksTest.java b/src/test/java/storage/BlocksTest.java index 86d3b33a..5a48f059 100644 --- a/src/test/java/storage/BlocksTest.java +++ b/src/test/java/storage/BlocksTest.java @@ -55,7 +55,7 @@ void setup() throws IOException { */ @AfterEach void cleanup() throws IOException { - db.closeDb(); + db.close(); FileUtils.deleteDirectory(new File(tempdir.toString())); } diff --git a/src/test/java/unittest/storage/TempBlocksMapDB.java b/src/test/java/unittest/storage/TempBlocksMapDB.java new file mode 100644 index 00000000..b9b41543 --- /dev/null +++ b/src/test/java/unittest/storage/TempBlocksMapDB.java @@ -0,0 +1,81 @@ +package unittest.storage; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; + +import model.lightchain.Block; +import model.lightchain.Identifier; +import storage.Blocks; +import storage.mapdb.BlocksMapDb; + +public class TempBlocksMapDB implements Blocks { + private static final String TEMP_DIR = "tempdir"; + private static final String TEMP_FILE_ID = "tempfileID.db"; + private static final String TEMP_FILE_HEIGHT = "tempfileHEIGHT.db"; + + private final Blocks blocks; + private final Path tempDir; + + public TempBlocksMapDB() { + Path currentRelativePath = Paths.get(""); + try { + this.tempDir = Files.createTempDirectory(currentRelativePath, TEMP_DIR); + } catch (IOException e) { + throw new RuntimeException(e); + } + this.blocks = new BlocksMapDb(getTemporaryPath(TEMP_FILE_ID), getTemporaryPath(TEMP_FILE_HEIGHT)); + } + + @Override + public boolean has(Identifier blockId) { + return this.blocks.has(blockId); + } + + @Override + public boolean add(Block block) { + return this.blocks.add(block); + } + + @Override + public boolean remove(Identifier blockId) { + return this.blocks.remove(blockId); + } + + @Override + public Block byId(Identifier blockId) { + return this.blocks.byId(blockId); + } + + @Override + public Block atHeight(long height) { + return this.blocks.atHeight(height); + } + + @Override + public ArrayList all() { + return this.blocks.all(); + } + + public void close() { + this.blocks.close(); + try { + Files.deleteIfExists(this.tempDir); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + private String getTemporaryPath(String label) { + Path temporaryDir; + Path currentRelativePath = Paths.get(""); + try { + temporaryDir = Files.createTempDirectory(currentRelativePath, TEMP_DIR); + } catch (IOException e) { + throw new RuntimeException(e); + } + return temporaryDir.toAbsolutePath() + "/" + label; + } +} From 36f2e985775392ccc80418f90df44410751858d7 Mon Sep 17 00:00:00 2001 From: Yahya Date: Thu, 29 Sep 2022 08:17:39 -0700 Subject: [PATCH 13/13] refactors tests with temporary blocks database --- .../protocol/engines/IngestEngineTest.java | 494 ++++++------------ 1 file changed, 151 insertions(+), 343 deletions(-) diff --git a/src/test/java/protocol/engines/IngestEngineTest.java b/src/test/java/protocol/engines/IngestEngineTest.java index 9b0d3b28..46129c1a 100644 --- a/src/test/java/protocol/engines/IngestEngineTest.java +++ b/src/test/java/protocol/engines/IngestEngineTest.java @@ -34,6 +34,7 @@ import unittest.fixtures.BlockFixture; import unittest.fixtures.EntityFixture; import unittest.fixtures.ValidatedTransactionFixture; +import unittest.storage.TempBlocksMapDB; /** * Encapsulates tests for ingest engine implementation. @@ -43,11 +44,29 @@ public class IngestEngineTest { private static final String TEMP_FILE_ID = "tempfileID.db"; private static final String TEMP_FILE_HEIGHT = "tempfileHEIGHT.db"; + /** + * Get the temporary path. + * + * @param label dir name, height or id. + * @return temporary path. + */ + private static String getTemporaryPath(String label) { + Path temporaryDir = null; + Path currentRelativePath = Paths.get(""); + try { + temporaryDir = Files.createTempDirectory(currentRelativePath, TEMP_DIR); + } catch (IOException e) { + Assertions.fail(); + } + return temporaryDir.toAbsolutePath() + "/" + label; + } /** * Evaluates that when a new validated block arrives at ingest engine, * the engine adds the block to its mocked block storage database. * The engine also adds hash of all the transactions of block into its "transactions" database. + *

+ * This test is using a mocked block storage database. */ @Test public void testValidatedSingleBlock_MockedStorage() { @@ -62,38 +81,30 @@ public void testValidatedSingleBlock_MockedStorage() { * Evaluates that when a new validated block arrives at ingest engine, * the engine adds the block to its real block storage database. * The engine also adds hash of all the transactions of block into its "transactions" database. + *

+ * This test is using a real block storage database. */ @Test public void testValidatedSingleBlock_RealStorage() { - - Path tempdir = null; - BlocksMapDb db = null; + Blocks blocks = null; try { - Path currentRelativePath = Paths.get(""); - tempdir = Files.createTempDirectory(currentRelativePath, TEMP_DIR); - db = new BlocksMapDb(getTemporaryPath(TEMP_FILE_ID), - getTemporaryPath(TEMP_FILE_HEIGHT)); - Blocks blocks = db; + blocks = new TempBlocksMapDB(); Block block = BlockFixture.newBlock(); testValidatedSingleBlock(blocks, block); Assertions.assertTrue(blocks.has(block.id())); - } catch (IOException e) { - Assertions.fail(); } finally { - db.closeDb(); - try { - FileUtils.deleteDirectory(new File(tempdir.toString())); - } catch (IOException e) { - Assertions.fail(); + if (blocks != null) { + blocks.close(); } } - } /** * Evaluates that when two validated blocks arrive at ingest engine SEQUENTIALLY, * the engine adds the blocks to its mocked block storage database. * The engine also adds hash of all the transactions of blocks into its "transactions" database. + *

+ * This test is using a mocked blocks storage database. */ @Test public void testValidatedTwoBlocks_MockedStorage() { @@ -115,31 +126,23 @@ public void testValidatedTwoBlocks_MockedStorage() { * Evaluates that when two validated blocks arrive at ingest engine SEQUENTIALLY, * the engine adds the blocks to its real block storage database. * The engine also adds hash of all the transactions of blocks into its "transactions" database. + *

+ * This test is using a real block storage database. */ @Test public void testValidatedTwoBlocks_RealStorage() { - Path tempdir = null; - BlocksMapDb db = null; + Blocks blocks = null; try { - Path currentRelativePath = Paths.get(""); - tempdir = Files.createTempDirectory(currentRelativePath, TEMP_DIR); - db = new BlocksMapDb(getTemporaryPath(TEMP_FILE_ID), - getTemporaryPath(TEMP_FILE_HEIGHT)); - Blocks blocks = db; + blocks = new TempBlocksMapDB(); Block block1 = BlockFixture.newBlock(); Block block2 = BlockFixture.newBlock(); testValidatedTwoBlocks(blocks, block1, block2); Assertions.assertTrue(blocks.has(block1.id())); Assertions.assertTrue(blocks.has(block2.id())); - } catch (IOException e) { - Assertions.fail(); } finally { - db.closeDb(); - try { - FileUtils.deleteDirectory(new File(tempdir.toString())); - } catch (IOException e) { - Assertions.fail(); + if (blocks != null) { + blocks.close(); } } @@ -150,17 +153,13 @@ public void testValidatedTwoBlocks_RealStorage() { * * @param blocks mocked or real block. */ + // TODO: this method should be refactored with parameters option. public void testValidatedTwoBlocks(Blocks blocks, Block block1, Block block2) { Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); - IngestEngine ingestEngine = this.mockIngestEngineForEntities( - new ArrayList<>(Arrays.asList(block1, block2)), - seenEntities, - transactionIds, - pendingTransactions, - blocks); + IngestEngine ingestEngine = this.mockIngestEngineForEntities(new ArrayList<>(Arrays.asList(block1, block2)), seenEntities, transactionIds, pendingTransactions, blocks); ingestEngine.process(block1); ingestEngine.process(block2); @@ -168,13 +167,14 @@ public void testValidatedTwoBlocks(Blocks blocks, Block block1, Block block2) { verifyBlockHappyPathCalled(block1, blocks, pendingTransactions, transactionIds, seenEntities); verifyBlockHappyPathCalled(block2, blocks, pendingTransactions, transactionIds, seenEntities); - } /** * Evaluates that when two validated blocks arrive at ingest engine concurrently, * the engine adds the blocks to its mocked block storage database. * The engine also adds hash of all the transactions of blocks into its "transactions" database. + *

+ * This test is using a mocked block storage database. */ @Test public void testValidatedTwoBlocks_Concurrently_MockedBlocks() { @@ -194,33 +194,26 @@ public void testValidatedTwoBlocks_Concurrently_MockedBlocks() { * Evaluates that when two validated blocks arrive at ingest engine concurrently, * the engine adds the blocks to its real block storage database. * The engine also adds hash of all the transactions of blocks into its "transactions" database. + *

+ * This test is using a real block storage database. */ @Test public void testValidatedTwoBlocks_Concurrently_RealBlocks() { - Path tempdir = null; - BlocksMapDb db = null; + Blocks blocks = null; try { - Path currentRelativePath = Paths.get(""); - tempdir = Files.createTempDirectory(currentRelativePath, TEMP_DIR); - db = new BlocksMapDb(getTemporaryPath(TEMP_FILE_ID), - getTemporaryPath(TEMP_FILE_HEIGHT)); - Blocks blocks = db; + blocks = new TempBlocksMapDB(); + Block block1 = BlockFixture.newBlock(); Block block2 = BlockFixture.newBlock(); + testValidatedTwoBlocks_Concurrently(blocks, block1, block2); Assertions.assertTrue(blocks.has(block1.id())); Assertions.assertTrue(blocks.has(block2.id())); - } catch (IOException e) { - Assertions.fail(); } finally { - db.closeDb(); - try { - FileUtils.deleteDirectory(new File(tempdir.toString())); - } catch (IOException e) { - Assertions.fail(); + if (blocks != null) { + blocks.close(); } } - } /** @@ -228,21 +221,15 @@ public void testValidatedTwoBlocks_Concurrently_RealBlocks() { * * @param blocks mocked or real block. */ + // TODO: this method should be refactored with parameters option. public void testValidatedTwoBlocks_Concurrently(Blocks blocks, Block block1, Block block2) { Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); - IngestEngine ingestEngine = this.mockIngestEngineForEntities( - new ArrayList<>(Arrays.asList(block1, block2)), - seenEntities, - transactionIds, - pendingTransactions, - blocks); + IngestEngine ingestEngine = this.mockIngestEngineForEntities(new ArrayList<>(Arrays.asList(block1, block2)), seenEntities, transactionIds, pendingTransactions, blocks); - this.processEntitiesConcurrently( - ingestEngine, - new ArrayList<>(Arrays.asList(block1, block2))); + this.processEntitiesConcurrently(ingestEngine, new ArrayList<>(Arrays.asList(block1, block2))); // verification for block 1 verifyBlockHappyPathCalled(block1, blocks, pendingTransactions, transactionIds, seenEntities); @@ -255,6 +242,8 @@ public void testValidatedTwoBlocks_Concurrently(Blocks blocks, Block block1, Blo * Evaluates that when two same validated blocks arrive at ingest engine (second one should be ignored), * the engine adds the blocks to its mocked block storage database. * The engine also adds hash of all the transactions of blocks into its "transactions" database. + *

+ * This test is using a mocked block storage database. */ @Test public void testValidated_SameTwoBlocks_MockedStorage() { @@ -263,50 +252,28 @@ public void testValidated_SameTwoBlocks_MockedStorage() { when(blocks.has(block.id())).thenReturn(false); testValidated_SameTwoBlocks(blocks, block); verify(blocks, times(1)).add(block); - } /** * Evaluates that when two same validated blocks arrive at ingest engine (second one should be ignored), * the engine adds the blocks to its real block storage database. * The engine also adds hash of all the transactions of blocks into its "transactions" database. + *

+ * This test is using a real block storage database. */ @Test - public void testValidated_SameTwoBlocks_RealStorage() throws IOException { - Path tempdir = null; - BlocksMapDb db = null; + public void testValidated_SameTwoBlocks_RealStorage() { + Blocks blocks = null; try { - Path currentRelativePath = Paths.get(""); - tempdir = Files.createTempDirectory(currentRelativePath, TEMP_DIR); - db = new BlocksMapDb(getTemporaryPath(TEMP_FILE_ID), - getTemporaryPath(TEMP_FILE_HEIGHT)); - Blocks blocks = db; + blocks = new TempBlocksMapDB(); Block block = BlockFixture.newBlock(); testValidated_SameTwoBlocks(blocks, block); Assertions.assertTrue(blocks.has(block.id())); - } catch (IOException e) { - Assertions.fail(); } finally { - db.closeDb(); - FileUtils.deleteDirectory(new File(tempdir.toString())); - } - } - - /** - * Get the temporary path. - * - * @param label dir name, height or id. - * @return temporary path. - */ - private static String getTemporaryPath(String label) { - Path temporaryDir = null; - Path currentRelativePath = Paths.get(""); - try { - temporaryDir = Files.createTempDirectory(currentRelativePath, TEMP_DIR); - } catch (IOException e) { - Assertions.fail(); + if (blocks != null) { + blocks.close(); + } } - return temporaryDir.toAbsolutePath() + "/" + label; } /** @@ -314,6 +281,7 @@ private static String getTemporaryPath(String label) { * * @param blocks mocked or real blocks. */ + // TODO: this method should be refactored with parameters option. public void testValidated_SameTwoBlocks(Blocks blocks, Block block) { Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); @@ -321,12 +289,7 @@ public void testValidated_SameTwoBlocks(Blocks blocks, Block block) { when(seenEntities.has(block.id())).thenReturn(false); - IngestEngine ingestEngine = this.mockIngestEngineForEntities( - new ArrayList<>(List.of(block)), - seenEntities, - transactionIds, - pendingTransactions, - blocks); + IngestEngine ingestEngine = this.mockIngestEngineForEntities(new ArrayList<>(List.of(block)), seenEntities, transactionIds, pendingTransactions, blocks); ingestEngine.process(block); when(seenEntities.has(block.id())).thenReturn(true); // block is already seen @@ -340,6 +303,8 @@ public void testValidated_SameTwoBlocks(Blocks blocks, Block block) { * Evaluates that when a new validated block (with shared transactions in pendingTx) arrive at ingest engine, * the engine adds the blocks to its real block storage database. * The engine also removes hash of the transactions of blocks from pendingTransactions. + *

+ * This test is using a mocked block storage database. */ @Test public void testValidatedBlock_PendingTransaction_MockedStorage() { @@ -354,28 +319,20 @@ public void testValidatedBlock_PendingTransaction_MockedStorage() { * Evaluates that when a new validated block (with shared transactions in pendingTx) arrive at ingest engine, * the engine adds the blocks to its real block storage database. * The engine also removes hash of the transactions of blocks from pendingTransactions. + *

+ * This test is using a real block storage database. */ @Test public void testValidatedBlock_PendingTransaction_RealStorage() { - Path tempdir = null; - BlocksMapDb db = null; + Blocks blocks = null; try { - Path currentRelativePath = Paths.get(""); - tempdir = Files.createTempDirectory(currentRelativePath, TEMP_DIR); - db = new BlocksMapDb(getTemporaryPath(TEMP_FILE_ID), - getTemporaryPath(TEMP_FILE_HEIGHT)); - Blocks blocks = db; + blocks = new TempBlocksMapDB(); Block block = BlockFixture.newBlock(); testValidatedBlock_PendingTransaction(blocks, block); Assertions.assertTrue(blocks.has(block.id())); - } catch (IOException e) { - Assertions.fail(); } finally { - db.closeDb(); - try { - FileUtils.deleteDirectory(new File(tempdir.toString())); - } catch (IOException e) { - Assertions.fail(); + if (blocks != null) { + blocks.close(); } } } @@ -385,17 +342,13 @@ public void testValidatedBlock_PendingTransaction_RealStorage() { * * @param blocks mocked or real block. */ + // TODO: this method should be refactored with parameters option. public void testValidatedBlock_PendingTransaction(Blocks blocks, Block block) { Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); - IngestEngine ingestEngine = this.mockIngestEngineForEntities( - new ArrayList<>(List.of(block)), - seenEntities, - transactionIds, - pendingTransactions, - blocks); + IngestEngine ingestEngine = this.mockIngestEngineForEntities(new ArrayList<>(List.of(block)), seenEntities, transactionIds, pendingTransactions, blocks); for (Transaction tx : block.getTransactions()) { when(pendingTransactions.has(tx.id())).thenReturn(true); @@ -415,6 +368,8 @@ public void testValidatedBlock_PendingTransaction(Blocks blocks, Block block) { * Evaluates that when two new validated blocks (with a shared transactions in pendingTx, disjoint set) * arrive at ingest engine, the engine adds the blocks to its real block storage database. * The engine also removes the hash of the single shared transaction among blocks from pending transactions. + *

+ * This test is using a mocked block storage database. */ @Test public void testConcurrentBlock_DisjointSet_MockedStorage() { @@ -424,40 +379,31 @@ public void testConcurrentBlock_DisjointSet_MockedStorage() { testConcurrentBlock_SeenTransaction_DisjointSet(blocks, block1, block2); verify(blocks, times(1)).add(block1); verify(blocks, times(1)).add(block2); - } /** * Evaluates that when two new validated blocks (with a shared transactions in pendingTx, disjoint set) * arrive at ingest engine, the engine adds the blocks to its real block storage database. * The engine also removes the hash of the single shared transaction among blocks from pending transactions. + *

+ * This test is using a real block storage database. */ @Test public void testConcurrentBlock_DisjointSet_RealStorage() { - Path tempdir = null; - BlocksMapDb db = null; + Blocks blocks = null; try { - Path currentRelativePath = Paths.get(""); - tempdir = Files.createTempDirectory(currentRelativePath, TEMP_DIR); - db = new BlocksMapDb(getTemporaryPath(TEMP_FILE_ID), - getTemporaryPath(TEMP_FILE_HEIGHT)); - Blocks blocks = db; + blocks = new TempBlocksMapDB(); + Block block1 = BlockFixture.newBlock(); Block block2 = BlockFixture.newBlock(); testConcurrentBlock_SeenTransaction_DisjointSet(blocks, block1, block2); Assertions.assertTrue(blocks.has(block1.id())); Assertions.assertTrue(blocks.has(block2.id())); - } catch (IOException e) { - Assertions.fail(); } finally { - db.closeDb(); - try { - FileUtils.deleteDirectory(new File(tempdir.toString())); - } catch (IOException e) { - Assertions.fail(); + if (blocks != null) { + blocks.close(); } } - } /** @@ -465,6 +411,7 @@ public void testConcurrentBlock_DisjointSet_RealStorage() { * * @param blocks mocked or real block. */ + // TODO: this method should be refactored with parameters option. private void testConcurrentBlock_SeenTransaction_DisjointSet(Blocks blocks, Block block1, Block block2) { Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); @@ -472,20 +419,13 @@ private void testConcurrentBlock_SeenTransaction_DisjointSet(Blocks blocks, Bloc ArrayList accounts = new ArrayList<>(AccountFixture.newAccounts(10, 10).values()); - IngestEngine ingestEngine = this.mockIngestEngineForEntities( - new ArrayList<>(Arrays.asList(block1, block2)), - seenEntities, - transactionIds, - pendingTransactions, - blocks); + IngestEngine ingestEngine = this.mockIngestEngineForEntities(new ArrayList<>(Arrays.asList(block1, block2)), seenEntities, transactionIds, pendingTransactions, blocks); // simulates 1 shared transaction for each block when(pendingTransactions.has(block1.getTransactions()[0].id())).thenReturn(true); when(pendingTransactions.has(block2.getTransactions()[0].id())).thenReturn(true); - processEntitiesConcurrently( - ingestEngine, - new ArrayList<>(Arrays.asList(block1, block2))); + processEntitiesConcurrently(ingestEngine, new ArrayList<>(Arrays.asList(block1, block2))); // verification for block1 verifyBlockHappyPathCalled(block1, blocks, pendingTransactions, transactionIds, seenEntities); @@ -503,24 +443,18 @@ private void testConcurrentBlock_SeenTransaction_DisjointSet(Blocks blocks, Bloc * * @param blocks mocked or real block. */ + // TODO: this method should be refactored with parameters option. private void testConcurrentBlock_OverlappingSet(Blocks blocks, Block block1, Block block2) { Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); - IngestEngine ingestEngine = this.mockIngestEngineForEntities( - new ArrayList<>(Arrays.asList(block1, block2)), - seenEntities, - transactionIds, - pendingTransactions, - blocks); + IngestEngine ingestEngine = this.mockIngestEngineForEntities(new ArrayList<>(Arrays.asList(block1, block2)), seenEntities, transactionIds, pendingTransactions, blocks); // simulates an overlapping set of shared transactions when(pendingTransactions.has(any(Identifier.class))).thenReturn(true); - processEntitiesConcurrently( - ingestEngine, - new ArrayList<>(Arrays.asList(block1, block2))); + processEntitiesConcurrently(ingestEngine, new ArrayList<>(Arrays.asList(block1, block2))); // verification for block1 verifyBlockHappyPathCalled(block1, blocks, pendingTransactions, transactionIds, seenEntities); @@ -539,6 +473,8 @@ private void testConcurrentBlock_OverlappingSet(Blocks blocks, Block block1, Blo * Evaluates that when two new validated blocks (with shared transactions in pendingTx, overlapping set) * arrive at ingest engine, the engine adds the blocks to its mocked block storage database. * The engine also removes hash of the transactions of blocks from pendingTransactions. + *

+ * This test is using a mocked block storage database. */ @Test public void testConcurrentBlock_OverlappingSet_MockedStorage() { @@ -556,46 +492,35 @@ public void testConcurrentBlock_OverlappingSet_MockedStorage() { * Evaluates that when two new validated blocks (with shared transactions in pendingTx, overlapping set) * arrive at ingest engine, the engine adds the blocks to its real block storage database. * The engine also removes hash of the transactions of blocks from pendingTransactions. + *

+ * This test is using a real block storage database. */ @Test public void testConcurrentBlock_OverlappingSet_RealStorage() { - Path tempdir = null; - BlocksMapDb db = null; + Blocks blocks = null; try { - Path currentRelativePath = Paths.get(""); - tempdir = Files.createTempDirectory(currentRelativePath, TEMP_DIR); - db = new BlocksMapDb(getTemporaryPath(TEMP_FILE_ID), - getTemporaryPath(TEMP_FILE_HEIGHT)); - Blocks blocks = db; + blocks = new TempBlocksMapDB(); + Block block1 = BlockFixture.newBlock(); Block block2 = BlockFixture.newBlock(); testConcurrentBlock_OverlappingSet(blocks, block1, block2); Assertions.assertTrue(blocks.has(block1.id())); Assertions.assertTrue(blocks.has(block2.id())); - } catch (IOException e) { - Assertions.fail(); } finally { - db.closeDb(); - try { - FileUtils.deleteDirectory(new File(tempdir.toString())); - } catch (IOException e) { - Assertions.fail(); + if (blocks != null) { + blocks.close(); } } } + // TODO: this method should be refactored with parameters option. private void testValidated_AlreadyIngestedBlock(Blocks blocks, Block block) { Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); Transactions pendingTransactions = mock(Transactions.class); - IngestEngine ingestEngine = this.mockIngestEngineForEntities( - new ArrayList<>(List.of(block)), - seenEntities, - transactionIds, - pendingTransactions, - blocks); + IngestEngine ingestEngine = this.mockIngestEngineForEntities(new ArrayList<>(List.of(block)), seenEntities, transactionIds, pendingTransactions, blocks); when(seenEntities.has(block.id())).thenReturn(true); // block is already ingested @@ -612,6 +537,8 @@ private void testValidated_AlreadyIngestedBlock(Blocks blocks, Block block) { /** * Evaluates that when an already ingested validated block arrives at ingest engine, * the engine discards the block right away. + *

+ * This test is using a mocked block storage database. */ @Test public void testValidated_AlreadyIngestedBlock_MockedStorage() { @@ -624,27 +551,19 @@ public void testValidated_AlreadyIngestedBlock_MockedStorage() { /** * Evaluates that when an already ingested validated block arrives at ingest engine, * the engine discards the block right away. + *

+ * This test is using a real block storage database. */ @Test public void testValidated_AlreadyIngestedBlock_RealStorage() { - Path tempdir = null; - BlocksMapDb db = null; + Blocks blocks = null; try { - Path currentRelativePath = Paths.get(""); - tempdir = Files.createTempDirectory(currentRelativePath, TEMP_DIR); - db = new BlocksMapDb(getTemporaryPath(TEMP_FILE_ID), - getTemporaryPath(TEMP_FILE_HEIGHT)); - Blocks blocks = db; + blocks = new TempBlocksMapDB(); Block block = BlockFixture.newBlock(); testValidated_AlreadyIngestedBlock(blocks, block); - } catch (IOException e) { - Assertions.fail(); } finally { - db.closeDb(); - try { - FileUtils.deleteDirectory(new File(tempdir.toString())); - } catch (IOException e) { - Assertions.fail(); + if (blocks != null) { + blocks.close(); } } @@ -662,12 +581,7 @@ private void testValidatedTransaction(Blocks blocks) { ValidatedTransaction tx = ValidatedTransactionFixture.newValidatedTransaction(); - IngestEngine ingestEngine = this.mockIngestEngineForEntities( - new ArrayList<>(List.of(tx)), - seenEntities, - transactionIds, - pendingTransactions, - blocks); + IngestEngine ingestEngine = this.mockIngestEngineForEntities(new ArrayList<>(List.of(tx)), seenEntities, transactionIds, pendingTransactions, blocks); ingestEngine.process(tx); @@ -677,6 +591,8 @@ private void testValidatedTransaction(Blocks blocks) { /** * Evaluates that when a new validated transaction arrives at ingest engine, with mocked blocks storage, * the engine adds hash of the transaction into its pending transactions' database. + *

+ * This test is using a mocked block storage database. */ @Test public void testValidatedTransaction_MockedStorage() { @@ -687,29 +603,20 @@ public void testValidatedTransaction_MockedStorage() { /** * Evaluates that when a new validated transaction arrives at ingest engine, with real blocks storage, * the engine adds hash of the transaction into its pending transactions' database. + *

+ * This test is using a real block storage database. */ @Test public void testValidatedTransaction_RealStorage() { - Path tempdir = null; - BlocksMapDb db = null; + Blocks blocks = null; try { - Path currentRelativePath = Paths.get(""); - tempdir = Files.createTempDirectory(currentRelativePath, TEMP_DIR); - db = new BlocksMapDb(getTemporaryPath(TEMP_FILE_ID), - getTemporaryPath(TEMP_FILE_HEIGHT)); - Blocks blocks = db; + blocks = new TempBlocksMapDB(); testValidatedTransaction(blocks); - } catch (IOException e) { - Assertions.fail(); } finally { - db.closeDb(); - try { - FileUtils.deleteDirectory(new File(tempdir.toString())); - } catch (IOException e) { - Assertions.fail(); + if (blocks != null) { + blocks.close(); } } - } /** @@ -717,6 +624,7 @@ public void testValidatedTransaction_RealStorage() { * * @param blocks mocked or real block. */ + // TODO: this method should be refactored with parameters option. private void testValidated_TwoTransactions(Blocks blocks) { Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); @@ -728,12 +636,7 @@ private void testValidated_TwoTransactions(Blocks blocks) { when(transactionIds.has(any(Identifier.class))).thenReturn(false); when(pendingTransactions.has(any(Identifier.class))).thenReturn(false); - IngestEngine ingestEngine = this.mockIngestEngineForEntities( - new ArrayList<>(Arrays.asList(tx1, tx2)), - seenEntities, - transactionIds, - pendingTransactions, - blocks); + IngestEngine ingestEngine = this.mockIngestEngineForEntities(new ArrayList<>(Arrays.asList(tx1, tx2)), seenEntities, transactionIds, pendingTransactions, blocks); ingestEngine.process(tx1); ingestEngine.process(tx2); @@ -759,26 +662,15 @@ public void testValidated_TwoTransactions_MockedBlocksStorage() { */ @Test public void testValidated_TwoTransactions_RealBlocksStorage() { - Path tempdir = null; - BlocksMapDb db = null; + Blocks blocks = null; try { - Path currentRelativePath = Paths.get(""); - tempdir = Files.createTempDirectory(currentRelativePath, TEMP_DIR); - db = new BlocksMapDb(getTemporaryPath(TEMP_FILE_ID), - getTemporaryPath(TEMP_FILE_HEIGHT)); - Blocks blocks = db; + blocks = new TempBlocksMapDB(); testValidated_TwoTransactions(blocks); - } catch (IOException e) { - Assertions.fail(); } finally { - db.closeDb(); - try { - FileUtils.deleteDirectory(new File(tempdir.toString())); - } catch (IOException e) { - Assertions.fail(); + if (blocks != null) { + blocks.close(); } } - } /** @@ -786,6 +678,7 @@ public void testValidated_TwoTransactions_RealBlocksStorage() { * * @param blocks mocked or real block. */ + // TODO: this method should be refactored with parameters option. private void testConcurrent_ValidatedTwoTransactions(Blocks blocks) { Identifiers seenEntities = mock(Identifiers.class); Identifiers transactionIds = mock(Identifiers.class); @@ -794,16 +687,9 @@ private void testConcurrent_ValidatedTwoTransactions(Blocks blocks) { ValidatedTransaction tx1 = ValidatedTransactionFixture.newValidatedTransaction(); ValidatedTransaction tx2 = ValidatedTransactionFixture.newValidatedTransaction(); - IngestEngine ingestEngine = this.mockIngestEngineForEntities( - new ArrayList<>(Arrays.asList(tx1, tx2)), - seenEntities, - transactionIds, - pendingTransactions, - blocks); + IngestEngine ingestEngine = this.mockIngestEngineForEntities(new ArrayList<>(Arrays.asList(tx1, tx2)), seenEntities, transactionIds, pendingTransactions, blocks); - processEntitiesConcurrently( - ingestEngine, - new ArrayList<>(List.of(tx1, tx2))); + processEntitiesConcurrently(ingestEngine, new ArrayList<>(List.of(tx1, tx2))); // verification of tx1 verifyTransactionHappyPathCalled(tx1, seenEntities, transactionIds, pendingTransactions); @@ -820,7 +706,6 @@ private void testConcurrent_ValidatedTwoTransactions(Blocks blocks) { public void testConcurrent_TwoTransactions_MockedBlockStorage() { Blocks blocks = mock(Blocks.class); testConcurrent_ValidatedTwoTransactions(blocks); - } /** @@ -829,23 +714,13 @@ public void testConcurrent_TwoTransactions_MockedBlockStorage() { */ @Test public void testConcurrent_TwoTransactions_RealBlockStorage() { - Path tempdir = null; - BlocksMapDb db = null; + Blocks blocks = null; try { - Path currentRelativePath = Paths.get(""); - tempdir = Files.createTempDirectory(currentRelativePath, TEMP_DIR); - db = new BlocksMapDb(getTemporaryPath(TEMP_FILE_ID), - getTemporaryPath(TEMP_FILE_HEIGHT)); - Blocks blocks = db; + blocks = new TempBlocksMapDB(); testConcurrent_ValidatedTwoTransactions(blocks); - } catch (IOException e) { - Assertions.fail(); } finally { - db.closeDb(); - try { - FileUtils.deleteDirectory(new File(tempdir.toString())); - } catch (IOException e) { - Assertions.fail(); + if (blocks != null) { + blocks.close(); } } } @@ -865,12 +740,7 @@ private void testValidated_SameTwoTransactions(Blocks blocks) { when(transactionIds.has(tx.id())).thenReturn(false); when(pendingTransactions.has(tx.id())).thenReturn(false); - IngestEngine ingestEngine = this.mockIngestEngineForEntities( - new ArrayList<>(List.of(tx)), - seenEntities, - transactionIds, - pendingTransactions, - blocks); + IngestEngine ingestEngine = this.mockIngestEngineForEntities(new ArrayList<>(List.of(tx)), seenEntities, transactionIds, pendingTransactions, blocks); ingestEngine.process(tx); when(seenEntities.has(tx.id())).thenReturn(true); @@ -905,14 +775,13 @@ public void testValidated_SameTwoTransactions_RealBlocks() { try { Path currentRelativePath = Paths.get(""); tempdir = Files.createTempDirectory(currentRelativePath, TEMP_DIR); - db = new BlocksMapDb(getTemporaryPath(TEMP_FILE_ID), - getTemporaryPath(TEMP_FILE_HEIGHT)); + db = new BlocksMapDb(getTemporaryPath(TEMP_FILE_ID), getTemporaryPath(TEMP_FILE_HEIGHT)); Blocks blocks = db; testValidated_SameTwoTransactions(blocks); } catch (IOException e) { Assertions.fail(); } finally { - db.closeDb(); + db.close(); try { FileUtils.deleteDirectory(new File(tempdir.toString())); } catch (IOException e) { @@ -933,12 +802,7 @@ private void testAlreadyInTransaction_IdStorage(Blocks blocks) { ValidatedTransaction tx = ValidatedTransactionFixture.newValidatedTransaction(); - final IngestEngine ingestEngine = this.mockIngestEngineForEntities( - new ArrayList<>(List.of(tx)), - seenEntities, - transactionIds, - pendingTransactions, - blocks); + final IngestEngine ingestEngine = this.mockIngestEngineForEntities(new ArrayList<>(List.of(tx)), seenEntities, transactionIds, pendingTransactions, blocks); // transaction is not seen, but it is in transaction ids storage (as the result of processing a validated block). when(seenEntities.has(tx.id())).thenReturn(false); @@ -974,14 +838,13 @@ public void testAlreadyInTransaction_IdStorage_RealBlocks() { try { Path currentRelativePath = Paths.get(""); tempdir = Files.createTempDirectory(currentRelativePath, TEMP_DIR); - db = new BlocksMapDb(getTemporaryPath(TEMP_FILE_ID), - getTemporaryPath(TEMP_FILE_HEIGHT)); + db = new BlocksMapDb(getTemporaryPath(TEMP_FILE_ID), getTemporaryPath(TEMP_FILE_HEIGHT)); Blocks blocks = db; testAlreadyInTransaction_IdStorage(blocks); } catch (IOException e) { Assertions.fail(); } finally { - db.closeDb(); + db.close(); try { FileUtils.deleteDirectory(new File(tempdir.toString())); } catch (IOException e) { @@ -1011,14 +874,13 @@ public void testNeitherBlockNorTransaction_RealBlocks() { try { Path currentRelativePath = Paths.get(""); tempdir = Files.createTempDirectory(currentRelativePath, TEMP_DIR); - db = new BlocksMapDb(getTemporaryPath(TEMP_FILE_ID), - getTemporaryPath(TEMP_FILE_HEIGHT)); + db = new BlocksMapDb(getTemporaryPath(TEMP_FILE_ID), getTemporaryPath(TEMP_FILE_HEIGHT)); Blocks blocks = db; testNeitherBlockNorTransaction(blocks); } catch (IOException e) { Assertions.fail(); } finally { - db.closeDb(); + db.close(); try { FileUtils.deleteDirectory(new File(tempdir.toString())); } catch (IOException e) { @@ -1040,13 +902,7 @@ public void testNeitherBlockNorTransaction(Blocks blocks) { Identifiers seenEntities = mock(Identifiers.class); Entity e = new EntityFixture(); // not a block nor a transaction - IngestEngine ingestEngine = new IngestEngine( - state, - blocks, - transactionIds, - pendingTransactions, - seenEntities, - assigner); + IngestEngine ingestEngine = new IngestEngine(state, blocks, transactionIds, pendingTransactions, seenEntities, assigner); Assertions.assertThrows(IllegalArgumentException.class, () -> ingestEngine.process(e)); } @@ -1063,16 +919,9 @@ private void testConcurrent_TransactionAndBlock_NonOverlapping(Blocks blocks) { ValidatedTransaction validatedTx = ValidatedTransactionFixture.newValidatedTransaction(); Block block = BlockFixture.newBlock(); - IngestEngine ingestEngine = this.mockIngestEngineForEntities( - new ArrayList<>(Arrays.asList(block, validatedTx)), - seenEntities, - transactionIds, - pendingTransactions, - blocks); + IngestEngine ingestEngine = this.mockIngestEngineForEntities(new ArrayList<>(Arrays.asList(block, validatedTx)), seenEntities, transactionIds, pendingTransactions, blocks); - processEntitiesConcurrently( - ingestEngine, - new ArrayList<>(List.of(block, validatedTx))); + processEntitiesConcurrently(ingestEngine, new ArrayList<>(List.of(block, validatedTx))); // verification for block verifyBlockHappyPathCalled(block, blocks, pendingTransactions, transactionIds, seenEntities); @@ -1108,14 +957,13 @@ public void testConcurrent_TransactionAndBlock_NonOverlapping_RealBlocks() { try { Path currentRelativePath = Paths.get(""); tempdir = Files.createTempDirectory(currentRelativePath, TEMP_DIR); - db = new BlocksMapDb(getTemporaryPath(TEMP_FILE_ID), - getTemporaryPath(TEMP_FILE_HEIGHT)); + db = new BlocksMapDb(getTemporaryPath(TEMP_FILE_ID), getTemporaryPath(TEMP_FILE_HEIGHT)); Blocks blocks = db; testConcurrent_TransactionAndBlock_NonOverlapping(blocks); } catch (IOException e) { Assertions.fail(); } finally { - db.closeDb(); + db.close(); try { FileUtils.deleteDirectory(new File(tempdir.toString())); } catch (IOException e) { @@ -1135,12 +983,7 @@ private void testIncludedTransaction_BlockFirst(Blocks blocks, Block block) { Transactions pendingTransactions = mock(Transactions.class); ValidatedTransaction validatedTx = block.getTransactions()[0]; // the transaction is in the block - IngestEngine ingestEngine = this.mockIngestEngineForEntities( - new ArrayList<>(Arrays.asList(block, validatedTx)), - seenEntities, - transactionIds, - pendingTransactions, - blocks); + IngestEngine ingestEngine = this.mockIngestEngineForEntities(new ArrayList<>(Arrays.asList(block, validatedTx)), seenEntities, transactionIds, pendingTransactions, blocks); // process block ingestEngine.process(block); @@ -1191,8 +1034,7 @@ public void testIncludedTransaction_BlockFirst_RealBlocks() { try { Path currentRelativePath = Paths.get(""); tempdir = Files.createTempDirectory(currentRelativePath, TEMP_DIR); - db = new BlocksMapDb(getTemporaryPath(TEMP_FILE_ID), - getTemporaryPath(TEMP_FILE_HEIGHT)); + db = new BlocksMapDb(getTemporaryPath(TEMP_FILE_ID), getTemporaryPath(TEMP_FILE_HEIGHT)); Blocks blocks = db; Block block = BlockFixture.newBlock(); testIncludedTransaction_BlockFirst(blocks, block); @@ -1200,7 +1042,7 @@ public void testIncludedTransaction_BlockFirst_RealBlocks() { } catch (IOException e) { Assertions.fail(); } finally { - db.closeDb(); + db.close(); try { FileUtils.deleteDirectory(new File(tempdir.toString())); } catch (IOException e) { @@ -1221,12 +1063,7 @@ private void testIncludedTransaction_TransactionFirst(Blocks blocks, Block block ValidatedTransaction validatedTx = block.getTransactions()[0]; // the transaction is in the block - IngestEngine ingestEngine = this.mockIngestEngineForEntities( - new ArrayList<>(Arrays.asList(block, validatedTx)), - seenEntities, - transactionIds, - pendingTransactions, - blocks); + IngestEngine ingestEngine = this.mockIngestEngineForEntities(new ArrayList<>(Arrays.asList(block, validatedTx)), seenEntities, transactionIds, pendingTransactions, blocks); // process transaction first. ingestEngine.process(validatedTx); @@ -1280,8 +1117,7 @@ public void testIncludedTransaction_TransactionFirst_RealBlocks() { try { Path currentRelativePath = Paths.get(""); tempdir = Files.createTempDirectory(currentRelativePath, TEMP_DIR); - db = new BlocksMapDb(getTemporaryPath(TEMP_FILE_ID), - getTemporaryPath(TEMP_FILE_HEIGHT)); + db = new BlocksMapDb(getTemporaryPath(TEMP_FILE_ID), getTemporaryPath(TEMP_FILE_HEIGHT)); Blocks blocks = db; Block block = BlockFixture.newBlock(); testIncludedTransaction_TransactionFirst(blocks, block); @@ -1289,7 +1125,7 @@ public void testIncludedTransaction_TransactionFirst_RealBlocks() { } catch (IOException e) { Assertions.fail(); } finally { - db.closeDb(); + db.close(); try { FileUtils.deleteDirectory(new File(tempdir.toString())); } catch (IOException e) { @@ -1311,12 +1147,7 @@ public void testIncludedTransaction_TransactionFirst_RealBlocks() { * @param txIds the transaction identifiers. * @param seenEntities identifiers of processed entities by engine. */ - private void verifyBlockHappyPathCalled( - Block block, - Blocks blocks, - Transactions pendingTx, - Identifiers txIds, - Identifiers seenEntities) { + private void verifyBlockHappyPathCalled(Block block, Blocks blocks, Transactions pendingTx, Identifiers txIds, Identifiers seenEntities) { verify(seenEntities, times(1)).add(block.id()); for (Transaction tx : block.getTransactions()) { verify(pendingTx, times(1)).has(tx.id()); @@ -1333,11 +1164,7 @@ private void verifyBlockHappyPathCalled( * @param txIds the transaction identifiers. * @param pendingTx the pending transactions identifiers. */ - private void verifyTransactionHappyPathCalled( - Transaction transaction, - Identifiers seenEntities, - Identifiers txIds, - Transactions pendingTx) { + private void verifyTransactionHappyPathCalled(Transaction transaction, Identifiers seenEntities, Identifiers txIds, Transactions pendingTx) { verify(seenEntities, times(1)).add(transaction.id()); verify(txIds, times(1)).has(transaction.id()); @@ -1354,12 +1181,7 @@ private void verifyTransactionHappyPathCalled( * @param blocks the blocks storage component. * @return mocked ingest engine with mocked components. */ - private IngestEngine mockIngestEngineForEntities( - ArrayList entities, - Identifiers seenEntities, - Identifiers txIds, - Transactions pendingTx, - Blocks blocks) { + private IngestEngine mockIngestEngineForEntities(ArrayList entities, Identifiers seenEntities, Identifiers txIds, Transactions pendingTx, Blocks blocks) { Snapshot snapshot = mock(Snapshot.class); AssignerInf assigner = mock(AssignerInf.class); @@ -1393,13 +1215,7 @@ private IngestEngine mockIngestEngineForEntities( } - return new IngestEngine( - state, - blocks, - txIds, - pendingTx, - seenEntities, - assigner); + return new IngestEngine(state, blocks, txIds, pendingTx, seenEntities, assigner); } /** @@ -1408,9 +1224,7 @@ private IngestEngine mockIngestEngineForEntities( * @param ingestEngine ingest engine. * @param entities the entities to be processed. */ - private void processEntitiesConcurrently( - IngestEngine ingestEngine, - ArrayList entities) { + private void processEntitiesConcurrently(IngestEngine ingestEngine, ArrayList entities) { AtomicInteger threadError = new AtomicInteger(); int concurrencyDegree = entities.size(); @@ -1475,14 +1289,8 @@ private void testValidatedSingleBlock(Blocks blocks, Block block) { when(seenEntities.has(block.id())).thenReturn(false); - IngestEngine ingestEngine = this.mockIngestEngineForEntities( - new ArrayList<>(List.of(block)), - seenEntities, - transactionIds, - pendingTransactions, - blocks); + IngestEngine ingestEngine = this.mockIngestEngineForEntities(new ArrayList<>(List.of(block)), seenEntities, transactionIds, pendingTransactions, blocks); ingestEngine.process(block); verifyBlockHappyPathCalled(block, blocks, pendingTransactions, transactionIds, seenEntities); } - } \ No newline at end of file