Search in sources :

Example 31 with DummyJob

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

the class CancelFailingJobsTest method testCancelWithoutNetworkPersistent.

public void testCancelWithoutNetworkPersistent(boolean async, TagConstraint constraint) throws InterruptedException {
    JobManager jobManager = createJobManager(new Configuration.Builder(RuntimeEnvironment.application).minConsumerCount(5).networkUtil(networkUtil));
    networkUtil.setNetworkStatus(NetworkUtil.DISCONNECTED, true);
    jobManager.addJob(new DummyJob(new Params(1).persist().groupBy("group").addTags("tag")));
    jobManager.addJob(new DummyJob(new Params(2).persist().groupBy("group").addTags("tag")));
    jobManager.addJob(new DummyJob(new Params(3).persist().groupBy("group").addTags("tag")));
    final CancelResult[] result = new CancelResult[1];
    if (async) {
        final CountDownLatch cancelLatch = new CountDownLatch(1);
        jobManager.cancelJobsInBackground(new CancelResult.AsyncCancelCallback() {

            @Override
            public void onCancelled(CancelResult cancelResult) {
                result[0] = cancelResult;
                cancelLatch.countDown();
            }
        }, constraint, "tag");
        cancelLatch.await(2, TimeUnit.SECONDS);
    } else {
        result[0] = jobManager.cancelJobs(TagConstraint.ANY, "tag");
    }
    assertThat("all jobs should be canceled", result[0].getCancelledJobs().size(), is(3));
    assertThat("no jobs should fail to cancel", result[0].getFailedToCancel().size(), is(0));
    final CountDownLatch runLatch = persistentLatches[latchCounter++];
    jobManager.addJob(new PersistentDummyJob(new Params(3).persist().groupBy("group").addTags("tag"), latchCounter - 1));
    networkUtil.setNetworkStatus(NetworkUtil.METERED, true);
    assertThat("new job should run w/o any issues", runLatch.await(2, TimeUnit.SECONDS), is(true));
}
Also used : CancelResult(com.birbit.android.jobqueue.CancelResult) 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) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 32 with DummyJob

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

the class CancelFailingJobsTest method testCancelWithoutNetwork.

public void testCancelWithoutNetwork(boolean async, TagConstraint constraint) throws InterruptedException {
    final int jobCount = 30;
    JobManager jobManager = createJobManager(new Configuration.Builder(RuntimeEnvironment.application).minConsumerCount(5).networkUtil(networkUtil).timer(mockTimer));
    networkUtil.setNetworkStatus(NetworkUtil.DISCONNECTED, true);
    for (int i = 0; i < jobCount; i++) {
        jobManager.addJob(new FailingJob(new Params(i).groupBy("group").addTags("tag")));
    }
    final CancelResult[] result = new CancelResult[1];
    if (async) {
        final CountDownLatch cancelLatch = new CountDownLatch(1);
        jobManager.cancelJobsInBackground(new CancelResult.AsyncCancelCallback() {

            @Override
            public void onCancelled(CancelResult cancelResult) {
                result[0] = cancelResult;
                cancelLatch.countDown();
            }
        }, constraint, "tag");
        assertThat(cancelLatch.await(jobCount, TimeUnit.SECONDS), is(true));
    } else {
        result[0] = jobManager.cancelJobs(TagConstraint.ANY, "tag");
    }
    assertThat("all jobs should be cancelled", result[0].getCancelledJobs().size(), is(jobCount));
    assertThat("no jobs should fail to cancel", result[0].getFailedToCancel().size(), is(0));
    final CountDownLatch runLatch = new CountDownLatch(1);
    jobManager.addJob(new DummyJob(new Params(1).groupBy("group").addTags("tag")) {

        @Override
        public void onRun() throws Throwable {
            super.onRun();
            runLatch.countDown();
        }
    });
    networkUtil.setNetworkStatus(NetworkUtil.UNMETERED, true);
    assertThat("new job should run w/o any issues", runLatch.await(2, TimeUnit.SECONDS), is(true));
}
Also used : CancelResult(com.birbit.android.jobqueue.CancelResult) 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) TagConstraint(com.birbit.android.jobqueue.TagConstraint)

Example 33 with DummyJob

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

the class AddInBackgroundTest method addInBackground.

@Test
public void addInBackground() throws Throwable {
    long currentThreadId = Thread.currentThread().getId();
    final AtomicLong onAddedThreadId = new AtomicLong();
    final CountDownLatch addedLatch = new CountDownLatch(2);
    final Job dummyJob = new DummyJob(new Params(1).setDelayMs(delayed ? 1000 : 0)) {

        @Override
        public void onAdded() {
            super.onAdded();
            onAddedThreadId.set(Thread.currentThread().getId());
            addedLatch.countDown();
        }
    };
    JobManager jobManager = createJobManager();
    jobManager.stop();
    if (useCallback) {
        jobManager.addJobInBackground(dummyJob, new AsyncAddCallback() {

            @Override
            public void onAdded() {
                addedLatch.countDown();
            }
        });
    } else {
        addedLatch.countDown();
        jobManager.addJobInBackground(dummyJob);
    }
    addedLatch.await();
    MatcherAssert.assertThat("thread ids should be different. delayed:" + delayed, currentThreadId, CoreMatchers.not(onAddedThreadId.get()));
    if (useCallback) {
        JobHolder holder = new JobManagerThreadRunnable<JobHolder>(jobManager) {

            @Override
            public JobHolder onRun() {
                return findJobFromQueues(dummyJob.getId());
            }
        }.run();
        MatcherAssert.assertThat("there should be a job in the holder. id:" + dummyJob.getId() + ", delayed:" + delayed, holder, CoreMatchers.notNullValue());
        MatcherAssert.assertThat("id callback should have the proper id:", holder.getJob(), CoreMatchers.is(dummyJob));
    }
}
Also used : JobHolder(com.birbit.android.jobqueue.JobHolder) AtomicLong(java.util.concurrent.atomic.AtomicLong) DummyJob(com.birbit.android.jobqueue.test.jobs.DummyJob) Params(com.birbit.android.jobqueue.Params) JobManager(com.birbit.android.jobqueue.JobManager) AsyncAddCallback(com.birbit.android.jobqueue.AsyncAddCallback) CountDownLatch(java.util.concurrent.CountDownLatch) DummyJob(com.birbit.android.jobqueue.test.jobs.DummyJob) Job(com.birbit.android.jobqueue.Job) Test(org.junit.Test)

Example 34 with DummyJob

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

the class JobQueueTestBase method createNewJobHolder.

public static JobHolder createNewJobHolder(Params params, Timer timer) {
    long delay = getDelayMsField(params).get();
    long deadline = getDeadlineMsField(params).get();
    boolean cancelOnDeadline = Boolean.TRUE.equals(getCancelOnDeadlineDeadlineField(params).get());
    DummyJob job = new DummyJob(params);
    //noinspection WrongConstant
    return new JobHolder.Builder().priority(params.getPriority()).groupId(params.getGroupId()).job(job).id(job.getId()).persistent(params.isPersistent()).tags(job.getTags()).createdNs(timer.nanoTime()).deadline(deadline > 0 ? timer.nanoTime() + deadline * JobManager.NS_PER_MS : Params.FOREVER, cancelOnDeadline).delayUntilNs(delay > 0 ? timer.nanoTime() + delay * JobManager.NS_PER_MS : JobManager.NOT_DELAYED_JOB_DELAY).requiredNetworkType(getNetworkTypeField(params).get()).runningSessionId(JobManager.NOT_RUNNING_SESSION_ID).build();
}
Also used : DummyJob(com.birbit.android.jobqueue.test.jobs.DummyJob)

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