diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2013-04-30 07:12:56 +0200 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2013-04-30 07:12:56 +0200 |
commit | 0a0b01664cf620f983549999b24a7740594a57d4 (patch) | |
tree | 3a226f4e33ec986f3bae598a547690b3f99e2e8d /src/test/java/io/trygvis/queue | |
download | jdbc-queue-master.tar.gz jdbc-queue-master.tar.bz2 jdbc-queue-master.tar.xz jdbc-queue-master.zip |
Diffstat (limited to 'src/test/java/io/trygvis/queue')
-rw-r--r-- | src/test/java/io/trygvis/queue/QueueTest.java | 170 |
1 files changed, 170 insertions, 0 deletions
diff --git a/src/test/java/io/trygvis/queue/QueueTest.java b/src/test/java/io/trygvis/queue/QueueTest.java new file mode 100644 index 0000000..1aa77d7 --- /dev/null +++ b/src/test/java/io/trygvis/queue/QueueTest.java @@ -0,0 +1,170 @@ +package io.trygvis.queue; + +import com.jolbox.bonecp.*; +import io.trygvis.queue.spring.*; +import org.junit.*; +import org.junit.runner.*; +import org.slf4j.*; +import org.slf4j.bridge.*; +import org.springframework.beans.factory.annotation.*; +import org.springframework.context.annotation.*; +import org.springframework.context.support.*; +import org.springframework.jdbc.core.*; +import org.springframework.jdbc.datasource.*; +import org.springframework.test.context.*; +import org.springframework.test.context.junit4.*; +import org.springframework.transaction.*; +import org.springframework.transaction.support.*; + +import javax.sql.*; +import java.util.*; + +import static java.lang.System.*; +import static org.junit.Assert.*; +import static org.springframework.transaction.TransactionDefinition.*; + +@SuppressWarnings("SpringJavaAutowiringInspection") +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = TestConfig.class) +public class QueueTest { + + @Autowired + private TransactionTemplate transactionTemplate; + + @Autowired + private JdbcTemplate jdbcTemplate; + + @Autowired + private AsyncService asyncService; + + private static Logger log; + + @BeforeClass + public static void beforeClass() { + SLF4JBridgeHandler.install(); + + log = LoggerFactory.getLogger(QueueTest.class); + + String username = getProperty("user.name"); + setProperty("database.url", getProperty("jdbc.url", "jdbc:postgresql://localhost/" + username)); + setProperty("database.username", username); + setProperty("database.password", username); + } + + public int counter; + AsyncService.AsyncCallable callback = new AsyncService.AsyncCallable() { + public void run(List<String> args) throws Exception { + System.out.println("QueueTest.run"); + counter++; + } + }; + + Queue queue; + + @Before + public void before() { + queue = transactionTemplate.execute(new TransactionCallback<Queue>() { + public Queue doInTransaction(TransactionStatus status) { + return asyncService.registerQueue("wat", 10, callback); + } + }); + + int count = jdbcTemplate.update("DELETE FROM task"); + log.info("Deleted {} tasks", count); + } + + public void after() { + asyncService.stopQueue(queue); + } + +// @Test +// public void testQueuePolling() { +// long start = currentTimeMillis(); +// Task abc = transactionTemplate.execute(new TransactionCallback<Task>() { +// public Task doInTransaction(TransactionStatus status) { +// return asyncService.schedule(queue, "abc").getTask(); +// } +// }); +// +// while(!abc.isDone()) { +// abc = asyncService.update(abc); +// } +// +// long end = currentTimeMillis(); +// log.info("Completed in {}ms", end - start); +// +// assertEquals(1, counter); +// } + + @Test + public void testQueueWaiting() throws Exception { + long start = currentTimeMillis(); + TaskRef abc = transactionTemplate.execute(new TransactionCallback<TaskRef>() { + public TaskRef doInTransaction(TransactionStatus status) { + return asyncService.schedule(queue, "abc"); + } + }); + + log.info("Waiting for signal"); + abc.waitFor(1000); + long end = currentTimeMillis(); + log.info("Completed in {}ms", end - start); + + assertEquals(1, counter); + } +} + +@Configuration +@Import(value = QueueSpringConfig.class) +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) { + BoneCPDataSource ds = new BoneCPDataSource(); + + ds.setLogStatementsEnabled(true); + + ds.setJdbcUrl(jdbcUrl); + ds.setUsername(username); + ds.setPassword(password); + + ds.setIdleConnectionTestPeriodInSeconds(60); + ds.setIdleMaxAgeInSeconds(240); + ds.setMaxConnectionsPerPartition(40); + ds.setMinConnectionsPerPartition(0); + ds.setPartitionCount(1); + ds.setAcquireIncrement(1); + ds.setStatementsCacheSize(1000); + ds.setReleaseHelperThreads(3); + ds.setStatisticsEnabled(true); + return new TransactionAwareDataSourceProxy(new LazyConnectionDataSourceProxy(ds)); + } + + @Bean + public PlatformTransactionManager transactionManager(DataSource dataSource) { + return new DataSourceTransactionManager(dataSource); + } + + @Bean + public TransactionTemplate transactionTemplate(PlatformTransactionManager platformTransactionManager) { + DefaultTransactionDefinition td = new DefaultTransactionDefinition(); + td.setPropagationBehavior(PROPAGATION_REQUIRED); + td.setIsolationLevel(ISOLATION_READ_COMMITTED); + return new TransactionTemplate(platformTransactionManager, td); + } +}
\ No newline at end of file |