Skip to content

Commit f8753ad

Browse files
committed
Peer tests and code documentation
1 parent 46c70a3 commit f8753ad

File tree

6 files changed

+18
-10
lines changed

6 files changed

+18
-10
lines changed

convex-peer/src/main/java/convex/net/NIOServer.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,9 @@ public void launch(String bindAddress, Integer port) throws IOException {
8686
ssc.socket().setReuseAddress(true);
8787

8888
bindAddress = (bindAddress == null) ? "::" : bindAddress;
89-
InetSocketAddress address;
9089

90+
// Bind to a port
91+
InetSocketAddress address;
9192
if (port==0) {
9293
try {
9394
address = new InetSocketAddress(bindAddress, Constants.DEFAULT_PEER_PORT);
@@ -102,10 +103,13 @@ public void launch(String bindAddress, Integer port) throws IOException {
102103
ssc.bind(address);
103104
}
104105

106+
// Find out which port we actually bound to
105107
address = (InetSocketAddress) ssc.getLocalAddress();
106-
ssc.configureBlocking(false);
107108
port = ssc.socket().getLocalPort();
108109

110+
// change to bnon-blocking mode
111+
ssc.configureBlocking(false);
112+
109113
// Register for accept. Do this before selection loop starts and
110114
// before we return from launch!
111115
selector = Selector.open();

convex-peer/src/main/java/convex/peer/API.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public static Server launchPeer(Map<Keyword, Object> peerConfig) {
9090
Server server = Server.create(config);
9191
server.launch();
9292
return server;
93-
} catch (Throwable t) {
93+
} catch (Exception t) {
9494
throw Utils.sneakyThrow(t);
9595
} finally {
9696
Stores.setCurrent(tempStore);

convex-peer/src/main/java/convex/peer/AThreadedComponent.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public void run() {
2929
loop();
3030
} catch (InterruptedException e) {
3131
log.trace("Component thread interrupted: {}",thread);
32+
Thread.currentThread().interrupt();
3233
break;
3334
} catch (Exception e) {
3435
log.warn("Unexpected exception in "+AThreadedComponent.this.getClass().getSimpleName(),e);

convex-peer/src/main/java/convex/peer/Server.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ public String getHostname() {
343343
/**
344344
* Launch the Peer Server, including all main server threads
345345
*/
346-
public void launch() {
346+
public void launch() throws IOException {
347347
AStore savedStore=Stores.current();
348348
try {
349349
Stores.setCurrent(store);
@@ -368,7 +368,6 @@ public void launch() {
368368
propagator.start();
369369
transactionHandler.start();
370370
executor.start();
371-
372371

373372
// Connect to source peer if specified
374373
if (getConfig().containsKey(Keywords.SOURCE)) {
@@ -386,10 +385,7 @@ public void launch() {
386385
}
387386

388387
goLive();
389-
log.info( "Peer Server started at "+nio.getHostAddress()+" with peer key: {}",getPeerKey());
390-
} catch (Exception e) {
391-
close();
392-
throw new Error("Failed to launch Server", e);
388+
log.info( "Peer server started on port "+nio.getPort()+" with peer key: {}",getPeerKey());
393389
} finally {
394390
Stores.setCurrent(savedStore);
395391
}

convex-peer/src/test/java/convex/api/ConvexRemoteTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
import static org.junit.jupiter.api.Assertions.assertEquals;
44
import static org.junit.jupiter.api.Assertions.assertFalse;
55
import static org.junit.jupiter.api.Assertions.assertNull;
6+
import static org.junit.jupiter.api.Assertions.assertThrows;
67
import static org.junit.jupiter.api.Assertions.assertTrue;
78

89
import java.io.IOException;
10+
import java.net.InetSocketAddress;
911
import java.util.concurrent.ExecutionException;
1012
import java.util.concurrent.Future;
1113
import java.util.concurrent.TimeUnit;
@@ -69,6 +71,11 @@ public void testConnection() throws IOException, TimeoutException {
6971
}
7072
}
7173

74+
@Test
75+
public void testBadConnect() throws IOException, TimeoutException {
76+
assertThrows(IOException.class,()->Convex.connect(new InetSocketAddress("localhost", 0)));
77+
}
78+
7279
@Test
7380
public void testConvex() throws IOException, TimeoutException {
7481
synchronized (network.SERVER) {

convex-peer/src/test/java/convex/peer/TestNetwork.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public synchronized ConvexRemote getClient(AKeyPair kp) {
9898
network.CONVEX.transferSync(addr, Coin.GOLD);
9999
ConvexRemote client=Convex.connect(network.SERVER.getHostAddress(),addr,kp);
100100
return client;
101-
} catch (Throwable t) {
101+
} catch (Exception t) {
102102
throw Utils.sneakyThrow(t);
103103
}
104104
}

0 commit comments

Comments
 (0)