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.internal.transport.tcp;
00018
00019 import static org.junit.Assert.assertEquals;
00020 import static org.junit.Assert.assertTrue;
00021 import static org.junit.Assert.fail;
00022
00023 import org.junit.After;
00024 import org.junit.Before;
00025 import org.junit.Test;
00026 import org.ros.address.AdvertiseAddress;
00027 import org.ros.address.BindAddress;
00028 import org.ros.address.InetAddressFactory;
00029
00030 import java.net.InetSocketAddress;
00031 import java.net.UnknownHostException;
00032 import java.util.concurrent.Executors;
00033 import java.util.concurrent.ScheduledExecutorService;
00034
00039 public class TcpRosServerTest {
00040 private ScheduledExecutorService executorService;
00041
00042 @Before
00043 public void setup() {
00044 executorService = Executors.newScheduledThreadPool(10);
00045 }
00046
00047 @After
00048 public void tearDown() {
00049 executorService.shutdown();
00050 }
00051
00052 @Test
00053 public void testGetAddressFailsIfServerNotRunning() throws UnknownHostException {
00054 TcpRosServer tcpRosServer =
00055 new TcpRosServer(BindAddress.newPublic(), AdvertiseAddress.newPublic(), null, null,
00056 executorService);
00057
00058 try {
00059 tcpRosServer.getAddress();
00060 fail();
00061 } catch (RuntimeException e) {
00062
00063 }
00064
00065 tcpRosServer.start();
00066 InetSocketAddress address = tcpRosServer.getAddress();
00067 assertTrue(address.getPort() > 0);
00068 assertEquals(InetAddressFactory.newNonLoopback().getCanonicalHostName(), address.getAddress()
00069 .getHostName());
00070 tcpRosServer.shutdown();
00071
00072 try {
00073 tcpRosServer.getAddress();
00074 fail();
00075 } catch (RuntimeException e) {
00076
00077 }
00078 }
00079
00080 @Test
00081 public void testFailIfPortTaken() {
00082 TcpRosServer firstServer =
00083 new TcpRosServer(BindAddress.newPublic(), AdvertiseAddress.newPublic(), null, null,
00084 executorService);
00085 firstServer.start();
00086 try {
00087 TcpRosServer secondServer =
00088 new TcpRosServer(BindAddress.newPublic(firstServer.getAddress().getPort()),
00089 AdvertiseAddress.newPublic(), null, null, executorService);
00090 secondServer.start();
00091 fail();
00092 } catch (RuntimeException e) {
00093
00094 }
00095 firstServer.shutdown();
00096 }
00097
00098 @Test
00099 public void testFailIfStartedWhileRunning() {
00100 TcpRosServer tcpRosServer =
00101 new TcpRosServer(BindAddress.newPublic(), AdvertiseAddress.newPublic(), null, null,
00102 executorService);
00103 tcpRosServer.start();
00104 try {
00105 tcpRosServer.start();
00106 fail();
00107 } catch (RuntimeException e) {
00108
00109 }
00110 tcpRosServer.shutdown();
00111 }
00112 }