Search in sources :

Example 1 with EnabledForTestGroups

use of cn.taketoday.core.testfixture.EnabledForTestGroups in project today-infrastructure by TAKETODAY.

the class JdbcTransactionManagerTests method transactionWithTimeout.

@ParameterizedTest(name = "transaction with {0} second timeout")
@ValueSource(ints = { 1, 10 })
@EnabledForTestGroups(LONG_RUNNING)
public void transactionWithTimeout(int timeout) throws Exception {
    PreparedStatement ps = mock(PreparedStatement.class);
    given(con.getAutoCommit()).willReturn(true);
    given(con.prepareStatement("some SQL statement")).willReturn(ps);
    TransactionTemplate tt = new TransactionTemplate(tm);
    tt.setTimeout(timeout);
    boolean condition1 = !TransactionSynchronizationManager.hasResource(ds);
    assertThat(condition1).as("Hasn't thread connection").isTrue();
    try {
        tt.execute(new TransactionCallbackWithoutResult() {

            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) {
                try {
                    Thread.sleep(1500);
                } catch (InterruptedException ex) {
                }
                try {
                    Connection con = DataSourceUtils.getConnection(ds);
                    PreparedStatement ps = con.prepareStatement("some SQL statement");
                    DataSourceUtils.applyTransactionTimeout(ps, ds);
                } catch (SQLException ex) {
                    throw new DataAccessResourceFailureException("", ex);
                }
            }
        });
        if (timeout <= 1) {
            fail("Should have thrown TransactionTimedOutException");
        }
    } catch (TransactionTimedOutException ex) {
        if (timeout <= 1) {
        // expected
        } else {
            throw ex;
        }
    }
    boolean condition = !TransactionSynchronizationManager.hasResource(ds);
    assertThat(condition).as("Hasn't thread connection").isTrue();
    if (timeout > 1) {
        verify(ps).setQueryTimeout(timeout - 1);
        verify(con).commit();
    } else {
        verify(con).rollback();
    }
    InOrder ordered = inOrder(con);
    ordered.verify(con).setAutoCommit(false);
    ordered.verify(con).setAutoCommit(true);
    verify(con).close();
}
Also used : TransactionTimedOutException(cn.taketoday.transaction.TransactionTimedOutException) InOrder(org.mockito.InOrder) SQLException(java.sql.SQLException) UncategorizedSQLException(cn.taketoday.jdbc.UncategorizedSQLException) DataAccessResourceFailureException(cn.taketoday.dao.DataAccessResourceFailureException) TransactionTemplate(cn.taketoday.transaction.support.TransactionTemplate) Connection(java.sql.Connection) TransactionStatus(cn.taketoday.transaction.TransactionStatus) PreparedStatement(java.sql.PreparedStatement) TransactionCallbackWithoutResult(cn.taketoday.transaction.support.TransactionCallbackWithoutResult) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) EnabledForTestGroups(cn.taketoday.core.testfixture.EnabledForTestGroups)

Example 2 with EnabledForTestGroups

use of cn.taketoday.core.testfixture.EnabledForTestGroups in project today-infrastructure by TAKETODAY.

the class ScheduledExecutorFactoryBeanTests method withInitialDelayRepeatedExecutionIsSetUpAndFiresCorrectly.

@Test
@EnabledForTestGroups(LONG_RUNNING)
void withInitialDelayRepeatedExecutionIsSetUpAndFiresCorrectly() throws Exception {
    Runnable runnable = mock(Runnable.class);
    ScheduledExecutorTask task = new ScheduledExecutorTask(runnable);
    task.setPeriod(500);
    // nice long wait...
    task.setDelay(3000);
    ScheduledExecutorFactoryBean factory = new ScheduledExecutorFactoryBean();
    factory.setScheduledExecutorTasks(task);
    factory.afterPropertiesSet();
    pauseToLetTaskStart(1);
    // invoke destroy before tasks have even been scheduled...
    factory.destroy();
    // Mock must never have been called
    verify(runnable, never()).run();
}
Also used : NoOpRunnable(cn.taketoday.core.task.NoOpRunnable) Test(org.junit.jupiter.api.Test) EnabledForTestGroups(cn.taketoday.core.testfixture.EnabledForTestGroups)

Example 3 with EnabledForTestGroups

use of cn.taketoday.core.testfixture.EnabledForTestGroups in project today-infrastructure by TAKETODAY.

the class EnableSchedulingTests method withFixedRateTask.

@Test
@EnabledForTestGroups(LONG_RUNNING)
public void withFixedRateTask() throws InterruptedException {
    ctx = new AnnotationConfigApplicationContext(FixedRateTaskConfig.class);
    assertThat(ctx.getBean(ScheduledTaskHolder.class).getScheduledTasks().size()).isEqualTo(2);
    Thread.sleep(100);
    assertThat(ctx.getBean(AtomicInteger.class).get()).isGreaterThanOrEqualTo(10);
}
Also used : AnnotationConfigApplicationContext(cn.taketoday.context.annotation.AnnotationConfigApplicationContext) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.jupiter.api.Test) EnabledForTestGroups(cn.taketoday.core.testfixture.EnabledForTestGroups)

Example 4 with EnabledForTestGroups

use of cn.taketoday.core.testfixture.EnabledForTestGroups in project today-infrastructure by TAKETODAY.

the class EnableSchedulingTests method withSubclass.

@Test
@EnabledForTestGroups(LONG_RUNNING)
public void withSubclass() throws InterruptedException {
    ctx = new AnnotationConfigApplicationContext(FixedRateTaskConfigSubclass.class);
    assertThat(ctx.getBean(ScheduledTaskHolder.class).getScheduledTasks().size()).isEqualTo(2);
    Thread.sleep(100);
    assertThat(ctx.getBean(AtomicInteger.class).get()).isGreaterThanOrEqualTo(10);
}
Also used : AnnotationConfigApplicationContext(cn.taketoday.context.annotation.AnnotationConfigApplicationContext) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.jupiter.api.Test) EnabledForTestGroups(cn.taketoday.core.testfixture.EnabledForTestGroups)

Example 5 with EnabledForTestGroups

use of cn.taketoday.core.testfixture.EnabledForTestGroups in project today-infrastructure by TAKETODAY.

the class EnableSchedulingTests method withTaskAddedVia_configureTasks.

@Test
@EnabledForTestGroups(LONG_RUNNING)
public void withTaskAddedVia_configureTasks() throws InterruptedException {
    ctx = new AnnotationConfigApplicationContext(SchedulingEnabled_withTaskAddedVia_configureTasks.class);
    Thread.sleep(100);
    assertThat(ctx.getBean(ThreadAwareWorker.class).executedByThread).startsWith("taskScheduler-");
}
Also used : AnnotationConfigApplicationContext(cn.taketoday.context.annotation.AnnotationConfigApplicationContext) Test(org.junit.jupiter.api.Test) EnabledForTestGroups(cn.taketoday.core.testfixture.EnabledForTestGroups)

Aggregations

EnabledForTestGroups (cn.taketoday.core.testfixture.EnabledForTestGroups)32 Test (org.junit.jupiter.api.Test)28 AnnotationConfigApplicationContext (cn.taketoday.context.annotation.AnnotationConfigApplicationContext)18 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)12 NoOpRunnable (cn.taketoday.core.task.NoOpRunnable)10 DataAccessResourceFailureException (cn.taketoday.dao.DataAccessResourceFailureException)4 UncategorizedSQLException (cn.taketoday.jdbc.UncategorizedSQLException)4 TransactionStatus (cn.taketoday.transaction.TransactionStatus)4 TransactionTimedOutException (cn.taketoday.transaction.TransactionTimedOutException)4 TransactionCallbackWithoutResult (cn.taketoday.transaction.support.TransactionCallbackWithoutResult)4 TransactionTemplate (cn.taketoday.transaction.support.TransactionTemplate)4 Connection (java.sql.Connection)4 PreparedStatement (java.sql.PreparedStatement)4 SQLException (java.sql.SQLException)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 ValueSource (org.junit.jupiter.params.provider.ValueSource)4 InOrder (org.mockito.InOrder)4