http://git-wip-us.apache.org/repos/asf/hadoop/blob/55b59b8e/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/launcher/testservices/ExceptionInExecuteLaunchableService.java ---------------------------------------------------------------------- diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/launcher/testservices/ExceptionInExecuteLaunchableService.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/launcher/testservices/ExceptionInExecuteLaunchableService.java new file mode 100644 index 0000000..405544f --- /dev/null +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/launcher/testservices/ExceptionInExecuteLaunchableService.java @@ -0,0 +1,96 @@ +/* + * 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.hadoop.service.launcher.testservices; + +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.service.launcher.AbstractLaunchableService; +import org.apache.hadoop.service.launcher.LauncherExitCodes; +import org.apache.hadoop.service.launcher.ServiceLaunchException; +import org.apache.hadoop.util.ExitCodeProvider; + +import java.io.IOException; +import java.util.List; + +/** + * Raise an exception in the execute() method; the exception type can + * be configured from the CLI. + */ +public class ExceptionInExecuteLaunchableService extends + AbstractLaunchableService { + + public static final String NAME = + "org.apache.hadoop.service.launcher.testservices.ExceptionInExecuteLaunchableService"; + public static final String ARG_THROW_SLE = "--SLE"; + public static final String ARG_THROW_IOE = "--IOE"; + public static final String ARG_THROWABLE = "--throwable"; + public static final String SLE_TEXT = "SLE raised in execute()"; + public static final String OTHER_EXCEPTION_TEXT = "Other exception"; + + public static final String EXIT_IN_IOE_TEXT = "Exit in IOE"; + public static final int IOE_EXIT_CODE = 64; + private ExType exceptionType = ExType.EX; + + public ExceptionInExecuteLaunchableService() { + super("ExceptionInExecuteLaunchedService"); + } + + @Override + public Configuration bindArgs(Configuration config, List<String> args) throws + Exception { + if (args.contains(ARG_THROW_SLE)) { + exceptionType = ExType.SLE; + } else if (args.contains(ARG_THROW_IOE)) { + exceptionType = ExType.IOE; + } else if (args.contains(ARG_THROWABLE)) { + exceptionType = ExType.THROWABLE; + } + return super.bindArgs(config, args); + } + + @Override + public int execute() throws Exception { + switch (exceptionType) { + case SLE: + throw new ServiceLaunchException(LauncherExitCodes.EXIT_OTHER_FAILURE, + SLE_TEXT); + case IOE: + throw new IOECodedException(); + case THROWABLE: + throw new OutOfMemoryError("OOM"); + case EX: + default: + throw new Exception(OTHER_EXCEPTION_TEXT); + } + } + + enum ExType {EX, SLE, IOE, THROWABLE} + + public static class IOECodedException extends IOException implements + ExitCodeProvider { + + public IOECodedException() { + super(EXIT_IN_IOE_TEXT); + } + + @Override + public int getExitCode() { + return IOE_EXIT_CODE; + } + } +}
http://git-wip-us.apache.org/repos/asf/hadoop/blob/55b59b8e/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/launcher/testservices/FailInConstructorService.java ---------------------------------------------------------------------- diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/launcher/testservices/FailInConstructorService.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/launcher/testservices/FailInConstructorService.java new file mode 100644 index 0000000..9fde1b3 --- /dev/null +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/launcher/testservices/FailInConstructorService.java @@ -0,0 +1,33 @@ +/* + * 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.hadoop.service.launcher.testservices; + +/** + * Service which fails in its constructor. + */ +public class FailInConstructorService extends FailureTestService { + + public static final String NAME = + "org.apache.hadoop.service.launcher.testservices.FailInConstructorService"; + + public FailInConstructorService() { + super(false, false, false, 0); + throw new NullPointerException("oops"); + } +} http://git-wip-us.apache.org/repos/asf/hadoop/blob/55b59b8e/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/launcher/testservices/FailInInitService.java ---------------------------------------------------------------------- diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/launcher/testservices/FailInInitService.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/launcher/testservices/FailInInitService.java new file mode 100644 index 0000000..e4f93c4 --- /dev/null +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/launcher/testservices/FailInInitService.java @@ -0,0 +1,38 @@ +/* + * 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.hadoop.service.launcher.testservices; + +/** + * Service which fails in its init() operation. + */ +public class FailInInitService extends FailureTestService { + public static final String NAME = + "org.apache.hadoop.service.launcher.testservices.FailInInitService"; + public static final int EXIT_CODE = -1; + + public FailInInitService() { + super(true, false, false, 0 + ); + } + + @Override + int getExitCode() { + return EXIT_CODE; + } +} http://git-wip-us.apache.org/repos/asf/hadoop/blob/55b59b8e/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/launcher/testservices/FailInStartService.java ---------------------------------------------------------------------- diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/launcher/testservices/FailInStartService.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/launcher/testservices/FailInStartService.java new file mode 100644 index 0000000..32049f2 --- /dev/null +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/launcher/testservices/FailInStartService.java @@ -0,0 +1,37 @@ +/* + * 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.hadoop.service.launcher.testservices; + +/** + * Service which fails in its start() operation. + */ +public class FailInStartService extends FailureTestService { + public static final String NAME = + "org.apache.hadoop.service.launcher.testservices.FailInStartService"; + public static final int EXIT_CODE = -2; + + public FailInStartService() { + super(false, true, false, 0); + } + + @Override + int getExitCode() { + return EXIT_CODE; + } +} http://git-wip-us.apache.org/repos/asf/hadoop/blob/55b59b8e/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/launcher/testservices/FailingStopInStartService.java ---------------------------------------------------------------------- diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/launcher/testservices/FailingStopInStartService.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/launcher/testservices/FailingStopInStartService.java new file mode 100644 index 0000000..6b8b07a --- /dev/null +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/launcher/testservices/FailingStopInStartService.java @@ -0,0 +1,47 @@ +/* + * 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.hadoop.service.launcher.testservices; + +/** + * This service stops during its start operation. + */ +public class FailingStopInStartService extends FailureTestService { + public static final String NAME = + "org.apache.hadoop.service.launcher.testservices.FailingStopInStartService"; + public static final int EXIT_CODE = -4; + + public FailingStopInStartService() { + super(false, false, true, 0); + } + + @Override + protected void serviceStart() throws Exception { + super.serviceStart(); + try { + stop(); + } catch (Exception e) { + //this is secretly swallowed + } + } + + @Override + int getExitCode() { + return EXIT_CODE; + } +} http://git-wip-us.apache.org/repos/asf/hadoop/blob/55b59b8e/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/launcher/testservices/FailureTestService.java ---------------------------------------------------------------------- diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/launcher/testservices/FailureTestService.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/launcher/testservices/FailureTestService.java new file mode 100644 index 0000000..8faf620 --- /dev/null +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/launcher/testservices/FailureTestService.java @@ -0,0 +1,55 @@ +/* + * 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.hadoop.service.launcher.testservices; + +import org.apache.hadoop.service.BreakableService; +import org.apache.hadoop.service.launcher.ServiceLaunchException; + +/** + * Launcher test service that does not take CLI arguments. + */ +public class FailureTestService extends BreakableService { + + private final int delay; + + public FailureTestService(boolean failOnInit, + boolean failOnStart, + boolean failOnStop, + int delay) { + super(failOnInit, failOnStart, failOnStop); + this.delay = delay; + } + + @Override + protected void serviceStop() throws Exception { + if (delay > 0) { + Thread.sleep(delay); + } + super.serviceStop(); + } + + @Override + protected Exception createFailureException(String action) { + return new ServiceLaunchException(getExitCode(), toString()); + } + + int getExitCode() { + return -1; + } +} http://git-wip-us.apache.org/repos/asf/hadoop/blob/55b59b8e/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/launcher/testservices/InitInConstructorLaunchableService.java ---------------------------------------------------------------------- diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/launcher/testservices/InitInConstructorLaunchableService.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/launcher/testservices/InitInConstructorLaunchableService.java new file mode 100644 index 0000000..541ac68 --- /dev/null +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/launcher/testservices/InitInConstructorLaunchableService.java @@ -0,0 +1,63 @@ +/* + * 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.hadoop.service.launcher.testservices; + +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.service.launcher.AbstractLaunchableService; +import org.junit.Assert; + +import java.util.List; + +/** + * Init in the constructor and make sure that it isn't inited again. + */ +public class InitInConstructorLaunchableService extends + AbstractLaunchableService { + + public static final String NAME = + "org.apache.hadoop.service.launcher.testservices.InitInConstructorLaunchableService"; + private final Configuration originalConf = new Configuration(); + + public InitInConstructorLaunchableService() { + super("InitInConstructorLaunchableService"); + init(originalConf); + } + + @Override + public void init(Configuration conf) { + Assert.assertEquals(STATE.NOTINITED, getServiceState()); + super.init(conf); + } + + @Override + public Configuration bindArgs(Configuration config, List<String> args) + throws Exception { + Assert.assertEquals(STATE.INITED, getServiceState()); + Assert.assertTrue(isInState(STATE.INITED)); + Assert.assertNotSame(getConfig(), config); + return null; + } + + @Override + public int execute() throws Exception { + Assert.assertEquals(STATE.STARTED, getServiceState()); + Assert.assertSame(originalConf, getConfig()); + return super.execute(); + } +} http://git-wip-us.apache.org/repos/asf/hadoop/blob/55b59b8e/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/launcher/testservices/LaunchableRunningService.java ---------------------------------------------------------------------- diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/launcher/testservices/LaunchableRunningService.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/launcher/testservices/LaunchableRunningService.java new file mode 100644 index 0000000..91d0f2e --- /dev/null +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/launcher/testservices/LaunchableRunningService.java @@ -0,0 +1,111 @@ +/* + * 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.hadoop.service.launcher.testservices; + +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.service.launcher.LaunchableService; +import org.apache.hadoop.service.launcher.LauncherExitCodes; +import org.junit.Assert; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.List; + +/** + * A service which implements {@link LaunchableService}. + * It + * <ol> + * <li>does nothing in its {@link #serviceStart()}</li> + * <li>does its sleep+ maybe fail operation in its {@link #execute()} + * method</li> + * <li>gets the failing flag from the argument {@link #ARG_FAILING} first, + * the config file second.</li> + * <li>returns 0 for a successful execute</li> + * <li>returns a configurable exit code for a failing execute</li> + * <li>generates a new configuration in {@link #bindArgs(Configuration, List)} + * to verify that these propagate.</li> + * </ol> + */ +public class LaunchableRunningService extends RunningService implements + LaunchableService { + public static final String NAME = + "org.apache.hadoop.service.launcher.testservices.LaunchableRunningService"; + public static final String ARG_FAILING = "--failing"; + public static final String EXIT_CODE_PROP = "exit.code"; + private static final Logger LOG = + LoggerFactory.getLogger(LaunchableRunningService.class); + private int exitCode = 0; + + public LaunchableRunningService() { + this("LaunchableRunningService"); + } + + public LaunchableRunningService(String name) { + super(name); + } + + @Override + public Configuration bindArgs(Configuration config, List<String> args) throws + Exception { + Assert.assertEquals(STATE.NOTINITED, getServiceState()); + for (String arg : args) { + LOG.info(arg); + } + Configuration newConf = new Configuration(config); + if (args.contains(ARG_FAILING)) { + LOG.info("CLI contains " + ARG_FAILING); + failInRun = true; + newConf.setInt(EXIT_CODE_PROP, LauncherExitCodes.EXIT_OTHER_FAILURE); + } + return newConf; + } + + @Override + protected void serviceInit(Configuration conf) throws Exception { + super.serviceInit(conf); + if (conf.getBoolean(FAIL_IN_RUN, false)) { + //if the conf value says fail, the exit code goes to it too + exitCode = LauncherExitCodes.EXIT_FAIL; + } + // the exit code can be read off the property + exitCode = conf.getInt(EXIT_CODE_PROP, exitCode); + } + + @Override + protected void serviceStart() throws Exception { + // no-op + } + + @Override + public int execute() throws Exception { + Thread.sleep(delayTime); + if (failInRun) { + return exitCode; + } + return 0; + } + + public int getExitCode() { + return exitCode; + } + + public void setExitCode(int exitCode) { + this.exitCode = exitCode; + } +} http://git-wip-us.apache.org/repos/asf/hadoop/blob/55b59b8e/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/launcher/testservices/NoArgsAllowedService.java ---------------------------------------------------------------------- diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/launcher/testservices/NoArgsAllowedService.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/launcher/testservices/NoArgsAllowedService.java new file mode 100644 index 0000000..602cb15 --- /dev/null +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/launcher/testservices/NoArgsAllowedService.java @@ -0,0 +1,64 @@ +/* + * 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.hadoop.service.launcher.testservices; + +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.service.launcher.AbstractLaunchableService; +import org.apache.hadoop.service.launcher.LauncherExitCodes; +import org.apache.hadoop.service.launcher.ServiceLaunchException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.List; +import java.util.Map; + +/** + * service that does not allow any arguments. + */ +public class NoArgsAllowedService extends AbstractLaunchableService { + + private static final Logger LOG = + LoggerFactory.getLogger(NoArgsAllowedService.class); + + public NoArgsAllowedService() { + super("NoArgsAllowedService"); + } + + public static final String NAME = + "org.apache.hadoop.service.launcher.testservices.NoArgsAllowedService"; + + @Override + public Configuration bindArgs(Configuration config, List<String> args) + throws Exception { + Configuration configuration = super.bindArgs(config, args); + if (!args.isEmpty()) { + StringBuilder argsList = new StringBuilder(); + for (String arg : args) { + argsList.append('"').append(arg).append("\" "); + } + LOG.error("Got {} arguments: {}", args.size(), argsList); + throw new ServiceLaunchException( + LauncherExitCodes.EXIT_COMMAND_ARGUMENT_ERROR, + "Expected 0 arguments but got %d: %s", + args.size(), + argsList); + } + return configuration; + } +} http://git-wip-us.apache.org/repos/asf/hadoop/blob/55b59b8e/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/launcher/testservices/NullBindLaunchableService.java ---------------------------------------------------------------------- diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/launcher/testservices/NullBindLaunchableService.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/launcher/testservices/NullBindLaunchableService.java new file mode 100644 index 0000000..6aeebf4 --- /dev/null +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/launcher/testservices/NullBindLaunchableService.java @@ -0,0 +1,46 @@ +/* + * 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.hadoop.service.launcher.testservices; + +import org.apache.hadoop.conf.Configuration; + +import java.util.List; + +/** + * An extension of {@link LaunchableRunningService} which returns null from + * the {@link #bindArgs(Configuration, List)} method. + */ +public class NullBindLaunchableService extends LaunchableRunningService { + public static final String NAME = + "org.apache.hadoop.service.launcher.testservices.NullBindLaunchableService"; + + public NullBindLaunchableService() { + this("NullBindLaunchableService"); + } + + public NullBindLaunchableService(String name) { + super(name); + } + + @Override + public Configuration bindArgs(Configuration config, List<String> args) + throws Exception { + return null; + } +} http://git-wip-us.apache.org/repos/asf/hadoop/blob/55b59b8e/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/launcher/testservices/RunningService.java ---------------------------------------------------------------------- diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/launcher/testservices/RunningService.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/launcher/testservices/RunningService.java new file mode 100644 index 0000000..3093aa1 --- /dev/null +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/launcher/testservices/RunningService.java @@ -0,0 +1,84 @@ +/* + * 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.hadoop.service.launcher.testservices; + +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.service.AbstractService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class RunningService extends AbstractService implements Runnable { + private static final Logger LOG = + LoggerFactory.getLogger(RunningService.class); + public static final String NAME = + "org.apache.hadoop.service.launcher.testservices.RunningService"; + public static final int DELAY = 100; + + /** + * Property on delay times. + */ + public static final String DELAY_TIME = "delay.time"; + public static final String FAIL_IN_RUN = "fail.runnable"; + public static final String FAILURE_MESSAGE = "FAIL_IN_RUN"; + private boolean interrupted; + + public int delayTime = DELAY; + public boolean failInRun; + + public RunningService() { + super("RunningService"); + } + + public RunningService(String name) { + super(name); + } + + @Override + protected void serviceInit(Configuration conf) throws Exception { + super.serviceInit(conf); + delayTime = getConfig().getInt(DELAY_TIME, delayTime); + failInRun = getConfig().getBoolean(FAIL_IN_RUN, failInRun); + } + + @Override + protected void serviceStart() throws Exception { + Thread thread = new Thread(this); + thread.setName(getName()); + thread.start(); + } + + @Override + public void run() { + try { + Thread.sleep(delayTime); + if (failInRun) { + noteFailure(new Exception(FAILURE_MESSAGE)); + } + } catch (InterruptedException e) { + interrupted = true; + LOG.info("Interrupted"); + } + stop(); + } + + public boolean isInterrupted() { + return interrupted; + } + +} http://git-wip-us.apache.org/repos/asf/hadoop/blob/55b59b8e/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/launcher/testservices/StoppingInStartLaunchableService.java ---------------------------------------------------------------------- diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/launcher/testservices/StoppingInStartLaunchableService.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/launcher/testservices/StoppingInStartLaunchableService.java new file mode 100644 index 0000000..49593b1 --- /dev/null +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/launcher/testservices/StoppingInStartLaunchableService.java @@ -0,0 +1,49 @@ +/* + * 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.hadoop.service.launcher.testservices; + +import org.apache.hadoop.service.launcher.AbstractLaunchableService; +import org.apache.hadoop.service.launcher.LauncherExitCodes; +import org.apache.hadoop.service.launcher.ServiceLaunchException; + +/** + * Try to stop() in service start; in execute() raise an exception. + */ +public class StoppingInStartLaunchableService + extends AbstractLaunchableService { + + public static final String NAME = + "org.apache.hadoop.service.launcher.testservices.StoppingInStartLaunchableService"; + public StoppingInStartLaunchableService(String name) { + super(name); + } + + @Override + protected void serviceStart() throws Exception { + super.serviceStart(); + stop(); + } + + @Override + public int execute() throws Exception { + throw new ServiceLaunchException( + LauncherExitCodes.EXIT_SERVICE_LIFECYCLE_EXCEPTION, + "Should not have been executed"); + } +} http://git-wip-us.apache.org/repos/asf/hadoop/blob/55b59b8e/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/launcher/testservices/StringConstructorOnlyService.java ---------------------------------------------------------------------- diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/launcher/testservices/StringConstructorOnlyService.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/launcher/testservices/StringConstructorOnlyService.java new file mode 100644 index 0000000..4f243c5 --- /dev/null +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/launcher/testservices/StringConstructorOnlyService.java @@ -0,0 +1,39 @@ +/* + * 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.hadoop.service.launcher.testservices; + +import org.apache.hadoop.service.launcher.AbstractLaunchableService; + +/** + * Service that only has one constructor that takes a string. + * This is the standard base class of a YARN service, so handle it + * in the launch + */ +public class StringConstructorOnlyService extends AbstractLaunchableService { + + + public StringConstructorOnlyService(String name) { + super(name); + } + + public static final String NAME = + "org.apache.hadoop.service.launcher.testservices.StringConstructorOnlyService"; + + +} http://git-wip-us.apache.org/repos/asf/hadoop/blob/55b59b8e/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/test/GenericTestUtils.java ---------------------------------------------------------------------- diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/test/GenericTestUtils.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/test/GenericTestUtils.java index 96ba123..7b94784 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/test/GenericTestUtils.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/test/GenericTestUtils.java @@ -31,6 +31,7 @@ import java.lang.management.ThreadInfo; import java.lang.management.ThreadMXBean; import java.lang.reflect.InvocationTargetException; import java.util.Arrays; +import java.util.Locale; import java.util.Random; import java.util.Set; import java.util.concurrent.CountDownLatch; @@ -692,4 +693,37 @@ public abstract class GenericTestUtils { bld.append(" + ").append(l).append("\n"); } } + + /** + * Formatted fail, via {@link String#format(String, Object...)}. + * @param format format string + * @param args argument list. If the last argument is a throwable, it + * is used as the inner cause of the exception + * @throws AssertionError with the formatted message + */ + public static void failf(String format, Object... args) { + String message = String.format(Locale.ENGLISH, format, args); + AssertionError error = new AssertionError(message); + int len = args.length; + if (len > 0 && args[len - 1] instanceof Throwable) { + error.initCause((Throwable) args[len - 1]); + } + throw error; + } + + /** + * Conditional formatted fail, via {@link String#format(String, Object...)}. + * @param condition condition: if true the method fails + * @param format format string + * @param args argument list. If the last argument is a throwable, it + * is used as the inner cause of the exception + * @throws AssertionError with the formatted message + */ + public static void failif(boolean condition, + String format, + Object... args) { + if (condition) { + failf(format, args); + } + } } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
