Skip to content

Commit abb46d9

Browse files
committed
added test to count the transactions
1 parent 7ac1eb7 commit abb46d9

File tree

5 files changed

+108
-9
lines changed

5 files changed

+108
-9
lines changed

core/src/main/java/org/sterl/spring/persistent_tasks/task/model/RegisteredTask.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,8 @@ public void accept(T state) {
3838
public RetryStrategy retryStrategy() {
3939
return this.fun.retryStrategy();
4040
}
41+
@Override
42+
public boolean isTransactional() {
43+
return this.fun.isTransactional();
44+
}
4145
}

core/src/test/java/org/sterl/spring/persistent_tasks/AbstractSpringTest.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import org.springframework.context.annotation.Configuration;
1616
import org.springframework.context.annotation.Primary;
1717
import org.springframework.stereotype.Component;
18-
import org.springframework.test.context.ActiveProfiles;
1918
import org.springframework.transaction.support.TransactionTemplate;
2019
import org.sterl.spring.persistent_tasks.api.SpringBeanTask;
2120
import org.sterl.spring.persistent_tasks.api.TaskId;
@@ -30,6 +29,7 @@
3029
import org.sterl.spring.sample_app.SampleApp;
3130
import org.sterl.test.AsyncAsserts;
3231

32+
import jakarta.persistence.EntityManager;
3333
import lombok.RequiredArgsConstructor;
3434
import uk.co.jemos.podam.api.PodamFactory;
3535
import uk.co.jemos.podam.api.PodamFactoryImpl;
@@ -63,6 +63,8 @@ public class AbstractSpringTest {
6363
protected TransactionTemplate trx;
6464
@Autowired
6565
protected AsyncAsserts asserts;
66+
@Autowired
67+
protected HibernateAsserts hibernateAsserts;
6668

6769
protected final PodamFactory pm = new PodamFactoryImpl();
6870

@@ -72,6 +74,11 @@ public static class TaskConfig {
7274
AsyncAsserts asserts() {
7375
return new AsyncAsserts();
7476
}
77+
78+
@Bean
79+
HibernateAsserts hibernateAsserts(EntityManager entityManager) {
80+
return new HibernateAsserts(entityManager);
81+
}
7582

7683
@Primary
7784
@Bean("schedulerA")
@@ -147,6 +154,7 @@ protected Optional<TriggerEntity> runNextTrigger() {
147154

148155
@BeforeEach
149156
public void beforeEach() throws Exception {
157+
hibernateAsserts.reset();
150158
triggerService.deleteAll();
151159
historyService.deleteAll();
152160
asserts.clear();
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.sterl.spring.persistent_tasks;
2+
3+
import static org.junit.jupiter.api.Assertions.fail;
4+
5+
import org.hibernate.Session;
6+
import org.hibernate.SessionFactory;
7+
import org.hibernate.stat.Statistics;
8+
9+
import jakarta.persistence.EntityManager;
10+
11+
public class HibernateAsserts {
12+
private final Statistics statistics;
13+
14+
public HibernateAsserts(EntityManager entityManager) {
15+
try (Session session = entityManager.unwrap(org.hibernate.Session.class)) {
16+
@SuppressWarnings("resource")
17+
SessionFactory factory = session.getSessionFactory();
18+
factory.getStatistics().setStatisticsEnabled(true);
19+
statistics = factory.getStatistics();
20+
}
21+
}
22+
23+
public HibernateAsserts assertTrxCount(int expected) {
24+
long value = statistics.getTransactionCount();
25+
if (value != expected) {
26+
logSummary();
27+
fail("Expected " + expected + " TransactionCount, but found " + value);
28+
}
29+
return this;
30+
}
31+
32+
public HibernateAsserts assertInsertCount(int expected) {
33+
long value = statistics.getEntityInsertCount();
34+
if (value != expected) {
35+
logSummary();
36+
fail("Expected " + expected + " EntityInsertCount, but found " + value);
37+
}
38+
return this;
39+
}
40+
41+
public void reset() {
42+
statistics.clear();
43+
}
44+
45+
public void logSummary() {
46+
statistics.logSummary();
47+
}
48+
}

core/src/test/java/org/sterl/spring/persistent_tasks/scheduler/SchedulerServiceTransactionTest.java

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,14 @@
2323
class SchedulerServiceTransactionTest extends AbstractSpringTest {
2424

2525
private SchedulerService subject;
26-
@Autowired private AtomicBoolean sendError;
26+
private static AtomicBoolean sendError = new AtomicBoolean(false);
27+
private static AtomicBoolean inTrx = new AtomicBoolean(false);
2728
@Autowired private PersonRepository personRepository;
2829

2930
@Configuration
3031
static class Config {
3132
@Bean
32-
AtomicBoolean sendError() {
33-
return new AtomicBoolean(false);
34-
}
35-
@Bean
36-
SpringBeanTask<String> savePerson(PersonRepository personRepository, AtomicBoolean sendError) {
33+
SpringBeanTask<String> savePerson(PersonRepository personRepository) {
3734
return new SpringBeanTask<>() {
3835
@Transactional
3936
@Override
@@ -46,6 +43,10 @@ public void accept(String name) {
4643
public RetryStrategy retryStrategy() {
4744
return RetryStrategy.THREE_RETRIES_IMMEDIATELY;
4845
}
46+
@Override
47+
public boolean isTransactional() {
48+
return inTrx.get();
49+
}
4950
};
5051
}
5152
}
@@ -56,17 +57,55 @@ public void beforeEach() throws Exception {
5657
subject = schedulerService;
5758
personRepository.deleteAllInBatch();
5859
sendError.set(false);
60+
inTrx.set(false);
5961
}
60-
62+
6163
@Test
6264
void testSaveEntity() throws Exception {
6365
// GIVEN
6466
final var trigger = TaskTriggerBuilder.newTrigger("savePerson").state("Paul").build();
6567

6668
// WHEN
69+
hibernateAsserts.reset();
6770
subject.runOrQueue(trigger).get();
6871

6972
// THEN
73+
// AND one the service, one the event and one more status update,
74+
// one more to save the trigger
75+
hibernateAsserts.assertTrxCount(4);
76+
assertThat(personRepository.count()).isOne();
77+
}
78+
79+
@Test
80+
void testSaveTransactions() throws Exception {
81+
// GIVEN
82+
final var request = TaskTriggerBuilder.newTrigger("savePerson").state("Paul").build();
83+
var trigger = triggerService.queue(request);
84+
85+
// WHEN
86+
hibernateAsserts.reset();
87+
triggerService.run(trigger);
88+
89+
// THEN
90+
// AND one the service, one the event and one more status update
91+
hibernateAsserts.assertTrxCount(3);
92+
assertThat(personRepository.count()).isOne();
93+
}
94+
95+
96+
@Test
97+
void testTrxCountTriggerService() throws Exception {
98+
// GIVEN
99+
final var request = TaskTriggerBuilder.newTrigger("savePerson").state("Paul").build();
100+
var trigger = triggerService.queue(request);
101+
inTrx.set(true);
102+
103+
// WHEN
104+
hibernateAsserts.reset();
105+
triggerService.run(trigger);
106+
107+
// THEN
108+
hibernateAsserts.assertTrxCount(1);
70109
assertThat(personRepository.count()).isOne();
71110
}
72111

core/src/test/java/org/sterl/spring/sample_app/component/EditSchedulerStatusComponentTest.java renamed to core/src/test/java/org/sterl/spring/persistent_tasks/scheduler/component/EditSchedulerStatusComponentTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.sterl.spring.sample_app.component;
1+
package org.sterl.spring.persistent_tasks.scheduler.component;
22

33
import static org.assertj.core.api.Assertions.assertThat;
44

0 commit comments

Comments
 (0)