NO-JIRA Fixing test race on ClientCrashTest

This commit is contained in:
Clebert Suconic 2018-05-10 12:18:08 -04:00
parent 9d62e1d854
commit 32ec602eaf
5 changed files with 34 additions and 24 deletions

View File

@ -46,8 +46,13 @@ public class Wait {
return waitFor(condition, MAX_WAIT_MILLIS);
}
public static void assertEquals(long size, LongCondition condition) throws Exception {
boolean result = waitFor(() -> condition.getCount() == size);
assertEquals(size, condition, MAX_WAIT_MILLIS);
}
public static void assertEquals(long size, LongCondition condition, long timeout) throws Exception {
boolean result = waitFor(() -> condition.getCount() == size, timeout);
if (!result) {
Assert.fail(size + " != " + condition.getCount());
@ -56,7 +61,11 @@ public class Wait {
public static void assertEquals(int size, IntCondition condition) throws Exception {
boolean result = waitFor(() -> condition.getCount() == size);
assertEquals(size, condition, MAX_WAIT_MILLIS);
}
public static void assertEquals(int size, IntCondition condition, long timeout) throws Exception {
boolean result = waitFor(() -> condition.getCount() == size, timeout);
if (!result) {
Assert.fail(size + " != " + condition.getCount());

View File

@ -16,6 +16,8 @@
*/
package org.apache.activemq.artemis.tests.integration.clientcrash;
import java.util.concurrent.TimeUnit;
import org.apache.activemq.artemis.api.core.Message;
import org.apache.activemq.artemis.api.core.SimpleString;
import org.apache.activemq.artemis.api.core.client.ClientConsumer;
@ -29,6 +31,7 @@ import org.apache.activemq.artemis.jms.client.ActiveMQTextMessage;
import org.apache.activemq.artemis.tests.integration.IntegrationTestLogger;
import org.apache.activemq.artemis.tests.util.SpawnedVMSupport;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
@ -93,6 +96,10 @@ public class ClientCrashTest extends ClientTestBase {
// if the client is too fast you race the send before the queue was created, missing a message
p = SpawnedVMSupport.spawnVM(CrashClient.class.getName());
Assert.assertTrue(p.waitFor(1, TimeUnit.MINUTES));
Assert.assertEquals(CrashClient.OK, p.exitValue());
ClientConsumer consumer = session.createConsumer(ClientCrashTest.QUEUE);
ClientProducer producer = session.createProducer(ClientCrashTest.QUEUE);
@ -103,16 +110,13 @@ public class ClientCrashTest extends ClientTestBase {
assertNotNull("no message received", messageFromClient);
assertEquals(ClientCrashTest.MESSAGE_TEXT_FROM_CLIENT, messageFromClient.getBodyBuffer().readString());
assertActiveConnections(1 + 1); // One local and one from the other vm
assertActiveSession(1 + 1);
assertActiveConnections( 1); // One local and one from the other vm
assertActiveSession(1);
ClientMessage message = session.createMessage(ActiveMQTextMessage.TYPE, false, 0, System.currentTimeMillis(), (byte) 1);
message.getBodyBuffer().writeString(ClientCrashTest.MESSAGE_TEXT_FROM_SERVER);
producer.send(message);
ClientCrashTest.log.debug("waiting for the client VM to crash ...");
p.waitFor();
assertEquals(9, p.exitValue());
System.out.println("VM Exited");
@ -145,16 +149,16 @@ public class ClientCrashTest extends ClientTestBase {
p = SpawnedVMSupport.spawnVM(CrashClient2.class.getName());
ClientCrashTest.log.debug("waiting for the client VM to crash ...");
p.waitFor();
Assert.assertTrue(p.waitFor(1, TimeUnit.MINUTES));
assertEquals(9, p.exitValue());
assertEquals(CrashClient2.OK, p.exitValue());
System.out.println("VM Exited");
long timeout = ClientCrashTest.CONNECTION_TTL + ClientCrashTest.PING_PERIOD + 10000;
assertActiveConnections(1, timeout);
assertActiveSession(1, timeout);
assertActiveSession(2, timeout);
ClientConsumer consumer = session.createConsumer(ClientCrashTest.QUEUE2);

View File

@ -18,8 +18,8 @@ package org.apache.activemq.artemis.tests.integration.clientcrash;
import org.apache.activemq.artemis.core.config.Configuration;
import org.apache.activemq.artemis.core.server.ActiveMQServer;
import org.apache.activemq.artemis.junit.Wait;
import org.apache.activemq.artemis.tests.util.SpawnedTestBase;
import org.junit.Assert;
import org.junit.Before;
public abstract class ClientTestBase extends SpawnedTestBase {
@ -41,11 +41,7 @@ public abstract class ClientTestBase extends SpawnedTestBase {
}
protected void assertActiveConnections(final int expectedActiveConnections, long timeout) throws Exception {
timeout += System.currentTimeMillis();
while (timeout > System.currentTimeMillis() && server.getActiveMQServerControl().getConnectionCount() != expectedActiveConnections) {
Thread.sleep(100);
}
Assert.assertEquals(expectedActiveConnections, server.getActiveMQServerControl().getConnectionCount());
Wait.assertEquals(expectedActiveConnections, server.getActiveMQServerControl()::getConnectionCount, timeout);
}
protected void assertActiveSession(final int expectedActiveSession) throws Exception {
@ -53,11 +49,7 @@ public abstract class ClientTestBase extends SpawnedTestBase {
}
protected void assertActiveSession(final int expectedActiveSession, long timeout) throws Exception {
timeout += System.currentTimeMillis();
while (timeout > System.currentTimeMillis() && server.getSessions().size() != expectedActiveSession) {
Thread.sleep(100);
}
Assert.assertEquals(expectedActiveSession, server.getSessions().size());
Wait.assertEquals(expectedActiveSession, server.getSessions()::size, timeout);
}
}

View File

@ -33,6 +33,9 @@ import org.apache.activemq.artemis.tests.integration.IntegrationTestLogger;
* Code to be run in an external VM, via main()
*/
public class CrashClient {
public static int OK = 9;
public static int NOT_OK = 1;
// Constants ------------------------------------------------------------------------------------
private static final IntegrationTestLogger log = IntegrationTestLogger.LOGGER;
@ -58,10 +61,10 @@ public class CrashClient {
producer.send(message);
// exit without closing the session properly
System.exit(9);
System.exit(OK);
} catch (Throwable t) {
CrashClient.log.error(t.getMessage(), t);
System.exit(1);
System.exit(NOT_OK);
}
}

View File

@ -33,6 +33,8 @@ import org.apache.activemq.artemis.tests.integration.IntegrationTestLogger;
* Code to be run in an external VM, via main()
*/
public class CrashClient2 {
public static final int OK = 9;
// Constants ------------------------------------------------------------------------------------
private static final IntegrationTestLogger log = IntegrationTestLogger.LOGGER;
@ -72,7 +74,7 @@ public class CrashClient2 {
}
// exit without closing the session properly
System.exit(9);
System.exit(OK);
} catch (Throwable t) {
log.error(t.getMessage(), t);
System.exit(1);