# Using Tango servers without DB for unit testing

Intended audience: advanced developers, Programming language: java

## Problem overview

One wants to test own server using full tango stack. This test must not be related to any environment and be performed together with other tests.

## Detailed cases

Useful for benchmarking. Testing without setting up the whole Tango infrastructure.

## Solution overview

It is possible to start Tango server without db: see Device server without database

## Java:

Below is how one can integrate it into JUnit framework

     1    public class ServerTest {
     2
     3        public static final String NO_DB_GIOP_PORT = "12345";//any random non-occupied port
     4        public static final String NO_DB_INSTANCE = "test_server";
     5        public static final String NO_DB_DEVICE_NAME = "test/junit/" +NO_DB_INSTANCE;
     6        //our server will run in a dedicated thread
     7        private final ExecutorService server = Executors.newSingleThreadExecutor(new ThreadFactory() {
     8            @Override
     9            public Thread newThread(Runnable r) {
    10                Thread thread = new Thread(r);
    11                thread.setName("My fancy Tango server");
    12                thread.setDaemon(true);
    13                return thread;
    14            }
    15        });
    16
    17        @Before
    18        public void before() throws InterruptedException {
    19            System.setProperty("OAPort", NO_DB_GIOP_PORT);
    20
    21            final CountDownLatch latch = new CountDownLatch(1);
    22            server.submit(new Runnable() {
    23                @Override
    24                public void run() {
    25                    ServerManager.getInstance().start(new String[]{NO_DB_INSTANCE, "-nodb", "-dlist", NO_DB_DEVICE_NAME}, TestServer.class);//TestServer - is our Tango server we want to test against
    26                    latch.countDown();
    27                }
    28            });
    29            //make sure server has been started before leaving method
    30            latch.await();
    31        }
    32
    33        @After
    34        public void after() {
    35            server.shutdownNow();
    36        }
    37
    38
    39        @Test
    40        public void testGetClientId() throws Exception {
    41            TangoProxy proxy = TangoProxies.newDeviceProxyWrapper("tango://localhost:" + NO_DB_GIOP_PORT + "/" + NO_DB_DEVICE_NAME + "#dbase=no");
    42
    43            assertSame(DeviceState.ON,proxy.readAttribute("State"));
    44        }
    45    }

It is also possible to use 3rd party Tango server to test against. Suppose our Tango server uses other Tango server to perform its tasks (Data aggregation, state monitoring etc). In this case 3rd party binary can be added to the project. This binary can be then launched from within JUnit test:

     1    //this test cases uses precompiled TangoTest stored in {PRJ_ROOT}/exec/tango/<win64|debian> folder
     2    public class TangoTestProxyWrapperTest {
     3        public static final String TANGO_DEV_NAME = "test/local/0";
     4        public static final int TANGO_PORT = 16547;
     5        public static final String TEST_TANGO = "tango://localhost:" + TANGO_PORT + "/" + TANGO_DEV_NAME + "#dbase=no";
     6        public static final String X64 = "x64";
     7        public static final String LINUX = "linux";
     8        public static final String WINDOWS_7 = "windows 7";
     9        public static final String AMD64 = "amd64";
    10
    11        private static Process PRC;
    12
    13        @BeforeClass
    14        public static void beforeClass() throws Exception {
    15            String crtDir = System.getProperty("user.dir");
    16            //TODO define executable according to current OS
    17            String os = System.getProperty("os.name");
    18            String arch = System.getProperty("os.arch");
    19            StringBuilder bld = new StringBuilder(crtDir);
    20            //TODO other platforms or rely on the environmet
    21            if (LINUX.equalsIgnoreCase(os) && AMD64.equals(arch))
    22                bld.append("/exec/tango/debian/").append("TangoTest");
    23            else if (WINDOWS_7.equalsIgnoreCase(os) && AMD64.equals(arch))
    24                bld.append("\\exec\\tango\\win64\\").append("TangoTest");
    25            else
    26                throw new RuntimeException(String.format("Unsupported platform: name=%s arch=%s", os, arch));
    27
    28            PRC = new ProcessBuilder(bld.toString(), "test", "-ORBendPoint", "giop:tcp::" + TANGO_PORT, "-nodb", "-dlist", TANGO_DEV_NAME)
    29                    .start();
    30
    31            //drain slave's out stream
    32            new Thread(new Runnable() {
    33                @Override
    34                public void run() {
    35                    char bite;
    36                    try {
    37                        while ((bite = (char) PRC.getInputStream().read()) > -1) {
    38                            System.out.print(bite);
    39                        }
    40                    } catch (IOException ignore) {
    41                    }
    42                }
    43            }).start();
    44
    45            //drains slave's err stream
    46            new Thread(new Runnable() {
    47                @Override
    48                public void run() {
    49                    char bite;
    50                    try {
    51                        while ((bite = (char) PRC.getErrorStream().read()) > -1) {
    52                            System.err.print(bite);
    53                        }
    54                    } catch (IOException ignore) {
    55                    }
    56                }
    57            }).start();
    58        }
    59
    60        @AfterClass
    61        public static void afterClass() throws Exception {
    62            PRC.destroy();
    63        }
    64
    65        //this test directly writes/reads  to/from TangoTest double_scalar_w
    66        @Test
    67        public void testWriteReadAttribute_Double() throws Exception {
    68            TangoProxy instance = TangoProxies.newDeviceProxyWrapper(TEST_TANGO);
    69
    70            instance.writeAttribute("double_scalar_w", 0.1984D);
    71
    72            double result = instance.<Double>readAttribute("double_scalar_w");
    73
    74            assertEquals(0.1984D, result);
    75        }
    76
    77        //in other test case one can create instance of his own server (see previous code snippet)
    78
    79    }

## CPP:

//TODO

## Python:

//TODO