private static final Logger LOG = LoggerFactory.getLogger(EchoServerTestRun.class);
@Test
public void testEchoServer() throws InterruptedException, ExecutionException, IOException,
URISyntaxException, TimeoutException {
TwillRunner runner = YarnTestUtils.getTwillRunner();
TwillController 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(YarnTestUtils.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(YarnTestUtils.waitForSize(echoServices, 3, 60));
echoServices = controller.discoverService("echo2");
// Decrease number of instances
controller.changeInstances("EchoServer", 1);
Assert.assertTrue(YarnTestUtils.waitForSize(echoServices, 1, 60));
// Increase number of instances again
controller.changeInstances("EchoServer", 2);
Assert.assertTrue(YarnTestUtils.waitForSize(echoServices, 2, 60));
// Make sure still only one app is running
Iterable<TwillRunner.LiveInfo> apps = runner.lookupLive();
Assert.assertTrue(YarnTestUtils.waitForSize(apps, 1, 60));
// Creates a new runner service to check it can regain control over running app.
TwillRunnerService runnerService = YarnTestUtils.createTwillRunnerService();
runnerService.startAndWait();