Skip to content

Commit 32fb91d

Browse files
committed
Fixed more tests
Signed-off-by: Ivan Malygin <[email protected]>
1 parent 2972d23 commit 32fb91d

File tree

9 files changed

+53
-21
lines changed

9 files changed

+53
-21
lines changed

platform-sdk/swirlds-merkledb/src/main/java/com/swirlds/merkledb/MerkleDbDataSource.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,6 @@ public MerkleDbDataSource(
372372

373373
// Update count of open databases
374374
COUNT_OF_OPEN_DATABASES.increment();
375-
new Exception().printStackTrace();
376375

377376
logger.info(
378377
MERKLE_DB.getMarker(),

platform-sdk/swirlds-platform-core/src/test/java/com/swirlds/platform/SignedStateFileReadWriteTest.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ void writeHashInfoFileTest() throws IOException {
116116

117117
final String fileString = sb.toString();
118118
assertTrue(fileString.contains(hashInfoString), "hash info string not found");
119+
state.release();
119120
}
120121

121122
@Test
@@ -129,7 +130,7 @@ void writeThenReadStateFileTest() throws IOException {
129130
assertFalse(exists(signatureSetFile), "signature set file should not yet exist");
130131

131132
State state = signedState.getState();
132-
state.copy();
133+
state.copy().release();
133134
TestMerkleCryptoFactory.getInstance().digestTreeSync(((TestMerkleStateRoot) state).getRoot());
134135
state.createSnapshot(testDirectory);
135136
writeSignatureSetFile(testDirectory, signedState);
@@ -156,6 +157,8 @@ void writeThenReadStateFileTest() throws IOException {
156157
deserializedSignedState.reservedSignedState().get().getState().getHash(),
157158
"hash should match");
158159
assertNotSame(signedState, deserializedSignedState.reservedSignedState(), "state should be a different object");
160+
signedState.getState().release();
161+
deserializedSignedState.reservedSignedState().get().getState().release();
159162
}
160163

161164
@Test
@@ -180,7 +183,7 @@ void writeSavedStateToDiskTest() throws IOException {
180183
.build();
181184

182185
// make immutable
183-
signedState.getState().copy();
186+
signedState.getState().copy().release();
184187
TestMerkleCryptoFactory.getInstance().digestTreeSync((signedState.getState()).getRoot());
185188

186189
writeSignedStateToDisk(

platform-sdk/swirlds-platform-core/src/test/java/com/swirlds/platform/StateFileManagerTests.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -151,19 +151,16 @@ private void validateSavingOfState(final SignedState originalState, final Path s
151151
TestPlatformContextBuilder.create().build().getConfiguration();
152152
final DeserializedSignedState deserializedSignedState =
153153
readStateFile(stateFile, TEST_PLATFORM_STATE_FACADE, PlatformContext.create(configuration));
154-
hashState(deserializedSignedState.reservedSignedState().get().getState());
154+
SignedState signedState = deserializedSignedState.reservedSignedState().get();
155+
hashState(signedState.getState());
155156

156157
assertNotNull(deserializedSignedState.originalHash(), "hash should not be null");
157-
assertNotSame(
158-
deserializedSignedState.reservedSignedState().get(),
159-
originalState,
160-
"deserialized object should not be the same");
161-
162-
assertEquals(
163-
originalState.getState().getHash(),
164-
deserializedSignedState.reservedSignedState().get().getState().getHash(),
165-
"hash should match");
158+
assertNotSame(signedState, originalState, "deserialized object should not be the same");
159+
160+
assertEquals(originalState.getState().getHash(), signedState.getState().getHash(), "hash should match");
166161
assertEquals(originalState.getState().getHash(), deserializedSignedState.originalHash(), "hash should match");
162+
163+
signedState.getState().release();
167164
}
168165

169166
@ParameterizedTest
@@ -475,6 +472,6 @@ private static void hashState(MerkleNodeState state) {
475472
}
476473

477474
private static void makeImmutable(SignedState signedState) {
478-
signedState.getState().copy();
475+
signedState.getState().copy().release();
479476
}
480477
}

platform-sdk/swirlds-platform-core/src/test/java/com/swirlds/platform/reconnect/DefaultSignedStateValidatorTests.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// SPDX-License-Identifier: Apache-2.0
22
package com.swirlds.platform.reconnect;
33

4+
import static com.swirlds.common.test.fixtures.AssertionUtils.assertEventuallyEquals;
45
import static com.swirlds.common.utility.Threshold.MAJORITY;
56
import static com.swirlds.platform.test.fixtures.state.TestPlatformStateFacade.TEST_PLATFORM_STATE_FACADE;
67
import static org.hiero.base.crypto.test.fixtures.CryptoRandomUtils.randomHash;
@@ -13,13 +14,16 @@
1314
import com.swirlds.common.test.fixtures.Randotron;
1415
import com.swirlds.common.test.fixtures.platform.TestPlatformContextBuilder;
1516
import com.swirlds.merkledb.MerkleDb;
17+
import com.swirlds.merkledb.MerkleDbDataSource;
1618
import com.swirlds.platform.crypto.SignatureVerifier;
1719
import com.swirlds.platform.state.signed.SignedState;
1820
import com.swirlds.platform.state.signed.SignedStateInvalidException;
1921
import com.swirlds.platform.state.signed.SignedStateValidationData;
2022
import com.swirlds.platform.test.fixtures.addressbook.RandomRosterEntryBuilder;
2123
import com.swirlds.platform.test.fixtures.state.RandomSignedStateGenerator;
2224
import edu.umd.cs.findbugs.annotations.NonNull;
25+
import java.time.Duration;
26+
import java.time.temporal.ChronoUnit;
2327
import java.util.ArrayList;
2428
import java.util.HashMap;
2529
import java.util.LinkedList;
@@ -247,6 +251,11 @@ void setUp() {
247251
@AfterEach
248252
void tearDown() {
249253
RandomSignedStateGenerator.releaseAllBuiltSignedStates();
254+
assertEventuallyEquals(
255+
0L,
256+
MerkleDbDataSource::getCountOfOpenDatabases,
257+
Duration.of(5, ChronoUnit.SECONDS),
258+
"All databases should be closed");
250259
}
251260

252261
@ParameterizedTest

platform-sdk/swirlds-platform-core/src/test/java/com/swirlds/platform/reconnect/RandomVirtualMapReconnectTests.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
import java.util.stream.Stream;
5252
import org.hiero.base.crypto.DigestType;
5353
import org.junit.jupiter.api.AfterEach;
54-
import org.junit.jupiter.api.BeforeAll;
5554
import org.junit.jupiter.api.DisplayName;
5655
import org.junit.jupiter.api.Test;
5756
import org.junit.jupiter.params.ParameterizedTest;
@@ -185,11 +184,6 @@ private static Stream<Arguments> buildArguments() {
185184
return arguments.stream();
186185
}
187186

188-
@BeforeAll
189-
static void beforeAll() {
190-
assertEquals(0, MerkleDbDataSource.getCountOfOpenDatabases());
191-
}
192-
193187
@ParameterizedTest
194188
@MethodSource("buildArguments")
195189
@DisplayName("Random Operations Reconnect Test")

platform-sdk/swirlds-platform-core/src/test/java/com/swirlds/platform/state/IssDetectorTests.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// SPDX-License-Identifier: Apache-2.0
22
package com.swirlds.platform.state;
33

4+
import static com.swirlds.common.test.fixtures.AssertionUtils.assertEventuallyEquals;
45
import static com.swirlds.common.utility.Threshold.MAJORITY;
56
import static com.swirlds.common.utility.Threshold.SUPER_MAJORITY;
67
import static com.swirlds.platform.state.RoundHashValidatorTests.generateCatastrophicNodeHashes;
@@ -23,6 +24,7 @@
2324
import com.swirlds.common.test.fixtures.GaussianWeightGenerator;
2425
import com.swirlds.common.test.fixtures.Randotron;
2526
import com.swirlds.common.test.fixtures.WeightGenerator;
27+
import com.swirlds.merkledb.MerkleDbDataSource;
2628
import com.swirlds.platform.consensus.ConsensusConfig;
2729
import com.swirlds.platform.state.iss.DefaultIssDetector;
2830
import com.swirlds.platform.state.iss.IssDetector;
@@ -33,6 +35,8 @@
3335
import com.swirlds.platform.test.fixtures.addressbook.RandomRosterBuilder;
3436
import com.swirlds.platform.test.fixtures.state.RandomSignedStateGenerator;
3537
import edu.umd.cs.findbugs.annotations.NonNull;
38+
import java.time.Duration;
39+
import java.time.temporal.ChronoUnit;
3640
import java.util.ArrayList;
3741
import java.util.Collection;
3842
import java.util.HashMap;
@@ -47,6 +51,7 @@
4751
import org.hiero.consensus.model.notification.IssNotification.IssType;
4852
import org.hiero.consensus.model.transaction.ScopedSystemTransaction;
4953
import org.hiero.consensus.roster.RosterUtils;
54+
import org.junit.jupiter.api.AfterEach;
5055
import org.junit.jupiter.api.DisplayName;
5156
import org.junit.jupiter.api.Test;
5257

@@ -79,6 +84,7 @@ void stateReservationIsReleased() {
7984
1,
8085
stateWrapperForTest.get().getReservationCount(),
8186
"The test caller should still have a reservation on the state");
87+
stateWrapperForTest.get().getState().release();
8288
}
8389

8490
@Test
@@ -635,6 +641,15 @@ void ignoredRoundTest() {
635641
assertMarkerFile(IssType.OTHER_ISS.toString(), false);
636642
}
637643

644+
@AfterEach
645+
void tearDown() {
646+
assertEventuallyEquals(
647+
0L,
648+
MerkleDbDataSource::getCountOfOpenDatabases,
649+
Duration.of(5, ChronoUnit.SECONDS),
650+
"All databases should be closed");
651+
}
652+
638653
private static Map<NodeId, ScopedSystemTransaction<StateSignatureTransaction>> generateSystemTransactions(
639654
final long roundNumber, @NonNull final RoundHashValidatorTests.HashGenerationData hashGenerationData) {
640655

platform-sdk/swirlds-platform-core/src/test/java/com/swirlds/platform/state/MerkleStateRootTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -922,4 +922,13 @@ void calculateHash_idempotent() {
922922
assertSame(hash1, hash2);
923923
}
924924
}
925+
926+
@AfterEach
927+
void tearDown() {
928+
assertEventuallyEquals(
929+
0L,
930+
MerkleDbDataSource::getCountOfOpenDatabases,
931+
Duration.of(5, ChronoUnit.SECONDS),
932+
"All databases should be closed");
933+
}
925934
}

platform-sdk/swirlds-platform-core/src/testFixtures/java/com/swirlds/platform/test/fixtures/state/RandomSignedStateGenerator.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
import java.util.Random;
4747
import java.util.stream.IntStream;
4848
import java.util.stream.Stream;
49+
import org.apache.logging.log4j.LogManager;
50+
import org.apache.logging.log4j.Logger;
4951
import org.hiero.base.crypto.Hash;
5052
import org.hiero.base.crypto.Signature;
5153
import org.hiero.base.utility.CommonUtils;
@@ -58,6 +60,7 @@
5860
*/
5961
public class RandomSignedStateGenerator {
6062

63+
private static final Logger logger = LogManager.getLogger(RandomSignedStateGenerator.class);
6164
/**
6265
* Signed states now use virtual maps which are heavy RAM consumers. They need to be released
6366
* in order to avoid producing OOMs when running tests. This list tracks all signed states
@@ -514,7 +517,7 @@ public static void releaseAllBuiltSignedStates() {
514517
}
515518
});
516519
} catch (Exception e) {
517-
520+
logger.error("Exception while releasing state", e);
518521
}
519522
});
520523
try {

platform-sdk/swirlds-state-impl/src/test/java/com/swirlds/state/merkle/disk/OnDiskWritableStateTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import com.hedera.pbj.runtime.ParseException;
88
import com.swirlds.state.test.fixtures.merkle.MerkleTestBase;
9+
import com.swirlds.virtualmap.VirtualMap;
910
import java.util.Spliterators;
1011
import java.util.stream.StreamSupport;
1112
import org.junit.jupiter.api.AfterEach;
@@ -229,7 +230,9 @@ void smorgasbord() throws ParseException {
229230
// Now let's make a fast copy and create a new state and make some more
230231
// modifications and reads. And then let's throw them all away and make
231232
// sure the virtual map hasn't changed.
233+
final VirtualMap oldVirtualMap = fruitVirtualMap;
232234
fruitVirtualMap = fruitVirtualMap.copy();
235+
oldVirtualMap.release();
233236
state = new OnDiskWritableKVState<>(FRUIT_STATE_KEY, STRING_CODEC, STRING_CODEC, fruitVirtualMap);
234237
assertThat(state.get(A_KEY)).isEqualTo(APPLE);
235238
state.remove(B_KEY);

0 commit comments

Comments
 (0)