use of com.continuuity.weave.common.ServiceListenerAdapter in project weave by continuuity.
the class ControllerTest method testController.
@Test
public void testController() throws ExecutionException, InterruptedException, TimeoutException {
InMemoryZKServer zkServer = InMemoryZKServer.builder().build();
zkServer.startAndWait();
LOG.info("ZKServer: " + zkServer.getConnectionStr());
try {
RunId runId = RunIds.generate();
ZKClientService zkClientService = ZKClientService.Builder.of(zkServer.getConnectionStr()).build();
zkClientService.startAndWait();
Service service = createService(zkClientService, runId);
service.startAndWait();
WeaveController controller = getController(zkClientService, runId);
controller.sendCommand(Command.Builder.of("test").build()).get(2, TimeUnit.SECONDS);
controller.stop().get(2, TimeUnit.SECONDS);
Assert.assertEquals(ServiceController.State.TERMINATED, controller.state());
final CountDownLatch terminateLatch = new CountDownLatch(1);
service.addListener(new ServiceListenerAdapter() {
@Override
public void terminated(Service.State from) {
terminateLatch.countDown();
}
}, Threads.SAME_THREAD_EXECUTOR);
Assert.assertTrue(service.state() == Service.State.TERMINATED || terminateLatch.await(2, TimeUnit.SECONDS));
zkClientService.stopAndWait();
} finally {
zkServer.stopAndWait();
}
}
use of com.continuuity.weave.common.ServiceListenerAdapter in project weave by continuuity.
the class ControllerTest method testControllerBefore.
// Test controller created before service starts.
@Test
public void testControllerBefore() throws InterruptedException {
InMemoryZKServer zkServer = InMemoryZKServer.builder().build();
zkServer.startAndWait();
LOG.info("ZKServer: " + zkServer.getConnectionStr());
try {
RunId runId = RunIds.generate();
ZKClientService zkClientService = ZKClientService.Builder.of(zkServer.getConnectionStr()).build();
zkClientService.startAndWait();
final CountDownLatch runLatch = new CountDownLatch(1);
final CountDownLatch stopLatch = new CountDownLatch(1);
WeaveController controller = getController(zkClientService, runId);
controller.addListener(new ServiceListenerAdapter() {
@Override
public void running() {
runLatch.countDown();
}
@Override
public void terminated(Service.State from) {
stopLatch.countDown();
}
}, Threads.SAME_THREAD_EXECUTOR);
Service service = createService(zkClientService, runId);
service.start();
Assert.assertTrue(runLatch.await(2, TimeUnit.SECONDS));
Assert.assertFalse(stopLatch.await(2, TimeUnit.SECONDS));
service.stop();
Assert.assertTrue(stopLatch.await(2, TimeUnit.SECONDS));
} finally {
zkServer.stopAndWait();
}
}
use of com.continuuity.weave.common.ServiceListenerAdapter in project weave by continuuity.
the class ControllerTest method testControllerListener.
// Test controller listener receive first state change without state transition from service
@Test
public void testControllerListener() throws InterruptedException {
InMemoryZKServer zkServer = InMemoryZKServer.builder().build();
zkServer.startAndWait();
LOG.info("ZKServer: " + zkServer.getConnectionStr());
try {
RunId runId = RunIds.generate();
ZKClientService zkClientService = ZKClientService.Builder.of(zkServer.getConnectionStr()).build();
zkClientService.startAndWait();
Service service = createService(zkClientService, runId);
service.startAndWait();
final CountDownLatch runLatch = new CountDownLatch(1);
WeaveController controller = getController(zkClientService, runId);
controller.addListener(new ServiceListenerAdapter() {
@Override
public void running() {
runLatch.countDown();
}
}, Threads.SAME_THREAD_EXECUTOR);
Assert.assertTrue(runLatch.await(2, TimeUnit.SECONDS));
service.stopAndWait();
zkClientService.stopAndWait();
} finally {
zkServer.stopAndWait();
}
}
use of com.continuuity.weave.common.ServiceListenerAdapter in project weave by continuuity.
the class DistributeShellTestRun method testDistributedShell.
@Ignore
@Test
public void testDistributedShell() throws InterruptedException {
WeaveRunner weaveRunner = YarnTestSuite.getWeaveRunner();
WeaveController controller = weaveRunner.prepare(new DistributedShell("pwd", "ls -al")).addLogHandler(new PrinterLogHandler(new PrintWriter(System.out))).start();
final CountDownLatch stopLatch = new CountDownLatch(1);
controller.addListener(new ServiceListenerAdapter() {
@Override
public void terminated(Service.State from) {
stopLatch.countDown();
}
@Override
public void failed(Service.State from, Throwable failure) {
stopLatch.countDown();
}
}, Threads.SAME_THREAD_EXECUTOR);
Assert.assertTrue(stopLatch.await(10, TimeUnit.SECONDS));
}
use of com.continuuity.weave.common.ServiceListenerAdapter 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);
}
Aggregations