Skip to content

Commit

Permalink
Use dependency injected Clock in P2PDataStore
Browse files Browse the repository at this point in the history
Use the DI Clock object already available in P2PDataStore, instead
of calling System.currentTimeMillis() directly. These two functions
have the same behavior and switching over allows finer control
of time in the tests.
  • Loading branch information
julianknutsen committed Nov 12, 2019
1 parent e5f9261 commit de72d39
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions p2p/src/main/java/bisq/network/p2p/storage/P2PDataStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ public boolean addProtectedStorageEntry(ProtectedStorageEntry protectedStorageEn
hashMapChangedListeners.forEach(e -> e.onAdded(protectedStorageEntry));

// Record the updated sequence number and persist it. Higher delay so we can batch more items.
sequenceNumberMap.put(hashOfPayload, new MapValue(protectedStorageEntry.getSequenceNumber(), System.currentTimeMillis()));
sequenceNumberMap.put(hashOfPayload, new MapValue(protectedStorageEntry.getSequenceNumber(), this.clock.millis()));
sequenceNumberMapStorage.queueUpForSave(SequenceNumberMap.clone(sequenceNumberMap), 2000);

// Optionally, broadcast the add/update depending on the calling environment
Expand Down Expand Up @@ -472,7 +472,7 @@ public boolean refreshTTL(RefreshOfferMessage refreshTTLMessage,
printData("after refreshTTL");

// Record the latest sequence number and persist it
sequenceNumberMap.put(hashOfPayload, new MapValue(sequenceNumber, System.currentTimeMillis()));
sequenceNumberMap.put(hashOfPayload, new MapValue(sequenceNumber, this.clock.millis()));
sequenceNumberMapStorage.queueUpForSave(SequenceNumberMap.clone(sequenceNumberMap), 1000);

// Always broadcast refreshes
Expand All @@ -492,7 +492,6 @@ public boolean remove(ProtectedStorageEntry protectedStorageEntry,
// If we don't know about the target of this remove, ignore it
if (!map.containsKey(hashOfPayload)) {
log.debug("Remove data ignored as we don't have an entry for that data.");

return false;
}

Expand All @@ -513,7 +512,7 @@ public boolean remove(ProtectedStorageEntry protectedStorageEntry,
printData("after remove");

// Record the latest sequence number and persist it
sequenceNumberMap.put(hashOfPayload, new MapValue(protectedStorageEntry.getSequenceNumber(), System.currentTimeMillis()));
sequenceNumberMap.put(hashOfPayload, new MapValue(protectedStorageEntry.getSequenceNumber(), this.clock.millis()));
sequenceNumberMapStorage.queueUpForSave(SequenceNumberMap.clone(sequenceNumberMap), 300);

maybeAddToRemoveAddOncePayloads(protectedStoragePayload, hashOfPayload);
Expand Down Expand Up @@ -608,7 +607,7 @@ public boolean removeMailboxData(ProtectedMailboxStorageEntry protectedMailboxSt
printData("after removeMailboxData");

// Record the latest sequence number and persist it
sequenceNumberMap.put(hashOfPayload, new MapValue(sequenceNumber, System.currentTimeMillis()));
sequenceNumberMap.put(hashOfPayload, new MapValue(sequenceNumber, this.clock.millis()));
sequenceNumberMapStorage.queueUpForSave(SequenceNumberMap.clone(sequenceNumberMap), 300);

maybeAddToRemoveAddOncePayloads(protectedStoragePayload, hashOfPayload);
Expand Down Expand Up @@ -839,7 +838,7 @@ private static byte[] getCompactHash(ProtectedStoragePayload protectedStoragePay
// Get a new map with entries older than PURGE_AGE_DAYS purged from the given map.
private Map<ByteArray, MapValue> getPurgedSequenceNumberMap(Map<ByteArray, MapValue> persisted) {
Map<ByteArray, MapValue> purged = new HashMap<>();
long maxAgeTs = System.currentTimeMillis() - TimeUnit.DAYS.toMillis(PURGE_AGE_DAYS);
long maxAgeTs = this.clock.millis() - TimeUnit.DAYS.toMillis(PURGE_AGE_DAYS);
persisted.forEach((key, value) -> {
if (value.timeStamp > maxAgeTs)
purged.put(key, value);
Expand Down

0 comments on commit de72d39

Please sign in to comment.