NtpTimeProviderTest.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2011 Google Inc.
00003  * 
00004  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
00005  * use this file except in compliance with the License. You may obtain a copy of
00006  * the License at
00007  * 
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
00013  * License for the specific language governing permissions and limitations under
00014  * the License.
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           // Ignored. This is only a sanity check.
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 }


rosjava_core
Author(s):
autogenerated on Wed Aug 26 2015 16:06:49