Skip to content

Commit

Permalink
Merge pull request #111 from hapifhir/wait-for-socket-to-close-2-4
Browse files Browse the repository at this point in the history
wait for socket close on simple server termination
  • Loading branch information
jamesagnew authored Nov 1, 2023
2 parents 2a2c295 + 4182dc0 commit d041b86
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
5 changes: 4 additions & 1 deletion hapi-base/src/main/java/ca/uhn/hl7v2/app/SimpleServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,10 @@ protected void handle() {
@Override
protected void afterTermination() {
super.afterTermination();
acceptor.stop();
// use stopAndWait (instead of stop) to ensure port is released when this function returns,
// so that components using this server class can reuse the port without having to add
// logic to wait for port to be released
acceptor.stopAndWait();
}

/**
Expand Down
20 changes: 14 additions & 6 deletions hapi-test/src/test/java/ca/uhn/hl7v2/app/SimpleServerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

import static org.awaitility.Awaitility.await;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ConnectException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
Expand Down Expand Up @@ -207,7 +208,6 @@ public void testRejectAttemptToStartTwice() throws InterruptedException {

}

@SuppressWarnings("resource")
@Test
public void testShutdownCleanly() throws InterruptedException, IOException {

Expand All @@ -217,13 +217,21 @@ public void testShutdownCleanly() throws InterruptedException, IOException {

Thread.sleep(1000);

Socket s = new Socket();
s.connect(new InetSocketAddress("127.0.0.1", port));
try (Socket s = new Socket(); Socket s2 = new Socket()) {
s.connect(new InetSocketAddress("127.0.0.1", port));

assertTrue(s.isConnected());
assertTrue(s.isConnected());

ss.stopAndWait();
ss.stopAndWait();

//ensure that port is released and new connections are refused after ss.stopAndWait is called
try {
s2.connect(new InetSocketAddress("127.0.0.1", port));
fail("Expected the connection to be refused after server is stopped but it did not");
} catch (ConnectException exception) {
assertFalse(s2.isConnected());
}
}
}

@Test
Expand Down
7 changes: 7 additions & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
<title>HAPI</title>
</properties>
<body>
<release version="2.4.1" date="2023-11-01">
<action type="fix" dev="Emre Dincturk">
SimpleServer now waits for its server socket to be closed when stopAndWait is called.
This makes it easier for consumers of SimpleServer to reuse the port after stopping the server
without implementing a wait for the port to be released themselves.
</action>
</release>
<release version="2.5" date="2023-10-30">
<action type="change" dev="James Agnew">
HAPI HL7v2 2.5 is identical to HAPI FHIR 2.4, but uses the jakarta.servlet
Expand Down

0 comments on commit d041b86

Please sign in to comment.