From 7caa5b1f1e08f99cfe4465f091f47e2966d78aa7 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sun, 23 Jun 2013 09:37:57 +0200 Subject: o Initial import of JDBC queue. --- .../io/trygvis/test/spring/PlainSpringTest.java | 88 ++++++++++++++++++++++ .../java/io/trygvis/test/spring/TestConfig.java | 57 ++++++++++++++ 2 files changed, 145 insertions(+) create mode 100644 src/test/java/io/trygvis/test/spring/PlainSpringTest.java create mode 100755 src/test/java/io/trygvis/test/spring/TestConfig.java (limited to 'src/test/java/io/trygvis/test/spring') diff --git a/src/test/java/io/trygvis/test/spring/PlainSpringTest.java b/src/test/java/io/trygvis/test/spring/PlainSpringTest.java new file mode 100644 index 0000000..93372f6 --- /dev/null +++ b/src/test/java/io/trygvis/test/spring/PlainSpringTest.java @@ -0,0 +1,88 @@ +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; + +@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 queueA = queueService.getQueue("a", 1000, true); +// final AtomicReference> refA = new AtomicReference<>(); + asyncService.registerQueue(queueA.queue, req, new TaskEffect() { + @Override + public List apply(Task task) throws Exception { +// refA.set(task.arguments); +// synchronized (refA) { +// refA.notify(); +// } + System.out.println("task.arguments = " + task.arguments); + return asList(task.childTask("b", new Date(), task.arguments.get(0), "world")); + } + }); + + QueueExecutor queueB = queueService.getQueue("b", 1000, true); + final AtomicReference> refB = new AtomicReference<>(); + asyncService.registerQueue(queueB.queue, req, new TaskEffect() { + @Override + public List apply(Task task) throws Exception { +// System.out.println("task.arguments = " + task.arguments); + refB.set(task.arguments); + synchronized (refB) { + refB.notify(); + } + return emptyList(); + } + }); + + synchronized (refB) { + System.out.println("Scheduling task"); + queueService.schedule(queueA.queue, new Date(), asList("hello")); + System.out.println("Task scheduled, waiting"); + refB.wait(10000); + System.out.println("Back!"); + } + +// System.out.println("refA.get() = " + refA.get()); + System.out.println("refB.get() = " + refB.get()); + + assertThat(refB.get()).containsExactly("hello", "world"); + } +} diff --git a/src/test/java/io/trygvis/test/spring/TestConfig.java b/src/test/java/io/trygvis/test/spring/TestConfig.java new file mode 100755 index 0000000..4df8ce1 --- /dev/null +++ b/src/test/java/io/trygvis/test/spring/TestConfig.java @@ -0,0 +1,57 @@ +package io.trygvis.test.spring; + +import com.jolbox.bonecp.BoneCPDataSource; +import io.trygvis.test.DbUtil; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.datasource.DataSourceTransactionManager; +import org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy; +import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy; +import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.transaction.annotation.EnableTransactionManagement; +import org.springframework.transaction.support.TransactionTemplate; + +import javax.sql.DataSource; +import java.sql.SQLException; + +@Configuration +@ComponentScan(basePackages = "io.trygvis") +@EnableTransactionManagement +public class TestConfig { + + @Bean + public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() throws Exception { + return new PropertySourcesPlaceholderConfigurer() {{ + setProperties(System.getProperties()); + setLocalOverride(true); + }}; + } + + @Bean + public JdbcTemplate jdbcTemplate(DataSource dataSource) { + return new JdbcTemplate(dataSource); + } + + @Bean + public DataSource dataSource(@Value("${database.url}") String jdbcUrl, + @Value("${database.username}") String username, + @Value("${database.password}") String password) throws SQLException { + BoneCPDataSource ds = DbUtil.createDataSource(jdbcUrl, username, password); + + return new TransactionAwareDataSourceProxy(new LazyConnectionDataSourceProxy(ds)); + } + + @Bean + public PlatformTransactionManager transactionManager(DataSource dataSource) { + return new DataSourceTransactionManager(dataSource); + } + + @Bean + public TransactionTemplate transactionTemplate(PlatformTransactionManager platformTransactionManager) { + return new TransactionTemplate(platformTransactionManager); + } +} -- cgit v1.2.3