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
No related branches found
No related tags found
No related merge requests found
...@@ -63,6 +63,7 @@ public class NtpTimeProvider implements TimeProvider { ...@@ -63,6 +63,7 @@ public class NtpTimeProvider implements TimeProvider {
this.scheduledExecutorService = scheduledExecutorService; this.scheduledExecutorService = scheduledExecutorService;
wallTimeProvider = new WallTimeProvider(); wallTimeProvider = new WallTimeProvider();
ntpClient = new NTPUDPClient(); ntpClient = new NTPUDPClient();
ntpClient.setDefaultTimeout(500); // timeout to 500ms
offset = 0; offset = 0;
scheduledFuture = null; scheduledFuture = null;
} }
...@@ -70,12 +71,19 @@ public class NtpTimeProvider implements TimeProvider { ...@@ -70,12 +71,19 @@ public class NtpTimeProvider implements TimeProvider {
/** /**
* Update the current time offset from the configured NTP host. * 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 { public void updateTime() throws IOException {
List<Long> offsets = Lists.newArrayList(); List<Long> offsets = Lists.newArrayList();
int failures = 0;
for (int i = 0; i < SAMPLE_SIZE; i++) { for (int i = 0; i < SAMPLE_SIZE; i++) {
try {
offsets.add(computeOffset()); offsets.add(computeOffset());
} catch (IOException e) {
if ( ++failures > SAMPLE_SIZE/2 ) {
throw e;
}
}
} }
offset = CollectionMath.median(offsets); offset = CollectionMath.median(offsets);
log.info(String.format("NTP time offset: %d ms", offset)); log.info(String.format("NTP time offset: %d ms", offset));
......
...@@ -45,7 +45,7 @@ public class NtpTimeProviderTest extends RosTest { ...@@ -45,7 +45,7 @@ public class NtpTimeProviderTest extends RosTest {
nodeMainExecutor.execute(new AbstractNodeMain() { nodeMainExecutor.execute(new AbstractNodeMain() {
@Override @Override
public GraphName getDefaultNodeName() { public GraphName getDefaultNodeName() {
return GraphName.of("node"); return GraphName.of("ntp_time_provider");
} }
@Override @Override
...@@ -53,7 +53,6 @@ public class NtpTimeProviderTest extends RosTest { ...@@ -53,7 +53,6 @@ public class NtpTimeProviderTest extends RosTest {
try { try {
ntpTimeProvider.updateTime(); ntpTimeProvider.updateTime();
} catch (IOException e) { } catch (IOException e) {
System.out.println("Dude");
// Ignored. This is only a sanity check. // Ignored. This is only a sanity check.
} }
ntpTimeProvider.getCurrentTime(); ntpTimeProvider.getCurrentTime();
...@@ -62,6 +61,8 @@ public class NtpTimeProviderTest extends RosTest { ...@@ -62,6 +61,8 @@ public class NtpTimeProviderTest extends RosTest {
latch.countDown(); latch.countDown();
} }
}, nodeConfiguration); }, 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