mirror of
https://github.com/bisq-network/bisq.git
synced 2025-02-22 14:42:37 +01:00
[TESTS] Add tests for P2PDataStorage in order to safely refactor
These tests create real versions of the supported Payload & Entry types and run them through the 3 entry points (onMessage, Init, standard add()/remove()/refresh(), to verify the expected return values, internal state changes, and external signals (listeners, broadcasts). The tests are involved and I am proposing future work to make many of the objects more testable that will greatly reduce the work and tests cases needed. This work identified a few unexpected scenarios and potential bugs that are addressed in dependent pull requests. Code coverage when running P2PDataStorageTest: Before: Line: 4% Branch: 0% After: Line: 78% Branch 76%
This commit is contained in:
parent
ad9360636b
commit
80928663c0
8 changed files with 1698 additions and 12 deletions
|
@ -221,6 +221,7 @@ configure(project(':p2p')) {
|
|||
testCompileOnly "org.projectlombok:lombok:$lombokVersion"
|
||||
testAnnotationProcessor "org.projectlombok:lombok:$lombokVersion"
|
||||
testCompile("org.mockito:mockito-core:$mockitoVersion")
|
||||
testCompile project(':core')
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -47,7 +47,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
|
|||
@Getter
|
||||
@ToString
|
||||
@Slf4j
|
||||
public final class Alert implements ProtectedStoragePayload, ExpirablePayload {
|
||||
public class Alert implements ProtectedStoragePayload, ExpirablePayload {
|
||||
private final String message;
|
||||
private final boolean isUpdateInfo;
|
||||
private final String version;
|
||||
|
|
|
@ -776,7 +776,7 @@ public class P2PDataStorage implements MessageListener, ConnectionListener, Pers
|
|||
broadcaster.broadcast(message, sender, listener, isDataOwner);
|
||||
}
|
||||
|
||||
private ByteArray get32ByteHashAsByteArray(NetworkPayload data) {
|
||||
public static ByteArray get32ByteHashAsByteArray(NetworkPayload data) {
|
||||
return new ByteArray(P2PDataStorage.get32ByteHash(data));
|
||||
}
|
||||
|
||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -0,0 +1,42 @@
|
|||
/*
|
||||
* This file is part of Bisq.
|
||||
*
|
||||
* Bisq is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or (at
|
||||
* your option) any later version.
|
||||
*
|
||||
* Bisq is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
|
||||
* License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package bisq.network.p2p.storage.mocks;
|
||||
|
||||
import bisq.network.p2p.storage.P2PDataStorage;
|
||||
import bisq.network.p2p.storage.payload.PersistableNetworkPayload;
|
||||
import bisq.network.p2p.storage.persistence.AppendOnlyDataStoreService;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class AppendOnlyDataStoreServiceFake extends AppendOnlyDataStoreService {
|
||||
private final Map<P2PDataStorage.ByteArray, PersistableNetworkPayload> map;
|
||||
|
||||
public AppendOnlyDataStoreServiceFake() {
|
||||
super(null);
|
||||
map = new HashMap<>();
|
||||
}
|
||||
|
||||
public Map<P2PDataStorage.ByteArray, PersistableNetworkPayload> getMap() {
|
||||
return map;
|
||||
}
|
||||
|
||||
public void put(P2PDataStorage.ByteArray hashAsByteArray, PersistableNetworkPayload payload) {
|
||||
map.put(hashAsByteArray, payload);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
/*
|
||||
* This file is part of Bisq.
|
||||
*
|
||||
* Bisq is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or (at
|
||||
* your option) any later version.
|
||||
*
|
||||
* Bisq is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
|
||||
* License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package bisq.network.p2p.storage.mocks;
|
||||
|
||||
import bisq.network.p2p.storage.payload.DateTolerantPayload;
|
||||
|
||||
import java.time.Clock;
|
||||
|
||||
public class DateTolerantPayloadStub implements DateTolerantPayload {
|
||||
private final boolean dateInTolerance;
|
||||
|
||||
public DateTolerantPayloadStub(boolean dateInTolerance) {
|
||||
this.dateInTolerance = dateInTolerance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDateInTolerance(Clock clock) {
|
||||
return this.dateInTolerance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public protobuf.PersistableNetworkPayload toProtoMessage() {
|
||||
throw new UnsupportedOperationException("Stub does not support protobuf");
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] getHash() {
|
||||
return new byte[] { 1 };
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean verifyHashSize() {
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
/*
|
||||
* This file is part of Bisq.
|
||||
*
|
||||
* Bisq is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or (at
|
||||
* your option) any later version.
|
||||
*
|
||||
* Bisq is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
|
||||
* License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package bisq.network.p2p.storage.mocks;
|
||||
|
||||
import bisq.network.p2p.storage.payload.PersistableNetworkPayload;
|
||||
|
||||
public class PersistableNetworkPayloadStub implements PersistableNetworkPayload {
|
||||
private final boolean hashSizeValid;
|
||||
|
||||
public PersistableNetworkPayloadStub(boolean hashSizeValid) {
|
||||
this.hashSizeValid = hashSizeValid;
|
||||
}
|
||||
|
||||
@Override
|
||||
public protobuf.PersistableNetworkPayload toProtoMessage() {
|
||||
throw new UnsupportedOperationException("Stub does not support protobuf");
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] getHash() {
|
||||
return new byte[] { 1 };
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean verifyHashSize() {
|
||||
return this.hashSizeValid;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
/*
|
||||
* This file is part of Bisq.
|
||||
*
|
||||
* Bisq is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or (at
|
||||
* your option) any later version.
|
||||
*
|
||||
* Bisq is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
|
||||
* License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package bisq.network.p2p.storage.mocks;
|
||||
|
||||
import bisq.network.p2p.storage.P2PDataStorage;
|
||||
import bisq.network.p2p.storage.payload.ProtectedStorageEntry;
|
||||
import bisq.network.p2p.storage.persistence.ProtectedDataStoreService;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class ProtectedDataStoreServiceFake extends ProtectedDataStoreService {
|
||||
private final Map<P2PDataStorage.ByteArray, ProtectedStorageEntry> map;
|
||||
|
||||
public ProtectedDataStoreServiceFake() {
|
||||
super();
|
||||
map = new HashMap<>();
|
||||
}
|
||||
|
||||
public Map<P2PDataStorage.ByteArray, ProtectedStorageEntry> getMap() {
|
||||
return map;
|
||||
}
|
||||
|
||||
public void put(P2PDataStorage.ByteArray hashAsByteArray, ProtectedStorageEntry entry) {
|
||||
map.put(hashAsByteArray, entry);
|
||||
}
|
||||
public ProtectedStorageEntry remove(P2PDataStorage.ByteArray hash, ProtectedStorageEntry protectedStorageEntry) {
|
||||
return map.remove(hash);
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue