TestWithNetworkConnections, InboundMessageQueuer: migrate from SettableFuture to CompletableFuture

This commit is contained in:
Sean Gilligan 2022-03-07 22:27:23 +01:00 committed by Andreas Schildbach
parent da9c12312f
commit 658514986f
2 changed files with 8 additions and 9 deletions

View File

@ -17,7 +17,6 @@
package org.bitcoinj.testing;
import org.bitcoinj.core.*;
import com.google.common.util.concurrent.SettableFuture;
import java.net.InetAddress;
import java.net.InetSocketAddress;
@ -25,13 +24,14 @@ import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CompletableFuture;
/**
* An extension of {@link PeerSocketHandler} that keeps inbound messages in a queue for later processing
*/
public abstract class InboundMessageQueuer extends PeerSocketHandler {
public final BlockingQueue<Message> inboundMessages = new ArrayBlockingQueue<>(1000);
public final Map<Long, SettableFuture<Void>> mapPingFutures = new HashMap<>();
public final Map<Long, CompletableFuture<Void>> mapPingFutures = new HashMap<>();
public Peer peer;
public BloomFilter lastReceivedFilter;
@ -51,9 +51,9 @@ public abstract class InboundMessageQueuer extends PeerSocketHandler {
@Override
protected void processMessage(Message m) throws Exception {
if (m instanceof Ping) {
SettableFuture<Void> future = mapPingFutures.get(((Ping) m).getNonce());
CompletableFuture<Void> future = mapPingFutures.get(((Ping) m).getNonce());
if (future != null) {
future.set(null);
future.complete(null);
return;
}
}

View File

@ -30,8 +30,6 @@ import org.bitcoinj.utils.Threading;
import org.bitcoinj.wallet.KeyChainGroup;
import org.bitcoinj.wallet.Wallet;
import com.google.common.util.concurrent.SettableFuture;
import javax.annotation.Nullable;
import javax.net.SocketFactory;
import java.io.IOException;
@ -39,6 +37,7 @@ import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicBoolean;
@ -198,7 +197,7 @@ public class TestWithNetworkConnections {
private void outboundPingAndWait(final InboundMessageQueuer p, long nonce) throws Exception {
// Send a ping and wait for it to get to the other side
SettableFuture<Void> pingReceivedFuture = SettableFuture.create();
CompletableFuture<Void> pingReceivedFuture = new CompletableFuture<>();
p.mapPingFutures.put(nonce, pingReceivedFuture);
p.peer.sendMessage(new Ping(nonce));
pingReceivedFuture.get();
@ -207,10 +206,10 @@ public class TestWithNetworkConnections {
private void inboundPongAndWait(final InboundMessageQueuer p, final long nonce) throws Exception {
// Receive a ping (that the Peer doesn't see) and wait for it to get through the socket
final SettableFuture<Void> pongReceivedFuture = SettableFuture.create();
final CompletableFuture<Void> pongReceivedFuture = new CompletableFuture<>();
PreMessageReceivedEventListener listener = (p1, m) -> {
if (m instanceof Pong && ((Pong) m).getNonce() == nonce) {
pongReceivedFuture.set(null);
pongReceivedFuture.complete(null);
return null;
}
return m;