http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/a4c0e5fd/core/src/test/java/brooklyn/util/internal/ssh/sshj/SshjToolAsyncStubIntegrationTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/brooklyn/util/internal/ssh/sshj/SshjToolAsyncStubIntegrationTest.java b/core/src/test/java/brooklyn/util/internal/ssh/sshj/SshjToolAsyncStubIntegrationTest.java deleted file mode 100644 index df0330a..0000000 --- a/core/src/test/java/brooklyn/util/internal/ssh/sshj/SshjToolAsyncStubIntegrationTest.java +++ /dev/null @@ -1,177 +0,0 @@ -/* - * 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 brooklyn.util.internal.ssh.sshj; - -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertTrue; - -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.util.List; - -import org.apache.brooklyn.core.internal.BrooklynFeatureEnablement; -import org.testng.annotations.AfterMethod; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Test; - -import brooklyn.util.exceptions.Exceptions; -import brooklyn.util.internal.ssh.SshAbstractTool.SshAction; -import brooklyn.util.internal.ssh.sshj.SshjTool.ShellAction; -import brooklyn.util.time.Duration; - -import com.google.common.base.Function; -import com.google.common.base.Predicate; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.Lists; - -/** - * Tests for async-exec with {@link SshjTool}, where it stubs out the actual ssh commands - * to return a controlled sequence of responses. - */ -public class SshjToolAsyncStubIntegrationTest { - - static class InjectedResult { - Predicate<SshjTool.ShellAction> expected; - Function<SshjTool.ShellAction, Integer> result; - - InjectedResult(Predicate<SshjTool.ShellAction> expected, Function<SshjTool.ShellAction, Integer> result) { - this.expected = expected; - this.result = result; - } - } - - private SshjTool tool; - private List<InjectedResult> sequence; - int counter = 0; - private boolean origFeatureEnablement; - - @BeforeMethod(alwaysRun=true) - public void setUp() throws Exception { - origFeatureEnablement = BrooklynFeatureEnablement.enable(BrooklynFeatureEnablement.FEATURE_SSH_ASYNC_EXEC); - sequence = Lists.newArrayList(); - counter = 0; - - tool = new SshjTool(ImmutableMap.<String,Object>of("host", "localhost")) { - @SuppressWarnings("unchecked") - protected <T, C extends SshAction<T>> T acquire(C action, int sshTries, Duration sshTriesTimeout) { - if (action instanceof SshjTool.ShellAction) { - SshjTool.ShellAction shellAction = (SshjTool.ShellAction) action; - InjectedResult injectedResult = sequence.get(counter); - assertTrue(injectedResult.expected.apply(shellAction), "counter="+counter+"; cmds="+shellAction.commands); - counter++; - return (T) injectedResult.result.apply(shellAction); - } - return super.acquire(action, sshTries, sshTriesTimeout); - } - }; - } - - @AfterMethod(alwaysRun=true) - public void tearDown() throws Exception { - try { - if (tool != null) tool.disconnect(); - } finally { - BrooklynFeatureEnablement.setEnablement(BrooklynFeatureEnablement.FEATURE_SSH_ASYNC_EXEC, origFeatureEnablement); - } - } - - private Predicate<SshjTool.ShellAction> containsCmd(final String cmd) { - return new Predicate<SshjTool.ShellAction>() { - @Override public boolean apply(ShellAction input) { - return input != null && input.commands.toString().contains(cmd); - } - }; - } - - private Function<SshjTool.ShellAction, Integer> returning(final int result, final String stdout, final String stderr) { - return new Function<SshjTool.ShellAction, Integer>() { - @Override public Integer apply(ShellAction input) { - try { - if (stdout != null && input.out != null) input.out.write(stdout.getBytes()); - if (stderr != null && input.err != null) input.err.write(stderr.getBytes()); - } catch (IOException e) { - throw Exceptions.propagate(e); - } - return result; - } - }; - } - - @Test(groups="Integration") - public void testPolls() throws Exception { - sequence = ImmutableList.of( - new InjectedResult(containsCmd("nohup"), returning(0, "", "")), - new InjectedResult(containsCmd("# Long poll"), returning(0, "mystringToStdout", "mystringToStderr"))); - - runTest(0, "mystringToStdout", "mystringToStderr"); - assertEquals(counter, sequence.size()); - } - - @Test(groups="Integration") - public void testPollsAndReturnsNonZeroExitCode() throws Exception { - sequence = ImmutableList.of( - new InjectedResult(containsCmd("nohup"), returning(0, "", "")), - new InjectedResult(containsCmd("# Long poll"), returning(123, "mystringToStdout", "mystringToStderr")), - new InjectedResult(containsCmd("# Retrieve status"), returning(0, "123", ""))); - - runTest(123, "mystringToStdout", "mystringToStderr"); - assertEquals(counter, sequence.size()); - } - - @Test(groups="Integration") - public void testPollsRepeatedly() throws Exception { - sequence = ImmutableList.of( - new InjectedResult(containsCmd("nohup"), returning(0, "", "")), - new InjectedResult(containsCmd("# Long poll"), returning(125, "mystringToStdout", "mystringToStderr")), - new InjectedResult(containsCmd("# Retrieve status"), returning(0, "", "")), - new InjectedResult(containsCmd("# Long poll"), returning(125, "mystringToStdout2", "mystringToStderr2")), - new InjectedResult(containsCmd("# Retrieve status"), returning(0, "", "")), - new InjectedResult(containsCmd("# Long poll"), returning(-1, "mystringToStdout3", "mystringToStderr3")), - new InjectedResult(containsCmd("# Long poll"), returning(125, "mystringToStdout4", "mystringToStderr4")), - new InjectedResult(containsCmd("# Retrieve status"), returning(0, "", "")), - new InjectedResult(containsCmd("# Long poll"), returning(0, "mystringToStdout5", "mystringToStderr5"))); - - runTest(0, - "mystringToStdout"+"mystringToStdout2"+"mystringToStdout3"+"mystringToStdout4"+"mystringToStdout5", - "mystringToStderr"+"mystringToStderr2"+"mystringToStderr3"+"mystringToStderr4"+"mystringToStderr5"); - assertEquals(counter, sequence.size()); - } - - protected void runTest(int expectedExit, String expectedStdout, String expectedStderr) throws Exception { - List<String> cmds = ImmutableList.of("abc"); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - ByteArrayOutputStream err = new ByteArrayOutputStream(); - int exitCode = tool.execScript( - ImmutableMap.of( - "out", out, - "err", err, - SshjTool.PROP_EXEC_ASYNC.getName(), true, - SshjTool.PROP_NO_EXTRA_OUTPUT.getName(), true, - SshjTool.PROP_EXEC_ASYNC_POLLING_TIMEOUT.getName(), Duration.ONE_MILLISECOND), - cmds, - ImmutableMap.<String,String>of()); - String outStr = new String(out.toByteArray()); - String errStr = new String(err.toByteArray()); - - assertEquals(exitCode, expectedExit); - assertEquals(outStr.trim(), expectedStdout); - assertEquals(errStr.trim(), expectedStderr); - } -}
http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/a4c0e5fd/core/src/test/java/brooklyn/util/internal/ssh/sshj/SshjToolIntegrationTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/brooklyn/util/internal/ssh/sshj/SshjToolIntegrationTest.java b/core/src/test/java/brooklyn/util/internal/ssh/sshj/SshjToolIntegrationTest.java deleted file mode 100644 index f1e354c..0000000 --- a/core/src/test/java/brooklyn/util/internal/ssh/sshj/SshjToolIntegrationTest.java +++ /dev/null @@ -1,313 +0,0 @@ -/* - * 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 brooklyn.util.internal.ssh.sshj; - -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertFalse; -import static org.testng.Assert.assertNotNull; -import static org.testng.Assert.assertTrue; -import static org.testng.Assert.fail; - -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.util.Arrays; -import java.util.List; -import java.util.Map; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; -import java.util.concurrent.atomic.AtomicInteger; -import java.util.concurrent.atomic.AtomicReference; - -import net.schmizz.sshj.connection.channel.direct.Session; - -import org.apache.brooklyn.core.internal.BrooklynFeatureEnablement; -import org.testng.annotations.Test; - -import brooklyn.test.Asserts; -import brooklyn.util.exceptions.Exceptions; -import brooklyn.util.exceptions.RuntimeTimeoutException; -import brooklyn.util.internal.ssh.SshException; -import brooklyn.util.internal.ssh.SshTool; -import brooklyn.util.internal.ssh.SshToolAbstractIntegrationTest; -import brooklyn.util.os.Os; -import brooklyn.util.time.Duration; - -import com.google.common.base.Stopwatch; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; - -/** - * Test the operation of the {@link SshJschTool} utility class. - */ -public class SshjToolIntegrationTest extends SshToolAbstractIntegrationTest { - - @Override - protected SshTool newUnregisteredTool(Map<String,?> flags) { - return new SshjTool(flags); - } - - // TODO requires vt100 terminal emulation to work? - @Test(enabled = false, groups = {"Integration"}) - public void testExecShellWithCommandTakingStdin() throws Exception { - // Uses `tee` to redirect stdin to the given file; cntr-d (i.e. char 4) stops tee with exit code 0 - String content = "blah blah"; - String out = execShellDirectWithTerminalEmulation("tee "+remoteFilePath, content, ""+(char)4, "echo file contents: `cat "+remoteFilePath+"`"); - - assertTrue(out.contains("file contents: blah blah"), "out="+out); - } - - @Test(groups = {"Integration"}) - public void testGivesUpAfterMaxRetries() throws Exception { - final AtomicInteger callCount = new AtomicInteger(); - - final SshTool localtool = new SshjTool(ImmutableMap.of("sshTries", 3, "host", "localhost", "privateKeyFile", "~/.ssh/id_rsa")) { - protected SshAction<Session> newSessionAction() { - callCount.incrementAndGet(); - throw new RuntimeException("Simulating ssh execution failure"); - } - }; - - tools.add(localtool); - try { - localtool.execScript(ImmutableMap.<String,Object>of(), ImmutableList.of("true")); - fail(); - } catch (SshException e) { - if (!e.toString().contains("out of retries")) throw e; - assertEquals(callCount.get(), 3); - } - } - - @Test(groups = {"Integration"}) - public void testReturnsOnSuccessWhenRetrying() throws Exception { - final AtomicInteger callCount = new AtomicInteger(); - final int successOnAttempt = 2; - final SshTool localtool = new SshjTool(ImmutableMap.of("sshTries", 3, "host", "localhost", "privateKeyFile", "~/.ssh/id_rsa")) { - protected SshAction<Session> newSessionAction() { - callCount.incrementAndGet(); - if (callCount.incrementAndGet() >= successOnAttempt) { - return super.newSessionAction(); - } else { - throw new RuntimeException("Simulating ssh execution failure"); - } - } - }; - - tools.add(localtool); - localtool.execScript(ImmutableMap.<String,Object>of(), ImmutableList.of("true")); - assertEquals(callCount.get(), successOnAttempt); - } - - @Test(groups = {"Integration"}) - public void testGivesUpAfterMaxTime() throws Exception { - final AtomicInteger callCount = new AtomicInteger(); - final SshTool localtool = new SshjTool(ImmutableMap.of("sshTriesTimeout", 1000, "host", "localhost", "privateKeyFile", "~/.ssh/id_rsa")) { - protected SshAction<Session> newSessionAction() { - callCount.incrementAndGet(); - try { - Thread.sleep(600); - } catch (InterruptedException e) { - throw Exceptions.propagate(e); - } - throw new RuntimeException("Simulating ssh execution failure"); - } - }; - - tools.add(localtool); - try { - localtool.execScript(ImmutableMap.<String,Object>of(), ImmutableList.of("true")); - fail(); - } catch (RuntimeTimeoutException e) { - if (!e.toString().contains("out of time")) throw e; - assertEquals(callCount.get(), 2); - } - } - - @Test(groups = {"Integration"}) - public void testUsesCustomLocalTempDir() throws Exception { - class SshjToolForTest extends SshjTool { - public SshjToolForTest(Map<String, ?> map) { - super(map); - } - public File getLocalTempDir() { - return localTempDir; - } - }; - - final SshjToolForTest localtool = new SshjToolForTest(ImmutableMap.<String, Object>of("host", "localhost")); - assertNotNull(localtool.getLocalTempDir()); - assertEquals(localtool.getLocalTempDir(), new File(Os.tidyPath(SshjTool.PROP_LOCAL_TEMP_DIR.getDefaultValue()))); - - String customTempDir = Os.tmp(); - final SshjToolForTest localtool2 = new SshjToolForTest(ImmutableMap.of( - "host", "localhost", - SshjTool.PROP_LOCAL_TEMP_DIR.getName(), customTempDir)); - assertEquals(localtool2.getLocalTempDir(), new File(customTempDir)); - - String customRelativeTempDir = "~/tmp"; - final SshjToolForTest localtool3 = new SshjToolForTest(ImmutableMap.of( - "host", "localhost", - SshjTool.PROP_LOCAL_TEMP_DIR.getName(), customRelativeTempDir)); - assertEquals(localtool3.getLocalTempDir(), new File(Os.tidyPath(customRelativeTempDir))); - } - - @Test(groups = {"Integration"}) - public void testAsyncExecStdoutAndStderr() throws Exception { - boolean origFeatureEnablement = BrooklynFeatureEnablement.enable(BrooklynFeatureEnablement.FEATURE_SSH_ASYNC_EXEC); - try { - // Include a sleep, to ensure that the contents retrieved in first poll and subsequent polls are appended - List<String> cmds = ImmutableList.of( - "echo mystringToStdout", - "echo mystringToStderr 1>&2", - "sleep 5", - "echo mystringPostSleepToStdout", - "echo mystringPostSleepToStderr 1>&2"); - - ByteArrayOutputStream out = new ByteArrayOutputStream(); - ByteArrayOutputStream err = new ByteArrayOutputStream(); - int exitCode = tool.execScript( - ImmutableMap.of( - "out", out, - "err", err, - SshjTool.PROP_EXEC_ASYNC.getName(), true, - SshjTool.PROP_NO_EXTRA_OUTPUT.getName(), true, - SshjTool.PROP_EXEC_ASYNC_POLLING_TIMEOUT.getName(), Duration.ONE_SECOND), - cmds, - ImmutableMap.<String,String>of()); - String outStr = new String(out.toByteArray()); - String errStr = new String(err.toByteArray()); - - assertEquals(exitCode, 0); - assertEquals(outStr.trim(), "mystringToStdout\nmystringPostSleepToStdout"); - assertEquals(errStr.trim(), "mystringToStderr\nmystringPostSleepToStderr"); - } finally { - BrooklynFeatureEnablement.setEnablement(BrooklynFeatureEnablement.FEATURE_SSH_ASYNC_EXEC, origFeatureEnablement); - } - } - - @Test(groups = {"Integration"}) - public void testAsyncExecReturnsExitCode() throws Exception { - boolean origFeatureEnablement = BrooklynFeatureEnablement.enable(BrooklynFeatureEnablement.FEATURE_SSH_ASYNC_EXEC); - try { - int exitCode = tool.execScript( - ImmutableMap.of(SshjTool.PROP_EXEC_ASYNC.getName(), true), - ImmutableList.of("exit 123"), - ImmutableMap.<String,String>of()); - assertEquals(exitCode, 123); - } finally { - BrooklynFeatureEnablement.setEnablement(BrooklynFeatureEnablement.FEATURE_SSH_ASYNC_EXEC, origFeatureEnablement); - } - } - - @Test(groups = {"Integration"}) - public void testAsyncExecTimesOut() throws Exception { - Stopwatch stopwatch = Stopwatch.createStarted(); - boolean origFeatureEnablement = BrooklynFeatureEnablement.enable(BrooklynFeatureEnablement.FEATURE_SSH_ASYNC_EXEC); - try { - tool.execScript( - ImmutableMap.of(SshjTool.PROP_EXEC_ASYNC.getName(), true, SshjTool.PROP_EXEC_TIMEOUT.getName(), Duration.millis(1)), - ImmutableList.of("sleep 60"), - ImmutableMap.<String,String>of()); - fail(); - } catch (Exception e) { - TimeoutException te = Exceptions.getFirstThrowableOfType(e, TimeoutException.class); - if (te == null) throw e; - } finally { - BrooklynFeatureEnablement.setEnablement(BrooklynFeatureEnablement.FEATURE_SSH_ASYNC_EXEC, origFeatureEnablement); - } - - long seconds = stopwatch.elapsed(TimeUnit.SECONDS); - assertTrue(seconds < 30, "exec took "+seconds+" seconds"); - } - - @Test(groups = {"Integration"}) - public void testAsyncExecAbortsIfProcessFails() throws Exception { - final AtomicReference<Throwable> error = new AtomicReference<Throwable>(); - Thread thread = new Thread(new Runnable() { - @Override - public void run() { - try { - Stopwatch stopwatch = Stopwatch.createStarted(); - int exitStatus = tool.execScript( - ImmutableMap.of(SshjTool.PROP_EXEC_ASYNC.getName(), true, SshjTool.PROP_EXEC_TIMEOUT.getName(), Duration.millis(1)), - ImmutableList.of("sleep 63"), - ImmutableMap.<String,String>of()); - - assertEquals(exitStatus, 143 /* 128 + Signal number (SIGTERM) */); - - long seconds = stopwatch.elapsed(TimeUnit.SECONDS); - assertTrue(seconds < 30, "exec took "+seconds+" seconds"); - } catch (Throwable t) { - error.set(t); - } - }}); - - boolean origFeatureEnablement = BrooklynFeatureEnablement.enable(BrooklynFeatureEnablement.FEATURE_SSH_ASYNC_EXEC); - try { - thread.start(); - - Asserts.succeedsEventually(new Runnable() { - @Override - public void run() { - int exitStatus = tool.execCommands(ImmutableMap.<String,Object>of(), ImmutableList.of("ps aux| grep \"sleep 63\" | grep -v grep")); - assertEquals(exitStatus, 0); - }}); - - tool.execCommands(ImmutableMap.<String,Object>of(), ImmutableList.of("ps aux| grep \"sleep 63\" | grep -v grep | awk '{print($2)}' | xargs kill")); - - thread.join(30*1000); - assertFalse(thread.isAlive()); - if (error.get() != null) { - throw Exceptions.propagate(error.get()); - } - } finally { - thread.interrupt(); - BrooklynFeatureEnablement.setEnablement(BrooklynFeatureEnablement.FEATURE_SSH_ASYNC_EXEC, origFeatureEnablement); - } - } - - - protected String execShellDirect(List<String> cmds) { - return execShellDirect(cmds, ImmutableMap.<String,Object>of()); - } - - protected String execShellDirect(List<String> cmds, Map<String,?> env) { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - int exitcode = ((SshjTool)tool).execShellDirect(ImmutableMap.of("out", out), cmds, env); - String outstr = new String(out.toByteArray()); - assertEquals(exitcode, 0, outstr); - return outstr; - } - - private String execShellDirectWithTerminalEmulation(String... cmds) { - return execShellDirectWithTerminalEmulation(Arrays.asList(cmds)); - } - - private String execShellDirectWithTerminalEmulation(List<String> cmds) { - return execShellDirectWithTerminalEmulation(cmds, ImmutableMap.<String,Object>of()); - } - - private String execShellDirectWithTerminalEmulation(List<String> cmds, Map<String,?> env) { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - int exitcode = ((SshjTool)tool).execShellDirect(ImmutableMap.of("allocatePTY", true, "out", out), cmds, env); - String outstr = new String(out.toByteArray()); - assertEquals(exitcode, 0, outstr); - return outstr; - } - -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/a4c0e5fd/core/src/test/java/brooklyn/util/internal/ssh/sshj/SshjToolPerformanceTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/brooklyn/util/internal/ssh/sshj/SshjToolPerformanceTest.java b/core/src/test/java/brooklyn/util/internal/ssh/sshj/SshjToolPerformanceTest.java deleted file mode 100644 index 0c79bf1..0000000 --- a/core/src/test/java/brooklyn/util/internal/ssh/sshj/SshjToolPerformanceTest.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * 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 brooklyn.util.internal.ssh.sshj; - -import java.util.Map; - -import org.testng.annotations.Test; - -import brooklyn.util.internal.ssh.SshTool; -import brooklyn.util.internal.ssh.SshToolAbstractPerformanceTest; - -/** - * Test the performance of different variants of invoking the sshj tool. - * - * Intended for human-invocation and inspection, to see which parts are most expensive. - */ -public class SshjToolPerformanceTest extends SshToolAbstractPerformanceTest { - - @Override - protected SshTool newSshTool(Map<String,?> flags) { - return new SshjTool(flags); - } - - // Need to have at least one test method here (rather than just inherited) for eclipse to recognize it - @Test(enabled = false) - public void testDummy() throws Exception { - } -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/a4c0e5fd/core/src/test/java/brooklyn/util/mutex/WithMutexesTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/brooklyn/util/mutex/WithMutexesTest.java b/core/src/test/java/brooklyn/util/mutex/WithMutexesTest.java deleted file mode 100644 index cde25d3..0000000 --- a/core/src/test/java/brooklyn/util/mutex/WithMutexesTest.java +++ /dev/null @@ -1,126 +0,0 @@ -/* - * 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 brooklyn.util.mutex; - -import java.util.Arrays; -import java.util.Collections; -import java.util.Map; - -import org.testng.Assert; -import org.testng.annotations.Test; - -public class WithMutexesTest { - - @Test - public void testOneAcquisitionAndRelease() throws InterruptedException { - MutexSupport m = new MutexSupport(); - Map<String, SemaphoreWithOwners> sems; - SemaphoreWithOwners s; - try { - m.acquireMutex("foo", "something foo"); - sems = m.getAllSemaphores(); - Assert.assertEquals(sems.size(), 1); - s = sems.get("foo"); - Assert.assertEquals(s.getDescription(), "something foo"); - Assert.assertEquals(s.getOwningThreads(), Arrays.asList(Thread.currentThread())); - Assert.assertEquals(s.getRequestingThreads(), Collections.emptyList()); - Assert.assertTrue(s.isInUse()); - Assert.assertTrue(s.isCallingThreadAnOwner()); - } finally { - m.releaseMutex("foo"); - } - Assert.assertFalse(s.isInUse()); - Assert.assertFalse(s.isCallingThreadAnOwner()); - Assert.assertEquals(s.getDescription(), "something foo"); - Assert.assertEquals(s.getOwningThreads(), Collections.emptyList()); - Assert.assertEquals(s.getRequestingThreads(), Collections.emptyList()); - - sems = m.getAllSemaphores(); - Assert.assertEquals(sems, Collections.emptyMap()); - } - - @Test(groups = "Integration") //just because it takes a wee while - public void testBlockingAcquisition() throws InterruptedException { - final MutexSupport m = new MutexSupport(); - m.acquireMutex("foo", "something foo"); - - Assert.assertFalse(m.tryAcquireMutex("foo", "something else")); - - Thread t = new Thread() { - public void run() { - try { - m.acquireMutex("foo", "thread 2 foo"); - } catch (InterruptedException e) { - e.printStackTrace(); - } - m.releaseMutex("foo"); - } - }; - t.start(); - - t.join(500); - Assert.assertTrue(t.isAlive()); - Assert.assertEquals(m.getSemaphore("foo").getRequestingThreads(), Arrays.asList(t)); - - m.releaseMutex("foo"); - - t.join(1000); - Assert.assertFalse(t.isAlive()); - - Assert.assertEquals(m.getAllSemaphores(), Collections.emptyMap()); - } - - - public static class SampleWithMutexesDelegatingMixin implements WithMutexes { - - /* other behaviour would typically go here... */ - - WithMutexes mutexSupport = new MutexSupport(); - - @Override - public void acquireMutex(String mutexId, String description) throws InterruptedException { - mutexSupport.acquireMutex(mutexId, description); - } - - @Override - public boolean tryAcquireMutex(String mutexId, String description) { - return mutexSupport.tryAcquireMutex(mutexId, description); - } - - @Override - public void releaseMutex(String mutexId) { - mutexSupport.releaseMutex(mutexId); - } - - @Override - public boolean hasMutex(String mutexId) { - return mutexSupport.hasMutex(mutexId); - } - } - - @Test - public void testDelegatingMixinPattern() throws InterruptedException { - WithMutexes m = new SampleWithMutexesDelegatingMixin(); - m.acquireMutex("foo", "sample"); - Assert.assertTrue(m.hasMutex("foo")); - Assert.assertFalse(m.hasMutex("bar")); - m.releaseMutex("foo"); - Assert.assertFalse(m.hasMutex("foo")); - } -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/a4c0e5fd/core/src/test/java/brooklyn/util/osgi/OsgisTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/brooklyn/util/osgi/OsgisTest.java b/core/src/test/java/brooklyn/util/osgi/OsgisTest.java deleted file mode 100644 index 49f8017..0000000 --- a/core/src/test/java/brooklyn/util/osgi/OsgisTest.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * 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 brooklyn.util.osgi; - -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertFalse; - -import org.osgi.framework.Version; -import org.testng.annotations.Test; - -import brooklyn.util.osgi.Osgis.VersionedName; - -public class OsgisTest { - - @Test - public void testParseOsgiIdentifier() throws Exception { - assertEquals(Osgis.parseOsgiIdentifier("a.b").get(), new VersionedName("a.b", null)); - assertEquals(Osgis.parseOsgiIdentifier("a.b:0.1.2").get(), new VersionedName("a.b", Version.parseVersion("0.1.2"))); - assertEquals(Osgis.parseOsgiIdentifier("a.b:0.0.0.SNAPSHOT").get(), new VersionedName("a.b", Version.parseVersion("0.0.0.SNAPSHOT"))); - assertFalse(Osgis.parseOsgiIdentifier("a.b:0.notanumber.2").isPresent()); // invalid version - assertFalse(Osgis.parseOsgiIdentifier("a.b:0.1.2:3.4.5").isPresent()); // too many colons - assertFalse(Osgis.parseOsgiIdentifier("a.b:0.0.0_SNAPSHOT").isPresent()); // invalid version - assertFalse(Osgis.parseOsgiIdentifier("").isPresent()); - } -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/a4c0e5fd/core/src/test/java/brooklyn/util/ssh/BashCommandsIntegrationTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/brooklyn/util/ssh/BashCommandsIntegrationTest.java b/core/src/test/java/brooklyn/util/ssh/BashCommandsIntegrationTest.java deleted file mode 100644 index accac56..0000000 --- a/core/src/test/java/brooklyn/util/ssh/BashCommandsIntegrationTest.java +++ /dev/null @@ -1,501 +0,0 @@ -/* - * 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 brooklyn.util.ssh; - -import static brooklyn.util.ssh.BashCommands.sudo; -import static java.lang.String.format; -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertFalse; -import static org.testng.Assert.assertNotEquals; -import static org.testng.Assert.assertTrue; - -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.IOException; -import java.net.ServerSocket; -import java.util.Arrays; -import java.util.Collection; -import java.util.List; - -import org.apache.brooklyn.api.management.ManagementContext; -import org.apache.brooklyn.test.entity.LocalManagementContextForTests; -import org.apache.commons.io.FileUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.testng.Assert; -import org.testng.annotations.AfterMethod; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Test; - -import brooklyn.entity.basic.Entities; -import org.apache.brooklyn.location.basic.LocalhostMachineProvisioningLocation; -import org.apache.brooklyn.location.basic.SshMachineLocation; -import brooklyn.util.javalang.JavaClassNames; -import brooklyn.util.net.Networking; -import brooklyn.util.os.Os; -import brooklyn.util.task.BasicExecutionContext; -import brooklyn.util.task.ssh.SshTasks; -import brooklyn.util.task.system.ProcessTaskWrapper; -import brooklyn.util.text.Identifiers; -import brooklyn.util.text.Strings; -import brooklyn.util.time.Duration; - -import com.google.common.base.Charsets; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; -import com.google.common.io.Files; - -public class BashCommandsIntegrationTest { - - private static final Logger log = LoggerFactory.getLogger(BashCommandsIntegrationTest.class); - - private ManagementContext mgmt; - private BasicExecutionContext exec; - - private File destFile; - private File sourceNonExistantFile; - private File sourceFile1; - private File sourceFile2; - private String sourceNonExistantFileUrl; - private String sourceFileUrl1; - private String sourceFileUrl2; - private SshMachineLocation loc; - - private String localRepoFilename = "localrepofile.txt"; - private File localRepoBasePath; - private File localRepoEntityBasePath; - private String localRepoEntityVersionPath; - private File localRepoEntityFile; - - @BeforeMethod(alwaysRun=true) - public void setUp() throws Exception { - mgmt = new LocalManagementContextForTests(); - exec = new BasicExecutionContext(mgmt.getExecutionManager()); - - destFile = Os.newTempFile(getClass(), "commoncommands-test-dest.txt"); - - sourceNonExistantFile = new File("/this/does/not/exist/ERQBETJJIG1234"); - sourceNonExistantFileUrl = sourceNonExistantFile.toURI().toString(); - - sourceFile1 = Os.newTempFile(getClass(), "commoncommands-test.txt"); - sourceFileUrl1 = sourceFile1.toURI().toString(); - Files.write("mysource1".getBytes(), sourceFile1); - - sourceFile2 = Os.newTempFile(getClass(), "commoncommands-test2.txt"); - sourceFileUrl2 = sourceFile2.toURI().toString(); - Files.write("mysource2".getBytes(), sourceFile2); - - localRepoEntityVersionPath = JavaClassNames.simpleClassName(this)+"-test-dest-"+Identifiers.makeRandomId(8); - localRepoBasePath = new File(format("%s/.brooklyn/repository", System.getProperty("user.home"))); - localRepoEntityBasePath = new File(localRepoBasePath, localRepoEntityVersionPath); - localRepoEntityFile = new File(localRepoEntityBasePath, localRepoFilename); - localRepoEntityBasePath.mkdirs(); - Files.write("mylocal1".getBytes(), localRepoEntityFile); - - loc = mgmt.getLocationManager().createLocation(LocalhostMachineProvisioningLocation.spec()).obtain(); - } - - @AfterMethod(alwaysRun=true) - public void tearDown() throws Exception { - if (sourceFile1 != null) sourceFile1.delete(); - if (sourceFile2 != null) sourceFile2.delete(); - if (destFile != null) destFile.delete(); - if (localRepoEntityFile != null) localRepoEntityFile.delete(); - if (localRepoEntityBasePath != null) FileUtils.deleteDirectory(localRepoEntityBasePath); - if (loc != null) loc.close(); - if (mgmt != null) Entities.destroyAll(mgmt); - } - - @Test(groups="Integration") - public void testSudo() throws Exception { - ByteArrayOutputStream outStream = new ByteArrayOutputStream(); - ByteArrayOutputStream errStream = new ByteArrayOutputStream(); - String cmd = sudo("whoami"); - int exitcode = loc.execCommands(ImmutableMap.of("out", outStream, "err", errStream), "test", ImmutableList.of(cmd)); - String outstr = new String(outStream.toByteArray()); - String errstr = new String(errStream.toByteArray()); - - assertEquals(exitcode, 0, "out="+outstr+"; err="+errstr); - assertTrue(outstr.contains("root"), "out="+outstr+"; err="+errstr); - } - - public void testDownloadUrl() throws Exception { - List<String> cmds = BashCommands.commandsToDownloadUrlsAs( - ImmutableList.of(sourceFileUrl1), - destFile.getAbsolutePath()); - int exitcode = loc.execCommands("test", cmds); - - assertEquals(0, exitcode); - assertEquals(Files.readLines(destFile, Charsets.UTF_8), ImmutableList.of("mysource1")); - } - - @Test(groups="Integration") - public void testDownloadFirstSuccessfulFile() throws Exception { - List<String> cmds = BashCommands.commandsToDownloadUrlsAs( - ImmutableList.of(sourceNonExistantFileUrl, sourceFileUrl1, sourceFileUrl2), - destFile.getAbsolutePath()); - int exitcode = loc.execCommands("test", cmds); - - assertEquals(0, exitcode); - assertEquals(Files.readLines(destFile, Charsets.UTF_8), ImmutableList.of("mysource1")); - } - - @Test(groups="Integration") - public void testDownloadToStdout() throws Exception { - ProcessTaskWrapper<String> t = SshTasks.newSshExecTaskFactory(loc, - "cd "+destFile.getParentFile().getAbsolutePath(), - BashCommands.downloadToStdout(Arrays.asList(sourceFileUrl1))+" | sed s/my/your/") - .requiringZeroAndReturningStdout().newTask(); - - String result = exec.submit(t).get(); - assertTrue(result.trim().equals("yoursource1"), "Wrong contents of stdout download: "+result); - } - - @Test(groups="Integration") - public void testAlternativesWhereFirstSucceeds() throws Exception { - ProcessTaskWrapper<Integer> t = SshTasks.newSshExecTaskFactory(loc) - .add(BashCommands.alternatives(Arrays.asList("echo first", "exit 88"))) - .newTask(); - - Integer returnCode = exec.submit(t).get(); - String stdout = t.getStdout(); - String stderr = t.getStderr(); - log.info("alternatives for good first command gave: "+returnCode+"; err="+stderr+"; out="+stdout); - assertTrue(stdout.contains("first"), "errcode="+returnCode+"; stdout="+stdout+"; stderr="+stderr); - assertEquals(returnCode, (Integer)0); - } - - @Test(groups="Integration") - public void testAlternatives() throws Exception { - ProcessTaskWrapper<Integer> t = SshTasks.newSshExecTaskFactory(loc) - .add(BashCommands.alternatives(Arrays.asList("asdfj_no_such_command_1", "exit 88"))) - .newTask(); - - Integer returnCode = exec.submit(t).get(); - log.info("alternatives for bad commands gave: "+returnCode+"; err="+new String(t.getStderr())+"; out="+new String(t.getStdout())); - assertEquals(returnCode, (Integer)88); - } - - @Test(groups="Integration") - public void testRequireTestHandlesFailure() throws Exception { - ProcessTaskWrapper<?> t = SshTasks.newSshExecTaskFactory(loc) - .add(BashCommands.requireTest("-f "+sourceNonExistantFile.getPath(), - "The requested file does not exist")).newTask(); - - exec.submit(t).get(); - assertNotEquals(t.getExitCode(), (Integer)0); - assertTrue(t.getStderr().contains("The requested file"), "Expected message in: "+t.getStderr()); - assertTrue(t.getStdout().contains("The requested file"), "Expected message in: "+t.getStdout()); - } - - @Test(groups="Integration") - public void testRequireTestHandlesSuccess() throws Exception { - ProcessTaskWrapper<?> t = SshTasks.newSshExecTaskFactory(loc) - .add(BashCommands.requireTest("-f "+sourceFile1.getPath(), - "The requested file does not exist")).newTask(); - - exec.submit(t).get(); - assertEquals(t.getExitCode(), (Integer)0); - assertTrue(t.getStderr().equals(""), "Expected no stderr messages, but got: "+t.getStderr()); - } - - @Test(groups="Integration") - public void testRequireFileHandlesFailure() throws Exception { - ProcessTaskWrapper<?> t = SshTasks.newSshExecTaskFactory(loc) - .add(BashCommands.requireFile(sourceNonExistantFile.getPath())).newTask(); - - exec.submit(t).get(); - assertNotEquals(t.getExitCode(), (Integer)0); - assertTrue(t.getStderr().contains("required file"), "Expected message in: "+t.getStderr()); - assertTrue(t.getStderr().contains(sourceNonExistantFile.getPath()), "Expected message in: "+t.getStderr()); - assertTrue(t.getStdout().contains("required file"), "Expected message in: "+t.getStdout()); - assertTrue(t.getStdout().contains(sourceNonExistantFile.getPath()), "Expected message in: "+t.getStdout()); - } - - @Test(groups="Integration") - public void testRequireFileHandlesSuccess() throws Exception { - ProcessTaskWrapper<?> t = SshTasks.newSshExecTaskFactory(loc) - .add(BashCommands.requireFile(sourceFile1.getPath())).newTask(); - - exec.submit(t).get(); - assertEquals(t.getExitCode(), (Integer)0); - assertTrue(t.getStderr().equals(""), "Expected no stderr messages, but got: "+t.getStderr()); - } - - @Test(groups="Integration") - public void testRequireFailureExitsImmediately() throws Exception { - ProcessTaskWrapper<?> t = SshTasks.newSshExecTaskFactory(loc) - .add(BashCommands.requireTest("-f "+sourceNonExistantFile.getPath(), - "The requested file does not exist")) - .add("echo shouldnae come here").newTask(); - - exec.submit(t).get(); - assertNotEquals(t.getExitCode(), (Integer)0); - assertTrue(t.getStderr().contains("The requested file"), "Expected message in: "+t.getStderr()); - assertTrue(t.getStdout().contains("The requested file"), "Expected message in: "+t.getStdout()); - Assert.assertFalse(t.getStdout().contains("shouldnae"), "Expected message in: "+t.getStdout()); - } - - @Test(groups="Integration") - public void testPipeMultiline() throws Exception { - String output = execRequiringZeroAndReturningStdout(loc, - BashCommands.pipeTextTo("hello world\n"+"and goodbye\n", "wc")).get(); - - assertEquals(Strings.replaceAllRegex(output, "\\s+", " ").trim(), "3 4 25"); - } - - @Test(groups="Integration") - public void testWaitForFileContentsWhenAbortingOnFail() throws Exception { - String fileContent = "mycontents"; - String cmd = BashCommands.waitForFileContents(destFile.getAbsolutePath(), fileContent, Duration.ONE_SECOND, true); - - int exitcode = loc.execCommands("test", ImmutableList.of(cmd)); - assertEquals(exitcode, 1); - - Files.write(fileContent, destFile, Charsets.UTF_8); - int exitcode2 = loc.execCommands("test", ImmutableList.of(cmd)); - assertEquals(exitcode2, 0); - } - - @Test(groups="Integration") - public void testWaitForFileContentsWhenNotAbortingOnFail() throws Exception { - String fileContent = "mycontents"; - String cmd = BashCommands.waitForFileContents(destFile.getAbsolutePath(), fileContent, Duration.ONE_SECOND, false); - - String output = execRequiringZeroAndReturningStdout(loc, cmd).get(); - assertTrue(output.contains("Couldn't find"), "output="+output); - - Files.write(fileContent, destFile, Charsets.UTF_8); - String output2 = execRequiringZeroAndReturningStdout(loc, cmd).get(); - assertFalse(output2.contains("Couldn't find"), "output="+output2); - } - - @Test(groups="Integration") - public void testWaitForFileContentsWhenContentsAppearAfterStart() throws Exception { - String fileContent = "mycontents"; - - String cmd = BashCommands.waitForFileContents(destFile.getAbsolutePath(), fileContent, Duration.THIRTY_SECONDS, false); - ProcessTaskWrapper<String> t = execRequiringZeroAndReturningStdout(loc, cmd); - exec.submit(t); - - // sleep for long enough to ensure the ssh command is definitely executing - Thread.sleep(5*1000); - assertFalse(t.isDone()); - - Files.write(fileContent, destFile, Charsets.UTF_8); - String output = t.get(); - assertFalse(output.contains("Couldn't find"), "output="+output); - } - - @Test(groups="Integration", dependsOnMethods="testSudo") - public void testWaitForPortFreeWhenAbortingOnTimeout() throws Exception { - ServerSocket serverSocket = openServerSocket(); - try { - int port = serverSocket.getLocalPort(); - String cmd = BashCommands.waitForPortFree(port, Duration.ONE_SECOND, true); - - int exitcode = loc.execCommands("test", ImmutableList.of(cmd)); - assertEquals(exitcode, 1); - - serverSocket.close(); - assertTrue(Networking.isPortAvailable(port)); - int exitcode2 = loc.execCommands("test", ImmutableList.of(cmd)); - assertEquals(exitcode2, 0); - } finally { - serverSocket.close(); - } - } - - @Test(groups="Integration", dependsOnMethods="testSudo") - public void testWaitForPortFreeWhenNotAbortingOnTimeout() throws Exception { - ServerSocket serverSocket = openServerSocket(); - try { - int port = serverSocket.getLocalPort(); - String cmd = BashCommands.waitForPortFree(port, Duration.ONE_SECOND, false); - - String output = execRequiringZeroAndReturningStdout(loc, cmd).get(); - assertTrue(output.contains(port+" still in use"), "output="+output); - - serverSocket.close(); - assertTrue(Networking.isPortAvailable(port)); - String output2 = execRequiringZeroAndReturningStdout(loc, cmd).get(); - assertFalse(output2.contains("still in use"), "output="+output2); - } finally { - serverSocket.close(); - } - } - - @Test(groups="Integration", dependsOnMethods="testSudo") - public void testWaitForPortFreeWhenFreedAfterStart() throws Exception { - ServerSocket serverSocket = openServerSocket(); - try { - int port = serverSocket.getLocalPort(); - - String cmd = BashCommands.waitForPortFree(port, Duration.THIRTY_SECONDS, false); - ProcessTaskWrapper<String> t = execRequiringZeroAndReturningStdout(loc, cmd); - exec.submit(t); - - // sleep for long enough to ensure the ssh command is definitely executing - Thread.sleep(5*1000); - assertFalse(t.isDone()); - - serverSocket.close(); - assertTrue(Networking.isPortAvailable(port)); - String output = t.get(); - assertFalse(output.contains("still in use"), "output="+output); - } finally { - serverSocket.close(); - } - } - - - // Disabled by default because of risk of overriding /etc/hosts in really bad way if doesn't work properly! - // As a manual visual inspection test, consider first manually creating /etc/hostname and /etc/sysconfig/network - // so that it looks like debian+ubuntu / CentOS/RHEL. - @Test(groups={"Integration"}, enabled=false) - public void testSetHostnameUnqualified() throws Exception { - runSetHostname("br-"+Identifiers.makeRandomId(8).toLowerCase(), null, false); - } - - @Test(groups={"Integration"}, enabled=false) - public void testSetHostnameQualified() throws Exception { - runSetHostname("br-"+Identifiers.makeRandomId(8).toLowerCase()+".brooklyn.incubator.apache.org", null, false); - } - - @Test(groups={"Integration"}, enabled=false) - public void testSetHostnameNullDomain() throws Exception { - runSetHostname("br-"+Identifiers.makeRandomId(8).toLowerCase(), null, true); - } - - @Test(groups={"Integration"}, enabled=false) - public void testSetHostnameNonNullDomain() throws Exception { - runSetHostname("br-"+Identifiers.makeRandomId(8).toLowerCase(), "brooklyn.incubator.apache.org", true); - } - - protected void runSetHostname(String newHostname, String newDomain, boolean includeDomain) throws Exception { - String fqdn = (includeDomain && Strings.isNonBlank(newDomain)) ? newHostname + "." + newDomain : newHostname; - - LocalManagementContextForTests mgmt = new LocalManagementContextForTests(); - SshMachineLocation loc = mgmt.getLocationManager().createLocation(LocalhostMachineProvisioningLocation.spec()).obtain(); - - execRequiringZeroAndReturningStdout(loc, sudo("cp /etc/hosts /etc/hosts-orig-testSetHostname")).get(); - execRequiringZeroAndReturningStdout(loc, BashCommands.ifFileExistsElse0("/etc/hostname", sudo("cp /etc/hostname /etc/hostname-orig-testSetHostname"))).get(); - execRequiringZeroAndReturningStdout(loc, BashCommands.ifFileExistsElse0("/etc/sysconfig/network", sudo("cp /etc/sysconfig/network /etc/sysconfig/network-orig-testSetHostname"))).get(); - - String origHostname = getHostnameNoArgs(loc); - assertTrue(Strings.isNonBlank(origHostname)); - - try { - List<String> cmd = (includeDomain) ? BashCommands.setHostname(newHostname, newDomain) : BashCommands.setHostname(newHostname); - execRequiringZeroAndReturningStdout(loc, cmd).get(); - - String actualHostnameUnqualified = getHostnameUnqualified(loc); - String actualHostnameFullyQualified = getHostnameFullyQualified(loc); - - // TODO On OS X at least, we aren't actually setting the domain name; we're just letting - // the user pass in what the domain name is. We do add this properly to /etc/hosts - // (e.g. first line is "127.0.0.1 br-g4x5wgx8.brooklyn.incubator.apache.org br-g4x5wgx8 localhost") - // but subsequent calls to `hostname -f` returns the unqualified. Similarly, `domainname` - // returns blank. Therefore we can't assert that it equals our expected val (because we just made - // it up - "brooklyn.incubator.apache.org"). - // assertEquals(actualHostnameFullyQualified, fqdn); - assertEquals(actualHostnameUnqualified, Strings.getFragmentBetween(newHostname, null, ".")); - execRequiringZeroAndReturningStdout(loc, "ping -c1 -n -q "+actualHostnameUnqualified).get(); - execRequiringZeroAndReturningStdout(loc, "ping -c1 -n -q "+actualHostnameFullyQualified).get(); - - String result = execRequiringZeroAndReturningStdout(loc, "grep -n "+fqdn+" /etc/hosts").get(); - assertTrue(result.contains("localhost"), "line="+result); - log.info("result="+result); - - } finally { - execRequiringZeroAndReturningStdout(loc, sudo("cp /etc/hosts-orig-testSetHostname /etc/hosts")).get(); - execRequiringZeroAndReturningStdout(loc, BashCommands.ifFileExistsElse0("/etc/hostname-orig-testSetHostname", sudo("cp /etc/hostname-orig-testSetHostname /etc/hostname"))).get(); - execRequiringZeroAndReturningStdout(loc, BashCommands.ifFileExistsElse0("/etc/sysconfig/network-orig-testSetHostname", sudo("cp /etc/sysconfig/network-orig-testSetHostname /etc/sysconfig/network"))).get(); - execRequiringZeroAndReturningStdout(loc, sudo("hostname "+origHostname)).get(); - } - } - - // Marked disabled because not safe to run on your normal machine! It modifies /etc/hosts, which is dangerous if things go wrong! - @Test(groups={"Integration"}, enabled=false) - public void testModifyEtcHosts() throws Exception { - LocalManagementContextForTests mgmt = new LocalManagementContextForTests(); - SshMachineLocation loc = mgmt.getLocationManager().createLocation(LocalhostMachineProvisioningLocation.spec()).obtain(); - - execRequiringZeroAndReturningStdout(loc, sudo("cp /etc/hosts /etc/hosts-orig-testModifyEtcHosts")).get(); - int numLinesOrig = Integer.parseInt(execRequiringZeroAndReturningStdout(loc, "wc -l /etc/hosts").get().trim().split("\\s")[0]); - - try { - String cmd = BashCommands.prependToEtcHosts("1.2.3.4", "myhostnamefor1234.at.start", "myhostnamefor1234b"); - execRequiringZeroAndReturningStdout(loc, cmd).get(); - - String cmd2 = BashCommands.appendToEtcHosts("5.6.7.8", "myhostnamefor5678.at.end", "myhostnamefor5678"); - execRequiringZeroAndReturningStdout(loc, cmd2).get(); - - String grepFirst = execRequiringZeroAndReturningStdout(loc, "grep -n myhostnamefor1234 /etc/hosts").get(); - String grepLast = execRequiringZeroAndReturningStdout(loc, "grep -n myhostnamefor5678 /etc/hosts").get(); - int numLinesAfter = Integer.parseInt(execRequiringZeroAndReturningStdout(loc, "wc -l /etc/hosts").get().trim().split("\\s")[0]); - log.info("result: numLinesBefore="+numLinesOrig+"; numLinesAfter="+numLinesAfter+"; first="+grepFirst+"; last="+grepLast); - - assertTrue(grepFirst.startsWith("1:") && grepFirst.contains("1.2.3.4 myhostnamefor1234.at.start myhostnamefor1234"), "first="+grepFirst); - assertTrue(grepLast.startsWith((numLinesOrig+2)+":") && grepLast.contains("5.6.7.8 myhostnamefor5678.at.end myhostnamefor5678"), "last="+grepLast); - assertEquals(numLinesOrig + 2, numLinesAfter, "lines orig="+numLinesOrig+", after="+numLinesAfter); - } finally { - execRequiringZeroAndReturningStdout(loc, sudo("cp /etc/hosts-orig-testModifyEtcHosts /etc/hosts")).get(); - } - } - - private String getHostnameNoArgs(SshMachineLocation machine) { - String hostnameStdout = execRequiringZeroAndReturningStdout(machine, "echo FOREMARKER; hostname; echo AFTMARKER").get(); - return Strings.getFragmentBetween(hostnameStdout, "FOREMARKER", "AFTMARKER").trim(); - } - - private String getHostnameUnqualified(SshMachineLocation machine) { - String hostnameStdout = execRequiringZeroAndReturningStdout(machine, "echo FOREMARKER; hostname -s 2> /dev/null || hostname; echo AFTMARKER").get(); - return Strings.getFragmentBetween(hostnameStdout, "FOREMARKER", "AFTMARKER").trim(); - } - - private String getHostnameFullyQualified(SshMachineLocation machine) { - String hostnameStdout = execRequiringZeroAndReturningStdout(machine, "echo FOREMARKER; hostname --fqdn 2> /dev/null || hostname -f; echo AFTMARKER").get(); - return Strings.getFragmentBetween(hostnameStdout, "FOREMARKER", "AFTMARKER").trim(); - } - - private ProcessTaskWrapper<String> execRequiringZeroAndReturningStdout(SshMachineLocation loc, Collection<String> cmds) { - return execRequiringZeroAndReturningStdout(loc, cmds.toArray(new String[cmds.size()])); - } - - private ProcessTaskWrapper<String> execRequiringZeroAndReturningStdout(SshMachineLocation loc, String... cmds) { - ProcessTaskWrapper<String> t = SshTasks.newSshExecTaskFactory(loc, cmds) - .requiringZeroAndReturningStdout().newTask(); - exec.submit(t); - return t; - } - - private ServerSocket openServerSocket() { - int lowerBound = 40000; - int upperBound = 40100; - for (int i = lowerBound; i < upperBound; i++) { - try { - return new ServerSocket(i); - } catch (IOException e) { - // try next number - } - } - throw new IllegalStateException("No ports available in range "+lowerBound+" to "+upperBound); - } -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/a4c0e5fd/core/src/test/java/brooklyn/util/task/BasicTaskExecutionPerformanceTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/brooklyn/util/task/BasicTaskExecutionPerformanceTest.java b/core/src/test/java/brooklyn/util/task/BasicTaskExecutionPerformanceTest.java deleted file mode 100644 index 574c8c7..0000000 --- a/core/src/test/java/brooklyn/util/task/BasicTaskExecutionPerformanceTest.java +++ /dev/null @@ -1,206 +0,0 @@ -/* - * 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 brooklyn.util.task; - -import static org.testng.Assert.assertNull; -import static org.testng.Assert.assertTrue; - -import java.util.Collections; -import java.util.List; -import java.util.concurrent.Callable; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; - -import org.apache.brooklyn.api.management.Task; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.testng.annotations.AfterMethod; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Test; - -import brooklyn.util.collections.MutableMap; - -import com.google.common.base.Predicate; -import com.google.common.base.Stopwatch; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.Iterables; -import com.google.common.collect.Lists; -import com.google.common.util.concurrent.Callables; - -/** - * Test the operation of the {@link BasicTask} class. - * - * TODO clarify test purpose - */ -public class BasicTaskExecutionPerformanceTest { - private static final Logger log = LoggerFactory.getLogger(BasicTaskExecutionPerformanceTest.class); - - private static final int TIMEOUT_MS = 10*1000; - - private BasicExecutionManager em; - - public static final int MAX_OVERHEAD_MS = 1500; // was 750ms but saw 1.3s on buildhive - public static final int EARLY_RETURN_GRACE = 25; // saw 13ms early return on jenkins! - - @BeforeMethod(alwaysRun=true) - public void setUp() throws Exception { - em = new BasicExecutionManager("mycontext"); - } - - @AfterMethod(alwaysRun=true) - public void tearDown() throws Exception { - if (em != null) em.shutdownNow(); - } - - @SuppressWarnings("unchecked") - @Test - public void testScheduledTaskExecutedAfterDelay() throws Exception { - int delay = 100; - final CountDownLatch latch = new CountDownLatch(1); - - Callable<Task<?>> taskFactory = new Callable<Task<?>>() { - @Override public Task<?> call() { - return new BasicTask<Void>(new Runnable() { - @Override public void run() { - latch.countDown(); - }}); - }}; - ScheduledTask t = new ScheduledTask(taskFactory).delay(delay); - - Stopwatch stopwatch = Stopwatch.createStarted(); - em.submit(t); - - assertTrue(latch.await(TIMEOUT_MS, TimeUnit.MILLISECONDS)); - long actualDelay = stopwatch.elapsed(TimeUnit.MILLISECONDS); - - assertTrue(actualDelay > (delay-EARLY_RETURN_GRACE), "actualDelay="+actualDelay+"; delay="+delay); - assertTrue(actualDelay < (delay+MAX_OVERHEAD_MS), "actualDelay="+actualDelay+"; delay="+delay); - } - - @SuppressWarnings("unchecked") - @Test - public void testScheduledTaskExecutedAtRegularPeriod() throws Exception { - final int period = 100; - final int numTimestamps = 4; - final CountDownLatch latch = new CountDownLatch(1); - final List<Long> timestamps = Collections.synchronizedList(Lists.<Long>newArrayList()); - final Stopwatch stopwatch = Stopwatch.createStarted(); - - Callable<Task<?>> taskFactory = new Callable<Task<?>>() { - @Override public Task<?> call() { - return new BasicTask<Void>(new Runnable() { - @Override public void run() { - timestamps.add(stopwatch.elapsed(TimeUnit.MILLISECONDS)); - if (timestamps.size() >= numTimestamps) latch.countDown(); - }}); - }}; - ScheduledTask t = new ScheduledTask(taskFactory).delay(1).period(period); - em.submit(t); - - assertTrue(latch.await(TIMEOUT_MS, TimeUnit.MILLISECONDS)); - - synchronized (timestamps) { - long prev = timestamps.get(0); - for (long timestamp : timestamps.subList(1, timestamps.size())) { - assertTrue(timestamp > prev+period-EARLY_RETURN_GRACE, "timestamps="+timestamps); - assertTrue(timestamp < prev+period+MAX_OVERHEAD_MS, "timestamps="+timestamps); - prev = timestamp; - } - } - } - - @SuppressWarnings("unchecked") - @Test - public void testCanCancelScheduledTask() throws Exception { - final int period = 1; - final long checkPeriod = 250; - final List<Long> timestamps = Collections.synchronizedList(Lists.<Long>newArrayList()); - - Callable<Task<?>> taskFactory = new Callable<Task<?>>() { - @Override public Task<?> call() { - return new BasicTask<Void>(new Runnable() { - @Override public void run() { - timestamps.add(System.currentTimeMillis()); - }}); - }}; - ScheduledTask t = new ScheduledTask(taskFactory).period(period); - em.submit(t); - - t.cancel(); - long cancelTime = System.currentTimeMillis(); - int countImmediatelyAfterCancel = timestamps.size(); - Thread.sleep(checkPeriod); - int countWellAfterCancel = timestamps.size(); - - // should have at most 1 more execution after cancel - log.info("testCanCancelScheduledTask saw "+countImmediatelyAfterCancel+" then cancel then "+countWellAfterCancel+" total"); - assertTrue(countWellAfterCancel - countImmediatelyAfterCancel <= 2, "timestamps="+timestamps+"; cancelTime="+cancelTime); - } - - // Previously, when we used a CopyOnWriteArraySet, performance for submitting new tasks was - // terrible, and it degraded significantly as the number of previously executed tasks increased - // (e.g. 9s for first 1000; 26s for next 1000; 42s for next 1000). - @Test - public void testExecutionManagerPerformance() throws Exception { - // Was fixed at 1000 tasks, but was running out of virtual memory due to excessive thread creation - // on machines which were not able to execute the threads quickly. - final int NUM_TASKS = Math.min(500 * Runtime.getRuntime().availableProcessors(), 1000); - final int NUM_TIMES = 10; - final int MAX_ACCEPTABLE_TIME = 7500; // saw 5601ms on buildhive - - long tWarmup = execTasksAndWaitForDone(NUM_TASKS, ImmutableList.of("A")); - - List<Long> times = Lists.newArrayList(); - for (int i = 1; i <= NUM_TIMES; i++) { - times.add(execTasksAndWaitForDone(NUM_TASKS, ImmutableList.of("A"))); - } - - Long toobig = Iterables.find( - times, - new Predicate<Long>() { - public boolean apply(Long input) { - return input > MAX_ACCEPTABLE_TIME; - }}, - null); - assertNull(toobig, "warmup="+tWarmup+"; times="+times); - } - - @SuppressWarnings({ "unchecked", "rawtypes" }) - private long execTasksAndWaitForDone(int numTasks, List<?> tags) throws Exception { - List<Task<?>> tasks = Lists.newArrayList(); - long startTimestamp = System.currentTimeMillis(); - for (int i = 1; i < numTasks; i++) { - Task<?> t = new BasicTask(Callables.returning(null)); // no-op - em.submit(MutableMap.of("tags", tags), t); - tasks.add(t); - } - long submittedTimestamp = System.currentTimeMillis(); - - for (Task t : tasks) { - t.get(); - } - long endTimestamp = System.currentTimeMillis(); - long submitTime = submittedTimestamp - startTimestamp; - long totalTime = endTimestamp - startTimestamp; - - log.info("Executed {} tasks; {}ms total; {}ms to submit", new Object[] {numTasks, totalTime, submitTime}); - - return totalTime; - } -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/a4c0e5fd/core/src/test/java/brooklyn/util/task/BasicTaskExecutionTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/brooklyn/util/task/BasicTaskExecutionTest.java b/core/src/test/java/brooklyn/util/task/BasicTaskExecutionTest.java deleted file mode 100644 index 40660d4..0000000 --- a/core/src/test/java/brooklyn/util/task/BasicTaskExecutionTest.java +++ /dev/null @@ -1,460 +0,0 @@ -/* - * 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 brooklyn.util.task; - -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertFalse; -import static org.testng.Assert.assertNotNull; -import static org.testng.Assert.assertNull; -import static org.testng.Assert.assertTrue; -import static org.testng.Assert.fail; - -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; -import java.util.concurrent.Callable; -import java.util.concurrent.CancellationException; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; - -import org.apache.brooklyn.api.management.Task; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.testng.annotations.AfterMethod; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Test; - -import brooklyn.test.Asserts; -import brooklyn.util.collections.MutableMap; - -import com.google.common.base.Throwables; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableSet; -import com.google.common.collect.Lists; -import com.google.common.util.concurrent.Callables; - -/** - * Test the operation of the {@link BasicTask} class. - * - * TODO clarify test purpose - */ -public class BasicTaskExecutionTest { - private static final Logger log = LoggerFactory.getLogger(BasicTaskExecutionTest.class); - - private static final int TIMEOUT_MS = 10*1000; - - private BasicExecutionManager em; - private Map<Object, Object> data; - - @BeforeMethod(alwaysRun=true) - public void setUp() { - em = new BasicExecutionManager("mycontext"); - data = Collections.synchronizedMap(new HashMap<Object, Object>()); - data.clear(); - } - - @AfterMethod(alwaysRun=true) - public void tearDown() throws Exception { - if (em != null) em.shutdownNow(); - if (data != null) data.clear(); - } - - @Test - public void runSimpleBasicTask() throws Exception { - BasicTask<Object> t = new BasicTask<Object>(newPutCallable(1, "b")); - data.put(1, "a"); - Task<Object> t2 = em.submit(MutableMap.of("tag", "A"), t); - assertEquals("a", t.get()); - assertEquals("a", t2.get()); - assertEquals("b", data.get(1)); - } - - @Test - public void runSimpleRunnable() throws Exception { - data.put(1, "a"); - Task<?> t = em.submit(MutableMap.of("tag", "A"), newPutRunnable(1, "b")); - assertEquals(null, t.get()); - assertEquals("b", data.get(1)); - } - - @Test - public void runSimpleCallable() throws Exception { - data.put(1, "a"); - Task<?> t = em.submit(MutableMap.of("tag", "A"), newPutCallable(1, "b")); - assertEquals("a", t.get()); - assertEquals("b", data.get(1)); - } - - @Test - public void runBasicTaskWithWaits() throws Exception { - final CountDownLatch signalStarted = new CountDownLatch(1); - final CountDownLatch allowCompletion = new CountDownLatch(1); - final BasicTask<Object> t = new BasicTask<Object>(new Callable<Object>() { - public Object call() throws Exception { - Object result = data.put(1, "b"); - signalStarted.countDown(); - assertTrue(allowCompletion.await(TIMEOUT_MS, TimeUnit.MILLISECONDS)); - return result; - }}); - data.put(1, "a"); - - Task<?> t2 = em.submit(MutableMap.of("tag", "A"), t); - assertEquals(t, t2); - assertFalse(t.isDone()); - - assertTrue(signalStarted.await(TIMEOUT_MS, TimeUnit.MILLISECONDS)); - assertEquals("b", data.get(1)); - assertFalse(t.isDone()); - - log.debug("runBasicTaskWithWaits, BasicTask status: {}", t.getStatusDetail(false)); - - Asserts.succeedsEventually(new Runnable() { - public void run() { - String status = t.getStatusDetail(false); - assertTrue(status != null && status.toLowerCase().contains("waiting"), "status="+status); - }}); - - allowCompletion.countDown(); - assertEquals("a", t.get()); - } - - @Test - public void runMultipleBasicTasks() throws Exception { - data.put(1, 1); - BasicExecutionManager em = new BasicExecutionManager("mycontext"); - for (int i = 0; i < 2; i++) { - em.submit(MutableMap.of("tag", "A"), new BasicTask<Integer>(newIncrementCallable(1))); - em.submit(MutableMap.of("tag", "B"), new BasicTask<Integer>(newIncrementCallable((1)))); - } - int total = 0; - for (Object tag : em.getTaskTags()) { - log.debug("tag {}", tag); - for (Task<?> task : em.getTasksWithTag(tag)) { - log.debug("BasicTask {}, has {}", task, task.get()); - total += (Integer)task.get(); - } - } - assertEquals(10, total); - //now that all have completed: - assertEquals(5, data.get(1)); - } - - @Test - public void runMultipleBasicTasksMultipleTags() throws Exception { - data.put(1, 1); - Collection<Task<Integer>> tasks = Lists.newArrayList(); - tasks.add(em.submit(MutableMap.of("tag", "A"), new BasicTask<Integer>(newIncrementCallable(1)))); - tasks.add(em.submit(MutableMap.of("tags", ImmutableList.of("A","B")), new BasicTask<Integer>(newIncrementCallable(1)))); - tasks.add(em.submit(MutableMap.of("tags", ImmutableList.of("B","C")), new BasicTask<Integer>(newIncrementCallable(1)))); - tasks.add(em.submit(MutableMap.of("tags", ImmutableList.of("D")), new BasicTask<Integer>(newIncrementCallable(1)))); - int total = 0; - - for (Task<Integer> t : tasks) { - log.debug("BasicTask {}, has {}", t, t.get()); - total += t.get(); - } - assertEquals(10, total); - - //now that all have completed: - assertEquals(data.get(1), 5); - assertEquals(em.getTasksWithTag("A").size(), 2); - assertEquals(em.getTasksWithAnyTag(ImmutableList.of("A")).size(), 2); - assertEquals(em.getTasksWithAllTags(ImmutableList.of("A")).size(), 2); - - assertEquals(em.getTasksWithAnyTag(ImmutableList.of("A", "B")).size(), 3); - assertEquals(em.getTasksWithAllTags(ImmutableList.of("A", "B")).size(), 1); - assertEquals(em.getTasksWithAllTags(ImmutableList.of("B", "C")).size(), 1); - assertEquals(em.getTasksWithAnyTag(ImmutableList.of("A", "D")).size(), 3); - } - - @Test - public void testGetTaskById() throws Exception { - Task<?> t = new BasicTask<Void>(newNoop()); - em.submit(MutableMap.of("tag", "A"), t); - assertEquals(em.getTask(t.getId()), t); - } - - @Test - public void testRetrievingTasksWithTagsReturnsExpectedTask() throws Exception { - Task<?> t = new BasicTask<Void>(newNoop()); - em.submit(MutableMap.of("tag", "A"), t); - t.get(); - - assertEquals(em.getTasksWithTag("A"), ImmutableList.of(t)); - assertEquals(em.getTasksWithAnyTag(ImmutableList.of("A")), ImmutableList.of(t)); - assertEquals(em.getTasksWithAnyTag(ImmutableList.of("A", "B")), ImmutableList.of(t)); - assertEquals(em.getTasksWithAllTags(ImmutableList.of("A")), ImmutableList.of(t)); - } - - @Test - public void testRetrievingTasksWithTagsExcludesNonMatchingTasks() throws Exception { - Task<?> t = new BasicTask<Void>(newNoop()); - em.submit(MutableMap.of("tag", "A"), t); - t.get(); - - assertEquals(em.getTasksWithTag("B"), ImmutableSet.of()); - assertEquals(em.getTasksWithAnyTag(ImmutableList.of("B")), ImmutableSet.of()); - assertEquals(em.getTasksWithAllTags(ImmutableList.of("A", "B")), ImmutableSet.of()); - } - - @Test - public void testRetrievingTasksWithMultipleTags() throws Exception { - Task<?> t = new BasicTask<Void>(newNoop()); - em.submit(MutableMap.of("tags", ImmutableList.of("A", "B")), t); - t.get(); - - assertEquals(em.getTasksWithTag("A"), ImmutableList.of(t)); - assertEquals(em.getTasksWithTag("B"), ImmutableList.of(t)); - assertEquals(em.getTasksWithAnyTag(ImmutableList.of("A")), ImmutableList.of(t)); - assertEquals(em.getTasksWithAnyTag(ImmutableList.of("B")), ImmutableList.of(t)); - assertEquals(em.getTasksWithAnyTag(ImmutableList.of("A", "B")), ImmutableList.of(t)); - assertEquals(em.getTasksWithAllTags(ImmutableList.of("A", "B")), ImmutableList.of(t)); - assertEquals(em.getTasksWithAllTags(ImmutableList.of("A")), ImmutableList.of(t)); - assertEquals(em.getTasksWithAllTags(ImmutableList.of("B")), ImmutableList.of(t)); - } - - // ENGR-1796: if nothing matched first tag, then returned whatever matched second tag! - @Test - public void testRetrievingTasksWithAllTagsWhenFirstNotMatched() throws Exception { - Task<?> t = new BasicTask<Void>(newNoop()); - em.submit(MutableMap.of("tags", ImmutableList.of("A")), t); - t.get(); - - assertEquals(em.getTasksWithAllTags(ImmutableList.of("not_there","A")), ImmutableSet.of()); - } - - @Test - public void testRetrievedTasksIncludesTasksInProgress() throws Exception { - final CountDownLatch runningLatch = new CountDownLatch(1); - final CountDownLatch finishLatch = new CountDownLatch(1); - Task<Void> t = new BasicTask<Void>(new Callable<Void>() { - public Void call() throws Exception { - runningLatch.countDown(); - finishLatch.await(); - return null; - }}); - em.submit(MutableMap.of("tags", ImmutableList.of("A")), t); - - try { - runningLatch.await(TIMEOUT_MS, TimeUnit.MILLISECONDS); - - assertEquals(em.getTasksWithTag("A"), ImmutableList.of(t)); - } finally { - finishLatch.countDown(); - } - } - - @Test - public void cancelBeforeRun() throws Exception { - final CountDownLatch blockForever = new CountDownLatch(1); - - BasicTask<Integer> t = new BasicTask<Integer>(new Callable<Integer>() { - public Integer call() throws Exception { - blockForever.await(); return 42; - }}); - t.cancel(true); - assertTrue(t.isCancelled()); - assertTrue(t.isDone()); - assertTrue(t.isError()); - em.submit(MutableMap.of("tag", "A"), t); - try { - t.get(); - fail("get should have failed due to cancel"); - } catch (CancellationException e) { - // expected - } - assertTrue(t.isCancelled()); - assertTrue(t.isDone()); - assertTrue(t.isError()); - - log.debug("cancelBeforeRun status: {}", t.getStatusDetail(false)); - assertTrue(t.getStatusDetail(false).toLowerCase().contains("cancel")); - } - - @Test - public void cancelDuringRun() throws Exception { - final CountDownLatch signalStarted = new CountDownLatch(1); - final CountDownLatch blockForever = new CountDownLatch(1); - - BasicTask<Integer> t = new BasicTask<Integer>(new Callable<Integer>() { - public Integer call() throws Exception { - synchronized (data) { - signalStarted.countDown(); - blockForever.await(); - } - return 42; - }}); - em.submit(MutableMap.of("tag", "A"), t); - assertFalse(t.isCancelled()); - assertFalse(t.isDone()); - assertFalse(t.isError()); - - assertTrue(signalStarted.await(TIMEOUT_MS, TimeUnit.MILLISECONDS)); - t.cancel(true); - - assertTrue(t.isCancelled()); - assertTrue(t.isError()); - try { - t.get(); - fail("get should have failed due to cancel"); - } catch (CancellationException e) { - // expected - } - assertTrue(t.isCancelled()); - assertTrue(t.isDone()); - assertTrue(t.isError()); - } - - @Test - public void cancelAfterRun() throws Exception { - BasicTask<Integer> t = new BasicTask<Integer>(Callables.returning(42)); - em.submit(MutableMap.of("tag", "A"), t); - - assertEquals(t.get(), (Integer)42); - t.cancel(true); - assertFalse(t.isCancelled()); - assertFalse(t.isError()); - assertTrue(t.isDone()); - } - - @Test - public void errorDuringRun() throws Exception { - BasicTask<Void> t = new BasicTask<Void>(new Callable<Void>() { - public Void call() throws Exception { - throw new IllegalStateException("Simulating failure in errorDuringRun"); - }}); - - em.submit(MutableMap.of("tag", "A"), t); - - try { - t.get(); - fail("get should have failed due to error"); - } catch (Exception eo) { - Throwable e = Throwables.getRootCause(eo); - assertEquals("Simulating failure in errorDuringRun", e.getMessage()); - } - - assertFalse(t.isCancelled()); - assertTrue(t.isError()); - assertTrue(t.isDone()); - - log.debug("errorDuringRun status: {}", t.getStatusDetail(false)); - assertTrue(t.getStatusDetail(false).contains("Simulating failure in errorDuringRun"), "details="+t.getStatusDetail(false)); - } - - @Test - public void fieldsSetForSimpleBasicTask() throws Exception { - final CountDownLatch signalStarted = new CountDownLatch(1); - final CountDownLatch allowCompletion = new CountDownLatch(1); - - BasicTask<Integer> t = new BasicTask<Integer>(new Callable<Integer>() { - public Integer call() throws Exception { - signalStarted.countDown(); - allowCompletion.await(); - return 42; - }}); - assertEquals(null, t.getSubmittedByTask()); - assertEquals(-1, t.submitTimeUtc); - assertNull(t.getInternalFuture()); - - em.submit(MutableMap.of("tag", "A"), t); - assertTrue(signalStarted.await(TIMEOUT_MS, TimeUnit.MILLISECONDS)); - - assertTrue(t.submitTimeUtc > 0); - assertTrue(t.startTimeUtc >= t.submitTimeUtc); - assertNotNull(t.getInternalFuture()); - assertEquals(-1, t.endTimeUtc); - assertEquals(false, t.isCancelled()); - - allowCompletion.countDown(); - assertEquals(t.get(), (Integer)42); - assertTrue(t.endTimeUtc >= t.startTimeUtc); - - log.debug("BasicTask duration (millis): {}", (t.endTimeUtc - t.submitTimeUtc)); - } - - @Test - public void fieldsSetForBasicTaskSubmittedBasicTask() throws Exception { - //submitted BasicTask B is started by A, and waits for A to complete - BasicTask<Integer> t = new BasicTask<Integer>(MutableMap.of("displayName", "sample", "description", "some descr"), new Callable<Integer>() { - public Integer call() throws Exception { - em.submit(MutableMap.of("tag", "B"), new Callable<Integer>() { - public Integer call() throws Exception { - assertEquals(45, em.getTasksWithTag("A").iterator().next().get()); - return 46; - }}); - return 45; - }}); - em.submit(MutableMap.of("tag", "A"), t); - - t.blockUntilEnded(); - -// assertEquals(em.getAllTasks().size(), 2 - - BasicTask<?> tb = (BasicTask<?>) em.getTasksWithTag("B").iterator().next(); - assertEquals( 46, tb.get() ); - assertEquals( t, em.getTasksWithTag("A").iterator().next() ); - assertNull( t.getSubmittedByTask() ); - - BasicTask<?> submitter = (BasicTask<?>) tb.getSubmittedByTask(); - assertNotNull(submitter); - assertEquals("sample", submitter.displayName); - assertEquals("some descr", submitter.description); - assertEquals(t, submitter); - - assertTrue(submitter.submitTimeUtc <= tb.submitTimeUtc); - assertTrue(submitter.endTimeUtc <= tb.endTimeUtc); - - log.debug("BasicTask {} was submitted by {}", tb, submitter); - } - - private Callable<Object> newPutCallable(final Object key, final Object val) { - return new Callable<Object>() { - public Object call() { - return data.put(key, val); - } - }; - } - - private Callable<Integer> newIncrementCallable(final Object key) { - return new Callable<Integer>() { - public Integer call() { - synchronized (data) { - return (Integer) data.put(key, (Integer)data.get(key) + 1); - } - } - }; - } - - private Runnable newPutRunnable(final Object key, final Object val) { - return new Runnable() { - public void run() { - data.put(key, val); - } - }; - } - - private Runnable newNoop() { - return new Runnable() { - public void run() { - } - }; - } -}
