Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(1/6) [PR COMMENTS] Remove P2PDataStorage subclass in tests #3628

Merged
merged 2 commits into from
Nov 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions p2p/src/main/java/bisq/network/p2p/P2PModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,6 @@ protected void configure() {
bindConstant().annotatedWith(named(NetworkOptionKeys.MSG_THROTTLE_PER_10_SEC)).to(environment.getRequiredProperty(NetworkOptionKeys.MSG_THROTTLE_PER_10_SEC));
bindConstant().annotatedWith(named(NetworkOptionKeys.SEND_MSG_THROTTLE_TRIGGER)).to(environment.getRequiredProperty(NetworkOptionKeys.SEND_MSG_THROTTLE_TRIGGER));
bindConstant().annotatedWith(named(NetworkOptionKeys.SEND_MSG_THROTTLE_SLEEP)).to(environment.getRequiredProperty(NetworkOptionKeys.SEND_MSG_THROTTLE_SLEEP));
bindConstant().annotatedWith(named("MAX_SEQUENCE_NUMBER_MAP_SIZE_BEFORE_PURGE")).to(1000);
}
}
11 changes: 8 additions & 3 deletions p2p/src/main/java/bisq/network/p2p/storage/P2PDataStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@

import com.google.protobuf.ByteString;

import com.google.inject.name.Named;

import javax.inject.Inject;

import com.google.common.annotations.VisibleForTesting;
Expand Down Expand Up @@ -121,7 +123,9 @@ public class P2PDataStorage implements MessageListener, ConnectionListener, Pers
private final Set<ProtectedDataStoreListener> protectedDataStoreListeners = new CopyOnWriteArraySet<>();
private final Clock clock;

protected int maxSequenceNumberMapSizeBeforePurge;
/// The maximum number of items that must exist in the SequenceNumberMap before it is scheduled for a purge
/// which removes entries after PURGE_AGE_DAYS.
private final int maxSequenceNumberMapSizeBeforePurge;

///////////////////////////////////////////////////////////////////////////////////////////
// Constructor
Expand All @@ -134,20 +138,21 @@ public P2PDataStorage(NetworkNode networkNode,
ProtectedDataStoreService protectedDataStoreService,
ResourceDataStoreService resourceDataStoreService,
Storage<SequenceNumberMap> sequenceNumberMapStorage,
Clock clock) {
Clock clock,
@Named("MAX_SEQUENCE_NUMBER_MAP_SIZE_BEFORE_PURGE") int maxSequenceNumberBeforePurge) {
this.broadcaster = broadcaster;
this.appendOnlyDataStoreService = appendOnlyDataStoreService;
this.protectedDataStoreService = protectedDataStoreService;
this.resourceDataStoreService = resourceDataStoreService;
this.clock = clock;
this.maxSequenceNumberMapSizeBeforePurge = maxSequenceNumberBeforePurge;


networkNode.addMessageListener(this);
networkNode.addConnectionListener(this);

this.sequenceNumberMapStorage = sequenceNumberMapStorage;
sequenceNumberMapStorage.setNumMaxBackupFiles(5);
this.maxSequenceNumberMapSizeBeforePurge = 1000;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
* 2 & 3 Client API [addPersistableNetworkPayload(reBroadcast=(true && false))]
* 4. onMessage() [onMessage(AddPersistableNetworkPayloadMessage)]
*/
@SuppressWarnings("unused")
public class P2PDataStoragePersistableNetworkPayloadTest {

@RunWith(Parameterized.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
* 1. Client API [addProtectedStorageEntry(), refreshTTL(), remove()]
* 2. onMessage() [AddDataMessage, RefreshOfferMessage, RemoveDataMessage]
*/
@SuppressWarnings("unused")
public class P2PDataStorageProtectedStorageEntryTest {
@RunWith(Parameterized.class)
abstract public static class ProtectedStorageEntryTestBase {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public void removeExpiredEntries_PurgeSeqNrMap() throws CryptoException, NoSuchA

Assert.assertTrue(testState.mockedStorage.addProtectedStorageEntry(purgedProtectedStorageEntry, TestState.getTestNodeAddress(), null, true));

for (int i = 0; i < 4; ++i) {
for (int i = 0; i < MAX_SEQUENCE_NUMBER_MAP_SIZE_BEFORE_PURGE - 1; ++i) {
KeyPair ownerKeys = TestUtils.generateKeyPair();
ProtectedStoragePayload protectedStoragePayload = new PersistableExpirableProtectedStoragePayloadStub(ownerKeys.getPublic(), 0);
ProtectedStorageEntry tmpEntry = testState.mockedStorage.getProtectedStorageEntry(protectedStoragePayload, ownerKeys);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@
import org.junit.Before;
import org.junit.Test;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import static bisq.network.p2p.storage.TestState.*;
Expand Down Expand Up @@ -173,7 +170,7 @@ public void connectionClosedReduceTTLAndExpireItemsFromPeerPersistable()
class ExpirablePersistentProtectedStoragePayloadStub
extends ExpirableProtectedStoragePayloadStub implements PersistablePayload {

public ExpirablePersistentProtectedStoragePayloadStub(PublicKey ownerPubKey) {
private ExpirablePersistentProtectedStoragePayloadStub(PublicKey ownerPubKey) {
super(ownerPubKey, 0);
}
}
Expand Down
30 changes: 5 additions & 25 deletions p2p/src/test/java/bisq/network/p2p/storage/TestState.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@
import bisq.network.p2p.storage.payload.ProtectedMailboxStorageEntry;
import bisq.network.p2p.storage.payload.ProtectedStorageEntry;
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;

Expand All @@ -47,8 +45,6 @@

import java.security.PublicKey;

import java.time.Clock;

import java.util.concurrent.TimeUnit;

import org.junit.Assert;
Expand All @@ -63,43 +59,27 @@
* Used in the P2PDataStorage*Test(s) in order to leverage common test set up and validation.
*/
public class TestState {
static final int MAX_SEQUENCE_NUMBER_MAP_SIZE_BEFORE_PURGE = 5;

final P2PDataStorage mockedStorage;
final Broadcaster mockBroadcaster;

final AppendOnlyDataStoreListener appendOnlyDataStoreListener;
private final ProtectedDataStoreListener protectedDataStoreListener;
final HashMapChangedListener hashMapChangedListener;
private final HashMapChangedListener hashMapChangedListener;
private 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 P2PDataStorageForTest(mock(NetworkNode.class),
this.mockedStorage = new P2PDataStorage(mock(NetworkNode.class),
this.mockBroadcaster,
new AppendOnlyDataStoreServiceFake(),
new ProtectedDataStoreServiceFake(), mock(ResourceDataStoreService.class),
this.mockSeqNrStorage, this.clockFake);
this.mockSeqNrStorage, this.clockFake, MAX_SEQUENCE_NUMBER_MAP_SIZE_BEFORE_PURGE);

this.appendOnlyDataStoreListener = mock(AppendOnlyDataStoreListener.class);
this.protectedDataStoreListener = mock(ProtectedDataStoreListener.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class ProtectedStoragePayloadStub implements ProtectedStoragePayload {
@Getter
private PublicKey ownerPubKey;

protected Message messageMock;
protected final Message messageMock;

public ProtectedStoragePayloadStub(PublicKey ownerPubKey) {
this.ownerPubKey = ownerPubKey;
Expand Down