This is an automated email from the ASF dual-hosted git repository. rombert pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-hc-it.git
commit 080f55cac26cf5d4b0a4bd6a17a341d407d78c35 Author: Bertrand Delacretaz <[email protected]> AuthorDate: Mon Jan 5 16:20:43 2015 +0000 SLING-3501 - HealthCheckExecutorSelectionTest added git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1649570 13f79535-47bb-0310-9956-ffa450edef68 --- .../it/core/HealthCheckExecutorSelectionTest.java | 121 +++++++++++++++++++++ src/test/java/org/apache/sling/hc/it/core/U.java | 6 +- 2 files changed, 125 insertions(+), 2 deletions(-) diff --git a/src/test/java/org/apache/sling/hc/it/core/HealthCheckExecutorSelectionTest.java b/src/test/java/org/apache/sling/hc/it/core/HealthCheckExecutorSelectionTest.java new file mode 100644 index 0000000..91054b3 --- /dev/null +++ b/src/test/java/org/apache/sling/hc/it/core/HealthCheckExecutorSelectionTest.java @@ -0,0 +1,121 @@ +/* + * 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 SF 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.sling.hc.it.core; + +import java.util.ArrayList; +import java.util.Dictionary; +import java.util.Hashtable; +import java.util.List; +import java.util.UUID; + +import javax.inject.Inject; + +import org.apache.sling.hc.api.HealthCheck; +import org.apache.sling.hc.api.Result; +import org.apache.sling.hc.api.execution.HealthCheckExecutor; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.ops4j.pax.exam.Configuration; +import org.ops4j.pax.exam.Option; +import org.ops4j.pax.exam.junit.PaxExam; +import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceRegistration; + +/** Test the HealthCheckExecutor selection mechanism */ +@RunWith(PaxExam.class) +public class HealthCheckExecutorSelectionTest { + + @Inject + private HealthCheckExecutor executor; + + @Inject + private BundleContext bundleContext; + + private static String idA; + private static String idB; + + @SuppressWarnings("rawtypes") + private List<ServiceRegistration> regs = new ArrayList<ServiceRegistration>(); + + @Configuration + public Option[] config() { + return U.config(); + } + + private void registerHC(final String id) { + final HealthCheck hc = new HealthCheck() { + @Override + public Result execute() { + return new Result(Result.Status.OK, "All good for " + id); + } + + }; + + final Dictionary<String, Object> props = new Hashtable<String, Object>(); + props.put(HealthCheck.NAME, "name_" + id); + props.put(HealthCheck.TAGS, id); + + regs.add(bundleContext.registerService(HealthCheck.class, hc, props)); + } + + @BeforeClass + public static void setId() { + idA = UUID.randomUUID().toString(); + idB = UUID.randomUUID().toString(); + } + + @Before + public void setup() { + U.expectHealthChecks(0, executor, idA); + U.expectHealthChecks(0, executor, idB); + + registerHC(idA); + registerHC(idB); + registerHC(idB); + registerHC(idB); + } + + @After + @SuppressWarnings("rawtypes") + public void cleanup() { + for(ServiceRegistration r : regs) { + r.unregister(); + } + regs.clear(); + + U.expectHealthChecks(0, executor, idA); + U.expectHealthChecks(0, executor, idB); + } + + @Test + public void testDefaultSelectionA(){ + U.expectHealthChecks(1, executor, idA); + } + + @Test + public void testDefaultSelectionB(){ + U.expectHealthChecks(3, executor, idB); + } + @Test + public void testDefaultSelectionAandB(){ + U.expectHealthChecks(0, executor, idA, idB); + } +} diff --git a/src/test/java/org/apache/sling/hc/it/core/U.java b/src/test/java/org/apache/sling/hc/it/core/U.java index 99dc4ab..91333d7 100644 --- a/src/test/java/org/apache/sling/hc/it/core/U.java +++ b/src/test/java/org/apache/sling/hc/it/core/U.java @@ -38,9 +38,11 @@ public class U { /** Wait until the specified number of health checks are seen by supplied executor */ static void expectHealthChecks(int howMany, HealthCheckExecutor executor, String ... tags) { final long timeout = System.currentTimeMillis() + 10000L; + int count = 0; while(System.currentTimeMillis() < timeout) { final List<HealthCheckExecutionResult> results = executor.execute(tags); - if(results.size() == howMany) { + count = results.size(); + if(count== howMany) { return; } try { @@ -49,7 +51,7 @@ public class U { throw new RuntimeException("Unexpected InterruptedException"); } } - fail("Did not get " + howMany + " health checks with tags " + Arrays.asList(tags) + " after " + timeout + " msec"); + fail("Did not get " + howMany + " health checks with tags " + Arrays.asList(tags) + " after " + timeout + " msec (last count=" + count + ")"); } static Option[] config() { -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
