Search in sources :

Example 21 with DummyJob

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

the class NetworkJobWithConnectivityListenerTest method testNetworkJobWithConnectivityListener.

@Test
public void testNetworkJobWithConnectivityListener() throws Exception {
    final DummyNetworkUtilWithConnectivityEventSupport dummyNetworkUtil = new DummyNetworkUtilWithConnectivityEventSupport();
    final JobManager jobManager = createJobManager(new Configuration.Builder(RuntimeEnvironment.application).networkUtil(dummyNetworkUtil).timer(mockTimer));
    dummyNetworkUtil.setNetworkStatus(NetworkUtil.DISCONNECTED, true);
    final DummyJob dummyJob = new DummyJob(new Params(0).requireNetwork());
    jobManager.addJob(dummyJob);
    // no job to run so consumers should finish
    jobManager.waitUntilConsumersAreFinished();
    MatcherAssert.assertThat("count should be 1 as no jobs should be consumed w/o network", jobManager.count(), equalTo(1));
    // JobManager may wake up as idle right here and see the new network value. sleep to avoid it
    // count will trigger the queue and will result in another IDLE call. We need to wait until
    // it is handled.
    //noinspection SLEEP_IN_CODE
    Thread.sleep(2000);
    dummyNetworkUtil.setNetworkStatus(NetworkUtil.METERED, false);
    //noinspection SLEEP_IN_CODE
    //wait a little bit more to let consumer run
    Thread.sleep(5000);
    MatcherAssert.assertThat("even though network is recovered, job manager should not consume any job because it " + "does not know (we did not inform)", jobManager.count(), equalTo(1));
    waitUntilAJobIsDone(jobManager, new WaitUntilCallback() {

        @Override
        public void run() {
            dummyNetworkUtil.setNetworkStatus(NetworkUtil.METERED, true);
        }

        @Override
        public void assertJob(Job job) {
            MatcherAssert.assertThat("should be the added job", job, CoreMatchers.is((Job) dummyJob));
        }
    });
    MatcherAssert.assertThat("job manager should consume network job after it is informed that network is recovered", jobManager.count(), equalTo(0));
}
Also used : 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) DummyJob(com.birbit.android.jobqueue.test.jobs.DummyJob) Job(com.birbit.android.jobqueue.Job) Test(org.junit.Test)

Example 22 with DummyJob

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

the class SessionIdTest method testSessionId.

@Test
public void testSessionId() throws Throwable {
    JobManager jobManager = createJobManager();
    //we know job manager uses this value :/
    Long sessionId = mockTimer.nanoTime();
    jobManager.stop();
    Job[] jobs = new Job[] { new DummyJob(new Params(0)), new DummyJob(new Params(0).persist()) };
    for (Job job : jobs) {
        jobManager.addJob(job);
    }
    for (int i = 0; i < jobs.length; i++) {
        JobHolder jobHolder = nextJob(jobManager);
        MatcherAssert.assertThat("session id should be correct for job " + i, jobHolder.getRunningSessionId(), equalTo(sessionId));
    }
}
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) Job(com.birbit.android.jobqueue.Job) DummyJob(com.birbit.android.jobqueue.test.jobs.DummyJob) Test(org.junit.Test)

Example 23 with DummyJob

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

the class SingleIdTest method testSingleId.

private void testSingleId(boolean persistent) throws Throwable {
    JobManager jobManager = createJobManager();
    jobManager.stop();
    String singleId = "forks";
    DummyJob dummyJob1 = new DummyJob(new Params(0).setPersistent(persistent).setSingleId(singleId));
    String jobId1 = addJob(jobManager, dummyJob1);
    DummyJob dummyJob2 = new DummyJob(new Params(0).setPersistent(persistent));
    String jobId2 = addJob(jobManager, dummyJob2);
    assertThat("should add job if doesn't have singleId", jobManager.countReadyJobs(), is(2));
    DummyJob dummyJob3 = new DummyJob(new Params(0).setPersistent(persistent).setSingleId("otherId"));
    String jobId3 = addJob(jobManager, dummyJob3);
    assertThat("should add job if different singleId", jobManager.countReadyJobs(), is(3));
    DummyJob dummyJob4 = new DummyJob(new Params(0).setPersistent(persistent).setSingleId(singleId));
    addJob(jobManager, dummyJob4);
    assertThat("should not add job with same singleId", jobManager.countReadyJobs(), is(3));
    assertThat("should return the same id", nextJob(jobManager).getId(), is(jobId1));
    assertThat("should return the same id", nextJob(jobManager).getId(), is(jobId2));
    assertThat("should return the same id", nextJob(jobManager).getId(), is(jobId3));
    assertThat("should return the same id", nextJob(jobManager), is(nullValue()));
}
Also used : DummyJob(com.birbit.android.jobqueue.test.jobs.DummyJob) Params(com.birbit.android.jobqueue.Params) JobManager(com.birbit.android.jobqueue.JobManager)

Example 24 with DummyJob

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

the class SlowOnAddedTest method testNonPersistent.

@Test
public void testNonPersistent() throws InterruptedException {
    JobManager jobManager = createJobManager();
    CountDownLatch runLatch = new CountDownLatch(1);
    MyDummyJob job = new MyDummyJob(new Params(2), runLatch);
    for (int i = 0; i < 50; i++) {
        jobManager.addJob(new DummyJob(new Params(1)));
    }
    jobManager.addJob(job);
    runLatch.await();
    assertThat("on added should be called before on run", job.onAddedCntWhenRun, equalTo(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) RetryConstraint(com.birbit.android.jobqueue.RetryConstraint) Test(org.junit.Test)

Example 25 with DummyJob

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

the class JobParamsTest method assertParamsUnderstood.

@Test
public void assertParamsUnderstood() {
    MockTimer mockTimer = new MockTimer();
    JobHolder j1 = JobQueueTestBase.createNewJobHolder(new Params(1).requireNetwork(), mockTimer);
    assertThat("require network param should be understood properly", j1.getRequiredNetworkType(), equalTo(NetworkUtil.METERED));
    JobHolder j2 = JobQueueTestBase.createNewJobHolder(new Params(1).groupBy("blah"), mockTimer);
    assertThat("group param should be understood properly", j2.getGroupId(), equalTo("blah"));
    assertThat("require network param should be understood properly", j2.getRequiredNetworkType(), equalTo(NetworkUtil.DISCONNECTED));
    JobHolder j3 = JobQueueTestBase.createNewJobHolder(new Params(1).persist(), mockTimer);
    assertThat("persist param should be understood properly", j3.persistent, equalTo(true));
    JobHolder j4 = JobQueueTestBase.createNewJobHolder(new Params(1).setPersistent(false).setRequiresNetwork(false).setGroupId(null).setSingleId(null), mockTimer);
    assertThat("persist param should be understood properly", j4.persistent, equalTo(false));
    assertThat("require network param should be understood properly", j4.getRequiredNetworkType(), equalTo(NetworkUtil.DISCONNECTED));
    assertThat("group param should be understood properly", j4.groupId, nullValue());
    assertThat("single param should be understood properly", j4.getSingleInstanceId(), nullValue());
    mockTimer.incrementMs(2);
    DummyJob j15 = new DummyJob(new Params(1).singleInstanceBy("bloop"));
    assertThat("single param should be understood properly", j15.getSingleInstanceId(), endsWith("bloop"));
    assertThat("group param should be automatically set if single instance", j15.getRunGroupId(), notNullValue());
    mockTimer.setNow(150);
    JobHolder j6 = JobQueueTestBase.createNewJobHolder(new Params(1), mockTimer);
    assertThat("no deadline", j6.getDeadlineNs(), is(Params.FOREVER));
    JobHolder j7 = JobQueueTestBase.createNewJobHolder(new Params(1).overrideDeadlineToCancelInMs(100), mockTimer);
    assertThat("100 ms deadline", j7.getDeadlineNs(), is(100000150L));
    assertThat("100 ms deadline", j7.shouldCancelOnDeadline(), is(true));
    JobHolder j13 = JobQueueTestBase.createNewJobHolder(new Params(1).overrideDeadlineToCancelInMs(200), mockTimer);
    assertThat("100 ms deadline", j13.getDeadlineNs(), is(200000150L));
    assertThat("100 ms deadline", j7.shouldCancelOnDeadline(), is(true));
}
Also used : JobHolder(com.birbit.android.jobqueue.JobHolder) MockTimer(com.birbit.android.jobqueue.test.timer.MockTimer) DummyJob(com.birbit.android.jobqueue.test.jobs.DummyJob) Params(com.birbit.android.jobqueue.Params) Test(org.junit.Test)

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