Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 package org.ros.time;
00018
00019 import static org.junit.Assert.assertTrue;
00020
00021 import org.junit.Test;
00022 import org.ros.RosTest;
00023 import org.ros.address.InetAddressFactory;
00024 import org.ros.namespace.GraphName;
00025 import org.ros.node.AbstractNodeMain;
00026 import org.ros.node.ConnectedNode;
00027
00028 import java.io.IOException;
00029 import java.util.concurrent.CountDownLatch;
00030 import java.util.concurrent.Executors;
00031 import java.util.concurrent.TimeUnit;
00032
00036 public class NtpTimeProviderTest extends RosTest {
00037
00038 @Test
00039 public void testNtpUbuntuCom() throws InterruptedException {
00040 final NtpTimeProvider ntpTimeProvider =
00041 new NtpTimeProvider(InetAddressFactory.newFromHostString("ntp.ubuntu.com"),
00042 Executors.newScheduledThreadPool(Integer.MAX_VALUE));
00043 final CountDownLatch latch = new CountDownLatch(1);
00044 nodeConfiguration.setTimeProvider(ntpTimeProvider);
00045 nodeMainExecutor.execute(new AbstractNodeMain() {
00046 @Override
00047 public GraphName getDefaultNodeName() {
00048 return GraphName.of("node");
00049 }
00050
00051 @Override
00052 public void onStart(ConnectedNode connectedNode) {
00053 try {
00054 ntpTimeProvider.updateTime();
00055 } catch (IOException e) {
00056
00057 }
00058 ntpTimeProvider.getCurrentTime();
00059 System.out.println("System time: " + System.currentTimeMillis());
00060 System.out.println("NTP time: " + ntpTimeProvider.getCurrentTime().totalNsecs() / 1000000);
00061 latch.countDown();
00062 }
00063 }, nodeConfiguration);
00064 assertTrue(latch.await(1, TimeUnit.SECONDS));
00065 }
00066 }