Search in sources :

Example 1 with WeaveRunnerService

use of com.continuuity.weave.api.WeaveRunnerService in project weave by continuuity.

the class EchoServerTestRun method testEchoServer.

@Test
public void testEchoServer() throws InterruptedException, ExecutionException, IOException, URISyntaxException, TimeoutException {
    WeaveRunner runner = YarnTestSuite.getWeaveRunner();
    WeaveController controller = runner.prepare(new EchoServer(), ResourceSpecification.Builder.with().setVirtualCores(1).setMemory(1, ResourceSpecification.SizeUnit.GIGA).setInstances(2).build()).addLogHandler(new PrinterLogHandler(new PrintWriter(System.out, true))).withApplicationArguments("echo").withArguments("EchoServer", "echo2").start();
    final CountDownLatch running = new CountDownLatch(1);
    controller.addListener(new ServiceListenerAdapter() {

        @Override
        public void running() {
            running.countDown();
        }
    }, Threads.SAME_THREAD_EXECUTOR);
    Assert.assertTrue(running.await(30, TimeUnit.SECONDS));
    Iterable<Discoverable> echoServices = controller.discoverService("echo");
    Assert.assertTrue(YarnTestSuite.waitForSize(echoServices, 2, 60));
    for (Discoverable discoverable : echoServices) {
        String msg = "Hello: " + discoverable.getSocketAddress();
        Socket socket = new Socket(discoverable.getSocketAddress().getAddress(), discoverable.getSocketAddress().getPort());
        try {
            PrintWriter writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), Charsets.UTF_8), true);
            LineReader reader = new LineReader(new InputStreamReader(socket.getInputStream(), Charsets.UTF_8));
            writer.println(msg);
            Assert.assertEquals(msg, reader.readLine());
        } finally {
            socket.close();
        }
    }
    // Increase number of instances
    controller.changeInstances("EchoServer", 3);
    Assert.assertTrue(YarnTestSuite.waitForSize(echoServices, 3, 60));
    echoServices = controller.discoverService("echo2");
    // Decrease number of instances
    controller.changeInstances("EchoServer", 1);
    Assert.assertTrue(YarnTestSuite.waitForSize(echoServices, 1, 60));
    // Increase number of instances again
    controller.changeInstances("EchoServer", 2);
    Assert.assertTrue(YarnTestSuite.waitForSize(echoServices, 2, 60));
    // Make sure still only one app is running
    Iterable<WeaveRunner.LiveInfo> apps = runner.lookupLive();
    Assert.assertTrue(YarnTestSuite.waitForSize(apps, 1, 60));
    // Creates a new runner service to check it can regain control over running app.
    WeaveRunnerService runnerService = YarnTestSuite.createWeaveRunnerService();
    runnerService.startAndWait();
    try {
        Iterable<WeaveController> controllers = runnerService.lookup("EchoServer");
        Assert.assertTrue(YarnTestSuite.waitForSize(controllers, 1, 60));
        for (WeaveController c : controllers) {
            LOG.info("Stopping application: " + c.getRunId());
            c.stop().get(30, TimeUnit.SECONDS);
        }
        Assert.assertTrue(YarnTestSuite.waitForSize(apps, 0, 60));
    } finally {
        runnerService.stopAndWait();
    }
    // Sleep a bit before exiting.
    TimeUnit.SECONDS.sleep(2);
}
Also used : WeaveRunner(com.continuuity.weave.api.WeaveRunner) Discoverable(com.continuuity.weave.discovery.Discoverable) InputStreamReader(java.io.InputStreamReader) ServiceListenerAdapter(com.continuuity.weave.common.ServiceListenerAdapter) CountDownLatch(java.util.concurrent.CountDownLatch) LineReader(com.google.common.io.LineReader) PrinterLogHandler(com.continuuity.weave.api.logging.PrinterLogHandler) WeaveController(com.continuuity.weave.api.WeaveController) OutputStreamWriter(java.io.OutputStreamWriter) WeaveRunnerService(com.continuuity.weave.api.WeaveRunnerService) Socket(java.net.Socket) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Aggregations

WeaveController (com.continuuity.weave.api.WeaveController)1 WeaveRunner (com.continuuity.weave.api.WeaveRunner)1 WeaveRunnerService (com.continuuity.weave.api.WeaveRunnerService)1 PrinterLogHandler (com.continuuity.weave.api.logging.PrinterLogHandler)1 ServiceListenerAdapter (com.continuuity.weave.common.ServiceListenerAdapter)1 Discoverable (com.continuuity.weave.discovery.Discoverable)1 LineReader (com.google.common.io.LineReader)1 InputStreamReader (java.io.InputStreamReader)1 OutputStreamWriter (java.io.OutputStreamWriter)1 PrintWriter (java.io.PrintWriter)1 Socket (java.net.Socket)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Test (org.junit.Test)1