Skip to content

Commit

Permalink
[fix] flaky test
Browse files Browse the repository at this point in the history
  • Loading branch information
zolyfarkas committed Dec 9, 2023
1 parent fb22a56 commit ea4d800
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions spf4j-core/src/test/java/org/spf4j/net/SntpClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void test() throws IOException, InterruptedException, TimeoutException {
}

@Test
public void test2() throws IOException {
public void test2() throws IOException, InterruptedException, TimeoutException {
try (Closeable runUdpServer = runUdpServer(false)) {
Timing timing = SntpClient.requestTime("localhost", 50123, 20);
long currentTimeMachine = System.currentTimeMillis();
Expand Down Expand Up @@ -105,18 +105,19 @@ public void test3() {
Assert.assertTrue(Math.abs(currentTimeMillis - readTimeStamp) < 2);
}

public static Closeable runUdpServer(final boolean hickup) {
public static Closeable runUdpServer(final boolean hickup) throws InterruptedException,
TimeoutException, IOException {

return new Closeable() {
private volatile boolean terminated = false;
private final DatagramSocket socket = RetryPolicy.defaultPolicy().call(
() -> new DatagramSocket(50123), IOException.class, 2, TimeUnit.MINUTES);
private Future<?> server = org.spf4j.concurrent.DefaultExecutor.INSTANCE.submit(new AbstractRunnable(true) {

@Override
@SuppressFBWarnings("MDM_THREAD_YIELD") // not ideal
public void doRun() throws IOException, InterruptedException, TimeoutException {
public void doRun() throws IOException, InterruptedException {
boolean first = true;
try (DatagramSocket socket = RetryPolicy.defaultPolicy().call(
() -> new DatagramSocket(50123), IOException.class, 2, TimeUnit.MINUTES)) {
socket.setSoTimeout(1000);
byte[] buffer = new byte[48];
DatagramPacket request = new DatagramPacket(buffer, buffer.length);
Expand All @@ -142,14 +143,14 @@ public void doRun() throws IOException, InterruptedException, TimeoutException {
socket.send(response);
LOG.debug("Server reply to {} with {}", request.getPort(), buffer);
}
}
}
});

@Override
public void close() {
terminated = true;
server.cancel(true);
socket.close();
}
};

Expand Down

0 comments on commit ea4d800

Please sign in to comment.