git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@1438161 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Timothy A. Bish 2013-01-24 20:25:47 +00:00
parent c5184983cf
commit af9a1f0d0f
1 changed files with 28 additions and 27 deletions

View File

@ -26,14 +26,13 @@ import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.apache.activemq.store.kahadb.scheduler.JobSchedulerStoreImpl; import org.apache.activemq.store.kahadb.scheduler.JobSchedulerStoreImpl;
import org.apache.activemq.util.IOHelper;
import org.apache.activemq.util.ByteSequence; import org.apache.activemq.util.ByteSequence;
import org.apache.activemq.util.IOHelper;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
public class JobSchedulerTest { public class JobSchedulerTest {
private JobSchedulerStore store; private JobSchedulerStore store;
private JobScheduler scheduler; private JobScheduler scheduler;
@ -42,7 +41,7 @@ public class JobSchedulerTest {
final int COUNT = 10; final int COUNT = 10;
final CountDownLatch latch = new CountDownLatch(COUNT); final CountDownLatch latch = new CountDownLatch(COUNT);
scheduler.addListener(new JobListener() { scheduler.addListener(new JobListener() {
@Override
public void scheduledJob(String id, ByteSequence job) { public void scheduledJob(String id, ByteSequence job) {
latch.countDown(); latch.countDown();
} }
@ -53,7 +52,7 @@ public class JobSchedulerTest {
scheduler.schedule("id" + i, new ByteSequence(test.getBytes()), 1000); scheduler.schedule("id" + i, new ByteSequence(test.getBytes()), 1000);
} }
latch.await(5, TimeUnit.SECONDS); latch.await(5, TimeUnit.SECONDS);
assertEquals(0,latch.getCount()); assertEquals(0, latch.getCount());
} }
@Test @Test
@ -61,16 +60,15 @@ public class JobSchedulerTest {
final CountDownLatch latch = new CountDownLatch(1); final CountDownLatch latch = new CountDownLatch(1);
scheduler.addListener(new JobListener() { scheduler.addListener(new JobListener() {
@Override
public void scheduledJob(String id, ByteSequence job) { public void scheduledJob(String id, ByteSequence job) {
latch.countDown(); latch.countDown();
} }
}); });
Calendar current = Calendar.getInstance(); Calendar current = Calendar.getInstance();
current.add(Calendar.MINUTE, 1);
int minutes = current.get(Calendar.MINUTE) + 1; int minutes = current.get(Calendar.MINUTE);
int hour = current.get(Calendar.HOUR_OF_DAY); int hour = current.get(Calendar.HOUR_OF_DAY);
int day = current.get(Calendar.DAY_OF_WEEK) - 1; int day = current.get(Calendar.DAY_OF_WEEK) - 1;
@ -89,11 +87,10 @@ public class JobSchedulerTest {
final int COUNT = 10; final int COUNT = 10;
final CountDownLatch latch = new CountDownLatch(COUNT); final CountDownLatch latch = new CountDownLatch(COUNT);
scheduler.addListener(new JobListener() { scheduler.addListener(new JobListener() {
@Override
public void scheduledJob(String id, ByteSequence job) { public void scheduledJob(String id, ByteSequence job) {
latch.countDown(); latch.countDown();
} }
}); });
long time = 2000; long time = 2000;
for (int i = 0; i < COUNT; i++) { for (int i = 0; i < COUNT; i++) {
@ -118,11 +115,10 @@ public class JobSchedulerTest {
tearDown(); tearDown();
startStore(directory); startStore(directory);
scheduler.addListener(new JobListener() { scheduler.addListener(new JobListener() {
@Override
public void scheduledJob(String id, ByteSequence job) { public void scheduledJob(String id, ByteSequence job) {
latch.countDown(); latch.countDown();
} }
}); });
assertTrue(latch.getCount() == COUNT); assertTrue(latch.getCount() == COUNT);
latch.await(3000, TimeUnit.SECONDS); latch.await(3000, TimeUnit.SECONDS);
@ -137,12 +133,20 @@ public class JobSchedulerTest {
for (int i = 0; i < COUNT; i++) { for (int i = 0; i < COUNT; i++) {
String str = new String("test" + i); String str = new String("test" + i);
scheduler.schedule("id" + i, new ByteSequence(str.getBytes()), "", time, 1000, -1); scheduler.schedule("id" + i, new ByteSequence(str.getBytes()), "", time, 1000, -1);
} }
int size = scheduler.getAllJobs().size(); int size = scheduler.getAllJobs().size();
assertEquals(size, COUNT); assertEquals(size, COUNT);
long removeTime = scheduler.getNextScheduleTime(); long removeTime = scheduler.getNextScheduleTime();
scheduler.remove(removeTime); scheduler.remove(removeTime);
// If all jobs are not started within the same second we need to call remove again
if (size != 0) {
removeTime = scheduler.getNextScheduleTime();
scheduler.remove(removeTime);
}
size = scheduler.getAllJobs().size(); size = scheduler.getAllJobs().size();
assertEquals(0, size); assertEquals(0, size);
} }
@ -152,6 +156,7 @@ public class JobSchedulerTest {
final int COUNT = 10; final int COUNT = 10;
final String test = "TESTREMOVE"; final String test = "TESTREMOVE";
long time = 20000; long time = 20000;
for (int i = 0; i < COUNT; i++) { for (int i = 0; i < COUNT; i++) {
String str = new String("test" + i); String str = new String("test" + i);
scheduler.schedule("id" + i, new ByteSequence(str.getBytes()), "", time, 1000, -1); scheduler.schedule("id" + i, new ByteSequence(str.getBytes()), "", time, 1000, -1);
@ -172,16 +177,17 @@ public class JobSchedulerTest {
final int COUNT = 10; final int COUNT = 10;
final String ID = "id:"; final String ID = "id:";
long time = 20000; long time = 20000;
for (int i = 0; i < COUNT; i++) { for (int i = 0; i < COUNT; i++) {
String str = new String("test" + i); String str = new String("test" + i);
scheduler.schedule(ID + i, new ByteSequence(str.getBytes()), "", time, 10 + i, -1); scheduler.schedule(ID + i, new ByteSequence(str.getBytes()), "", time, 10 + i, -1);
} }
List<Job> list = scheduler.getAllJobs(); List<Job> list = scheduler.getAllJobs();
assertEquals(list.size(), COUNT); assertEquals(list.size(), COUNT);
int count = 0; int count = 0;
for (Job job : list) { for (Job job : list) {
assertEquals(job.getJobId(), ID + count); assertEquals(job.getJobId(), ID + count);
count++; count++;
} }
@ -195,18 +201,16 @@ public class JobSchedulerTest {
for (int i = 0; i < COUNT; i++) { for (int i = 0; i < COUNT; i++) {
String str = new String("test" + i); String str = new String("test" + i);
scheduler.schedule(ID + i, new ByteSequence(str.getBytes()), "", start + (i * 1000), 10000 + i, 0); scheduler.schedule(ID + i, new ByteSequence(str.getBytes()), "", start + (i * 1000), 10000 + i, 0);
} }
start = System.currentTimeMillis(); start = System.currentTimeMillis();
long finish = start + 12000+ (COUNT * 1000); long finish = start + 12000 + (COUNT * 1000);
List<Job> list = scheduler.getAllJobs(start, finish); List<Job> list = scheduler.getAllJobs(start, finish);
assertEquals( COUNT,list.size()); assertEquals(COUNT, list.size());
int count = 0; int count = 0;
for (Job job : list) { for (Job job : list) {
assertEquals(job.getJobId(), ID + count); assertEquals(job.getJobId(), ID + count);
count++; count++;
} }
@ -220,12 +224,10 @@ public class JobSchedulerTest {
for (int i = 0; i < COUNT; i++) { for (int i = 0; i < COUNT; i++) {
String str = new String("test" + i); String str = new String("test" + i);
scheduler.schedule(ID + i, new ByteSequence(str.getBytes()), "", start + (i * 1000), 10000 + i, 0); scheduler.schedule(ID + i, new ByteSequence(str.getBytes()), "", start + (i * 1000), 10000 + i, 0);
} }
start = System.currentTimeMillis(); start = System.currentTimeMillis();
long finish = start + 12000+ (COUNT * 1000); long finish = start + 12000 + (COUNT * 1000);
scheduler.removeAllJobs(start, finish); scheduler.removeAllJobs(start, finish);
assertTrue(scheduler.getAllJobs().isEmpty()); assertTrue(scheduler.getAllJobs().isEmpty());
@ -251,5 +253,4 @@ public class JobSchedulerTest {
public void tearDown() throws Exception { public void tearDown() throws Exception {
store.stop(); store.stop();
} }
} }