Remove mapping to user thread in client code

Add synchronous methods for tests. They new async methods lead to failing tests.
It could be probably fixed, but its quite an effort... Don't like to add code just for
tests but on the other hand, maybe those methods might be useful for other use cases as well.
This commit is contained in:
chimp1984 2020-10-21 14:45:40 -05:00
parent dd7f40d5bf
commit 68583d86ee
No known key found for this signature in database
GPG key ID: 9801B4EC591F90E3
7 changed files with 83 additions and 12 deletions

View file

@ -189,6 +189,15 @@ public class P2PDataStorage implements MessageListener, ConnectionListener, Pers
completeHandler);
}
// Uses synchronous execution on the userThread. Only used by tests. The async methods should be used by app code.
@VisibleForTesting
public void readPersistedSync() {
SequenceNumberMap persisted = persistenceManager.getPersisted();
if (persisted != null) {
sequenceNumberMap.setMap(getPurgedSequenceNumberMap(persisted.getMap()));
}
}
// Threading is done on the persistenceManager level
public void readFromResources(String postFix, Runnable completeHandler) {
BooleanProperty appendOnlyDataStoreServiceReady = new SimpleBooleanProperty();
@ -213,6 +222,17 @@ public class P2PDataStorage implements MessageListener, ConnectionListener, Pers
});
}
// Uses synchronous execution on the userThread. Only used by tests. The async methods should be used by app code.
@VisibleForTesting
public void readFromResourcesSync(String postFix) {
appendOnlyDataStoreService.readFromResourcesSync(postFix);
protectedDataStoreService.readFromResourcesSync(postFix);
resourceDataStoreService.readFromResourcesSync(postFix);
map.putAll(protectedDataStoreService.getMap());
}
///////////////////////////////////////////////////////////////////////////////////////////
// RequestData API
///////////////////////////////////////////////////////////////////////////////////////////

View file

@ -20,10 +20,10 @@ package bisq.network.p2p.storage.persistence;
import bisq.network.p2p.storage.P2PDataStorage;
import bisq.network.p2p.storage.payload.PersistableNetworkPayload;
import bisq.common.UserThread;
import javax.inject.Inject;
import com.google.common.annotations.VisibleForTesting;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@ -59,12 +59,19 @@ public class AppendOnlyDataStoreService {
services.forEach(service -> {
service.readFromResources(postFix, () -> {
if (remaining.decrementAndGet() == 0) {
UserThread.execute(completeHandler);
completeHandler.run();
}
});
});
}
// Uses synchronous execution on the userThread. Only used by tests. The async methods should be used by app code.
@VisibleForTesting
public void readFromResourcesSync(String postFix) {
services.forEach(service -> service.readFromResourcesSync(postFix));
}
public Map<P2PDataStorage.ByteArray, PersistableNetworkPayload> getMap() {
return services.stream()
.flatMap(service -> service.getMap().entrySet().stream())

View file

@ -20,11 +20,12 @@ package bisq.network.p2p.storage.persistence;
import bisq.network.p2p.storage.P2PDataStorage;
import bisq.network.p2p.storage.payload.ProtectedStorageEntry;
import bisq.common.UserThread;
import bisq.common.proto.persistable.PersistableEnvelope;
import javax.inject.Inject;
import com.google.common.annotations.VisibleForTesting;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@ -59,12 +60,18 @@ public class ProtectedDataStoreService {
services.forEach(service -> {
service.readFromResources(postFix, () -> {
if (remaining.decrementAndGet() == 0) {
UserThread.execute(completeHandler);
completeHandler.run();
}
});
});
}
// Uses synchronous execution on the userThread. Only used by tests. The async methods should be used by app code.
@VisibleForTesting
public void readFromResourcesSync(String postFix) {
services.forEach(service -> service.readFromResourcesSync(postFix));
}
public Map<P2PDataStorage.ByteArray, ProtectedStorageEntry> getMap() {
return services.stream()
.flatMap(service -> service.getMap().entrySet().stream())

View file

@ -17,11 +17,12 @@
package bisq.network.p2p.storage.persistence;
import bisq.common.UserThread;
import bisq.common.proto.persistable.PersistableEnvelope;
import javax.inject.Inject;
import com.google.common.annotations.VisibleForTesting;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
@ -49,9 +50,15 @@ public class ResourceDataStoreService {
services.forEach(service -> {
service.readFromResources(postFix, () -> {
if (remaining.decrementAndGet() == 0) {
UserThread.execute(completeHandler);
completeHandler.run();
}
});
});
}
// Uses synchronous execution on the userThread. Only used by tests. The async methods should be used by app code.
@VisibleForTesting
public void readFromResourcesSync(String postFix) {
services.forEach(service -> service.readFromResourcesSync(postFix));
}
}

View file

@ -22,6 +22,8 @@ import bisq.common.file.ResourceNotFoundException;
import bisq.common.persistence.PersistenceManager;
import bisq.common.proto.persistable.PersistableEnvelope;
import com.google.common.annotations.VisibleForTesting;
import java.nio.file.Paths;
import java.io.File;
@ -89,6 +91,19 @@ public abstract class StoreService<T extends PersistableEnvelope> {
}
}
// Uses synchronous execution on the userThread. Only used by tests. The async methods should be used by app code.
@VisibleForTesting
protected void readFromResourcesSync(String postFix) {
String fileName = getFileName();
makeFileFromResourceFile(fileName, postFix);
try {
readStoreSync();
} catch (Throwable t) {
makeFileFromResourceFile(fileName, postFix);
readStoreSync();
}
}
protected boolean makeFileFromResourceFile(String fileName, String postFix) {
String resourceFileName = fileName + postFix;
File dbDir = new File(absolutePathOfStorageDir);
@ -129,6 +144,23 @@ public abstract class StoreService<T extends PersistableEnvelope> {
});
}
// Uses synchronous execution on the userThread. Only used by tests. The async methods should be used by app code.
@VisibleForTesting
protected T getStoreSync(String fileName) {
T store = persistenceManager.getPersisted(fileName);
if (store == null) {
store = createStore();
}
return store;
}
// Uses synchronous execution on the userThread. Only used by tests. The async methods should be used by app code.
@VisibleForTesting
protected void readStoreSync() {
store = getStoreSync(getFileName());
initializePersistenceManager();
}
protected abstract void initializePersistenceManager();
protected abstract T createStore();

View file

@ -137,10 +137,8 @@ public class TestState {
sequenceNrMapPersistenceManager, clock, MAX_SEQUENCE_NUMBER_MAP_SIZE_BEFORE_PURGE);
// Currently TestState only supports reading ProtectedStorageEntries off disk.
p2PDataStorage.readFromResources("unused", () -> {
});
p2PDataStorage.readPersisted(() -> {
});
p2PDataStorage.readFromResourcesSync("unused");
p2PDataStorage.readPersistedSync();
p2PDataStorage.addHashMapChangedListener(hashMapChangedListener);
p2PDataStorage.addAppendOnlyDataStoreListener(appendOnlyDataStoreListener);

View file

@ -64,7 +64,7 @@ public class MapStoreServiceFake extends MapStoreService {
return true;
}
protected void readFromResources(String postFix, Runnable completeHandler) {
protected void readFromResourcesSync(String postFix) {
// do nothing. This Fake only supports in-memory storage.
}