Search in sources :

Example 6 with DummyJob

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

the class CancelWithNetworkToggleTest method testCancelWithoutNetwork.

public void testCancelWithoutNetwork(boolean async, TagConstraint constraint) throws InterruptedException {
    DummyNetworkUtilWithConnectivityEventSupport networkUtil = new DummyNetworkUtilWithConnectivityEventSupport();
    JobManager jobManager = createJobManager(new Configuration.Builder(RuntimeEnvironment.application).minConsumerCount(5).networkUtil(networkUtil).timer(mockTimer));
    networkUtil.setNetworkStatus(NetworkUtil.DISCONNECTED, true);
    jobManager.addJob(new DummyJob(new Params(1).requireNetwork().groupBy("group").addTags("tag")));
    jobManager.addJob(new DummyJob(new Params(2).requireNetwork().groupBy("group").addTags("tag")));
    jobManager.addJob(new DummyJob(new Params(3).requireNetwork().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 = new CountDownLatch(1);
    jobManager.addJob(new DummyJob(new Params(1).requireNetwork().groupBy("group").addTags("tag")) {

        @Override
        public void onRun() throws Throwable {
            super.onRun();
            runLatch.countDown();
        }
    });
    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) 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 7 with DummyJob

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

the class CancelWithNetworkToggleTest method testCancelWithoutNetworkPersistent.

public void testCancelWithoutNetworkPersistent(boolean async, TagConstraint constraint) throws InterruptedException {
    DummyNetworkUtilWithConnectivityEventSupport networkUtil = new DummyNetworkUtilWithConnectivityEventSupport();
    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().requireNetwork().groupBy("group").addTags("tag")));
    jobManager.addJob(new DummyJob(new Params(2).persist().requireNetwork().groupBy("group").addTags("tag")));
    jobManager.addJob(new DummyJob(new Params(3).persist().requireNetwork().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().requireNetwork().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 8 with DummyJob

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

the class ClearTest method testClear.

@Test
public void testClear() throws Exception {
    JobManager jobManager = createJobManager();
    final int LIMIT = 20;
    for (int i = 0; i < LIMIT; i++) {
        jobManager.addJob(new DummyJob(new Params(0).setPersistent(i % 2 == 1)));
    }
    jobManager.clear();
    MatcherAssert.assertThat("after clear, count should be 0", jobManager.count(), equalTo(0));
}
Also used : 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 9 with DummyJob

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

the class ConsumerCountTest method testMaxConsumerCount.

@Test
public void testMaxConsumerCount() throws Exception {
    int maxConsumerCount = 2;
    JobManager jobManager = createJobManager(new Configuration.Builder(RuntimeEnvironment.application).maxConsumerCount(maxConsumerCount).loadFactor(maxConsumerCount).timer(mockTimer));
    Semaphore semaphore = new Semaphore(maxConsumerCount);
    int totalJobCount = maxConsumerCount * 3;
    List<NeverEndingDummyJob> runningJobs = new ArrayList<>(totalJobCount);
    for (int i = 0; i < totalJobCount; i++) {
        NeverEndingDummyJob job = new NeverEndingDummyJob(new Params((int) (Math.random() * 3)), runLock, semaphore);
        runningJobs.add(job);
        jobManager.addJob(job);
    }
    Timer timer = new SystemTimer();
    //wait till enough jobs start
    long start = timer.nanoTime();
    long tenSeconds = TimeUnit.SECONDS.toNanos(10);
    while (timer.nanoTime() - start < tenSeconds && semaphore.tryAcquire()) {
        semaphore.release();
        //noinspection SLEEP_IN_CODE
        Thread.sleep(100);
    }
    MatcherAssert.assertThat("all consumers should start in 10 seconds", timer.nanoTime() - start > tenSeconds, is(false));
    //wait some more to ensure no more jobs are started
    //noinspection SLEEP_IN_CODE
    Thread.sleep(TimeUnit.SECONDS.toMillis(3));
    int totalRunningCount = 0;
    for (DummyJob job : runningJobs) {
        totalRunningCount += job.getOnRunCnt();
    }
    MatcherAssert.assertThat("only maxConsumerCount jobs should start", totalRunningCount, equalTo(maxConsumerCount));
    waitUntilJobsAreDone(jobManager, runningJobs, new Runnable() {

        @Override
        public void run() {
            runLock.countDown();
        }
    });
    MatcherAssert.assertThat("no jobs should remain", jobManager.count(), equalTo(0));
}
Also used : ArrayList(java.util.ArrayList) Params(com.birbit.android.jobqueue.Params) JobManager(com.birbit.android.jobqueue.JobManager) Semaphore(java.util.concurrent.Semaphore) SystemTimer(com.birbit.android.jobqueue.timer.SystemTimer) Timer(com.birbit.android.jobqueue.timer.Timer) DummyJob(com.birbit.android.jobqueue.test.jobs.DummyJob) SystemTimer(com.birbit.android.jobqueue.timer.SystemTimer) Test(org.junit.Test)

Example 10 with DummyJob

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

the class CountTest method testCount.

@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@Test
public void testCount() throws Exception {
    enableDebug();
    JobManager jobManager = createJobManager();
    jobManager.stop();
    for (int i = 0; i < 10; i++) {
        jobManager.addJob(new DummyJob(new Params(0).persist()));
        MatcherAssert.assertThat((int) jobManager.count(), equalTo(i * 2 + 1));
        jobManager.addJob(new DummyJob(new Params(0).persist()));
        MatcherAssert.assertThat((int) jobManager.count(), equalTo(i * 2 + 2));
    }
    final CountDownLatch jobsToRun = new CountDownLatch(20);
    jobManager.addCallback(new JobManagerCallbackAdapter() {

        @Override
        public void onAfterJobRun(@NonNull Job job, int resultCode) {
            if (resultCode == JobManagerCallback.RESULT_SUCCEED) {
                jobsToRun.countDown();
            }
        }
    });
    jobManager.start();
    MatcherAssert.assertThat("test sanity", jobsToRun.await(1, TimeUnit.MINUTES), is(true));
    MatcherAssert.assertThat((int) jobManager.count(), equalTo(0));
}
Also used : JobManagerCallbackAdapter(com.birbit.android.jobqueue.callback.JobManagerCallbackAdapter) 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) DummyJob(com.birbit.android.jobqueue.test.jobs.DummyJob) Job(com.birbit.android.jobqueue.Job) Test(org.junit.Test) TargetApi(android.annotation.TargetApi)

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