mirror of
https://github.com/bisq-network/bisq.git
synced 2025-02-24 15:10:44 +01:00
[TESTS] Lower entries required for purge in tests
The original test would take over 5 seconds. Allow tests to set the number of required entries before purge to a lower value so the tests can run faster with the same confidence.
This commit is contained in:
parent
898d7fcd4a
commit
454b2d79e1
2 changed files with 29 additions and 13 deletions
|
@ -124,6 +124,8 @@ public class P2PDataStorage implements MessageListener, ConnectionListener, Pers
|
|||
private final Set<ProtectedDataStoreListener> protectedDataStoreListeners = new CopyOnWriteArraySet<>();
|
||||
private final Clock clock;
|
||||
|
||||
protected int maxSequenceNumberMapSizeBeforePurge;
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Constructor
|
||||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
@ -148,6 +150,7 @@ public class P2PDataStorage implements MessageListener, ConnectionListener, Pers
|
|||
|
||||
this.sequenceNumberMapStorage = sequenceNumberMapStorage;
|
||||
sequenceNumberMapStorage.setNumMaxBackupFiles(5);
|
||||
this.maxSequenceNumberMapSizeBeforePurge = 1000;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -209,7 +212,7 @@ public class P2PDataStorage implements MessageListener, ConnectionListener, Pers
|
|||
});
|
||||
hashMapChangedListeners.forEach(HashMapChangedListener::onBatchRemoveExpiredDataCompleted);
|
||||
|
||||
if (sequenceNumberMap.size() > 1000)
|
||||
if (sequenceNumberMap.size() > this.maxSequenceNumberMapSizeBeforePurge)
|
||||
sequenceNumberMap.setMap(getPurgedSequenceNumberMap(sequenceNumberMap.getMap()));
|
||||
}
|
||||
|
||||
|
|
|
@ -24,7 +24,9 @@ import bisq.network.p2p.storage.payload.ProtectedStorageEntry;
|
|||
import bisq.network.p2p.storage.payload.ProtectedStoragePayload;
|
||||
import bisq.network.p2p.storage.payload.RequiresOwnerIsOnlinePayload;
|
||||
import bisq.network.p2p.storage.persistence.AppendOnlyDataStoreListener;
|
||||
import bisq.network.p2p.storage.persistence.AppendOnlyDataStoreService;
|
||||
import bisq.network.p2p.storage.persistence.ProtectedDataStoreListener;
|
||||
import bisq.network.p2p.storage.persistence.ProtectedDataStoreService;
|
||||
import bisq.network.p2p.storage.persistence.ResourceDataStoreService;
|
||||
import bisq.network.p2p.storage.persistence.SequenceNumberMap;
|
||||
|
||||
|
@ -111,12 +113,30 @@ public class P2PDataStorageTest {
|
|||
final Storage<SequenceNumberMap> mockSeqNrStorage;
|
||||
final ClockFake clockFake;
|
||||
|
||||
/**
|
||||
* Subclass of P2PDataStorage that allows for easier testing, but keeps all functionality
|
||||
*/
|
||||
static class P2PDataStorageForTest extends P2PDataStorage {
|
||||
|
||||
P2PDataStorageForTest(NetworkNode networkNode,
|
||||
Broadcaster broadcaster,
|
||||
AppendOnlyDataStoreService appendOnlyDataStoreService,
|
||||
ProtectedDataStoreService protectedDataStoreService,
|
||||
ResourceDataStoreService resourceDataStoreService,
|
||||
Storage<SequenceNumberMap> sequenceNumberMapStorage,
|
||||
Clock clock) {
|
||||
super(networkNode, broadcaster, appendOnlyDataStoreService, protectedDataStoreService, resourceDataStoreService, sequenceNumberMapStorage, clock);
|
||||
|
||||
this.maxSequenceNumberMapSizeBeforePurge = 5;
|
||||
}
|
||||
}
|
||||
|
||||
TestState() {
|
||||
this.mockBroadcaster = mock(Broadcaster.class);
|
||||
this.mockSeqNrStorage = mock(Storage.class);
|
||||
this.clockFake = new ClockFake();
|
||||
|
||||
this.mockedStorage = new P2PDataStorage(mock(NetworkNode.class),
|
||||
this.mockedStorage = new P2PDataStorageForTest(mock(NetworkNode.class),
|
||||
this.mockBroadcaster,
|
||||
new AppendOnlyDataStoreServiceFake(),
|
||||
new ProtectedDataStoreServiceFake(), mock(ResourceDataStoreService.class),
|
||||
|
@ -1611,20 +1631,20 @@ public class P2PDataStorageTest {
|
|||
verifyProtectedStorageRemove(this.testState, beforeState, protectedStorageEntry, true, false, false, false);
|
||||
}
|
||||
|
||||
// TESTCASE: Ensure we try to purge old entries sequence number map when size exceeds 1000 elements
|
||||
// TESTCASE: Ensure we try to purge old entries sequence number map when size exceeds the maximum size
|
||||
// and that entries less than PURGE_AGE_DAYS remain
|
||||
@Test
|
||||
public void removeExpiredEntries_PurgeSeqNrMap() throws CryptoException, NoSuchAlgorithmException {
|
||||
final int initialClockIncrement = 5;
|
||||
|
||||
// Add 500 entries to our sequence number map that will be purged
|
||||
// Add 4 entries to our sequence number map that will be purged
|
||||
KeyPair purgedOwnerKeys = TestUtils.generateKeyPair();
|
||||
ProtectedStoragePayload purgedProtectedStoragePayload = new PersistableExpirableProtectedStoragePayload(purgedOwnerKeys.getPublic(), 0);
|
||||
ProtectedStorageEntry purgedProtectedStorageEntry = testState.mockedStorage.getProtectedStorageEntry(purgedProtectedStoragePayload, purgedOwnerKeys);
|
||||
|
||||
Assert.assertTrue(testState.mockedStorage.addProtectedStorageEntry(purgedProtectedStorageEntry, getTestNodeAddress(), null, true));
|
||||
|
||||
for (int i = 0; i <= 499; ++i) {
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
KeyPair ownerKeys = TestUtils.generateKeyPair();
|
||||
ProtectedStoragePayload protectedStoragePayload = new PersistableExpirableProtectedStoragePayload(ownerKeys.getPublic(), 0);
|
||||
ProtectedStorageEntry tmpEntry = testState.mockedStorage.getProtectedStorageEntry(protectedStoragePayload, ownerKeys);
|
||||
|
@ -1635,20 +1655,13 @@ public class P2PDataStorageTest {
|
|||
// some values that will be purged and others that will stay.
|
||||
this.testState.clockFake.increment(TimeUnit.DAYS.toMillis(initialClockIncrement));
|
||||
|
||||
// Add another 501 entries that will not be purged
|
||||
// Add a final entry that will not be purged
|
||||
KeyPair keepOwnerKeys = TestUtils.generateKeyPair();
|
||||
ProtectedStoragePayload keepProtectedStoragePayload = new PersistableExpirableProtectedStoragePayload(keepOwnerKeys.getPublic(), 0);
|
||||
ProtectedStorageEntry keepProtectedStorageEntry = testState.mockedStorage.getProtectedStorageEntry(keepProtectedStoragePayload, keepOwnerKeys);
|
||||
|
||||
Assert.assertTrue(testState.mockedStorage.addProtectedStorageEntry(keepProtectedStorageEntry, getTestNodeAddress(), null, true));
|
||||
|
||||
for (int i = 0; i <= 500; ++i) {
|
||||
KeyPair ownerKeys = TestUtils.generateKeyPair();
|
||||
ProtectedStoragePayload protectedStoragePayload = new PersistableExpirableProtectedStoragePayload(ownerKeys.getPublic(), 0);
|
||||
ProtectedStorageEntry tmpEntry = testState.mockedStorage.getProtectedStorageEntry(protectedStoragePayload, ownerKeys);
|
||||
Assert.assertTrue(testState.mockedStorage.addProtectedStorageEntry(tmpEntry, getTestNodeAddress(), null, true));
|
||||
}
|
||||
|
||||
// P2PDataStorage::PURGE_AGE_DAYS == 10 days
|
||||
// Advance time past it so they will be valid purge targets
|
||||
this.testState.clockFake.increment(TimeUnit.DAYS.toMillis(P2PDataStorage.PURGE_AGE_DAYS + 1 - initialClockIncrement));
|
||||
|
|
Loading…
Add table
Reference in a new issue