Skip to content

Commit ebd6612

Browse files
authored
refactor: Migrate some tests to use event test util (#12471)
Signed-off-by: Austin Littley <[email protected]>
1 parent 2919070 commit ebd6612

File tree

4 files changed

+77
-105
lines changed

4 files changed

+77
-105
lines changed

platform-sdk/swirlds-platform-core/src/test/java/com/swirlds/platform/event/FutureEventBufferTests.java

+16-31
Original file line numberDiff line numberDiff line change
@@ -31,41 +31,14 @@
3131
import com.swirlds.config.extensions.test.fixtures.TestConfigBuilder;
3232
import com.swirlds.platform.consensus.NonAncientEventWindow;
3333
import com.swirlds.platform.eventhandling.EventConfig_;
34-
import com.swirlds.platform.system.BasicSoftwareVersion;
35-
import com.swirlds.platform.system.events.BaseEventHashedData;
36-
import com.swirlds.platform.system.events.BaseEventUnhashedData;
37-
import edu.umd.cs.findbugs.annotations.NonNull;
34+
import com.swirlds.platform.test.fixtures.event.TestingEventBuilder;
3835
import java.util.ArrayList;
3936
import java.util.Comparator;
4037
import java.util.List;
4138
import java.util.Random;
4239
import org.junit.jupiter.api.Test;
4340

4441
class FutureEventBufferTests {
45-
46-
/**
47-
* Generate an event with random data. Most fields don't need to be non-null, just enough to ensure events are
48-
* unique.
49-
*
50-
* @param random the random number generator
51-
* @param birthRound the birth round of the event
52-
* @return the event
53-
*/
54-
@NonNull
55-
final GossipEvent generateEvent(@NonNull final Random random, final long birthRound) {
56-
final BaseEventHashedData baseEventHashedData = new BaseEventHashedData(
57-
new BasicSoftwareVersion(1),
58-
new NodeId(random.nextInt(100)),
59-
null,
60-
List.of(),
61-
birthRound,
62-
randomInstant(random),
63-
null);
64-
final BaseEventUnhashedData baseEventUnhashedData = new BaseEventUnhashedData();
65-
66-
return new GossipEvent(baseEventHashedData, baseEventUnhashedData);
67-
}
68-
6942
/**
7043
* This test verifies the following:
7144
* <ul>
@@ -101,7 +74,11 @@ void futureEventsBufferedTest() {
10174
final int count = 1000;
10275
final List<GossipEvent> events = new ArrayList<>(count);
10376
for (int i = 0; i < count; i++) {
104-
final GossipEvent event = generateEvent(random, random.nextLong(1, maxFutureRound));
77+
final GossipEvent event = new TestingEventBuilder(random)
78+
.setBirthRound(random.nextLong(1, maxFutureRound))
79+
.setCreatorId(new NodeId(random.nextInt(100)))
80+
.setTimeCreated(randomInstant(random))
81+
.build();
10582
events.add(event);
10683
}
10784
// Put the events in topological order
@@ -182,7 +159,11 @@ void eventsGoAncientWhileBufferedTest() {
182159
final int count = 1000;
183160
final List<GossipEvent> events = new ArrayList<>(count);
184161
for (int i = 0; i < count; i++) {
185-
final GossipEvent event = generateEvent(random, random.nextLong(1, maxFutureRound));
162+
final GossipEvent event = new TestingEventBuilder(random)
163+
.setBirthRound(random.nextLong(1, maxFutureRound))
164+
.setCreatorId(new NodeId(random.nextInt(100)))
165+
.setTimeCreated(randomInstant(random))
166+
.build();
186167
events.add(event);
187168
}
188169
// Put the events in topological order
@@ -237,7 +218,11 @@ void eventInBufferIsReleasedOnTimeTest() {
237218

238219
final long roundsUntilRelease = random.nextLong(10, 20);
239220
final long eventBirthRound = pendingConsensusRound + roundsUntilRelease;
240-
final GossipEvent event = generateEvent(random, eventBirthRound);
221+
final GossipEvent event = new TestingEventBuilder(random)
222+
.setBirthRound(eventBirthRound)
223+
.setCreatorId(new NodeId(random.nextInt(100)))
224+
.setTimeCreated(randomInstant(random))
225+
.build();
241226

242227
// Event is from the future, we can't release it yet
243228
assertNull(futureEventBuffer.addEvent(event));

platform-sdk/swirlds-platform-core/src/test/java/com/swirlds/platform/system/events/BirthRoundMigrationShimTests.java

+15-17
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
package com.swirlds.platform.system.events;
1818

1919
import static com.swirlds.common.test.fixtures.RandomUtils.getRandomPrintSeed;
20-
import static com.swirlds.common.test.fixtures.RandomUtils.randomHash;
21-
import static com.swirlds.common.test.fixtures.RandomUtils.randomInstant;
2220
import static com.swirlds.platform.consensus.ConsensusConstants.ROUND_FIRST;
2321
import static org.junit.jupiter.api.Assertions.assertEquals;
2422
import static org.junit.jupiter.api.Assertions.assertSame;
@@ -30,8 +28,8 @@
3028
import com.swirlds.platform.event.GossipEvent;
3129
import com.swirlds.platform.system.BasicSoftwareVersion;
3230
import com.swirlds.platform.system.SoftwareVersion;
31+
import com.swirlds.platform.test.fixtures.event.TestingEventBuilder;
3332
import edu.umd.cs.findbugs.annotations.NonNull;
34-
import java.util.List;
3533
import java.util.Random;
3634
import org.junit.jupiter.api.Test;
3735

@@ -45,20 +43,20 @@ private GossipEvent buildEvent(
4543
final long generation,
4644
final long birthRound) {
4745

48-
final GossipEvent event = new GossipEvent(
49-
new BaseEventHashedData(
50-
softwareVersion,
51-
new NodeId(random.nextLong(1, 10)),
52-
new EventDescriptor(
53-
randomHash(random),
54-
new NodeId(random.nextInt(1, 10)),
55-
generation - 1 /* chose parent generation to yield desired self generation */,
56-
random.nextLong(birthRound - 2, birthRound + 1)) /* realistic range */,
57-
List.of() /* don't bother with other parents, unimportant for this test */,
58-
birthRound,
59-
randomInstant(random),
60-
null),
61-
new BaseEventUnhashedData());
46+
final NodeId creatorId = new NodeId(random.nextLong(1, 10));
47+
final GossipEvent selfParent = new TestingEventBuilder(random)
48+
.setCreatorId(creatorId)
49+
.setBirthRound(random.nextLong(birthRound - 2, birthRound + 1)) /* realistic range */
50+
.build();
51+
52+
final GossipEvent event = new TestingEventBuilder(random)
53+
.setSoftwareVersion(softwareVersion)
54+
.setCreatorId(creatorId)
55+
.setBirthRound(birthRound)
56+
.setSelfParent(selfParent)
57+
/* chose parent generation to yield desired self generation */
58+
.overrideSelfParentGeneration(generation - 1)
59+
.build();
6260

6361
platformContext.getCryptography().digestSync(event.getHashedData());
6462

platform-sdk/swirlds-unit-tests/core/swirlds-platform-test/src/test/java/com/swirlds/platform/test/EventStringsTest.java

+24-25
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,17 @@
1818

1919
import static org.junit.jupiter.api.Assertions.assertTrue;
2020

21-
import com.swirlds.common.crypto.Hash;
2221
import com.swirlds.common.platform.NodeId;
22+
import com.swirlds.common.test.fixtures.RandomUtils;
2323
import com.swirlds.common.test.fixtures.junit.tags.TestComponentTags;
2424
import com.swirlds.platform.EventStrings;
2525
import com.swirlds.platform.event.GossipEvent;
2626
import com.swirlds.platform.internal.EventImpl;
27-
import com.swirlds.platform.system.BasicSoftwareVersion;
2827
import com.swirlds.platform.system.events.BaseEventHashedData;
29-
import com.swirlds.platform.system.events.BaseEventUnhashedData;
30-
import com.swirlds.platform.system.events.EventConstants;
31-
import com.swirlds.platform.system.events.EventDescriptor;
32-
import com.swirlds.platform.system.transaction.ConsensusTransactionImpl;
28+
import com.swirlds.platform.test.fixtures.event.EventImplTestUtils;
29+
import com.swirlds.platform.test.fixtures.event.TestingEventBuilder;
3330
import java.time.Instant;
34-
import java.util.Collections;
31+
import java.util.Random;
3532
import org.junit.jupiter.api.DisplayName;
3633
import org.junit.jupiter.api.Tag;
3734
import org.junit.jupiter.api.Test;
@@ -42,7 +39,9 @@ class EventStringsTest {
4239
@Tag(TestComponentTags.PLATFORM)
4340
@DisplayName("Test event strings")
4441
void testCopy() {
45-
printAssert(EventStrings.toShortString((EventImpl) null), "null", "should indicate that the object is null");
42+
final Random random = RandomUtils.getRandomPrintSeed();
43+
44+
printAssert(EventStrings.toShortString(null), "null", "should indicate that the object is null");
4645
printAssert(EventStrings.toShortString((GossipEvent) null), "null", "should indicate that the object is null");
4746
printAssert(EventStrings.toShortString(new EventImpl()), "null", "should indicate that the object is null");
4847

@@ -53,31 +52,31 @@ void testCopy() {
5352

5453
final NodeId selfId = new NodeId(id);
5554
final NodeId otherId = new NodeId(opId);
56-
final EventDescriptor selfParent =
57-
new EventDescriptor(new Hash(), selfId, spGen, EventConstants.BIRTH_ROUND_UNDEFINED);
58-
final EventDescriptor otherParent =
59-
new EventDescriptor(new Hash(), otherId, opGen, EventConstants.BIRTH_ROUND_UNDEFINED);
6055

61-
BaseEventHashedData hashedData = new BaseEventHashedData(
62-
new BasicSoftwareVersion(1),
63-
selfId,
64-
selfParent,
65-
Collections.singletonList(otherParent),
66-
EventConstants.BIRTH_ROUND_UNDEFINED,
67-
Instant.now(),
68-
new ConsensusTransactionImpl[0]);
69-
hashedData.setHash(new Hash());
70-
BaseEventUnhashedData unhashedData = new BaseEventUnhashedData(new NodeId(opId), new byte[0]);
56+
final EventImpl selfParent =
57+
EventImplTestUtils.createEventImpl(new TestingEventBuilder(random).setCreatorId(selfId), null, null);
58+
final EventImpl otherParent =
59+
EventImplTestUtils.createEventImpl(new TestingEventBuilder(random).setCreatorId(otherId), null, null);
60+
61+
final TestingEventBuilder childBuilder = new TestingEventBuilder(random)
62+
.setCreatorId(selfId)
63+
.overrideSelfParentGeneration(spGen)
64+
.overrideOtherParentGeneration(opGen)
65+
.setTimeCreated(Instant.now());
66+
67+
final EventImpl eventImplChild = EventImplTestUtils.createEventImpl(childBuilder, selfParent, otherParent);
68+
final GossipEvent gossipEventChild = childBuilder.build();
69+
7170
printAssert(
72-
EventStrings.toShortString(new EventImpl(hashedData, unhashedData)),
71+
EventStrings.toShortString(eventImplChild),
7372
String.format("%d,%d", id, BaseEventHashedData.calculateGeneration(spGen, opGen)),
7473
"should have creator and generation");
7574
printAssert(
76-
EventStrings.toShortString(new GossipEvent(hashedData, unhashedData)),
75+
EventStrings.toShortString(gossipEventChild),
7776
String.format("%d,%d", id, BaseEventHashedData.calculateGeneration(spGen, opGen)),
7877
"should have creator and generation");
7978
printAssert(
80-
EventStrings.toMediumString(new EventImpl(hashedData, unhashedData)),
79+
EventStrings.toMediumString(eventImplChild),
8180
String.format("%d,%d", opId, opGen),
8281
"should have op creator and generation");
8382
}

platform-sdk/swirlds-unit-tests/core/swirlds-platform-test/src/test/java/com/swirlds/platform/test/event/tipset/TipsetEventCreatorTests.java

+22-32
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package com.swirlds.platform.test.event.tipset;
1818

1919
import static com.swirlds.common.test.fixtures.RandomUtils.getRandomPrintSeed;
20-
import static com.swirlds.common.test.fixtures.RandomUtils.randomHash;
2120
import static com.swirlds.common.test.fixtures.RandomUtils.randomSignature;
2221
import static com.swirlds.common.utility.CompareTo.isGreaterThanOrEqualTo;
2322
import static com.swirlds.platform.consensus.ConsensusConstants.ROUND_FIRST;
@@ -53,14 +52,13 @@
5352
import com.swirlds.platform.system.SoftwareVersion;
5453
import com.swirlds.platform.system.address.Address;
5554
import com.swirlds.platform.system.address.AddressBook;
56-
import com.swirlds.platform.system.events.BaseEventHashedData;
57-
import com.swirlds.platform.system.events.BaseEventUnhashedData;
5855
import com.swirlds.platform.system.events.ConsensusData;
5956
import com.swirlds.platform.system.events.EventConstants;
6057
import com.swirlds.platform.system.events.EventDescriptor;
6158
import com.swirlds.platform.system.transaction.ConsensusTransactionImpl;
6259
import com.swirlds.platform.system.transaction.SwirldTransaction;
6360
import com.swirlds.platform.test.fixtures.addressbook.RandomAddressBookGenerator;
61+
import com.swirlds.platform.test.fixtures.event.TestingEventBuilder;
6462
import edu.umd.cs.findbugs.annotations.NonNull;
6563
import edu.umd.cs.findbugs.annotations.Nullable;
6664
import java.time.Duration;
@@ -798,37 +796,29 @@ void sizeOneNetworkTest(final boolean advancingClock, final boolean useBirthRoun
798796
}
799797

800798
@NonNull
801-
private GossipEvent createMockEvent(
799+
private GossipEvent createTestEvent(
802800
@NonNull final Random random,
803801
@NonNull final NodeId creator,
804802
long selfParentGeneration,
805803
@Nullable final NodeId otherParentId,
806804
final long otherParentGeneration) {
807-
final GossipEvent event = mock(GossipEvent.class);
808805

809-
final BaseEventHashedData hashedData = mock(BaseEventHashedData.class);
810-
when(hashedData.getCreatorId()).thenReturn(creator);
811-
when(hashedData.getCreatorId()).thenReturn(creator);
812-
final long generation = Math.max(selfParentGeneration, otherParentGeneration) + 1;
813-
when(hashedData.getGeneration()).thenReturn(generation);
814-
when(event.getGeneration()).thenReturn(generation);
806+
final GossipEvent selfParent =
807+
new TestingEventBuilder(random).setCreatorId(creator).build();
815808

816-
final Hash hash = randomHash(random);
817-
when(hashedData.getHash()).thenReturn(hash);
809+
final TestingEventBuilder eventBuilder = new TestingEventBuilder(random)
810+
.setCreatorId(creator)
811+
.setSelfParent(selfParent)
812+
.overrideSelfParentGeneration(selfParentGeneration);
818813

819-
final EventDescriptor descriptor =
820-
new EventDescriptor(hash, creator, generation, -EventConstants.BIRTH_ROUND_UNDEFINED);
814+
if (otherParentId != null) {
815+
final GossipEvent otherParent =
816+
new TestingEventBuilder(random).setCreatorId(otherParentId).build();
821817

822-
when(hashedData.createEventDescriptor()).thenReturn(descriptor);
823-
when(event.getDescriptor()).thenReturn(descriptor);
824-
825-
when(event.getHashedData()).thenReturn(hashedData);
826-
827-
final BaseEventUnhashedData unhashedData = mock(BaseEventUnhashedData.class);
828-
when(unhashedData.getOtherId()).thenReturn(otherParentId);
829-
when(event.getUnhashedData()).thenReturn(unhashedData);
818+
eventBuilder.setOtherParent(otherParent).overrideOtherParentGeneration(otherParentGeneration);
819+
}
830820

831-
return event;
821+
return eventBuilder.build();
832822
}
833823

834824
/**
@@ -863,11 +853,11 @@ void frozenEventCreationBug() {
863853
final GossipEvent eventA1 = eventCreator.maybeCreateEvent();
864854
assertNotNull(eventA1);
865855

866-
final GossipEvent eventB1 = createMockEvent(
856+
final GossipEvent eventB1 = createTestEvent(
867857
random, nodeB, EventConstants.GENERATION_UNDEFINED, null, EventConstants.GENERATION_UNDEFINED);
868-
final GossipEvent eventC1 = createMockEvent(
858+
final GossipEvent eventC1 = createTestEvent(
869859
random, nodeC, EventConstants.GENERATION_UNDEFINED, null, EventConstants.GENERATION_UNDEFINED);
870-
final GossipEvent eventD1 = createMockEvent(
860+
final GossipEvent eventD1 = createTestEvent(
871861
random, nodeD, EventConstants.GENERATION_UNDEFINED, null, EventConstants.GENERATION_UNDEFINED);
872862

873863
eventCreator.registerEvent(eventB1);
@@ -896,7 +886,7 @@ void frozenEventCreationBug() {
896886
// but has not been updated in the current snapshot.
897887

898888
final NodeId otherParentId = eventA2.getUnhashedData().getOtherId();
899-
final GossipEvent legalOtherParent = createMockEvent(random, otherParentId, 0, nodeA, 0);
889+
final GossipEvent legalOtherParent = createTestEvent(random, otherParentId, 0, nodeA, 0);
900890

901891
eventCreator.registerEvent(legalOtherParent);
902892

@@ -936,13 +926,13 @@ void notRegisteringEventsFromNodesNotInAddressBook() {
936926
final GossipEvent eventA1 = eventCreator.maybeCreateEvent();
937927
assertNotNull(eventA1);
938928

939-
final GossipEvent eventB1 = createMockEvent(
929+
final GossipEvent eventB1 = createTestEvent(
940930
random, nodeB, EventConstants.GENERATION_UNDEFINED, null, EventConstants.GENERATION_UNDEFINED);
941-
final GossipEvent eventC1 = createMockEvent(
931+
final GossipEvent eventC1 = createTestEvent(
942932
random, nodeC, EventConstants.GENERATION_UNDEFINED, null, EventConstants.GENERATION_UNDEFINED);
943-
final GossipEvent eventD1 = createMockEvent(
933+
final GossipEvent eventD1 = createTestEvent(
944934
random, nodeD, EventConstants.GENERATION_UNDEFINED, null, EventConstants.GENERATION_UNDEFINED);
945-
final GossipEvent eventE1 = createMockEvent(
935+
final GossipEvent eventE1 = createTestEvent(
946936
random, nodeE, EventConstants.GENERATION_UNDEFINED, null, EventConstants.GENERATION_UNDEFINED);
947937

948938
eventCreator.registerEvent(eventB1);

0 commit comments

Comments
 (0)