From 674b81649732099ac8e0e2d4cdc565837144f5fc Mon Sep 17 00:00:00 2001 From: Rupert Smith Date: Fri, 11 Jan 2008 16:02:09 +0000 Subject: QPID-734. More TTL tests. git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/branches/M2.1@611212 13f79535-47bb-0310-9956-ffa450edef68 --- .../qpid/test/framework/BrokerLifecycleAware.java | 70 ++++++++++++++++ .../apache/qpid/test/framework/CauseFailure.java | 42 ++++++++++ .../test/framework/CauseFailureUserPrompt.java | 65 +++++++++++++++ .../qpid/test/framework/FrameworkBaseCase.java | 53 +++++++++++- .../framework/clocksynch/UDPClockReference.java | 6 +- .../framework/distributedtesting/Coordinator.java | 1 + .../test/framework/qpid/CauseFailureDecorator.java | 95 ++++++++++++++++++++++ .../qpid/test/framework/qpid/CauseFailureInVM.java | 70 ++++++++++++++++ .../test/framework/qpid/InVMBrokerDecorator.java | 18 +++- 9 files changed, 415 insertions(+), 5 deletions(-) create mode 100644 java/systests/src/main/java/org/apache/qpid/test/framework/BrokerLifecycleAware.java create mode 100644 java/systests/src/main/java/org/apache/qpid/test/framework/CauseFailure.java create mode 100644 java/systests/src/main/java/org/apache/qpid/test/framework/CauseFailureUserPrompt.java create mode 100644 java/systests/src/main/java/org/apache/qpid/test/framework/qpid/CauseFailureDecorator.java create mode 100644 java/systests/src/main/java/org/apache/qpid/test/framework/qpid/CauseFailureInVM.java (limited to 'java') diff --git a/java/systests/src/main/java/org/apache/qpid/test/framework/BrokerLifecycleAware.java b/java/systests/src/main/java/org/apache/qpid/test/framework/BrokerLifecycleAware.java new file mode 100644 index 0000000000..e8b7da2537 --- /dev/null +++ b/java/systests/src/main/java/org/apache/qpid/test/framework/BrokerLifecycleAware.java @@ -0,0 +1,70 @@ +/* + * + * 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.qpid.test.framework; + +/** + * BrokerLifecycleAware is an awareness interface implemented by test cases that can run control the life-cycle of + * the brokers on which they run. Its purpose is to expose additional instrumentation of brokers during testing, that + * enables tests to use an automated failure mechanism to simulate broker failures, and to re-start failed brokers. + * + *

+ *
CRC Card
Responsibilities Collaborations + *
Indicate whether or not a test case is using an in-vm broker. + *
Track which in-vm broker is currently in use. + *
Accept setting of a failure mechanism. {@link CauseFailure}. + *
+ * + * @todo Need to think about how to present the brokers through this interface. Thinking numbering the available + * brokers from 1 will do. Then can kill 1 and assume failing onto 2. Restart 1 and kill 2 and fail back onto + * 1 again? + */ +public interface BrokerLifecycleAware +{ + public void setInVmBrokers(); + + /** + * Indicates whether or not a test case is using in-vm brokers. + * + * @return true if the test is using in-vm brokers, false otherwise. + */ + public boolean usingInVmBroker(); + + /** + * Sets the currently live in-vm broker. + * + * @param i The currently live in-vm broker. + */ + public void setLiveBroker(int i); + + /** + * Reports the currently live in-vm broker. + * + * @return The currently live in-vm broker. + */ + public int getLiveBroker(); + + /** + * Accepts a failure mechanism. + * + * @param failureMechanism The failure mechanism. + */ + public void setFailureMechanism(CauseFailure failureMechanism); +} diff --git a/java/systests/src/main/java/org/apache/qpid/test/framework/CauseFailure.java b/java/systests/src/main/java/org/apache/qpid/test/framework/CauseFailure.java new file mode 100644 index 0000000000..8a5a9560a0 --- /dev/null +++ b/java/systests/src/main/java/org/apache/qpid/test/framework/CauseFailure.java @@ -0,0 +1,42 @@ +/* + * + * 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.qpid.test.framework; + +/** + * CauseFailure provides a method to cause a failure in a messaging broker, usually used in conjunction with fail-over + * or other failure mode testing. In some cases failures may be automated, for example by shutting down an in-vm broker, + * or by sending a special control signal to a broker over a network connection. In other cases, it may be preferable + * to ask a user interactively to cause a failure scenario, in which case an implementation may display a prompt or + * dialog box asking for notification once the failure has been caused. The purpose of this interface is to abstract + * the exact cause and nature of a failure out of failure test cases. + * + *

+ *
CRC Card
Responsibilities + *
Cause messaging broker failure. + *
+ */ +public interface CauseFailure +{ + /** + * Causes the active message broker to fail. + */ + void causeFailure(); +} diff --git a/java/systests/src/main/java/org/apache/qpid/test/framework/CauseFailureUserPrompt.java b/java/systests/src/main/java/org/apache/qpid/test/framework/CauseFailureUserPrompt.java new file mode 100644 index 0000000000..6b96ade674 --- /dev/null +++ b/java/systests/src/main/java/org/apache/qpid/test/framework/CauseFailureUserPrompt.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.qpid.test.framework; + +import org.apache.qpid.test.framework.CauseFailure; + +import java.io.IOException; + +/** + * Causes a message broker failure by interactively prompting the user to cause it. + * + *

+ *
CRC Card
Responsibilities Collaborations + *
Cause messaging broker failure. + *
+ */ +public class CauseFailureUserPrompt implements CauseFailure +{ + /** + * Causes the active message broker to fail. + */ + public void causeFailure() + { + waitForUser("Cause a broker failure now, then press Return."); + } + + /** + * Outputs a prompt to the console and waits for the user to press return. + * + * @param prompt The prompt to display on the console. + */ + private void waitForUser(String prompt) + { + System.out.println(prompt); + + try + { + System.in.read(); + } + catch (IOException e) + { + // Ignored. + } + + System.out.println("Continuing."); + } +} diff --git a/java/systests/src/main/java/org/apache/qpid/test/framework/FrameworkBaseCase.java b/java/systests/src/main/java/org/apache/qpid/test/framework/FrameworkBaseCase.java index 64b8569f52..e2106eaca0 100644 --- a/java/systests/src/main/java/org/apache/qpid/test/framework/FrameworkBaseCase.java +++ b/java/systests/src/main/java/org/apache/qpid/test/framework/FrameworkBaseCase.java @@ -23,6 +23,7 @@ package org.apache.qpid.test.framework; import org.apache.log4j.Logger; import org.apache.log4j.NDC; +import org.apache.qpid.test.framework.BrokerLifecycleAware; import org.apache.qpid.test.framework.sequencers.CircuitFactory; import uk.co.thebadgerset.junit.extensions.AsymptoticTestCase; @@ -46,7 +47,8 @@ import java.util.List; * Convert failed assertions to error messages. * */ -public class FrameworkBaseCase extends AsymptoticTestCase implements FrameworkTestContext, SetupTaskAware +public class FrameworkBaseCase extends AsymptoticTestCase implements FrameworkTestContext, SetupTaskAware, + BrokerLifecycleAware { /** Used for debugging purposes. */ private static final Logger log = Logger.getLogger(FrameworkBaseCase.class); @@ -60,6 +62,12 @@ public class FrameworkBaseCase extends AsymptoticTestCase implements FrameworkTe /** A default setup task processor to delegate setup tasks to. */ protected SetupTaskHandler taskHandler = new SetupTaskHandler(); + /** Flag used to track whether the test is in-vm or not. */ + protected boolean isUsingInVM; + + /** Holds the failure mechanism. */ + protected CauseFailure failureMechanism = new CauseFailureUserPrompt(); + /** * Creates a new test case with the specified name. * @@ -234,4 +242,47 @@ public class FrameworkBaseCase extends AsymptoticTestCase implements FrameworkTe { return methodName; } + + public void setInVmBrokers() + { + isUsingInVM = true; + } + + /** + * Indicates whether or not a test case is using in-vm brokers. + * + * @return true if the test is using in-vm brokers, false otherwise. + */ + public boolean usingInVmBroker() + { + return isUsingInVM; + } + + /** + * Sets the currently live in-vm broker. + * + * @param i The currently live in-vm broker. + */ + public void setLiveBroker(int i) + { } + + /** + * Reports the currently live in-vm broker. + * + * @return The currently live in-vm broker. + */ + public int getLiveBroker() + { + return 0; + } + + /** + * Accepts a failure mechanism. + * + * @param failureMechanism The failure mechanism. + */ + public void setFailureMechanism(CauseFailure failureMechanism) + { + this.failureMechanism = failureMechanism; + } } diff --git a/java/systests/src/main/java/org/apache/qpid/test/framework/clocksynch/UDPClockReference.java b/java/systests/src/main/java/org/apache/qpid/test/framework/clocksynch/UDPClockReference.java index f6195aa553..0b3f6865d6 100644 --- a/java/systests/src/main/java/org/apache/qpid/test/framework/clocksynch/UDPClockReference.java +++ b/java/systests/src/main/java/org/apache/qpid/test/framework/clocksynch/UDPClockReference.java @@ -22,12 +22,12 @@ package org.apache.qpid.test.framework.clocksynch; import org.apache.log4j.Logger; +import uk.co.thebadgerset.junit.extensions.ShutdownHookable; + import java.io.IOException; import java.net.*; import java.nio.ByteBuffer; -import uk.co.thebadgerset.junit.extensions.ShutdownHookable; - /** * UDPClockReference supplies a refernce clock signal (generated from System.nanoTime()). * @@ -49,7 +49,7 @@ public class UDPClockReference implements Runnable, ShutdownHookable private static final int TIMEOUT = 200; /** Defines the port to run the clock reference on. */ - public static final int REFERENCE_PORT = 4445; + public static final int REFERENCE_PORT = 4444; /** Holds the socket to receive clock reference requests on. */ protected DatagramSocket socket = null; diff --git a/java/systests/src/main/java/org/apache/qpid/test/framework/distributedtesting/Coordinator.java b/java/systests/src/main/java/org/apache/qpid/test/framework/distributedtesting/Coordinator.java index 1be2f338f7..02fcb7fb55 100644 --- a/java/systests/src/main/java/org/apache/qpid/test/framework/distributedtesting/Coordinator.java +++ b/java/systests/src/main/java/org/apache/qpid/test/framework/distributedtesting/Coordinator.java @@ -330,6 +330,7 @@ public class Coordinator extends TKTestRunner { log.debug("Top level handler caught execption.", e); console.info(e.getMessage()); + e.printStackTrace(); System.exit(EXCEPTION_EXIT); } } diff --git a/java/systests/src/main/java/org/apache/qpid/test/framework/qpid/CauseFailureDecorator.java b/java/systests/src/main/java/org/apache/qpid/test/framework/qpid/CauseFailureDecorator.java new file mode 100644 index 0000000000..2e41e5c0c6 --- /dev/null +++ b/java/systests/src/main/java/org/apache/qpid/test/framework/qpid/CauseFailureDecorator.java @@ -0,0 +1,95 @@ +/* + * + * 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.qpid.test.framework.qpid; + +import junit.framework.Test; +import junit.framework.TestResult; + +import org.apache.qpid.test.framework.BrokerLifecycleAware; +import org.apache.qpid.test.framework.CauseFailureUserPrompt; + +import uk.co.thebadgerset.junit.extensions.WrappedSuiteTestDecorator; + +/** + * CauseFailureDecorator applies decorations to {@link BrokerLifecycleAware} tests, so that they may use different failure + * mechanisms. It is capable of detecting when a test case uses in-vm brokers, and setting up an automatic failure + * for those tests, so that the current live broker can be shut-down by test cases. For external brokers, automatic + * failure could be implemented, for example by having a kill script. At the moment this sets up the failure to prompt + * a user interactively to cause a failure, using {@link CauseFailureUserPrompt}. + * + *

+ *
CRC Card
Responsibilities Collaborations + *
Setup automatic failures for in-vm brokers. {@link CauseFailureInVM} + *
Setup user generated failures for external brokers. {@link CauseFailureUserPrompt}. + *
+ *
+ * + * @todo Slight problem in that CauseFailureInVM is Qpid specific, whereas CauseFailureUserPrompt is not. Would like the + * failure decorator to be non-qpid specific so that it can test failure of any JMS implementation too. Either pass + * in class name of failure mechanism, set it up in the in-vm decorator instead of here but with prompt user as the + * default for when the in-vm decorator is not used? + */ +public class CauseFailureDecorator extends WrappedSuiteTestDecorator +{ + /** The test suite to run. */ + private Test test; + + /** + * Creates a wrapped test test decorator from another one. + * + * @param test The test test. + */ + public CauseFailureDecorator(WrappedSuiteTestDecorator test) + { + super(test); + this.test = test; + } + + /** + * Runs the tests with a LocalAMQPCircuitFactory. Only tests that extend FrameworkBaseCase are decorated. + * + * @param testResult The the results object to monitor the test results with. + */ + public void run(TestResult testResult) + { + for (Test test : getAllUnderlyingTests()) + { + if (test instanceof BrokerLifecycleAware) + { + BrokerLifecycleAware failureTest = (BrokerLifecycleAware) test; + failureTest.setFailureMechanism(new CauseFailureUserPrompt()); + } + } + + // Run the test. + test.run(testResult); + } + + /** + * Prints the name of the test for debugging purposes. + * + * @return The name of the test. + */ + public String toString() + { + return "CauseFailureDecorator: [test = \"" + test + "\"]"; + } +} diff --git a/java/systests/src/main/java/org/apache/qpid/test/framework/qpid/CauseFailureInVM.java b/java/systests/src/main/java/org/apache/qpid/test/framework/qpid/CauseFailureInVM.java new file mode 100644 index 0000000000..b63ac43601 --- /dev/null +++ b/java/systests/src/main/java/org/apache/qpid/test/framework/qpid/CauseFailureInVM.java @@ -0,0 +1,70 @@ +/* + * + * 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.qpid.test.framework.qpid; + +import org.apache.qpid.client.transport.TransportConnection; +import org.apache.qpid.server.registry.ApplicationRegistry; +import org.apache.qpid.test.framework.CauseFailure; +import org.apache.qpid.test.framework.BrokerLifecycleAware; + +/** + *

+ *
CRC Card
Responsibilities Collaborations + *
Cause messaging broker failure on the active in-vm broker. + * {@link TransportConnection}, {@link ApplicationRegistry} + *
+ */ +public class CauseFailureInVM implements CauseFailure +{ + /** Holds the in-vm broker instrumented test case to create failures for. */ + private BrokerLifecycleAware inVMTest; + + /** + * Creates an automated failure mechanism for testing against in-vm brokers. The test to create the mechanism + * for is specified, and as this failure is for in-vm brokers, the test must be {@link org.apache.qpid.test.framework.BrokerLifecycleAware}. The test + * must also report that it is currently being run against an in-vm broker, and it is a runtime error if it is not, + * as the creator of this failure mechanism should already have checked that it is. + * + * @param inVMTest The test case to create an automated failure mechanism for. + */ + public CauseFailureInVM(BrokerLifecycleAware inVMTest) + { + // Check that the test is really using in-vm brokers. + if (!inVMTest.usingInVmBroker()) + { + throw new RuntimeException( + "Cannot create in-vm broker failure mechanism for a test that is not using in-vm brokers."); + } + + this.inVMTest = inVMTest; + } + + /** + * Causes the active message broker to fail. + */ + public void causeFailure() + { + int liveBroker = inVMTest.getLiveBroker(); + + TransportConnection.killVMBroker(liveBroker); + ApplicationRegistry.remove(liveBroker); + } +} diff --git a/java/systests/src/main/java/org/apache/qpid/test/framework/qpid/InVMBrokerDecorator.java b/java/systests/src/main/java/org/apache/qpid/test/framework/qpid/InVMBrokerDecorator.java index 86a439f264..a7ea9f9c7f 100644 --- a/java/systests/src/main/java/org/apache/qpid/test/framework/qpid/InVMBrokerDecorator.java +++ b/java/systests/src/main/java/org/apache/qpid/test/framework/qpid/InVMBrokerDecorator.java @@ -26,6 +26,7 @@ import junit.framework.TestResult; import org.apache.qpid.client.transport.TransportConnection; import org.apache.qpid.client.vmbroker.AMQVMBrokerCreationException; import org.apache.qpid.server.registry.ApplicationRegistry; +import org.apache.qpid.test.framework.BrokerLifecycleAware; import org.apache.qpid.test.framework.FrameworkBaseCase; import uk.co.thebadgerset.junit.extensions.SetupTaskAware; @@ -43,6 +44,11 @@ import uk.co.thebadgerset.junit.extensions.WrappedSuiteTestDecorator; * * @todo May need to add a more fine grained injection point for the in-vm broker management, as this acts at the * suite level, rather than the individual test level. + * + * @todo Management of in-vm brokers for failure testing. Failure test setups may need to set their connection url to + * use multiple broker (vm://:1;vm://:2), with fail-over between them. There is round-robin fail-over, but also + * retry? A test case using an in-vm broker needs to record which one it is using, so that it can be + * killed/restarted. */ public class InVMBrokerDecorator extends WrappedSuiteTestDecorator { @@ -71,7 +77,7 @@ public class InVMBrokerDecorator extends WrappedSuiteTestDecorator { // Check that the test to have an in-vm broker setup/teardown task added to it, is actually a framework // test that can handle setup tasks. - if ((test instanceof FrameworkBaseCase) && (test instanceof SetupTaskAware)) + if ((test instanceof SetupTaskAware)) { SetupTaskAware frameworkTest = (SetupTaskAware) test; @@ -100,6 +106,16 @@ public class InVMBrokerDecorator extends WrappedSuiteTestDecorator ApplicationRegistry.remove(1); } }); + + // Check if the test is aware whether or not it can control the broker life cycle, and if so provide + // additional instrumentation for it to control the in-vm broker through. + if (test instanceof BrokerLifecycleAware) + { + BrokerLifecycleAware inVMTest = (BrokerLifecycleAware) test; + inVMTest.setInVmBrokers(); + inVMTest.setLiveBroker(1); + inVMTest.setFailureMechanism(new CauseFailureInVM(inVMTest)); + } } } -- cgit v1.2.1