jee7schedule
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
package com.baeldung.timer;
|
||||
|
||||
import com.jayway.awaitility.Awaitility;
|
||||
import org.hamcrest.Matchers;
|
||||
import org.jboss.arquillian.container.test.api.Deployment;
|
||||
import org.jboss.arquillian.junit.Arquillian;
|
||||
import org.jboss.shrinkwrap.api.ShrinkWrap;
|
||||
import org.jboss.shrinkwrap.api.spec.WebArchive;
|
||||
import org.jboss.shrinkwrap.resolver.api.maven.Maven;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.io.File;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static com.jayway.awaitility.Awaitility.await;
|
||||
import static com.jayway.awaitility.Awaitility.to;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
||||
|
||||
@RunWith(Arquillian.class)
|
||||
public class AutomaticTimerBeanTest {
|
||||
|
||||
//the @AutomaticTimerBean has a method called every 10 seconds
|
||||
//testing the difference ==> 100000
|
||||
final static long TIMEOUT = 10000l;
|
||||
|
||||
//the tolerance accepted , so if between two consecutive calls there has to be at least 9 or max 11 seconds.
|
||||
//because the timer service is not intended for real-time applications so it will not be exactly 10 seconds
|
||||
final static long TOLERANCE = 1000l;
|
||||
|
||||
@Inject
|
||||
TimerEventListener timerEventListener;
|
||||
|
||||
@Deployment
|
||||
public static WebArchive deploy() {
|
||||
File[] jars = Maven.resolver().loadPomFromFile("pom.xml")
|
||||
.resolve("com.jayway.awaitility:awaitility")
|
||||
.withTransitivity().asFile();
|
||||
|
||||
//only @AutomaticTimerBean is deployed not the other timers
|
||||
return ShrinkWrap.create(WebArchive.class)
|
||||
.addAsLibraries(jars)
|
||||
.addClasses(WithinWindowMatcher.class, TimerEvent.class, TimerEventListener.class, AutomaticTimerBean.class);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
public void should_receive_two_pings() {
|
||||
Awaitility.setDefaultTimeout(30, TimeUnit.SECONDS);
|
||||
//the test will wait here until two events are triggered
|
||||
await().untilCall(to(timerEventListener.getEvents()).size(), equalTo(2));
|
||||
|
||||
TimerEvent firstEvent = timerEventListener.getEvents().get(0);
|
||||
TimerEvent secondEvent = timerEventListener.getEvents().get(1);
|
||||
|
||||
long delay = secondEvent.getTime() - firstEvent.getTime();
|
||||
System.out.println("Actual timeout = " + delay);
|
||||
|
||||
//ensure that the delay between the events is more or less 10 seconds (no real time precision)
|
||||
assertThat(delay, Matchers.is(WithinWindowMatcher.withinWindow(TIMEOUT, TOLERANCE)));
|
||||
}
|
||||
}
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
package com.baeldung.timer;
|
||||
|
||||
import com.jayway.awaitility.Awaitility;
|
||||
import org.jboss.arquillian.container.test.api.Deployment;
|
||||
import org.jboss.arquillian.junit.Arquillian;
|
||||
import org.jboss.shrinkwrap.api.ShrinkWrap;
|
||||
import org.jboss.shrinkwrap.api.spec.WebArchive;
|
||||
import org.jboss.shrinkwrap.resolver.api.maven.Maven;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.io.File;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static com.jayway.awaitility.Awaitility.await;
|
||||
import static com.jayway.awaitility.Awaitility.to;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
|
||||
|
||||
@RunWith(Arquillian.class)
|
||||
public class ProgrammaticAtFixedRateTimerBeanTest {
|
||||
|
||||
final static long TIMEOUT = 1000;
|
||||
final static long TOLERANCE = 500l;
|
||||
|
||||
@Inject
|
||||
TimerEventListener timerEventListener;
|
||||
|
||||
@Deployment
|
||||
public static WebArchive deploy() {
|
||||
File[] jars = Maven.resolver().loadPomFromFile("pom.xml")
|
||||
.resolve("com.jayway.awaitility:awaitility")
|
||||
.withTransitivity().asFile();
|
||||
|
||||
return ShrinkWrap.create(WebArchive.class)
|
||||
.addAsLibraries(jars)
|
||||
.addClasses(WithinWindowMatcher.class, TimerEvent.class, TimerEventListener.class, ProgrammaticAtFixedRateTimerBean.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_receive_ten_pings() {
|
||||
|
||||
Awaitility.setDefaultTimeout(30, TimeUnit.SECONDS);
|
||||
|
||||
await().untilCall(to(timerEventListener.getEvents()).size(), equalTo(10));
|
||||
TimerEvent firstEvent = timerEventListener.getEvents().get(0);
|
||||
TimerEvent secondEvent = timerEventListener.getEvents().get(1);
|
||||
|
||||
long delay = secondEvent.getTime() - firstEvent.getTime();
|
||||
System.out.println("Actual timeout = " + delay);
|
||||
assertThat(delay, is(WithinWindowMatcher.withinWindow(TIMEOUT, TOLERANCE)));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.baeldung.timer;
|
||||
|
||||
import org.jboss.arquillian.container.test.api.Deployment;
|
||||
import org.jboss.arquillian.junit.Arquillian;
|
||||
import org.jboss.shrinkwrap.api.ShrinkWrap;
|
||||
import org.jboss.shrinkwrap.api.spec.WebArchive;
|
||||
import org.jboss.shrinkwrap.resolver.api.maven.Maven;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.io.File;
|
||||
|
||||
import static com.jayway.awaitility.Awaitility.await;
|
||||
import static com.jayway.awaitility.Awaitility.to;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
|
||||
|
||||
@RunWith(Arquillian.class)
|
||||
public class ProgrammaticTimerBeanTest {
|
||||
|
||||
final static long TIMEOUT = 5000l;
|
||||
final static long TOLERANCE = 1000l;
|
||||
|
||||
@Inject
|
||||
TimerEventListener timerEventListener;
|
||||
|
||||
@Deployment
|
||||
public static WebArchive deploy() {
|
||||
File[] jars = Maven.resolver().loadPomFromFile("pom.xml")
|
||||
.resolve("com.jayway.awaitility:awaitility")
|
||||
.withTransitivity().asFile();
|
||||
|
||||
return ShrinkWrap.create(WebArchive.class)
|
||||
.addAsLibraries(jars)
|
||||
.addClasses(WithinWindowMatcher.class, TimerEvent.class, TimerEventListener.class, ProgrammaticTimerBean.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_receive_two_pings() {
|
||||
|
||||
await().untilCall(to(timerEventListener.getEvents()).size(), equalTo(2));
|
||||
|
||||
TimerEvent firstEvent = timerEventListener.getEvents().get(0);
|
||||
TimerEvent secondEvent = timerEventListener.getEvents().get(1);
|
||||
|
||||
long delay = secondEvent.getTime() - firstEvent.getTime();
|
||||
System.out.println("Actual timeout = " + delay);
|
||||
assertThat(delay, is(WithinWindowMatcher.withinWindow(TIMEOUT, TOLERANCE)));
|
||||
}
|
||||
}
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
package com.baeldung.timer;
|
||||
|
||||
import com.jayway.awaitility.Awaitility;
|
||||
import org.jboss.arquillian.container.test.api.Deployment;
|
||||
import org.jboss.arquillian.junit.Arquillian;
|
||||
import org.jboss.shrinkwrap.api.ShrinkWrap;
|
||||
import org.jboss.shrinkwrap.api.spec.WebArchive;
|
||||
import org.jboss.shrinkwrap.resolver.api.maven.Maven;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.io.File;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static com.jayway.awaitility.Awaitility.await;
|
||||
import static com.jayway.awaitility.Awaitility.to;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
|
||||
|
||||
@RunWith(Arquillian.class)
|
||||
public class ProgrammaticWithFixedDelayTimerBeanTest {
|
||||
|
||||
final static long TIMEOUT = 15000l;
|
||||
final static long TOLERANCE = 1000l;
|
||||
|
||||
@Inject
|
||||
TimerEventListener timerEventListener;
|
||||
|
||||
@Deployment
|
||||
public static WebArchive deploy() {
|
||||
File[] jars = Maven.resolver().loadPomFromFile("pom.xml")
|
||||
.resolve("com.jayway.awaitility:awaitility")
|
||||
.withTransitivity().asFile();
|
||||
|
||||
return ShrinkWrap.create(WebArchive.class)
|
||||
.addAsLibraries(jars)
|
||||
.addClasses(WithinWindowMatcher.class, TimerEvent.class, TimerEventListener.class, ProgrammaticWithInitialFixedDelayTimerBean.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_receive_two_pings() {
|
||||
|
||||
Awaitility.setDefaultTimeout(30, TimeUnit.SECONDS);
|
||||
|
||||
// 10 seconds pause so we get the startTime and it will trigger first event
|
||||
long startTime = System.currentTimeMillis();
|
||||
|
||||
await().untilCall(to(timerEventListener.getEvents()).size(), equalTo(2));
|
||||
TimerEvent firstEvent = timerEventListener.getEvents().get(0);
|
||||
TimerEvent secondEvent = timerEventListener.getEvents().get(1);
|
||||
|
||||
long delay = secondEvent.getTime() - startTime;
|
||||
System.out.println("Actual timeout = " + delay);
|
||||
|
||||
//apx 15 seconds = 10 delay + 2 timers (first after a pause of 10 seconds and the next others every 5 seconds)
|
||||
assertThat(delay, is(WithinWindowMatcher.withinWindow(TIMEOUT, TOLERANCE)));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package com.baeldung.timer;
|
||||
|
||||
import com.jayway.awaitility.Awaitility;
|
||||
import org.hamcrest.Matchers;
|
||||
import org.jboss.arquillian.container.test.api.Deployment;
|
||||
import org.jboss.arquillian.junit.Arquillian;
|
||||
import org.jboss.shrinkwrap.api.ShrinkWrap;
|
||||
import org.jboss.shrinkwrap.api.spec.WebArchive;
|
||||
import org.jboss.shrinkwrap.resolver.api.maven.Maven;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.io.File;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static com.jayway.awaitility.Awaitility.await;
|
||||
import static com.jayway.awaitility.Awaitility.to;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
||||
|
||||
@RunWith(Arquillian.class)
|
||||
public class ScheduleTimerBeanTest {
|
||||
|
||||
final static long TIMEOUT = 5000l;
|
||||
final static long TOLERANCE = 1000l;
|
||||
|
||||
@Inject
|
||||
TimerEventListener timerEventListener;
|
||||
|
||||
@Deployment
|
||||
public static WebArchive deploy() {
|
||||
File[] jars = Maven.resolver().loadPomFromFile("pom.xml")
|
||||
.resolve("com.jayway.awaitility:awaitility")
|
||||
.withTransitivity().asFile();
|
||||
|
||||
return ShrinkWrap.create(WebArchive.class)
|
||||
.addAsLibraries(jars)
|
||||
.addClasses(WithinWindowMatcher.class, TimerEvent.class, TimerEventListener.class, ScheduleTimerBean.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_receive_three_pings() {
|
||||
|
||||
Awaitility.setDefaultTimeout(30, TimeUnit.SECONDS);
|
||||
await().untilCall(to(timerEventListener.getEvents()).size(), equalTo(3));
|
||||
|
||||
TimerEvent firstEvent = timerEventListener.getEvents().get(0);
|
||||
TimerEvent secondEvent = timerEventListener.getEvents().get(1);
|
||||
TimerEvent thirdEvent = timerEventListener.getEvents().get(2);
|
||||
|
||||
long delay = secondEvent.getTime() - firstEvent.getTime();
|
||||
assertThat(delay, Matchers.is(WithinWindowMatcher.withinWindow(TIMEOUT, TOLERANCE)));
|
||||
delay = thirdEvent.getTime() - secondEvent.getTime();
|
||||
assertThat(delay, Matchers.is(WithinWindowMatcher.withinWindow(TIMEOUT, TOLERANCE)));
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.timer;
|
||||
|
||||
import org.hamcrest.BaseMatcher;
|
||||
import org.hamcrest.Description;
|
||||
import org.hamcrest.Matcher;
|
||||
|
||||
class WithinWindowMatcher extends BaseMatcher<Long> {
|
||||
private final long timeout;
|
||||
private final long tolerance;
|
||||
|
||||
public WithinWindowMatcher(long timeout, long tolerance) {
|
||||
this.timeout = timeout;
|
||||
this.tolerance = tolerance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matches(Object item) {
|
||||
final Long actual = (Long) item;
|
||||
return Math.abs(actual - timeout) < tolerance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void describeTo(Description description) {
|
||||
//To change body of implemented methods use File | Settings | File Templates.
|
||||
}
|
||||
|
||||
public static Matcher<Long> withinWindow(final long timeout, final long tolerance) {
|
||||
return new WithinWindowMatcher(timeout, tolerance);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user