-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #171 from alkum/i2p-fix-client-needs-2-seeds-online
Extend PeerExchangeService with optimistic peer exchange
- Loading branch information
Showing
3 changed files
with
174 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
common/src/test/java/bisq/common/util/CompletableFutureUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package bisq.common.util; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
@Slf4j | ||
public class CompletableFutureUtilsTest { | ||
|
||
@Test | ||
public void testAllOf() throws ExecutionException, InterruptedException { | ||
|
||
CompletableFuture<Void> cfA = createCompletableFuture(1000, "A"); | ||
CompletableFuture<Void> cfB = createCompletableFuture(2000, "B"); | ||
CompletableFuture<Void> cfC = createCompletableFuture(3000, "C"); | ||
|
||
CompletableFutureUtils.allOf(cfA, cfB, cfC) | ||
.thenApply(res -> { | ||
log.info("CompletableFutureUtils.allOf(A, B, C) completed"); | ||
return null; | ||
}) | ||
.get(); | ||
} | ||
|
||
@Test | ||
public void testAnyOf_allSucceed() throws ExecutionException, InterruptedException { | ||
|
||
CompletableFuture<Void> cfA = createCompletableFuture(1000, "A"); | ||
CompletableFuture<Void> cfB = createCompletableFuture(2000, "B"); | ||
CompletableFuture<Void> cfC = createCompletableFuture(3000, "C"); | ||
|
||
// Completes as soon as the fastest future in the args | ||
CompletableFutureUtils.anyOf(cfA, cfB, cfC) | ||
.thenApply(res -> { | ||
log.info("CompletableFutureUtils.anyOf(A, B, C) completed"); | ||
return null; | ||
}) | ||
.get(); | ||
} | ||
|
||
@Test | ||
public void testAnyOfBoolean() throws ExecutionException, InterruptedException { | ||
|
||
CompletableFuture<Boolean> cfA = createCompletableFutureBool(1000, "A", false); | ||
CompletableFuture<Boolean> cfB = createCompletableFutureBool(2000, "B", true); | ||
CompletableFuture<Boolean> cfC = createCompletableFutureBool(3000, "C", true); | ||
|
||
// Completes as soon as the fastest future in the args | ||
// Has the value returned by the first future that completes | ||
CompletableFutureUtils.anyOf(cfA, cfB, cfC) | ||
.thenApply(res -> { | ||
log.info("CompletableFutureUtils.anyOf(A, B, C) completed: {}", res); | ||
return res; | ||
}) | ||
.get(); | ||
} | ||
|
||
@Test | ||
public void testAnyOfBoolean_filterVal() throws ExecutionException, InterruptedException { | ||
boolean expectedValue = true; | ||
|
||
// Completes when C completes (C = first future that completes with true) | ||
CompletableFutureUtils.anyOfBooleanMatchingFilterOrAll( | ||
expectedValue, | ||
createCompletableFutureBool(1000, "A", false), | ||
createCompletableFutureBool(2000, "B", false), | ||
createCompletableFutureBool(3000, "C", true) | ||
) | ||
.thenApply(res -> { | ||
log.info("CompletableFutureUtils.anyOfBooleanFiltered(A, B, C) completed: {}", res); | ||
return res; | ||
}) | ||
.get(); | ||
|
||
// Completes when B completes (B = first future that completes with true) | ||
CompletableFutureUtils.anyOfBooleanMatchingFilterOrAll( | ||
expectedValue, | ||
createCompletableFutureBool(1000, "A", false), | ||
createCompletableFutureBool(2000, "B", true), | ||
createCompletableFutureBool(3000, "C", true) | ||
) | ||
.thenApply(res -> { | ||
log.info("CompletableFutureUtils.anyOfBooleanFiltered(A, B, C) completed: {}", res); | ||
return res; | ||
}) | ||
.get(); | ||
} | ||
|
||
private CompletableFuture<Boolean> createCompletableFutureBool(long sleepMs, String msg, boolean val) { | ||
return CompletableFuture.supplyAsync(() -> { | ||
try { | ||
Thread.sleep(sleepMs); | ||
log.info("{} (waited {} ms: {})", msg, sleepMs, val); | ||
return val; | ||
} catch (InterruptedException e) { | ||
log.error("Interrupted: " + e.getMessage(), e); | ||
return false; | ||
} | ||
}); | ||
} | ||
|
||
private CompletableFuture<Void> createCompletableFuture(long sleepMs, String msg) { | ||
return CompletableFuture.runAsync(() -> { | ||
try { | ||
Thread.sleep(sleepMs); | ||
log.info("{} (waited {} ms)", msg, sleepMs); | ||
} catch (InterruptedException e) { | ||
log.error("Interrupted: " + e.getMessage(), e); | ||
} | ||
// return null; | ||
}); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters