Search in sources :

Example 11 with DummyJob

use of com.birbit.android.jobqueue.test.jobs.DummyJob in project android-priority-jobqueue by yigit.

the class DelayTest method testDelay.

public void testDelay(boolean persist) throws Throwable {
    JobManager jobManager = createJobManager();
    jobManager.stop();
    DummyJob delayedJob = new DummyJob(new Params(10).delayInMs(1000).setPersistent(persist));
    DummyJob nonDelayedJob = new DummyJob(new Params(0).setPersistent(persist));
    jobManager.addJob(delayedJob);
    jobManager.addJob(nonDelayedJob);
    JobHolder receivedJob = nextJob(jobManager);
    MatcherAssert.assertThat("non-delayed job should be served", receivedJob, notNullValue());
    MatcherAssert.assertThat("non-delayed job should id should match", receivedJob.getId(), equalTo(nonDelayedJob.getId()));
    removeJob(jobManager, receivedJob);
    MatcherAssert.assertThat("delayed job should not be served", nextJob(jobManager), nullValue());
    MatcherAssert.assertThat("job count should still be 1", jobManager.count(), equalTo(1));
    mockTimer.incrementMs(500);
    MatcherAssert.assertThat("delayed job should not be served", nextJob(jobManager), nullValue());
    MatcherAssert.assertThat("job count should still be 1", jobManager.count(), equalTo(1));
    mockTimer.incrementMs(2000);
    MatcherAssert.assertThat("job count should still be 1", jobManager.count(), equalTo(1));
    receivedJob = nextJob(jobManager);
    MatcherAssert.assertThat("now should be able to receive the delayed job.", receivedJob, notNullValue());
    if (receivedJob != null) {
        MatcherAssert.assertThat("received job should be the delayed job", receivedJob.getId(), equalTo(delayedJob.getId()));
    }
}
Also used : JobHolder(com.birbit.android.jobqueue.JobHolder) DummyJob(com.birbit.android.jobqueue.test.jobs.DummyJob) Params(com.birbit.android.jobqueue.Params) JobManager(com.birbit.android.jobqueue.JobManager)

Example 12 with DummyJob

use of com.birbit.android.jobqueue.test.jobs.DummyJob in project android-priority-jobqueue by yigit.

the class GroupingTest method testGrouping.

@Test
public void testGrouping() throws Throwable {
    JobManager jobManager = createJobManager();
    jobManager.stop();
    String jobId1 = addJob(jobManager, new DummyJob(new Params(0).groupBy("group1")));
    String jobId2 = addJob(jobManager, new DummyJob(new Params(0).groupBy("group1")));
    String jobId3 = addJob(jobManager, new DummyJob(new Params(0).persist().groupBy("group2")));
    String jobId4 = addJob(jobManager, new DummyJob(new Params(0).persist().groupBy("group1")));
    JobHolder nextJob = nextJob(jobManager);
    MatcherAssert.assertThat("next job should be the first job from group1", nextJob.getId(), equalTo(jobId1));
    JobHolder group2Job = nextJob(jobManager, Collections.singletonList("group1"));
    MatcherAssert.assertThat("since group 1 is running now, next job should be from group 2", group2Job.getId(), equalTo(jobId3));
    removeJob(jobManager, nextJob);
    JobHolder group1NextJob = nextJob(jobManager, Arrays.asList("group2"));
    MatcherAssert.assertThat("after removing job from group 1, another job from group1 should be returned", group1NextJob.getId(), equalTo(jobId2));
    MatcherAssert.assertThat("when jobs from both groups are running, no job should be returned from next job", nextJob(jobManager, Arrays.asList("group1", "group2")), is(nullValue()));
    removeJob(jobManager, group2Job);
    MatcherAssert.assertThat("even after group2 job is complete, no jobs should be returned" + "since we only have group1 jobs left", nextJob(jobManager, Arrays.asList("group1")), is(nullValue()));
}
Also used : JobHolder(com.birbit.android.jobqueue.JobHolder) DummyJob(com.birbit.android.jobqueue.test.jobs.DummyJob) Params(com.birbit.android.jobqueue.Params) JobManager(com.birbit.android.jobqueue.JobManager) Test(org.junit.Test)

Example 13 with DummyJob

use of com.birbit.android.jobqueue.test.jobs.DummyJob in project android-priority-jobqueue by yigit.

the class NetworkNextJobTest method testNetworkNextJob.

@Test
public void testNetworkNextJob() throws Throwable {
    DummyNetworkUtil dummyNetworkUtil = new DummyNetworkUtil();
    JobManager jobManager = createJobManager(new Configuration.Builder(RuntimeEnvironment.application).networkUtil(dummyNetworkUtil).timer(mockTimer));
    jobManager.stop();
    DummyJob dummyJob = new DummyJob(new Params(0).requireNetwork());
    jobManager.addJob(dummyJob);
    dummyNetworkUtil.setNetworkStatus(NetworkUtil.DISCONNECTED);
    MatcherAssert.assertThat("when there isn't any network, next job should return null", nextJob(jobManager), nullValue());
    MatcherAssert.assertThat("even if there is network, job manager should return correct count", jobManager.count(), equalTo(1));
    dummyNetworkUtil.setNetworkStatus(NetworkUtil.METERED);
    JobHolder retrieved = nextJob(jobManager);
    MatcherAssert.assertThat("when network is recovered, next job should be retrieved", retrieved, notNullValue());
}
Also used : JobHolder(com.birbit.android.jobqueue.JobHolder) Configuration(com.birbit.android.jobqueue.config.Configuration) DummyJob(com.birbit.android.jobqueue.test.jobs.DummyJob) Params(com.birbit.android.jobqueue.Params) JobManager(com.birbit.android.jobqueue.JobManager) Test(org.junit.Test)

Example 14 with DummyJob

use of com.birbit.android.jobqueue.test.jobs.DummyJob in project android-priority-jobqueue by yigit.

the class NonSerializableJobTest method test.

@Test
public void test() throws InterruptedException {
    final Throwable[] throwable = new Throwable[1];
    JobManager jobManager = createJobManager();
    final CountDownLatch latch = new CountDownLatch(1);
    jobManager.getJobManagerExecutionThread().setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {

        @Override
        public void uncaughtException(Thread thread, Throwable ex) {
            throwable[0] = ex;
            latch.countDown();
        }
    });
    jobManager.addJobInBackground(new DummyJob(new Params(0).persist()) {

        ICannotBeSerialized iCannotBeSerialized = new ICannotBeSerialized();
    });
    MatcherAssert.assertThat(latch.await(30, TimeUnit.SECONDS), CoreMatchers.is(true));
    MatcherAssert.assertThat(throwable[0] instanceof RuntimeException, CoreMatchers.is(true));
    MatcherAssert.assertThat(throwable[0].getCause() instanceof IOException, CoreMatchers.is(true));
}
Also used : DummyJob(com.birbit.android.jobqueue.test.jobs.DummyJob) Params(com.birbit.android.jobqueue.Params) JobManager(com.birbit.android.jobqueue.JobManager) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 15 with DummyJob

use of com.birbit.android.jobqueue.test.jobs.DummyJob in project android-priority-jobqueue by yigit.

the class SingleIdTest method testSingleIdRunning.

private void testSingleIdRunning(boolean persistent) throws InterruptedException {
    JobManager jobManager = createJobManager();
    String singleId = "dorks";
    CountDownLatch latchWait = new CountDownLatch(1);
    CountDownLatch latchRunning = new CountDownLatch(1);
    DummyJob dummyJob1 = new SerializableDummyTwoLatchJob(new Params(0).setPersistent(persistent).setSingleId(singleId).setGroupId(singleId), latchWait, latchRunning);
    addJob(jobManager, dummyJob1);
    jobManager.start();
    //let job1 start running
    latchRunning.await(5, TimeUnit.SECONDS);
    jobManager.stop();
    assertThat("should not be marked ready", jobManager.count(), is(0));
    CountDownLatch latchRunning2 = new CountDownLatch(1);
    DummyJob dummyJob2 = new SerializableDummyLatchJob(new Params(0).setPersistent(persistent).setSingleId(singleId).setGroupId(singleId), latchRunning2);
    addJob(jobManager, dummyJob2);
    assertThat("should add new job if first job was running", jobManager.count(), is(1));
    DummyJob dummyJob3 = new DummyJob(new Params(0).setPersistent(persistent).setSingleId(singleId).setGroupId(singleId));
    addJob(jobManager, dummyJob3);
    assertThat("should not add new job if already queued", jobManager.count(), is(1));
    //let job1 finish
    latchWait.countDown();
    jobManager.start();
    //wait until job2 runs
    assertThat("job should have run", latchRunning2.await(5, TimeUnit.SECONDS), is(true));
    jobManager.stopAndWaitUntilConsumersAreFinished();
    assertThat("job should not have run", dummyJob3.getOnRunCnt(), is(0));
    assertThat("should have called onCancel", dummyJob3.getOnCancelCnt(), is(1));
    DummyJob dummyJob4 = new DummyJob(new Params(0).setPersistent(persistent).setSingleId(singleId).setGroupId(singleId));
    addJob(jobManager, dummyJob4);
    assertThat("should be added if all others have run", jobManager.count(), is(1));
}
Also used : DummyJob(com.birbit.android.jobqueue.test.jobs.DummyJob) Params(com.birbit.android.jobqueue.Params) JobManager(com.birbit.android.jobqueue.JobManager) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

DummyJob (com.birbit.android.jobqueue.test.jobs.DummyJob)34 Params (com.birbit.android.jobqueue.Params)33 JobManager (com.birbit.android.jobqueue.JobManager)31 Test (org.junit.Test)23 CountDownLatch (java.util.concurrent.CountDownLatch)18 Job (com.birbit.android.jobqueue.Job)16 Configuration (com.birbit.android.jobqueue.config.Configuration)11 CancelResult (com.birbit.android.jobqueue.CancelResult)7 JobHolder (com.birbit.android.jobqueue.JobHolder)7 JobManagerCallbackAdapter (com.birbit.android.jobqueue.callback.JobManagerCallbackAdapter)6 TargetApi (android.annotation.TargetApi)5 RetryConstraint (com.birbit.android.jobqueue.RetryConstraint)3 ArrayList (java.util.ArrayList)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 Semaphore (java.util.concurrent.Semaphore)2 SuppressLint (android.annotation.SuppressLint)1 NonNull (android.support.annotation.NonNull)1 Nullable (android.support.annotation.Nullable)1 AsyncAddCallback (com.birbit.android.jobqueue.AsyncAddCallback)1 CancelReason (com.birbit.android.jobqueue.CancelReason)1