Skip to content
Snippets Groups Projects
Commit a3ce8143 authored by Daniel Stonier's avatar Daniel Stonier
Browse files

A more robust ntp provider.

Sets a timeout on the ntp client so it's udp ping pong doesn't block
indefinitely. This was picked up via the failing test, refer to #205.
parent 4237745c
Branches
Tags
No related merge requests found
......@@ -63,6 +63,7 @@ public class NtpTimeProvider implements TimeProvider {
this.scheduledExecutorService = scheduledExecutorService;
wallTimeProvider = new WallTimeProvider();
ntpClient = new NTPUDPClient();
ntpClient.setDefaultTimeout(500); // timeout to 500ms
offset = 0;
scheduledFuture = null;
}
......@@ -70,12 +71,19 @@ public class NtpTimeProvider implements TimeProvider {
/**
* Update the current time offset from the configured NTP host.
*
* @throws IOException
* @throws IOException : if ntpClient.getTime() fails too often.
*/
public void updateTime() throws IOException {
List<Long> offsets = Lists.newArrayList();
int failures = 0;
for (int i = 0; i < SAMPLE_SIZE; i++) {
try {
offsets.add(computeOffset());
} catch (IOException e) {
if ( ++failures > SAMPLE_SIZE/2 ) {
throw e;
}
}
}
offset = CollectionMath.median(offsets);
log.info(String.format("NTP time offset: %d ms", offset));
......
......@@ -45,7 +45,7 @@ public class NtpTimeProviderTest extends RosTest {
nodeMainExecutor.execute(new AbstractNodeMain() {
@Override
public GraphName getDefaultNodeName() {
return GraphName.of("node");
return GraphName.of("ntp_time_provider");
}
@Override
......@@ -53,7 +53,6 @@ public class NtpTimeProviderTest extends RosTest {
try {
ntpTimeProvider.updateTime();
} catch (IOException e) {
System.out.println("Dude");
// Ignored. This is only a sanity check.
}
ntpTimeProvider.getCurrentTime();
......@@ -62,6 +61,8 @@ public class NtpTimeProviderTest extends RosTest {
latch.countDown();
}
}, nodeConfiguration);
assertTrue(latch.await(1, TimeUnit.SECONDS));
boolean result = latch.await(10, TimeUnit.SECONDS);
//System.out.println("Latch waiting : " + latch.getCount() + " " + result + " [" + System.currentTimeMillis() + "]");
assertTrue(result);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment