Skip to content

Commit a12ee74

Browse files
committed
Simplified JMS-XA test somewhat and increased timeout
1 parent b78caf7 commit a12ee74

File tree

8 files changed

+47
-155
lines changed

8 files changed

+47
-155
lines changed

jms/jms-xa/src/main/java/org/javaee7/jms/xa/DeliveryStats.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package org.javaee7.jms.xa;
22

3+
import java.util.concurrent.CountDownLatch;
4+
35
import javax.ejb.Singleton;
46

57
@Singleton
68
public class DeliveryStats {
9+
10+
public static CountDownLatch countDownLatch = new CountDownLatch(1);
711

812
private long deliveredMessagesCount;
913

jms/jms-xa/src/main/java/org/javaee7/jms/xa/JMSListener.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.javaee7.jms.xa;
22

33
import static java.util.logging.Level.SEVERE;
4+
import static org.javaee7.jms.xa.DeliveryStats.countDownLatch;
45

56
import java.util.logging.Logger;
67

@@ -29,6 +30,7 @@ public void onMessage(Message message) {
2930
logger.info("Message received (async): " + message.getBody(String.class));
3031

3132
deliveryStats.messageDelivered();
33+
countDownLatch.countDown();
3234
} catch (JMSException ex) {
3335
logger.log(SEVERE, null, ex);
3436
}

jms/jms-xa/src/main/java/org/javaee7/jms/xa/UserManager.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ public User register(String email) {
1818
User user = new User(email);
1919

2020
entityManager.persist(user);
21-
jmsSender.sendMessage("Hello " + email);
21+
22+
String msg = "Hello " + email + " " + System.currentTimeMillis();
23+
24+
System.out.println("Sending JMS message " + msg);
25+
jmsSender.sendMessage(msg);
2226

2327
return user;
2428
}
Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package org.javaee7.jms.xa;
22

3+
import static java.util.concurrent.TimeUnit.SECONDS;
4+
import static org.javaee7.jms.xa.DeliveryStats.countDownLatch;
35
import static org.junit.Assert.assertEquals;
46

57
import org.javaee7.jms.xa.producers.NonXAConnectionFactoryProducer;
68
import org.javaee7.jms.xa.utils.AbstractUserManagerTest;
7-
import org.javaee7.jms.xa.utils.ReceptionSynchronizer;
89
import org.jboss.arquillian.container.test.api.Deployment;
910
import org.jboss.arquillian.junit.Arquillian;
1011
import org.jboss.shrinkwrap.api.spec.WebArchive;
11-
import org.junit.Before;
1212
import org.junit.Test;
1313
import org.junit.runner.RunWith;
1414

@@ -19,45 +19,35 @@ public class UserManagerNonXATest extends AbstractUserManagerTest {
1919
public static WebArchive createDeployment() {
2020
return createWebArchive().addClass(NonXAConnectionFactoryProducer.class);
2121
}
22-
23-
@Before
24-
public void reset() {
25-
deliveryStats.reset();
26-
assertEquals(0L, deliveryStats.getDeliveredMessagesCount());
27-
}
2822

2923
@Test
3024
public void emailAlreadyRegisteredNonXA() throws Exception {
25+
26+
System.out.println("Entering emailAlreadyRegisteredNonXA");
27+
3128
try {
32-
3329
// This email is already in DB so we should get an exception trying to register it.
34-
3530
userManager.register("jack@itcrowd.pl");
3631
} catch (Exception e) {
3732
logger.info("Got expected exception " + e);
3833
}
3934

40-
try {
41-
ReceptionSynchronizer.waitFor(JMSListener.class, "onMessage");
42-
} catch (AssertionError error) {
43-
logger.info("Got expected error " + error);
44-
logger.info("We're just making sure that we have waited long enough to let the message get to MDB");
45-
}
46-
35+
countDownLatch.await(90, SECONDS);
36+
37+
assertEquals("Timeout expired and countDownLatch did not reach 0", 0, countDownLatch.getCount());
4738
assertEquals("Message should be delivered despite transaction rollback", 1L, deliveryStats.getDeliveredMessagesCount());
4839
}
4940

5041
@Test
5142
public void happyPathNonXA() throws Exception {
43+
44+
System.out.println("Entering happyPathNonXA");
45+
5246
userManager.register("bernard@itcrowd.pl");
5347

54-
try {
55-
ReceptionSynchronizer.waitFor(JMSListener.class, "onMessage");
56-
} catch (AssertionError error) {
57-
logger.info("Got expected error " + error);
58-
logger.info("We're just making sure that we have waited long enough to let the message get to MDB");
59-
}
48+
countDownLatch.await(90, SECONDS);
6049

50+
assertEquals("Timeout expired and countDownLatch did not reach 0", 0, countDownLatch.getCount());
6151
assertEquals(1L, deliveryStats.getDeliveredMessagesCount());
6252
}
6353
}
Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
package org.javaee7.jms.xa;
22

3+
import static java.util.concurrent.TimeUnit.SECONDS;
4+
import static org.javaee7.jms.xa.DeliveryStats.countDownLatch;
35
import static org.junit.Assert.assertEquals;
4-
import static org.junit.Assert.fail;
56

67
import org.javaee7.jms.xa.producers.XAConnectionFactoryProducer;
78
import org.javaee7.jms.xa.utils.AbstractUserManagerTest;
8-
import org.javaee7.jms.xa.utils.ReceptionSynchronizer;
99
import org.jboss.arquillian.container.test.api.Deployment;
1010
import org.jboss.arquillian.junit.Arquillian;
1111
import org.jboss.shrinkwrap.api.spec.WebArchive;
12-
import org.junit.Before;
1312
import org.junit.Test;
1413
import org.junit.runner.RunWith;
1514

@@ -20,12 +19,6 @@ public class UserManagerXATest extends AbstractUserManagerTest {
2019
public static WebArchive createDeployment() {
2120
return createWebArchive().addClass(XAConnectionFactoryProducer.class);
2221
}
23-
24-
@Before
25-
public void reset() {
26-
deliveryStats.reset();
27-
assertEquals(0L, deliveryStats.getDeliveredMessagesCount());
28-
}
2922

3023
@Test
3124
public void emailAlreadyRegisteredXA() throws Exception {
@@ -36,22 +29,22 @@ public void emailAlreadyRegisteredXA() throws Exception {
3629
logger.info("Got expected exception " + e);
3730
}
3831

39-
try {
40-
ReceptionSynchronizer.waitFor(JMSListener.class, "onMessage");
41-
fail("Method should not have been invoked");
42-
} catch (AssertionError error) {
43-
logger.info("Got expected error " + error);
44-
logger.info("Message should not have been delivered due to transaction rollback");
45-
}
32+
// Wait for at most 30 seconds for the JMS method to NOT be called, since we're testing for something
33+
// to NOT happen we can never be 100% sure, but 30 seconds should cover almost all cases.
34+
countDownLatch.await(30, SECONDS);
4635

36+
assertEquals("countDownLatch was decreased meaning JMS method was called, but should not have been.", 1, countDownLatch.getCount());
4737
assertEquals("Message should not be delivered due to transaction rollback", 0L, deliveryStats.getDeliveredMessagesCount());
4838
}
4939

5040
@Test
5141
public void happyPathXA() throws Exception {
5242
userManager.register("bernard@itcrowd.pl");
53-
ReceptionSynchronizer.waitFor(JMSListener.class, "onMessage");
5443

44+
// Wait for at most 90 seconds for the JMS method to be called
45+
countDownLatch.await(90, SECONDS);
46+
47+
assertEquals("Timeout expired and countDownLatch did not reach 0 (so JMS method not called)", 0, countDownLatch.getCount());
5548
assertEquals(1L, deliveryStats.getDeliveredMessagesCount());
5649
}
5750
}

jms/jms-xa/src/test/java/org/javaee7/jms/xa/utils/AbstractUserManagerTest.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package org.javaee7.jms.xa.utils;
22

3+
import static org.javaee7.jms.xa.DeliveryStats.countDownLatch;
34
import static org.jboss.shrinkwrap.api.ShrinkWrap.create;
45
import static org.jboss.shrinkwrap.api.asset.EmptyAsset.INSTANCE;
6+
import static org.junit.Assert.assertEquals;
57

8+
import java.util.concurrent.CountDownLatch;
69
import java.util.logging.Logger;
710

811
import javax.ejb.EJB;
@@ -24,17 +27,22 @@ public class AbstractUserManagerTest {
2427

2528
protected static WebArchive createWebArchive() {
2629
return create(WebArchive.class)
27-
.addAsWebInfResource("ejb-jar.xml")
2830
.addAsWebInfResource(INSTANCE, "beans.xml")
2931
.addAsResource("META-INF/persistence.xml")
3032
.addAsResource("META-INF/load.sql")
31-
.addClass(ReceptionSynchronizer.class)
3233
.addClass(AbstractUserManagerTest.class)
3334
.addPackage(UserManager.class.getPackage());
3435
}
35-
36+
3637
@Before
37-
public void setUp() throws Exception {
38-
ReceptionSynchronizer.clear();
38+
public void reset() {
39+
System.out.println("Resetting stats and countdown latch");
40+
41+
countDownLatch = new CountDownLatch(1);
42+
deliveryStats.reset();
43+
44+
assertEquals("countDownLatch should start at 1", 1, countDownLatch.getCount());
45+
assertEquals(0L, deliveryStats.getDeliveredMessagesCount());
3946
}
47+
4048
}

jms/jms-xa/src/test/java/org/javaee7/jms/xa/utils/ReceptionSynchronizer.java

Lines changed: 0 additions & 89 deletions
This file was deleted.

jms/jms-xa/src/test/resources/ejb-jar.xml

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)