|
21 | 21 | * <p/> |
22 | 22 | * <ul> |
23 | 23 | * <li>A job should be in the in-flight list while being processed, but not afterwards.</li> |
24 | | - * <li>A job should be requeued when the worker is shut down immediately.</li> |
25 | | - * <li>A job should <i>not</i> be requeued when the worker shuts down after completing the job.</li> |
| 24 | + * <li>A job should be re-queued when the worker is shut down immediately.</li> |
| 25 | + * <li>A job should <i>not</i> be re-queued when the worker shuts down after completing the job.</li> |
26 | 26 | * </ul> |
27 | 27 | * |
28 | 28 | * @author Daniël de Kok <me@danieldk.eu> |
29 | 29 | */ |
30 | 30 | public class DurabilityTest { |
31 | | - private static final Job sleepJob = new Job("SleepAction", (Long) 3000L); |
32 | | - |
| 31 | + |
| 32 | + private static final Job sleepJob = new Job("SleepAction", 3000L); |
33 | 33 | private static final Config config = new ConfigBuilder().build(); |
34 | 34 |
|
35 | | - private static final String testQueue = "foo"; |
36 | | - |
37 | 35 | @Before |
38 | 36 | public void resetRedis() { |
39 | 37 | TestUtils.resetRedis(config); |
40 | 38 | } |
41 | 39 |
|
42 | 40 | @Test |
43 | 41 | public void testNotInterrupted() throws InterruptedException, JsonProcessingException { |
44 | | - TestUtils.enqueueJobs(testQueue, Arrays.asList(sleepJob), config); |
| 42 | + final String queue = "foo"; |
| 43 | + TestUtils.enqueueJobs(queue, Arrays.asList(sleepJob), config); |
45 | 44 |
|
46 | | - final Worker worker = new WorkerImpl(config, Arrays.asList(testQueue), |
| 45 | + final Worker worker = new WorkerImpl(config, Arrays.asList(queue), |
47 | 46 | new MapBasedJobFactory(JesqueUtils.map(JesqueUtils.entry("SleepAction", SleepAction.class)))); |
48 | 47 | final Thread workerThread = new Thread(worker); |
49 | 48 | workerThread.start(); |
50 | 49 |
|
51 | 50 | Thread.sleep(1000); |
52 | 51 |
|
53 | | - Jedis jedis = TestUtils.createJedis(config); |
54 | | - Assert.assertTrue("In-flight list should have length one when running the job", |
55 | | - jedis.llen(inFlightKey(worker)) == 1L); |
| 52 | + final Jedis jedis = TestUtils.createJedis(config); |
| 53 | + Assert.assertEquals("In-flight list should have length one when running the job", |
| 54 | + jedis.llen(inFlightKey(worker, queue)), (Long)1L); |
56 | 55 | Assert.assertEquals("Object on the in-flight list should be the first job", |
57 | | - ObjectMapperFactory.get().writeValueAsString(sleepJob), jedis.lindex(inFlightKey(worker), 0)); |
| 56 | + ObjectMapperFactory.get().writeValueAsString(sleepJob), |
| 57 | + jedis.lindex(inFlightKey(worker, queue), 0)); |
58 | 58 |
|
59 | 59 | TestUtils.stopWorker(worker, workerThread, false); |
60 | 60 |
|
61 | 61 | Assert.assertTrue("The job should not be requeued after succesful processing", |
62 | | - jedis.llen(JesqueUtils.createKey(config.getNamespace(), QUEUE, testQueue)) == 0L); |
63 | | - Assert.assertTrue("In-flight list should be empty when finishing a job", jedis.llen(inFlightKey(worker)) == 0L); |
| 62 | + jedis.llen(JesqueUtils.createKey(config.getNamespace(), QUEUE, queue)) == 0L); |
| 63 | + Assert.assertEquals("In-flight list should be empty when finishing a job", |
| 64 | + jedis.llen(inFlightKey(worker, queue)), (Long)0L); |
64 | 65 | } |
65 | 66 |
|
66 | 67 | @Test |
67 | 68 | public void testInterrupted() throws InterruptedException, JsonProcessingException { |
68 | | - TestUtils.enqueueJobs(testQueue, Arrays.asList(sleepJob), config); |
| 69 | + final String queue = "bar"; |
| 70 | + TestUtils.enqueueJobs(queue, Arrays.asList(sleepJob), config); |
69 | 71 |
|
70 | | - final Worker worker = new WorkerImpl(config, Arrays.asList(testQueue), |
| 72 | + final Worker worker = new WorkerImpl(config, Arrays.asList(queue), |
71 | 73 | new MapBasedJobFactory(JesqueUtils.map(JesqueUtils.entry("SleepAction", SleepAction.class)))); |
72 | 74 | final Thread workerThread = new Thread(worker); |
73 | 75 | workerThread.start(); |
74 | 76 |
|
75 | 77 | TestUtils.stopWorker(worker, workerThread, true); |
76 | 78 |
|
77 | | - Jedis jedis = TestUtils.createJedis(config); |
78 | | - Assert.assertTrue("Job should be requeued", jedis.llen(JesqueUtils.createKey(config.getNamespace(), QUEUE, testQueue)) == 1L); |
| 79 | + final Jedis jedis = TestUtils.createJedis(config); |
| 80 | + Assert.assertTrue("Job should be requeued", jedis.llen(JesqueUtils.createKey(config.getNamespace(), QUEUE, queue)) == 1L); |
79 | 81 | Assert.assertEquals("Incorrect job was requeued", ObjectMapperFactory.get().writeValueAsString(sleepJob), |
80 | | - jedis.lindex(JesqueUtils.createKey(config.getNamespace(), QUEUE, testQueue), 0)); |
81 | | - Assert.assertTrue("In-flight list should be empty when finishing a job", jedis.llen(inFlightKey(worker)) == 0L); |
| 82 | + jedis.lindex(JesqueUtils.createKey(config.getNamespace(), QUEUE, queue), 0)); |
| 83 | + Assert.assertTrue("In-flight list should be empty when finishing a job", jedis.llen(inFlightKey(worker, queue)) == 0L); |
82 | 84 | } |
83 | 85 |
|
84 | | - private String inFlightKey(Worker worker) { |
85 | | - return JesqueUtils.createKey(config.getNamespace(), ResqueConstants.INFLIGHT, worker.getName(), testQueue); |
| 86 | + private static String inFlightKey(final Worker worker, final String queue) { |
| 87 | + return JesqueUtils.createKey(config.getNamespace(), ResqueConstants.INFLIGHT, worker.getName(), queue); |
86 | 88 | } |
87 | | - |
88 | 89 | } |
0 commit comments