package io.trygvis.test.spring; import io.trygvis.async.AsyncService; import io.trygvis.queue.QueueExecutor; import io.trygvis.queue.QueueService; import io.trygvis.queue.Task; import io.trygvis.queue.TaskEffect; import io.trygvis.spring.DefaultConfig; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.sql.SQLException; import java.util.Date; import java.util.List; import java.util.concurrent.atomic.AtomicReference; import static java.lang.System.getProperty; import static java.lang.System.setProperty; import static java.util.Arrays.asList; import static java.util.Collections.emptyList; import static org.fest.assertions.Assertions.assertThat; import static org.junit.Assert.assertNotNull; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = {TestConfig.class, DefaultConfig.class}) public class PlainSpringTest { @Autowired private AsyncService asyncService; @Autowired private QueueService queueService; private final QueueService.TaskExecutionRequest req = new QueueService.TaskExecutionRequest(100, true); static { String username = getProperty("user.name"); setProperty("database.url", getProperty("jdbc.url", "jdbc:postgresql://localhost/" + username)); setProperty("database.username", username); setProperty("database.password", username); } @Test public void testBasic() throws SQLException, InterruptedException { QueueExecutor test = queueService.getQueue("test", 10, true); final AtomicReference> ref = new AtomicReference<>(); asyncService.registerQueue(test.queue, req, new TaskEffect() { @Override public List apply(Task task) throws Exception { System.out.println("PlainSpringTest.run"); ref.set(task.arguments); synchronized (ref) { ref.notify(); } return emptyList(); } }); synchronized (ref) { System.out.println("Scheduling task"); queueService.schedule(test.queue, new Date(), asList("hello", "world")); System.out.println("Task scheduled, waiting"); ref.wait(1000); System.out.println("Back!"); } List args = ref.get(); System.out.println("args = " + args); assertNotNull(args); assertThat(args).containsExactly("hello", "world"); } }