Skip to content

Instantly share code, notes, and snippets.

@gresrun
Last active December 13, 2015 19:19
Show Gist options
  • Save gresrun/4962067 to your computer and use it in GitHub Desktop.
Save gresrun/4962067 to your computer and use it in GitHub Desktop.
package net.greghaines.jesque;
import static net.greghaines.jesque.utils.JesqueUtils.entry;
import static net.greghaines.jesque.utils.JesqueUtils.map;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicBoolean;
import net.greghaines.jesque.worker.Worker;
import net.greghaines.jesque.worker.WorkerEvent;
import net.greghaines.jesque.worker.WorkerImpl;
import net.greghaines.jesque.worker.WorkerListener;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class LongRunningTest
{
private static final Logger log = LoggerFactory.getLogger(LongRunningTest.class);
private static final Config config = new ConfigBuilder().build();
private static void sleepTight(final long millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Before
public void resetRedis()
throws Exception
{
TestUtils.resetRedis(config);
}
@Test
public void dummy(){} // Makes JUnit happy that there's at least one test if the others are commented out
@SuppressWarnings("unchecked")
@Test
public void issue28()
throws InterruptedException
{
TestUtils.enqueueJobs("longRunning", Arrays.asList(new Job("LongRunningAction")), config);
final Worker worker2 = new WorkerImpl(config, Arrays.asList("longRunning"),
map(entry("LongRunningAction", LongRunningAction.class)));
final AtomicBoolean succesCallbackInvokedRef = new AtomicBoolean(false);
worker2.addListener(new WorkerListener() {
@Override
public void onEvent(final WorkerEvent event, final Worker worker, final String queue,
final Job job, final Object runner, final Object result, final Exception ex) {
succesCallbackInvokedRef.set(true);
log.info("SUCCCES- {}", job);
}
}, WorkerEvent.JOB_SUCCESS);
final Thread workerThread2 = new Thread(worker2);
workerThread2.start();
sleepTight(1000);
worker2.end(false);
workerThread2.join();
Assert.assertTrue("Success callback should have been called", succesCallbackInvokedRef.get());
}
public static class LongRunningAction implements Runnable
{
public LongRunningAction(){}
public void run()
{
sleepTight(10*60*1000); // Sleep for 10 minutes
}
}
}
12:31:40.843 [main] INFO net.greghaines.jesque.TestUtils - Resetting Redis for next test...
12:31:40.849 [main] INFO net.greghaines.jesque.TestUtils - Resetting Redis for next test...
12:41:41.132 [Worker-0 Jesque-DEVELOPMENT: RUNNING] INFO n.greghaines.jesque.LongRunningTest - SUCCCES- <Job className=LongRunningAction args=[]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment