Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 88F49200CBB for ; Tue, 4 Jul 2017 11:49:45 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 878B4160BEF; Tue, 4 Jul 2017 09:49:45 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id A748F160BE1 for ; Tue, 4 Jul 2017 11:49:44 +0200 (CEST) Received: (qmail 31826 invoked by uid 500); 4 Jul 2017 09:49:43 -0000 Mailing-List: contact common-commits-help@hadoop.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Delivered-To: mailing list common-commits@hadoop.apache.org Received: (qmail 31815 invoked by uid 99); 4 Jul 2017 09:49:43 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 04 Jul 2017 09:49:43 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id AB9E3DFB8A; Tue, 4 Jul 2017 09:49:43 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: stevel@apache.org To: common-commits@hadoop.apache.org Message-Id: X-Mailer: ASF-Git Admin Mailer Subject: hadoop git commit: HADOOP-14615. Add ServiceOperations.stopQuietly that accept slf4j logger API. Contributed by Wenxin He. Date: Tue, 4 Jul 2017 09:49:43 +0000 (UTC) archived-at: Tue, 04 Jul 2017 09:49:45 -0000 Repository: hadoop Updated Branches: refs/heads/trunk 186650d21 -> 1aaa7f1ea HADOOP-14615. Add ServiceOperations.stopQuietly that accept slf4j logger API. Contributed by Wenxin He. Project: http://git-wip-us.apache.org/repos/asf/hadoop/repo Commit: http://git-wip-us.apache.org/repos/asf/hadoop/commit/1aaa7f1e Tree: http://git-wip-us.apache.org/repos/asf/hadoop/tree/1aaa7f1e Diff: http://git-wip-us.apache.org/repos/asf/hadoop/diff/1aaa7f1e Branch: refs/heads/trunk Commit: 1aaa7f1eacab20d1c27f410333a536033cad1aab Parents: 186650d Author: Steve Loughran Authored: Tue Jul 4 10:48:02 2017 +0100 Committer: Steve Loughran Committed: Tue Jul 4 10:48:02 2017 +0100 ---------------------------------------------------------------------- .../hadoop/service/ServiceOperations.java | 20 ++++++ .../hadoop/service/TestServiceOperations.java | 65 ++++++++++++++++++++ 2 files changed, 85 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/hadoop/blob/1aaa7f1e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/service/ServiceOperations.java ---------------------------------------------------------------------- diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/service/ServiceOperations.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/service/ServiceOperations.java index 6c03e25..a0a77ce 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/service/ServiceOperations.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/service/ServiceOperations.java @@ -25,6 +25,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.hadoop.classification.InterfaceAudience.Public; import org.apache.hadoop.classification.InterfaceStability.Evolving; +import org.slf4j.Logger; /** * This class contains a set of methods to work with services, especially @@ -87,6 +88,25 @@ public final class ServiceOperations { return null; } + /** + * Stop a service; if it is null do nothing. Exceptions are caught and + * logged at warn level. (but not Throwables). This operation is intended to + * be used in cleanup operations + * + * @param log the log to warn at + * @param service a service; may be null + * @return any exception that was caught; null if none was. + * @see ServiceOperations#stopQuietly(Service) + */ + public static Exception stopQuietly(Logger log, Service service) { + try { + stop(service); + } catch (Exception e) { + log.warn("When stopping the service {} : {}", service.getName(), e, e); + return e; + } + return null; + } /** * Class to manage a list of {@link ServiceStateChangeListener} instances, http://git-wip-us.apache.org/repos/asf/hadoop/blob/1aaa7f1e/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/TestServiceOperations.java ---------------------------------------------------------------------- diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/TestServiceOperations.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/TestServiceOperations.java new file mode 100644 index 0000000..5df973d --- /dev/null +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/TestServiceOperations.java @@ -0,0 +1,65 @@ +/* + * 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; + +import org.apache.hadoop.test.GenericTestUtils.LogCapturer; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.runners.MockitoJUnitRunner; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.PrintWriter; + +import static org.apache.hadoop.test.GenericTestUtils.LogCapturer.captureLogs; +import static org.hamcrest.CoreMatchers.containsString; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +/** + * Test miscellaneous service operations through mocked failures. + */ +@RunWith(MockitoJUnitRunner.class) +public class TestServiceOperations { + + @Mock + private Service service; + + @Mock + private RuntimeException e; + + @Test + public void testStopQuietlyWhenServiceStopThrowsException() throws Exception { + Logger logger = LoggerFactory.getLogger(TestServiceOperations.class); + LogCapturer logCapturer = captureLogs(logger); + doThrow(e).when(service).stop(); + + ServiceOperations.stopQuietly(logger, service); + + assertThat(logCapturer.getOutput(), + containsString("When stopping the service " + service.getName() + + " : " + e)); + verify(e, times(1)).printStackTrace(Mockito.any(PrintWriter.class)); + } + +} \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: common-commits-unsubscribe@hadoop.apache.org For additional commands, e-mail: common-commits-help@hadoop.apache.org