[ https://issues.apache.org/jira/browse/TWILL-240?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16119022#comment-16119022 ]
ASF GitHub Bot commented on TWILL-240: -------------------------------------- Github user chtyim commented on a diff in the pull request: https://github.com/apache/twill/pull/58#discussion_r132029465 --- Diff: twill-yarn/src/test/java/org/apache/twill/yarn/EventHandlerTest.java --- @@ -0,0 +1,351 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.twill.yarn; + +import com.google.common.base.Joiner; +import com.google.common.base.Stopwatch; +import com.google.common.base.Throwables; +import com.google.common.collect.ImmutableMap; +import org.apache.twill.api.AbstractTwillRunnable; +import org.apache.twill.api.EventHandler; +import org.apache.twill.api.EventHandlerContext; +import org.apache.twill.api.TwillApplication; +import org.apache.twill.api.TwillController; +import org.apache.twill.api.TwillSpecification; +import org.apache.twill.api.logging.PrinterLogHandler; +import org.junit.Assert; +import org.junit.ClassRule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.File; +import java.io.IOException; +import java.io.PrintWriter; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +/** + * Tests {@link EventHandler} methods + */ +public final class EventHandlerTest extends BaseYarnTest { + private static final Logger LOG = LoggerFactory.getLogger(EventHandlerTest.class); + + @ClassRule + public static final TemporaryFolder TMP_FOLDER = new TemporaryFolder(); + public static final String STARTED_FILE = "started_file"; + public static final String RUN_FILE = "run_file"; + public static final String CONTAINER_LAUNCHED_FOLDER = "launched_folder"; + public static final String CONTAINER_STOPPED_FOLDER = "stopped_folder"; + public static final String COMPLETED_FILE = "completed_file"; + public static final String KILLED_FILE = "killed_file"; + public static final String ABORTED_FILE = "aborted_file"; + + @Test + public void testComplete() throws InterruptedException, ExecutionException, TimeoutException, IOException { + // Create a parent folder to be written by EventHandler + File parentFolder = TMP_FOLDER.newFolder(); + parentFolder.setWritable(true, false); + TwillController controller = getTwillRunner().prepare(new CompleteApplication(parentFolder.getAbsolutePath())) + .addLogHandler(new PrinterLogHandler(new PrintWriter(System.out, true))) + .start(); + + // Wait for the app to complete within 120 seconds. + try { + controller.awaitTerminated(120, TimeUnit.SECONDS); + Set<String> handlerFiles = new HashSet<>(Arrays.asList(parentFolder.list())); + Assert.assertEquals(5, handlerFiles.size()); + // EventHandler#started() method should be called to create a file + Assert.assertTrue(handlerFiles.contains(STARTED_FILE)); + // CompleteRunnable#run() method should be called to create a file after EventHandler#started() method is called + Assert.assertTrue(handlerFiles.contains(RUN_FILE)); + // EventHandler#containerLaunched(String, int, String) method should be called to create a folder + Assert.assertTrue(handlerFiles.contains(CONTAINER_LAUNCHED_FOLDER)); + // EventHandler#containerStopped(String, int, String, int) method should be called to create a folder + Assert.assertTrue(handlerFiles.contains(CONTAINER_STOPPED_FOLDER)); + // Assert that containerLaunched and containerStopped are called for the same containers + // for the same number of times + String[] containerLaunchedFiles = new File(parentFolder.getAbsolutePath(), CONTAINER_LAUNCHED_FOLDER).list(); + String[] containerStoppedFiles = new File(parentFolder.getAbsolutePath(), CONTAINER_STOPPED_FOLDER).list(); + Assert.assertEquals(containerLaunchedFiles.length, containerStoppedFiles.length); + Assert.assertTrue(Arrays.asList(containerLaunchedFiles).containsAll(Arrays.asList(containerStoppedFiles))); + // EventHandler#completed() method should be called to create a file + Assert.assertTrue(handlerFiles.contains(COMPLETED_FILE)); + } catch (Exception e) { + // kill the app as cleanup + controller.kill(); --- End diff -- This is not needed. The base test class will handle it. > Improve EventHandler to handle more application lifecycle events > ---------------------------------------------------------------- > > Key: TWILL-240 > URL: https://issues.apache.org/jira/browse/TWILL-240 > Project: Apache Twill > Issue Type: New Feature > Reporter: Chengfeng Mao > Assignee: Chengfeng Mao > Fix For: 0.12.0 > > > Application Master should be able to run application specific code when > certain lifecycle events happen by calling methods from EventHandler. For > instance, when the app first starts, completes, aborts and etc. -- This message was sent by Atlassian JIRA (v6.4.14#64029)