Skip to content

Commit 869df80

Browse files
committed
add test for new calculation of remaing delay for serial background tasks
1 parent 9286175 commit 869df80

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

‎AndroidAnnotations/androidannotations-core/androidannotations-test/src/main/java/org/androidannotations/test/ThreadActivity.java‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
@EActivity
3535
public class ThreadActivity extends Activity {
3636

37+
public static final long SERIAL_DELAY = 1000L;
38+
public boolean calledDelayed = false;
39+
3740
@Override
3841
public void onCreate(Bundle savedInstanceState) {
3942
super.onCreate(savedInstanceState);
@@ -151,4 +154,15 @@ void backgroundThrowException() {
151154
void uiThreadThrowException() {
152155
throw new RuntimeException();
153156
}
157+
158+
@Background(serial = "foo")
159+
protected void callDelayedSerial(Runnable callback) {
160+
delayedSerial(System.currentTimeMillis(), callback);
161+
}
162+
163+
@Background(serial = "foo", id = "delayedTask", delay = SERIAL_DELAY)
164+
protected void delayedSerial(final long execTime, Runnable callback) {
165+
calledDelayed = System.currentTimeMillis() - execTime >= SERIAL_DELAY;
166+
callback.run();
167+
}
154168
}

‎AndroidAnnotations/androidannotations-core/androidannotations-test/src/test/java/org/androidannotations/test/ThreadActivityTest.java‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,22 @@ public void uncaughtException(Thread thread, Throwable ex) {
364364
Assert.assertTrue("Exception should have been caught in the DefaultUncaughtExceptionHandler during @Background call.", propagatedExceptionToGlobalExceptionHandler);
365365
}
366366

367+
@Test
368+
public void testDelayedSerial() {
369+
BackgroundExecutor.setExecutor(Executors.newScheduledThreadPool(1));
370+
371+
final Object LOCK = new Object();
372+
activity.callDelayedSerial(new Runnable() {
373+
@Override
374+
public void run() {
375+
LOCK.notify();
376+
}
377+
});
378+
Assert.assertFalse(activity.calledDelayed);
379+
waitOn(LOCK, ThreadActivity.SERIAL_DELAY * 2);
380+
Assert.assertTrue(activity.calledDelayed);
381+
}
382+
367383
/**
368384
* Call wait() on the given object with the specified timeout. Avoid
369385
* boilerplate code like synchronized or try..catch.

0 commit comments

Comments
 (0)