Skip to content

Commit

Permalink
Peer tests and code documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Aug 22, 2024
1 parent 46c70a3 commit f8753ad
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 10 deletions.
8 changes: 6 additions & 2 deletions convex-peer/src/main/java/convex/net/NIOServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,9 @@ public void launch(String bindAddress, Integer port) throws IOException {
ssc.socket().setReuseAddress(true);

bindAddress = (bindAddress == null) ? "::" : bindAddress;
InetSocketAddress address;

// Bind to a port
InetSocketAddress address;
if (port==0) {
try {
address = new InetSocketAddress(bindAddress, Constants.DEFAULT_PEER_PORT);
Expand All @@ -102,10 +103,13 @@ public void launch(String bindAddress, Integer port) throws IOException {
ssc.bind(address);
}

// Find out which port we actually bound to
address = (InetSocketAddress) ssc.getLocalAddress();
ssc.configureBlocking(false);
port = ssc.socket().getLocalPort();

// change to bnon-blocking mode
ssc.configureBlocking(false);

// Register for accept. Do this before selection loop starts and
// before we return from launch!
selector = Selector.open();
Expand Down
2 changes: 1 addition & 1 deletion convex-peer/src/main/java/convex/peer/API.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public static Server launchPeer(Map<Keyword, Object> peerConfig) {
Server server = Server.create(config);
server.launch();
return server;
} catch (Throwable t) {
} catch (Exception t) {
throw Utils.sneakyThrow(t);
} finally {
Stores.setCurrent(tempStore);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public void run() {
loop();
} catch (InterruptedException e) {
log.trace("Component thread interrupted: {}",thread);
Thread.currentThread().interrupt();
break;
} catch (Exception e) {
log.warn("Unexpected exception in "+AThreadedComponent.this.getClass().getSimpleName(),e);
Expand Down
8 changes: 2 additions & 6 deletions convex-peer/src/main/java/convex/peer/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ public String getHostname() {
/**
* Launch the Peer Server, including all main server threads
*/
public void launch() {
public void launch() throws IOException {
AStore savedStore=Stores.current();
try {
Stores.setCurrent(store);
Expand All @@ -368,7 +368,6 @@ public void launch() {
propagator.start();
transactionHandler.start();
executor.start();


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

goLive();
log.info( "Peer Server started at "+nio.getHostAddress()+" with peer key: {}",getPeerKey());
} catch (Exception e) {
close();
throw new Error("Failed to launch Server", e);
log.info( "Peer server started on port "+nio.getPort()+" with peer key: {}",getPeerKey());
} finally {
Stores.setCurrent(savedStore);
}
Expand Down
7 changes: 7 additions & 0 deletions convex-peer/src/test/java/convex/api/ConvexRemoteTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -69,6 +71,11 @@ public void testConnection() throws IOException, TimeoutException {
}
}

@Test
public void testBadConnect() throws IOException, TimeoutException {
assertThrows(IOException.class,()->Convex.connect(new InetSocketAddress("localhost", 0)));
}

@Test
public void testConvex() throws IOException, TimeoutException {
synchronized (network.SERVER) {
Expand Down
2 changes: 1 addition & 1 deletion convex-peer/src/test/java/convex/peer/TestNetwork.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public synchronized ConvexRemote getClient(AKeyPair kp) {
network.CONVEX.transferSync(addr, Coin.GOLD);
ConvexRemote client=Convex.connect(network.SERVER.getHostAddress(),addr,kp);
return client;
} catch (Throwable t) {
} catch (Exception t) {
throw Utils.sneakyThrow(t);
}
}
Expand Down

0 comments on commit f8753ad

Please sign in to comment.