|
| 1 | +package org.javaee7.ejb.timer; |
| 2 | + |
| 3 | +import org.hamcrest.BaseMatcher; |
| 4 | +import org.hamcrest.Description; |
| 5 | +import org.hamcrest.Matcher; |
| 6 | +import org.jboss.arquillian.container.test.api.Deployment; |
| 7 | +import org.jboss.arquillian.junit.Arquillian; |
| 8 | +import org.jboss.shrinkwrap.api.ShrinkWrap; |
| 9 | +import org.jboss.shrinkwrap.api.spec.WebArchive; |
| 10 | +import org.jboss.shrinkwrap.resolver.api.maven.Maven; |
| 11 | +import org.junit.Test; |
| 12 | +import org.junit.runner.RunWith; |
| 13 | + |
| 14 | +import javax.inject.Inject; |
| 15 | +import java.io.File; |
| 16 | + |
| 17 | +import static com.jayway.awaitility.Awaitility.await; |
| 18 | +import static com.jayway.awaitility.Awaitility.to; |
| 19 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 20 | +import static org.hamcrest.Matchers.equalTo; |
| 21 | +import static org.hamcrest.Matchers.is; |
| 22 | +import static org.javaee7.ejb.timer.WithinWindowMatcher.withinWindow; |
| 23 | + |
| 24 | +/** |
| 25 | + * author: Jacek Jackowiak |
| 26 | + */ |
| 27 | +@RunWith(Arquillian.class) |
| 28 | +public class MultipleScheduleTimerBeanTest { |
| 29 | + |
| 30 | + final static long TIMEOUT = 0l; |
| 31 | + final static long TOLERANCE = 1000l; |
| 32 | + |
| 33 | + @Inject |
| 34 | + PingsListener pings; |
| 35 | + |
| 36 | + @Deployment |
| 37 | + public static WebArchive deploy() { |
| 38 | + File[] jars = Maven.resolver().loadPomFromFile("pom.xml") |
| 39 | + .resolve("com.jayway.awaitility:awaitility") |
| 40 | + .withTransitivity().asFile(); |
| 41 | + |
| 42 | + return ShrinkWrap.create(WebArchive.class) |
| 43 | + .addAsLibraries(jars) |
| 44 | + .addClasses(WithinWindowMatcher.class, Ping.class, PingsListener.class, MultipleScheduleTimerBean.class); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void should_receive_three_pings() { |
| 49 | + await().untilCall(to(pings.getPings()).size(), equalTo(3)); |
| 50 | + |
| 51 | + Ping firstPing = pings.getPings().get(0); |
| 52 | + Ping secondPing = pings.getPings().get(1); |
| 53 | + Ping thirdPing = pings.getPings().get(2); |
| 54 | + |
| 55 | + long delay = secondPing.getTime() - firstPing.getTime(); |
| 56 | + System.out.println("Actual timeout = " + delay); |
| 57 | + long delay2 = thirdPing.getTime() - secondPing.getTime(); |
| 58 | + System.out.println("Actual timeout = " + delay2); |
| 59 | + long smallerDelay = Math.min(delay, delay2); |
| 60 | + assertThat(smallerDelay, is(withinWindow(TIMEOUT, TOLERANCE))); |
| 61 | + } |
| 62 | +} |
0 commit comments