Skip to content

Commit

Permalink
Write tests for HistoricalDataStoreService.getMapSinceVersion(...)
Browse files Browse the repository at this point in the history
  • Loading branch information
alvasw committed Dec 21, 2024
1 parent 3e526c7 commit bd95df9
Show file tree
Hide file tree
Showing 4 changed files with 242 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
package bisq.core.account.witness;

import bisq.network.p2p.storage.P2PDataStorage;
import bisq.network.p2p.storage.payload.PersistableNetworkPayload;
import bisq.network.p2p.storage.persistence.PersistableNetworkPayloadStore;

import bisq.common.app.Version;

import java.nio.file.Path;

import java.io.File;

import java.util.HashMap;
import java.util.Map;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.anEmptyMap;
import static org.hamcrest.Matchers.is;


public class AccountAgeWitnessStorageServiceTest {
private DummyHistoricalDataStoreService storageService;

@BeforeEach
void setup(@TempDir Path tempDirPath) {
File tempDir = tempDirPath.toFile();
storageService = new DummyHistoricalDataStoreService(tempDir);
}

@Test
void emptyStore() {
Map<P2PDataStorage.ByteArray, PersistableNetworkPayload> mapSinceVersion =
storageService.getMapSinceVersion(Version.VERSION);
assertThat(mapSinceVersion, is(anEmptyMap()));
}

@Test
void testOnlyLiveData() {
Map<P2PDataStorage.ByteArray, PersistableNetworkPayload> liveDataMap = storageService.getStore().getMap();
DummyAccountAgeWitnessFactory.addNewAccountAgeWitnessesToMap(liveDataMap, 2);

Map<P2PDataStorage.ByteArray, PersistableNetworkPayload> mapSinceVersion =
storageService.getMapSinceVersion(Version.VERSION);

P2PDataStorage.ByteArray firstByteArray = new P2PDataStorage.ByteArray(new byte[]{0});
P2PDataStorage.ByteArray secondByteArray = new P2PDataStorage.ByteArray(new byte[]{1});

Map<P2PDataStorage.ByteArray, PersistableNetworkPayload> expected = Map.of(
firstByteArray, liveDataMap.get(firstByteArray),
secondByteArray, liveDataMap.get(secondByteArray));

assertThat(mapSinceVersion, is(expected));
}

@Test
void testOnlyStoreData() {
AccountAgeWitnessStore versionStore = DummyAccountAgeWitnessFactory
.createAccountAgeWitnessStore(1, 100);

Map<String, PersistableNetworkPayloadStore<? extends PersistableNetworkPayload>> storeByVersion = Map.of(
"1.8.0", versionStore);

storageService.setStoresByVersion(storeByVersion);

Map<P2PDataStorage.ByteArray, PersistableNetworkPayload> mapSinceVersion =
storageService.getMapSinceVersion("1.7.0");

Map<P2PDataStorage.ByteArray, PersistableNetworkPayload> expected = new HashMap<>(versionStore.getMap());
assertThat(mapSinceVersion, is(expected));
}

@Test
void testRequesterVersionNull() {
Map<P2PDataStorage.ByteArray, PersistableNetworkPayload> liveDataMap = storageService.getStore().getMap();
DummyAccountAgeWitnessFactory.addNewAccountAgeWitnessToMap(liveDataMap);

AccountAgeWitnessStore firstVersionStore = DummyAccountAgeWitnessFactory
.createAccountAgeWitnessStore(1, 100);
AccountAgeWitnessStore secondVersionStore = DummyAccountAgeWitnessFactory
.createAccountAgeWitnessStore(1, 200);

Map<String, PersistableNetworkPayloadStore<? extends PersistableNetworkPayload>> storeByVersion = Map.of(
"1.8.0", firstVersionStore,
"1.9.0", secondVersionStore);

storageService.setStoresByVersion(storeByVersion);

Map<P2PDataStorage.ByteArray, PersistableNetworkPayload> mapSinceVersion =
storageService.getMapSinceVersion(null);

Map<P2PDataStorage.ByteArray, PersistableNetworkPayload> expected = new HashMap<>();
expected.putAll(liveDataMap);
expected.putAll(firstVersionStore.getMap());
expected.putAll(secondVersionStore.getMap());

assertThat(mapSinceVersion, is(expected));
}

@Test
void testRequesterVersionIsOlder() {
Map<P2PDataStorage.ByteArray, PersistableNetworkPayload> liveDataMap = storageService.getStore().getMap();
DummyAccountAgeWitnessFactory.addNewAccountAgeWitnessToMap(liveDataMap);

AccountAgeWitnessStore firstVersionStore = DummyAccountAgeWitnessFactory.
createAccountAgeWitnessStore(1, 100);
AccountAgeWitnessStore secondVersionStore = DummyAccountAgeWitnessFactory.
createAccountAgeWitnessStore(1, 200);

Map<String, PersistableNetworkPayloadStore<? extends PersistableNetworkPayload>> storeByVersion = Map.of(
"1.8.0", firstVersionStore,
"1.9.0", secondVersionStore);

storageService.setStoresByVersion(storeByVersion);

Map<P2PDataStorage.ByteArray, PersistableNetworkPayload> mapSinceVersion =
storageService.getMapSinceVersion("1.8.5");

Map<P2PDataStorage.ByteArray, PersistableNetworkPayload> expected = new HashMap<>();
expected.putAll(liveDataMap);
expected.putAll(secondVersionStore.getMap());

assertThat(mapSinceVersion, is(expected));
}

@Test
void testRequesterVersionIsNewer() {
Map<P2PDataStorage.ByteArray, PersistableNetworkPayload> liveDataMap = storageService.getStore().getMap();
DummyAccountAgeWitnessFactory.addNewAccountAgeWitnessToMap(liveDataMap);

AccountAgeWitnessStore firstVersionStore = DummyAccountAgeWitnessFactory.
createAccountAgeWitnessStore(1, 100);
AccountAgeWitnessStore secondVersionStore = DummyAccountAgeWitnessFactory.
createAccountAgeWitnessStore(1, 200);

Map<String, PersistableNetworkPayloadStore<? extends PersistableNetworkPayload>> storeByVersion = Map.of(
"1.8.0", firstVersionStore,
"1.9.0", secondVersionStore);

storageService.setStoresByVersion(storeByVersion);

Map<P2PDataStorage.ByteArray, PersistableNetworkPayload> mapSinceVersion =
storageService.getMapSinceVersion("1.9.5");

Map<P2PDataStorage.ByteArray, PersistableNetworkPayload> expected = new HashMap<>(liveDataMap);
assertThat(mapSinceVersion, is(expected));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package bisq.core.account.witness;

import bisq.network.p2p.storage.P2PDataStorage;
import bisq.network.p2p.storage.payload.PersistableNetworkPayload;

import java.util.Map;

public class DummyAccountAgeWitnessFactory {

static AccountAgeWitnessStore createAccountAgeWitnessStore(int numberOfWitnesses, int startKeyOffset) {
AccountAgeWitnessStore accountAgeWitnessStore = new AccountAgeWitnessStore();
Map<P2PDataStorage.ByteArray, PersistableNetworkPayload> storeMap = accountAgeWitnessStore.getMap();
addNewAccountAgeWitnessesToMap(storeMap, numberOfWitnesses, startKeyOffset);
return accountAgeWitnessStore;
}

static void addNewAccountAgeWitnessesToMap(Map<P2PDataStorage.ByteArray, PersistableNetworkPayload> liveDataMap,
int numberOfWitnesses) {
addNewAccountAgeWitnessesToMap(liveDataMap, numberOfWitnesses, 0);
}

private static void addNewAccountAgeWitnessesToMap(Map<P2PDataStorage.ByteArray, PersistableNetworkPayload> liveDataMap,
int numberOfWitnesses,
int startKeyOffset) {
for (int i = 0; i < numberOfWitnesses; i++) {
addNewAccountAgeWitnessToMap(liveDataMap, startKeyOffset + i);
}
}

static void addNewAccountAgeWitnessToMap(Map<P2PDataStorage.ByteArray, PersistableNetworkPayload> liveDataMap) {
addNewAccountAgeWitnessToMap(liveDataMap, liveDataMap.size());
}

private static void addNewAccountAgeWitnessToMap(Map<P2PDataStorage.ByteArray, PersistableNetworkPayload> liveDataMap, int key) {
P2PDataStorage.ByteArray byteArray = new P2PDataStorage.ByteArray(new byte[]{(byte) key});
AccountAgeWitness accountAgeWitness = new AccountAgeWitness(new byte[]{(byte) key}, key + 1);
liveDataMap.put(byteArray, accountAgeWitness);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package bisq.core.account.witness;

import bisq.network.p2p.storage.payload.PersistableNetworkPayload;
import bisq.network.p2p.storage.persistence.HistoricalDataStoreService;
import bisq.network.p2p.storage.persistence.PersistableNetworkPayloadStore;

import com.google.common.collect.ImmutableMap;

import java.io.File;

import java.util.Collections;
import java.util.Map;

public class DummyHistoricalDataStoreService extends HistoricalDataStoreService<AccountAgeWitnessStore> {

public DummyHistoricalDataStoreService(File storageDir) {
super(storageDir, null);
store = new AccountAgeWitnessStore();
storesByVersion = ImmutableMap.copyOf(Collections.emptyMap());
}

@Override
public boolean canHandle(PersistableNetworkPayload payload) {
return false;
}

@Override
public String getFileName() {
return "";
}

@Override
protected void initializePersistenceManager() {

}

@Override
protected AccountAgeWitnessStore createStore() {
return null;
}

public AccountAgeWitnessStore getStore() {
return store;
}

public void setStoresByVersion(Map<String, PersistableNetworkPayloadStore<? extends PersistableNetworkPayload>>
storesByVersion) {
this.storesByVersion = ImmutableMap.copyOf(storesByVersion);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
*/
@Slf4j
public abstract class HistoricalDataStoreService<T extends PersistableNetworkPayloadStore<? extends PersistableNetworkPayload>> extends MapStoreService<T, PersistableNetworkPayload> {
private ImmutableMap<String, PersistableNetworkPayloadStore<? extends PersistableNetworkPayload>> storesByVersion;
protected ImmutableMap<String, PersistableNetworkPayloadStore<? extends PersistableNetworkPayload>> storesByVersion;
// Cache to avoid that we have to recreate the historical data at each request
private ImmutableMap<P2PDataStorage.ByteArray, PersistableNetworkPayload> allHistoricalPayloads;

Expand Down

0 comments on commit bd95df9

Please sign in to comment.