Skip to content

Commit

Permalink
chore: cleanup event building and mocking in tests (#11579)
Browse files Browse the repository at this point in the history
Signed-off-by: Lazar Petrovic <[email protected]>
  • Loading branch information
lpetrovic05 authored Feb 20, 2024
1 parent ec95616 commit c98292e
Show file tree
Hide file tree
Showing 17 changed files with 484 additions and 843 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,17 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.swirlds.common.crypto.Hash;
import com.swirlds.common.platform.NodeId;
import com.swirlds.common.test.fixtures.RandomUtils;
import com.swirlds.common.test.fixtures.TransactionUtils;
import com.swirlds.platform.internal.EventImpl;
import com.swirlds.platform.system.BasicSoftwareVersion;
import com.swirlds.platform.system.events.BaseEventHashedData;
import com.swirlds.platform.system.events.BaseEventUnhashedData;
import com.swirlds.platform.system.events.EventConstants;
import com.swirlds.platform.system.events.EventDescriptor;
import com.swirlds.platform.system.transaction.ConsensusTransaction;
import com.swirlds.platform.system.transaction.ConsensusTransactionImpl;
import com.swirlds.platform.system.transaction.SwirldTransaction;
import com.swirlds.platform.system.transaction.SystemTransaction;
import com.swirlds.platform.system.transaction.Transaction;
import com.swirlds.platform.test.fixtures.event.TestingEventBuilder;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
Expand All @@ -53,6 +46,7 @@
import org.junit.jupiter.params.provider.MethodSource;

public class EventImplTests {
private final TestingEventBuilder testingEventBuilder = TestingEventBuilder.builder();

private static Stream<Arguments> params() {
return Stream.of(
Expand All @@ -65,7 +59,8 @@ private static Stream<Arguments> params() {
void testNoSystemTransaction() {
final SwirldTransaction[] transactions = TransactionUtils.incrementingSwirldTransactions(100);

final EventImpl event = newEvent(transactions);
final EventImpl event =
testingEventBuilder.setDefaults().setTransactions(transactions).buildEventImpl();

assertDoesNotThrow(
event::systemTransactionIterator, "Getting the system transaction iterator should never throw.");
Expand All @@ -80,7 +75,8 @@ void testNoSystemTransaction() {
@Test
@DisplayName("findSystemTransactions() no transactions")
void testTransaction() {
final EventImpl event = newEvent(new ConsensusTransactionImpl[0]);
final EventImpl event =
testingEventBuilder.setDefaults().setNumberOfTransactions(0).buildEventImpl();

assertDoesNotThrow(
event::systemTransactionIterator, "Getting the system transaction iterator should never throw.");
Expand All @@ -95,7 +91,10 @@ void testTransaction() {
void testFindSystemTransactions(final Long seed, final int numTransactions) {
final TransactionData data = mixedTransactions(seed, numTransactions);

final EventImpl event = newEvent(data.transactions);
final EventImpl event = testingEventBuilder
.setDefaults()
.setTransactions(data.transactions())
.buildEventImpl();
verifySystemIterator(data, event);
assertEquals(
data.transactions.length - data.systemIndices.size(),
Expand All @@ -109,7 +108,10 @@ void testFindSystemTransactions(final Long seed, final int numTransactions) {
void testConsensusTransactionIterator(final Long seed, final int numTransactions) {
final TransactionData data = mixedTransactions(seed, numTransactions);

final EventImpl event = newEvent(data.transactions);
final EventImpl event = testingEventBuilder
.setDefaults()
.setTransactions(data.transactions())
.buildEventImpl();
final Iterator<ConsensusTransaction> iter = event.consensusTransactionIterator();
final Set<ConsensusTransaction> transactionSet = new HashSet<>();
while (iter.hasNext()) {
Expand All @@ -130,7 +132,10 @@ void testConsensusTransactionIterator(final Long seed, final int numTransactions
void testTransactionIterator(final Long seed, final int numTransactions) {
final TransactionData data = mixedTransactions(seed, numTransactions);

final EventImpl event = newEvent(data.transactions);
final EventImpl event = testingEventBuilder
.setDefaults()
.setTransactions(data.transactions())
.buildEventImpl();
final Iterator<Transaction> iter = event.transactionIterator();

final Set<Transaction> transactionSet = new HashSet<>();
Expand Down Expand Up @@ -223,7 +228,10 @@ void testConsensusReached() {
mixedTransactions[4] = TransactionUtils.incrementingSystemTransaction();

final Instant eventConsTime = Instant.now();
final EventImpl event = newEvent(mixedTransactions);
final EventImpl event = testingEventBuilder
.setDefaults()
.setTransactions(mixedTransactions)
.buildEventImpl();
event.setConsensusOrder(3L);
event.setConsensusTimestamp(eventConsTime);

Expand All @@ -240,22 +248,5 @@ void testConsensusReached() {
}
}

private static EventImpl newEvent(final ConsensusTransactionImpl[] transactions) {

final EventDescriptor selfDescriptor = new EventDescriptor(new Hash(), new NodeId(0), -1, -1);
final EventDescriptor otherDescriptor = new EventDescriptor(new Hash(), new NodeId(0), -1, -1);

return new EventImpl(
new BaseEventHashedData(
new BasicSoftwareVersion(1),
new NodeId(0L),
selfDescriptor,
Collections.singletonList(otherDescriptor),
EventConstants.BIRTH_ROUND_UNDEFINED,
Instant.now(),
transactions),
new BaseEventUnhashedData(new NodeId(0L), new byte[0]));
}

private record TransactionData(ConsensusTransactionImpl[] transactions, List<Integer> systemIndices) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,13 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.swirlds.common.crypto.CryptographyHolder;
import com.swirlds.common.platform.NodeId;
import com.swirlds.common.test.fixtures.RandomUtils;
import com.swirlds.common.test.fixtures.TransactionUtils;
import com.swirlds.platform.internal.EventImpl;
import com.swirlds.platform.metrics.SwirldStateMetrics;
import com.swirlds.platform.system.BasicSoftwareVersion;
import com.swirlds.platform.system.SwirldState;
import com.swirlds.platform.system.events.BaseEventHashedData;
import com.swirlds.platform.system.events.BaseEventUnhashedData;
import com.swirlds.platform.system.events.EventConstants;
import com.swirlds.platform.system.events.EventDescriptor;
import com.swirlds.platform.system.transaction.ConsensusTransactionImpl;
import java.time.Instant;
import java.util.Collections;
import com.swirlds.platform.test.fixtures.event.TestingEventBuilder;
import java.util.List;
import java.util.Random;
import java.util.function.Supplier;
Expand Down Expand Up @@ -69,29 +61,13 @@ void setup() {
@DisplayName("preHandle() invokes SwirldState.preHandle() with the correct arguments")
void testSwirldStatePreHandle() {
final Random r = RandomUtils.getRandomPrintSeed();
final EventImpl event = newEvent(TransactionUtils.incrementingMixedTransactions(r));
final EventImpl event = TestingEventBuilder.builder()
.setTransactions(TransactionUtils.incrementingMixedTransactions(r))
.buildEventImpl();

handler.preHandle(event, swirldState);

verify(swirldState, times(1).description("preHandle() invoked incorrect number of times"))
.preHandle(event);
}

private static EventImpl newEvent(final ConsensusTransactionImpl[] transactions) {

final EventDescriptor selfDescriptor = new EventDescriptor(
CryptographyHolder.get().getNullHash(), new NodeId(0), 0, EventConstants.BIRTH_ROUND_UNDEFINED);
final EventDescriptor otherDescriptor = new EventDescriptor(
CryptographyHolder.get().getNullHash(), new NodeId(0), 0, EventConstants.BIRTH_ROUND_UNDEFINED);
return new EventImpl(
new BaseEventHashedData(
new BasicSoftwareVersion(1),
new NodeId(0L),
selfDescriptor,
Collections.singletonList(otherDescriptor),
EventConstants.BIRTH_ROUND_UNDEFINED,
Instant.now(),
transactions),
new BaseEventUnhashedData(new NodeId(0L), new byte[0]));
}
}
Loading

0 comments on commit c98292e

Please sign in to comment.