From f8753ad0c51904798d169ac6c60b0b22d03b9bf7 Mon Sep 17 00:00:00 2001 From: mikera Date: Thu, 22 Aug 2024 18:42:41 +0100 Subject: [PATCH] Peer tests and code documentation --- convex-peer/src/main/java/convex/net/NIOServer.java | 8 ++++++-- convex-peer/src/main/java/convex/peer/API.java | 2 +- .../src/main/java/convex/peer/AThreadedComponent.java | 1 + convex-peer/src/main/java/convex/peer/Server.java | 8 ++------ .../src/test/java/convex/api/ConvexRemoteTest.java | 7 +++++++ convex-peer/src/test/java/convex/peer/TestNetwork.java | 2 +- 6 files changed, 18 insertions(+), 10 deletions(-) diff --git a/convex-peer/src/main/java/convex/net/NIOServer.java b/convex-peer/src/main/java/convex/net/NIOServer.java index 9b7f663c1..034677914 100644 --- a/convex-peer/src/main/java/convex/net/NIOServer.java +++ b/convex-peer/src/main/java/convex/net/NIOServer.java @@ -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); @@ -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(); diff --git a/convex-peer/src/main/java/convex/peer/API.java b/convex-peer/src/main/java/convex/peer/API.java index 3750f81f1..59c066161 100644 --- a/convex-peer/src/main/java/convex/peer/API.java +++ b/convex-peer/src/main/java/convex/peer/API.java @@ -90,7 +90,7 @@ public static Server launchPeer(Map peerConfig) { Server server = Server.create(config); server.launch(); return server; - } catch (Throwable t) { + } catch (Exception t) { throw Utils.sneakyThrow(t); } finally { Stores.setCurrent(tempStore); diff --git a/convex-peer/src/main/java/convex/peer/AThreadedComponent.java b/convex-peer/src/main/java/convex/peer/AThreadedComponent.java index c06aaec18..e0a85b144 100644 --- a/convex-peer/src/main/java/convex/peer/AThreadedComponent.java +++ b/convex-peer/src/main/java/convex/peer/AThreadedComponent.java @@ -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); diff --git a/convex-peer/src/main/java/convex/peer/Server.java b/convex-peer/src/main/java/convex/peer/Server.java index eaff1746b..2aa8563c1 100644 --- a/convex-peer/src/main/java/convex/peer/Server.java +++ b/convex-peer/src/main/java/convex/peer/Server.java @@ -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); @@ -368,7 +368,6 @@ public void launch() { propagator.start(); transactionHandler.start(); executor.start(); - // Connect to source peer if specified if (getConfig().containsKey(Keywords.SOURCE)) { @@ -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); } diff --git a/convex-peer/src/test/java/convex/api/ConvexRemoteTest.java b/convex-peer/src/test/java/convex/api/ConvexRemoteTest.java index 947f1bde7..56f8789ea 100644 --- a/convex-peer/src/test/java/convex/api/ConvexRemoteTest.java +++ b/convex-peer/src/test/java/convex/api/ConvexRemoteTest.java @@ -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; @@ -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) { diff --git a/convex-peer/src/test/java/convex/peer/TestNetwork.java b/convex-peer/src/test/java/convex/peer/TestNetwork.java index 2ab062633..ae2f081cd 100644 --- a/convex-peer/src/test/java/convex/peer/TestNetwork.java +++ b/convex-peer/src/test/java/convex/peer/TestNetwork.java @@ -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); } }