Skip to content

Commit

Permalink
Refactor timeout check (#2223)
Browse files Browse the repository at this point in the history
Use nano time for IT
Why: see infinite loops in tests and these might be resulted because of the VM and behaviour of this timeout check
if vm goes into a suspend mode (only for a short time) and awakes then the time comparison can be a problem and this
could explain the infinite loop. Saw similar things in #2105
  • Loading branch information
strehle committed Mar 3, 2023
1 parent 4a079b4 commit 3161e00
Showing 1 changed file with 4 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;

import static org.cloudfoundry.identity.statsd.integration.IntegrationTestUtils.*;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.junit.Assert.*;

class UaaMetricsEmitterIT {
private static final int WAIT_FOR_MESSAGE = 5500;
private static final long WAIT_FOR_MESSAGE = TimeUnit.MILLISECONDS.toNanos(5500);
private static DatagramSocket serverSocket;
private static byte[] receiveData;
private static DatagramPacket receivePacket;
Expand Down Expand Up @@ -114,7 +115,7 @@ void assertGenericMetrics(String statsDKey) {
}

private static Map<String, String> getMessages(List<String> fragments) throws IOException {
long startTime = System.currentTimeMillis();
long startTime = System.nanoTime();
Map<String, String> results = new HashMap<>();
do {
receiveData = new byte[65535];
Expand All @@ -130,7 +131,7 @@ private static Map<String, String> getMessages(List<String> fragments) throws IO
} catch (SocketTimeoutException e) {
//expected so that we keep looping
}
} while (results.size() < fragments.size() && (System.currentTimeMillis() < (startTime + UaaMetricsEmitterIT.WAIT_FOR_MESSAGE)));
} while (results.size() < fragments.size() && (System.nanoTime() < (startTime + UaaMetricsEmitterIT.WAIT_FOR_MESSAGE)));
return results;
}

Expand Down

0 comments on commit 3161e00

Please sign in to comment.