use of com.birbit.android.jobqueue.CancelResult in project android-priority-jobqueue by yigit.
the class MultiThreadTest method testMultiThreaded.
@Test
public void testMultiThreaded() throws Exception {
multiThreadedJobCounter = new AtomicInteger(0);
final JobManager jobManager = createJobManager(new Configuration.Builder(RuntimeEnvironment.application).loadFactor(3).maxConsumerCount(10));
int limit = 200;
ExecutorService executor = new ThreadPoolExecutor(20, 20, 60, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(limit));
final String cancelTag = "iWillBeCancelled";
Collection<Future<?>> futures = new LinkedList<Future<?>>();
for (int i = 0; i < limit; i++) {
final int id = i;
futures.add(executor.submit(new Runnable() {
@Override
public void run() {
final boolean persistent = Math.round(Math.random()) % 2 == 0;
boolean requiresNetwork = Math.round(Math.random()) % 2 == 0;
int priority = (int) (Math.round(Math.random()) % 10);
multiThreadedJobCounter.incrementAndGet();
Params params = new Params(priority).setRequiresNetwork(requiresNetwork).setPersistent(persistent);
if (Math.random() < .1) {
params.addTags(cancelTag);
}
jobManager.addJob(new DummyJobForMultiThread(id, params));
}
}));
}
// wait for some jobs to start
//noinspection SLEEP_IN_CODE
Thread.sleep(1000);
CancelResult cancelResult = jobManager.cancelJobs(TagConstraint.ALL, cancelTag);
for (int i = 0; i < cancelResult.getCancelledJobs().size(); i++) {
multiThreadedJobCounter.decrementAndGet();
}
for (Future<?> future : futures) {
future.get();
}
Log.d("TAG", "added all jobs");
//wait until all jobs are added
//noinspection DIRECT_TIME_ACCESS
long start = System.nanoTime();
//20 minutes
long timeLimit = JobManager.NS_PER_MS * 60000 * 20;
//noinspection DIRECT_TIME_ACCESS
while (System.nanoTime() - start < timeLimit && multiThreadedJobCounter.get() != 0) {
//noinspection SLEEP_IN_CODE
Thread.sleep(1000);
}
MatcherAssert.assertThat("jobmanager count should be 0", jobManager.count(), equalTo(0));
jobManager.stopAndWaitUntilConsumersAreFinished();
MatcherAssert.assertThat("multiThreadedJobCounter should be 0", multiThreadedJobCounter.get(), CoreMatchers.is(0));
}
use of com.birbit.android.jobqueue.CancelResult in project android-priority-jobqueue by yigit.
the class CancelBeforeRunningTest method testCancelBeforeRunning.
@Test
public void testCancelBeforeRunning() {
JobManager jobManager = createJobManager();
jobManager.stop();
DummyJob nonPersistentJob = new DummyJob(new Params(0).addTags("dummyTag"));
DummyJob persistentJob = new DummyJob(new Params(0).addTags("dummyTag").persist());
jobManager.addJob(nonPersistentJob);
jobManager.addJob(persistentJob);
CancelResult result = jobManager.cancelJobs(TagConstraint.ANY, "dummyTag");
assertThat("both jobs should be cancelled", result.getCancelledJobs().size(), is(2));
assertThat("both jobs should be cancelled", result.getFailedToCancel().size(), is(0));
for (Job j : result.getCancelledJobs()) {
DummyJob job = (DummyJob) j;
if (!job.isPersistent()) {
assertThat("job is still added", job.getOnAddedCnt(), is(1));
}
assertThat("job is cancelled", job.getOnCancelCnt(), is(1));
assertThat("job is NOT run", job.getOnRunCnt(), is(0));
}
}
use of com.birbit.android.jobqueue.CancelResult in project android-priority-jobqueue by yigit.
the class CancelWhileRunningTest method testCancelBeforeRunning.
@Test
public void testCancelBeforeRunning() throws InterruptedException {
JobManager jobManager = createJobManager(new Configuration.Builder(RuntimeEnvironment.application).minConsumerCount(5).timer(mockTimer));
JobWithEndLatch nonPersistent1 = new JobWithEndLatch(new Params(0).addTags("dummyTag"), true);
JobWithEndLatch nonPersistent2 = new JobWithEndLatch(new Params(0).addTags("dummyTag"), false);
DummyJob persistentJob1 = new PersistentJobWithEndLatch(new Params(0).addTags("dummyTag"), false);
DummyJob persistentJob2 = new PersistentJobWithEndLatch(new Params(0).addTags("dummyTag"), true);
jobManager.addJob(nonPersistent1);
jobManager.addJob(nonPersistent2);
jobManager.addJob(persistentJob1);
jobManager.addJob(persistentJob2);
onStartLatch.await();
nonPersistent1.onStartLatch.await();
nonPersistent2.onStartLatch.await();
final CancelResult[] resultHolder = new CancelResult[2];
final CountDownLatch cancelLatch = new CountDownLatch(1);
jobManager.cancelJobsInBackground(new CancelResult.AsyncCancelCallback() {
@Override
public void onCancelled(CancelResult cancelResult) {
resultHolder[0] = cancelResult;
cancelLatch.countDown();
}
}, TagConstraint.ANY, "dummyTag");
jobManager.cancelJobsInBackground(new CancelResult.AsyncCancelCallback() {
@Override
public void onCancelled(CancelResult cancelResult) {
resultHolder[1] = cancelResult;
}
}, TagConstraint.ANY, "dummyTag");
assertThat("result should not arrive until existing jobs finish", cancelLatch.await(4, TimeUnit.SECONDS), is(false));
onEndLatch.countDown();
nonPersistent1.onEndLatch.countDown();
nonPersistent2.onEndLatch.countDown();
assertThat("when jobs in question are finished, cancel callback should be triggered", cancelLatch.await(10, TimeUnit.SECONDS), is(true));
final CancelResult result = resultHolder[0];
JqLog.d("cancelled jobs %s", result.getCancelledJobs());
JqLog.d("failed to cancel %s", result.getFailedToCancel());
assertThat("two jobs should be cancelled", result.getCancelledJobs().size(), is(2));
assertThat("two jobs should fail to cancel", result.getFailedToCancel().size(), is(2));
for (Job j : result.getCancelledJobs()) {
FailingJob job = (FailingJob) j;
if (!job.isPersistent()) {
assertThat("job is still added", job.getOnAddedCnt(), is(1));
}
if (job.fail) {
assertThat("job is cancelled", job.getOnCancelCnt(), is(1));
} else {
assertThat("job could not be cancelled", job.getOnCancelCnt(), is(0));
}
}
for (Job j : result.getFailedToCancel()) {
FailingJob job = (FailingJob) j;
if (!job.isPersistent()) {
assertThat("job is still added", job.getOnAddedCnt(), is(1));
}
if (job.fail) {
assertThat("job is cancelled", job.getOnCancelCnt(), is(1));
} else {
assertThat("job could not be cancelled", job.getOnCancelCnt(), is(0));
}
}
assertThat("second cancel should not cancel anything", resultHolder[1].getCancelledJobs().size(), is(0));
assertThat("second cancel should not cancel anything", resultHolder[1].getFailedToCancel().size(), is(0));
}
use of com.birbit.android.jobqueue.CancelResult 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));
}
use of com.birbit.android.jobqueue.CancelResult 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));
}
Aggregations