Search in sources :

Example 66 with Params

use of com.birbit.android.jobqueue.Params in project android-priority-jobqueue by yigit.

the class KeepAliveTest method testKeepAlive.

@TargetApi(Build.VERSION_CODES.GINGERBREAD)
public void testKeepAlive(final DummyNetworkUtil networkUtil) throws Exception {
    int keepAlive = 3;
    final JobManager jobManager = createJobManager(new Configuration.Builder(RuntimeEnvironment.application).consumerKeepAlive(keepAlive).networkUtil(networkUtil).timer(mockTimer));
    //give it a little time to create first consumer
    final CountDownLatch jobDone = new CountDownLatch(1);
    jobManager.addCallback(new JobManagerCallbackAdapter() {

        @Override
        public void onDone(@NonNull Job job) {
            jobDone.countDown();
        }
    });
    jobManager.addJob(new DummyJob(new Params(0)));
    // Sync on job manager to ensure it handled add requests
    jobManager.count();
    MatcherAssert.assertThat("there should be 1 thread  actively waiting for jobs", jobManager.getActiveConsumerCount(), equalTo(1));
    MatcherAssert.assertThat(jobDone.await(1, TimeUnit.MINUTES), CoreMatchers.is(true));
    // Sync on job manager to ensure it handled add requests
    jobManager.count();
    mockTimer.incrementNs((long) (JobManager.NETWORK_CHECK_INTERVAL + TimeUnit.SECONDS.toNanos(keepAlive) + 1));
    FutureTask<Void> waitForConsumersFuture = new FutureTask<>(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            jobManager.waitUntilConsumersAreFinished();
            return null;
        }
    });
    new Thread(waitForConsumersFuture).start();
    waitForConsumersFuture.get(keepAlive * 10, TimeUnit.SECONDS);
    jobManager.waitUntilConsumersAreFinished();
    MatcherAssert.assertThat("after keep alive timeout, there should NOT be any threads waiting", jobManager.getActiveConsumerCount(), equalTo(0));
    //disable network and add a network bound job
    networkUtil.setNetworkStatus(NetworkUtil.DISCONNECTED);
    final DummyJob dj1 = new DummyJob(new Params(0).requireNetwork());
    jobManager.addJob(dj1);
    // sync add job request
    jobManager.count();
    mockTimer.incrementNs(JobManager.NETWORK_CHECK_INTERVAL + TimeUnit.SECONDS.toNanos(keepAlive) * 2);
    waitUntilAJobIsDone(jobManager, new WaitUntilCallback() {

        @Override
        public void run() {
            networkUtil.setNetworkStatus(NetworkUtil.METERED);
        }

        @Override
        public void assertJob(Job job) {
            Assert.assertThat("it should be dj1", job, is((Job) dj1));
        }
    });
    MatcherAssert.assertThat("when network is recovered, job should be handled", jobManager.count(), equalTo(0));
}
Also used : JobManagerCallbackAdapter(com.birbit.android.jobqueue.callback.JobManagerCallbackAdapter) 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) FutureTask(java.util.concurrent.FutureTask) DummyJob(com.birbit.android.jobqueue.test.jobs.DummyJob) Job(com.birbit.android.jobqueue.Job) TargetApi(android.annotation.TargetApi)

Example 67 with Params

use of com.birbit.android.jobqueue.Params in project android-priority-jobqueue by yigit.

the class LoadFactorTest method testGoIdleIfNextJobCannotBeRunNow.

@SuppressLint("SLEEP_IN_CODE")
@Test
public void testGoIdleIfNextJobCannotBeRunNow() throws InterruptedException {
    // see: https://github.com/yigit/android-priority-jobqueue/issues/262
    final AtomicInteger nextJobDelayCall = new AtomicInteger(1);
    JobManager jobManager = createJobManager(new Configuration.Builder(RuntimeEnvironment.application).maxConsumerCount(3).minConsumerCount(1).loadFactor(3).queueFactory(new QueueFactory() {

        @Override
        public JobQueue createPersistentQueue(Configuration configuration, long sessionId) {
            return new SqliteJobQueue(configuration, sessionId, new SqliteJobQueue.JavaSerializer());
        }

        @Override
        public JobQueue createNonPersistent(Configuration configuration, long sessionId) {
            return new SimpleInMemoryPriorityQueue(configuration, sessionId) {

                @Override
                public Long getNextJobDelayUntilNs(@NonNull Constraint constraint) {
                    nextJobDelayCall.incrementAndGet();
                    return super.getNextJobDelayUntilNs(constraint);
                }
            };
        }
    }).timer(mockTimer));
    final DummyJobWithStartEndLatch job1 = new DummyJobWithStartEndLatch(new Params(1));
    final DummyJobWithStartEndLatch job2 = new DummyJobWithStartEndLatch(new Params(1));
    jobManager.addJob(job1);
    jobManager.addJob(job2);
    assertThat(startLatch.await(5, TimeUnit.MINUTES), is(true));
    // give it some time to cool down, ugly but nothing to do
    Thread.sleep(2000);
    int startCount = nextJobDelayCall.get();
    Thread.sleep(5000);
    assertThat("JobManager should not query any more next jobs", nextJobDelayCall.get(), is(startCount));
    waitUntilAJobIsDone(jobManager, new WaitUntilCallback() {

        @Override
        public void run() {
            canEndLatch.countDown();
        }

        @Override
        public void assertJob(Job job) {
        }
    });
}
Also used : Configuration(com.birbit.android.jobqueue.config.Configuration) JobQueue(com.birbit.android.jobqueue.JobQueue) SqliteJobQueue(com.birbit.android.jobqueue.persistentQueue.sqlite.SqliteJobQueue) Constraint(com.birbit.android.jobqueue.Constraint) Params(com.birbit.android.jobqueue.Params) JobManager(com.birbit.android.jobqueue.JobManager) QueueFactory(com.birbit.android.jobqueue.QueueFactory) SuppressLint(android.annotation.SuppressLint) Constraint(com.birbit.android.jobqueue.Constraint) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SqliteJobQueue(com.birbit.android.jobqueue.persistentQueue.sqlite.SqliteJobQueue) SimpleInMemoryPriorityQueue(com.birbit.android.jobqueue.inMemoryQueue.SimpleInMemoryPriorityQueue) DummyJob(com.birbit.android.jobqueue.test.jobs.DummyJob) Job(com.birbit.android.jobqueue.Job) Test(org.junit.Test) SuppressLint(android.annotation.SuppressLint)

Example 68 with Params

use of com.birbit.android.jobqueue.Params in project android-priority-jobqueue by yigit.

the class NetworkJobTest method testNetworkJobWithTimeout.

@Test
public void testNetworkJobWithTimeout() throws InterruptedException {
    JobManagerTestBase.DummyNetworkUtil dummyNetworkUtil = new JobManagerTestBase.DummyNetworkUtil();
    dummyNetworkUtil.setNetworkStatus(NetworkUtil.DISCONNECTED);
    final JobManager jobManager = createJobManager(new Configuration.Builder(RuntimeEnvironment.application).networkUtil(dummyNetworkUtil).timer(mockTimer));
    final CountDownLatch runLatch = new CountDownLatch(1);
    DummyJob networkDummyJob = new DummyJob(addRequirement(new Params(1), 4)) {

        @Override
        public void onRun() throws Throwable {
            runLatch.countDown();
            super.onRun();
        }
    };
    jobManager.addJob(networkDummyJob);
    MatcherAssert.assertThat("job should not run", runLatch.await(3, TimeUnit.SECONDS), is(false));
    mockTimer.incrementMs(4);
    MatcherAssert.assertThat("job should run because network wait timed out", runLatch.await(3, TimeUnit.SECONDS), is(true));
}
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) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 69 with Params

use of com.birbit.android.jobqueue.Params in project android-priority-jobqueue by yigit.

the class NetworkJobTest method testPersistentNetworkJobWithTimeout.

@Test
public void testPersistentNetworkJobWithTimeout() throws InterruptedException {
    JobManagerTestBase.DummyNetworkUtil dummyNetworkUtil = new JobManagerTestBase.DummyNetworkUtil();
    dummyNetworkUtil.setNetworkStatus(NetworkUtil.DISCONNECTED);
    final JobManager jobManager = createJobManager(new Configuration.Builder(RuntimeEnvironment.application).networkUtil(dummyNetworkUtil).timer(mockTimer));
    PersistentDummyJob networkDummyJob = new PersistentDummyJob(addRequirement(new Params(1), 4));
    jobManager.addJob(networkDummyJob);
    MatcherAssert.assertThat("job should not run", persistentDummyJobRunLatch.await(3, TimeUnit.SECONDS), is(false));
    mockTimer.incrementMs(4);
    MatcherAssert.assertThat("job should run because network wait timed out", persistentDummyJobRunLatch.await(3, TimeUnit.SECONDS), is(true));
}
Also used : Configuration(com.birbit.android.jobqueue.config.Configuration) Params(com.birbit.android.jobqueue.Params) JobManager(com.birbit.android.jobqueue.JobManager) Test(org.junit.Test)

Example 70 with Params

use of com.birbit.android.jobqueue.Params in project android-priority-jobqueue by yigit.

the class NetworkJobTest method testNetworkJob.

@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@Test
public void testNetworkJob() throws Exception {
    enableDebug();
    JobManagerTestBase.DummyNetworkUtil dummyNetworkUtil = new JobManagerTestBase.DummyNetworkUtil();
    final JobManager jobManager = createJobManager(new Configuration.Builder(RuntimeEnvironment.application).networkUtil(dummyNetworkUtil).timer(mockTimer));
    jobManager.stop();
    DummyJob networkDummyJob = new DummyJob(addRequirement(new Params(5)));
    jobManager.addJob(networkDummyJob);
    DummyJob noNetworkDummyJob = new DummyJob(new Params(2));
    jobManager.addJob(noNetworkDummyJob);
    DummyJob networkPersistentJob = new DummyJob(addRequirement(new Params(6).persist()));
    jobManager.addJob(networkPersistentJob);
    DummyJob noNetworkPersistentJob = new DummyJob(new Params(1).persist());
    jobManager.addJob(noNetworkPersistentJob);
    MatcherAssert.assertThat("count should be correct if there are network and non-network jobs w/o network", jobManager.count(), equalTo(4));
    dummyNetworkUtil.setNetworkStatus(NetworkUtil.METERED);
    MatcherAssert.assertThat("count should be correct if there is network and non-network jobs w/o network", jobManager.count(), equalTo(4));
    dummyNetworkUtil.setNetworkStatus(NetworkUtil.DISCONNECTED);
    final CountDownLatch noNetworkLatch = new CountDownLatch(2);
    jobManager.addCallback(new JobManagerCallbackAdapter() {

        @Override
        public void onAfterJobRun(@NonNull Job job, int resultCode) {
            if (resultCode == JobManagerCallback.RESULT_SUCCEED) {
                MatcherAssert.assertThat("should be a no network job", job.requiresNetwork(), is(false));
                noNetworkLatch.countDown();
                if (noNetworkLatch.getCount() == 0) {
                    jobManager.removeCallback(this);
                }
            }
        }
    });
    jobManager.start();
    MatcherAssert.assertThat(noNetworkLatch.await(1, TimeUnit.MINUTES), is(true));
    MatcherAssert.assertThat("no network jobs should be executed even if there is no network", jobManager.count(), equalTo(2));
    final CountDownLatch networkLatch = new CountDownLatch(2);
    jobManager.addCallback(new JobManagerCallbackAdapter() {

        @Override
        public void onAfterJobRun(@NonNull Job job, int resultCode) {
            if (resultCode == JobManagerCallback.RESULT_SUCCEED) {
                MatcherAssert.assertThat("should be a network job", job.requiresNetwork(), is(true));
                networkLatch.countDown();
                if (networkLatch.getCount() == 0) {
                    jobManager.removeCallback(this);
                }
            }
        }
    });
    dummyNetworkUtil.setNetworkStatus(NetworkUtil.METERED);
    // network check delay, make public?
    mockTimer.incrementMs(10000);
    if (unmetered) {
        MatcherAssert.assertThat("if jobs require unmetered, they should not be run", networkLatch.await(10, TimeUnit.SECONDS), is(false));
        MatcherAssert.assertThat(networkLatch.getCount(), is(2L));
        dummyNetworkUtil.setNetworkStatus(NetworkUtil.UNMETERED);
        // network check delay
        mockTimer.incrementMs(10000);
    }
    MatcherAssert.assertThat(networkLatch.await(1, TimeUnit.MINUTES), is(true));
    MatcherAssert.assertThat("when network is recovered, all network jobs should be automatically consumed", jobManager.count(), equalTo(0));
}
Also used : JobManagerCallbackAdapter(com.birbit.android.jobqueue.callback.JobManagerCallbackAdapter) Configuration(com.birbit.android.jobqueue.config.Configuration) Params(com.birbit.android.jobqueue.Params) JobManager(com.birbit.android.jobqueue.JobManager) CountDownLatch(java.util.concurrent.CountDownLatch) RetryConstraint(com.birbit.android.jobqueue.RetryConstraint) DummyJob(com.birbit.android.jobqueue.test.jobs.DummyJob) DummyJob(com.birbit.android.jobqueue.test.jobs.DummyJob) Job(com.birbit.android.jobqueue.Job) Test(org.junit.Test) TargetApi(android.annotation.TargetApi)

Aggregations

Params (com.birbit.android.jobqueue.Params)95 Test (org.junit.Test)75 JobManager (com.birbit.android.jobqueue.JobManager)47 JobHolder (com.birbit.android.jobqueue.JobHolder)46 DummyJob (com.birbit.android.jobqueue.test.jobs.DummyJob)41 JobQueue (com.birbit.android.jobqueue.JobQueue)40 TestConstraint (com.birbit.android.jobqueue.TestConstraint)37 Job (com.birbit.android.jobqueue.Job)31 CountDownLatch (java.util.concurrent.CountDownLatch)31 Configuration (com.birbit.android.jobqueue.config.Configuration)17 RetryConstraint (com.birbit.android.jobqueue.RetryConstraint)12 TargetApi (android.annotation.TargetApi)9 CancelResult (com.birbit.android.jobqueue.CancelResult)9 Constraint (com.birbit.android.jobqueue.Constraint)8 TagConstraint (com.birbit.android.jobqueue.TagConstraint)8 JobManagerCallbackAdapter (com.birbit.android.jobqueue.callback.JobManagerCallbackAdapter)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 ArrayList (java.util.ArrayList)6 JobManagerCallback (com.birbit.android.jobqueue.callback.JobManagerCallback)4 SqliteJobQueue (com.birbit.android.jobqueue.persistentQueue.sqlite.SqliteJobQueue)3