Skip to content

Commit b9f2879

Browse files
committed
added wait for history insert
1 parent d6d1b1d commit b9f2879

File tree

4 files changed

+18
-5
lines changed

4 files changed

+18
-5
lines changed

core/src/main/java/org/sterl/spring/persistent_tasks/history/config/TriggerHistoryConfig.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22

33
import org.springframework.context.annotation.Bean;
44
import org.springframework.context.annotation.Configuration;
5-
import org.springframework.core.task.TaskExecutor;
65
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
76

87
@Configuration
98
public class TriggerHistoryConfig {
109

1110
@Bean
12-
TaskExecutor triggerHistoryExecutor() {
11+
ThreadPoolTaskExecutor triggerHistoryExecutor() {
1312
var taskExecutor = new ThreadPoolTaskExecutor();
1413
taskExecutor.setCorePoolSize(1);
1514
taskExecutor.setMaxPoolSize(4);

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.time.Duration;
55
import java.util.Optional;
66

7+
import org.awaitility.Awaitility;
78
import org.junit.jupiter.api.AfterEach;
89
import org.junit.jupiter.api.BeforeEach;
910
import org.springframework.beans.factory.annotation.Autowired;
@@ -15,6 +16,7 @@
1516
import org.springframework.context.annotation.Configuration;
1617
import org.springframework.context.annotation.Primary;
1718
import org.springframework.lang.Nullable;
19+
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
1820
import org.springframework.stereotype.Component;
1921
import org.springframework.test.context.ActiveProfiles;
2022
import org.springframework.test.context.event.RecordApplicationEvents;
@@ -65,6 +67,8 @@ public class AbstractSpringTest {
6567

6668
@Autowired
6769
protected HistoryService historyService;
70+
@Autowired
71+
private ThreadPoolTaskExecutor triggerHistoryExecutor;
6872

6973
@Autowired
7074
protected TransactionTemplate trx;
@@ -167,6 +171,10 @@ PersistentTask<Long> slowTask(AsyncAsserts asserts) {
167171
};
168172
}
169173
}
174+
175+
protected void awaidHistoryThreads() {
176+
Awaitility.await().until(() -> triggerHistoryExecutor.getActiveCount() == 0);
177+
}
170178

171179
@Deprecated
172180
protected Optional<RunningTriggerEntity> runNextTrigger() {

core/src/test/java/org/sterl/spring/persistent_tasks/task/TaskTransactionTest.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,17 +164,23 @@ void testRequiresNewHasOwnTransaction(String task) {
164164
@ValueSource(strings = {"transactionalClass", "transactionalClassAndMethod", "transactionalClosure"})
165165
void testTransactionalTask(String task) throws InterruptedException {
166166
// GIVEN
167+
personRepository.deleteAllInBatch();
167168
var t = triggerService.queue(TriggerBuilder
168169
.newTrigger(task, task).build());
169170

170171
// WHEN
171-
personRepository.deleteAllInBatch();
172172
hibernateAsserts.reset();
173173
triggerService.run(t).get();
174174

175175
// THEN
176176
asserts.awaitValue(task);
177-
hibernateAsserts.assertTrxCount(2);
177+
awaidHistoryThreads();
178+
hibernateAsserts
179+
// running trigger
180+
//.assertDeletedCount(1)
181+
// 1 running trigger, 3 history, 1 trigger completed
182+
.assertInsertCount(4)
183+
.assertTrxCount(2);
178184
assertThat(personRepository.count()).isEqualTo(1);
179185
}
180186
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public void awaitOrdered(Runnable fn, String value, String... values) {
131131
}
132132

133133
public ListAssert<String> assertValue(String value) {
134-
return assertThat(new ArrayList<>(values)).contains(value);
134+
return assertThat(values).contains(value);
135135
}
136136

137137
public void assertMissing(String value) {

0 commit comments

Comments
 (0)