-
Notifications
You must be signed in to change notification settings - Fork 14
/
SpringDataAsyncApplicationTests.java
215 lines (168 loc) · 8.15 KB
/
SpringDataAsyncApplicationTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package org.neo4j.examples.neo4j_from_the_jvm_ecoysystem.springdataasync;
import static org.assertj.core.api.Assertions.assertThat;
import java.time.Duration;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.dao.InvalidDataAccessResourceUsageException;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.testcontainers.containers.Neo4jContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
@Testcontainers(disabledWithoutDocker = true)
@SpringBootTest(properties = "spring.transaction.default-timeout = 2s")
class SpringDataAsyncApplicationTests {
@Container
private static final Neo4jContainer neo4j = new Neo4jContainer<>("neo4j:4.4")
.withReuse(true)
.withEnv("NEO4JLABS_PLUGINS", "[\"apoc\"]");
@DynamicPropertySource
static void neo4jProperties(DynamicPropertyRegistry registry) {
registry.add("spring.neo4j.uri", neo4j::getBoltUrl);
registry.add("spring.neo4j.authentication.password", neo4j::getAdminPassword);
registry.add("spring.neo4j.authentication.username", () -> "neo4j");
}
@Test
void shouldExecuteSimpleFind(@Autowired MovieRepository movieRepository) {
CompletableFuture<List<Movie>> movies = movieRepository.findAllByTitle("The Matrix");
assertThat(movies)
.succeedsWithin(Duration.ofSeconds(10))
.satisfies(l -> Assertions.assertThat(l).hasSize(1));
}
@Test
void shouldTimeOutWithCustomQuery(@Autowired MovieRepository movieRepository) {
CompletableFuture<List<Movie>> movies = movieRepository.findAllAsync();
assertThat(movies)
.failsWithin(Duration.ofSeconds(10))
.withThrowableOfType(ExecutionException.class)
.withCauseInstanceOf(InvalidDataAccessResourceUsageException.class)
.withMessageStartingWith(
"org.springframework.dao.InvalidDataAccessResourceUsageException: The transaction has been terminated. Retry your operation in a new transaction, and you should see a successful result.");
}
@Test
void serviceDelegatingToRepoShouldFail(@Autowired MovieService movieService) {
CompletableFuture<List<Movie>> movies = movieService.findAllAsync();
assertThat(movies)
.failsWithin(Duration.ofSeconds(10))
.withThrowableOfType(ExecutionException.class)
.withCauseInstanceOf(InvalidDataAccessResourceUsageException.class)
.withMessageStartingWith(
"org.springframework.dao.InvalidDataAccessResourceUsageException: The transaction has been terminated. Retry your operation in a new transaction, and you should see a successful result.");
}
@Nested
class NoDbInteraction {
@Autowired MovieService movieService;
@ParameterizedTest
@ValueSource(ints = { 1, 3 })
void noDbInteractionWillNeverTimeout(int seconds) {
CompletableFuture<String> aFutureString = movieService.someRandomString(Duration.ofSeconds(seconds));
assertThat(aFutureString)
.succeedsWithin(Duration.ofSeconds(10))
.isEqualTo("Foo");
}
}
@Test
void viaCustomExplicitTxCallInsideRepositoryFragment(@Autowired MovieRepository movieRepository) {
// It does not take it's way through the repository and declarative mechanism, therefore the exceptions are a bit different
CompletableFuture<String> aFutureString = movieRepository.getRandomString(Duration.ofSeconds(5));
assertThat(aFutureString)
.failsWithin(Duration.ofSeconds(10))
.withThrowableOfType(ExecutionException.class)
.withCauseInstanceOf(IllegalStateException.class)
.withMessageContaining("Transaction must be open, but has already been closed.");
aFutureString = movieRepository.getRandomString(Duration.ofSeconds(1));
assertThat(aFutureString)
.succeedsWithin(Duration.ofSeconds(10))
.isEqualTo("Foo");
// Make sure they do run in parallel even with the completed futures (without using supplyAsync)
int n = 10;
CompletableFuture<?>[] futures = new CompletableFuture[n];
for (int i = 0; i < n; ++i) {
futures[i] = movieRepository.getRandomString(Duration.ofMillis(1500))
.whenComplete((l, e) -> System.out.println(l));
}
assertThat(CompletableFuture.allOf(futures))
.succeedsWithin(Duration.ofSeconds(n / 2));
}
@Test
void customQueries2ClassesSolution(@Autowired AsyncCustomQueries asyncCustomQueries) {
CompletableFuture<Collection<String>> futureStrings = asyncCustomQueries
.getAll("call apoc.util.sleep($timeout) with ['a', 'b'] as x unwind x as y return y", String.class,
Map.of("timeout", Duration.ofSeconds(5).toMillis()), (t, r) -> r.get(0).asString());
assertThat(futureStrings)
.failsWithin(Duration.ofSeconds(10))
.withThrowableOfType(ExecutionException.class)
.withCauseInstanceOf(IllegalStateException.class)
.withMessageContaining("Transaction must be open, but has already been closed.");
futureStrings = asyncCustomQueries
.getAll("call apoc.util.sleep($timeout) with ['a', 'b'] as x unwind x as y return y", String.class,
Map.of("timeout", Duration.ofSeconds(1).toMillis()), (t, r) -> r.get(0).asString());
assertThat(futureStrings)
.succeedsWithin(Duration.ofSeconds(10))
.asList().containsExactly("a", "b");
}
@Test
void customQueries1ClassSolution(@Autowired OneClassSolution oneClassSolution) {
CompletableFuture<Collection<String>> futureStrings = oneClassSolution
.getAllAsync("call apoc.util.sleep($timeout) with ['a', 'b'] as x unwind x as y return y", String.class,
Map.of("timeout", Duration.ofSeconds(5).toMillis()), (t, r) -> r.get(0).asString());
assertThat(futureStrings)
.failsWithin(Duration.ofSeconds(10))
.withThrowableOfType(ExecutionException.class)
.withCauseInstanceOf(IllegalStateException.class)
.withMessageContaining("Transaction must be open, but has already been closed.");
futureStrings = oneClassSolution
.getAllAsync("call apoc.util.sleep($timeout) with ['a', 'b'] as x unwind x as y return y", String.class,
Map.of("timeout", Duration.ofSeconds(1).toMillis()), (t, r) -> r.get(0).asString());
assertThat(futureStrings)
.succeedsWithin(Duration.ofSeconds(10))
.asList().containsExactly("a", "b");
}
@Test
void repoDelegatingToCustomQueries(@Autowired MovieRepository movieRepository) {
CompletableFuture<Collection<String>> futureStrings = movieRepository.getRandomStrings(Duration.ofSeconds(5));
assertThat(futureStrings)
.failsWithin(Duration.ofSeconds(10))
.withThrowableOfType(ExecutionException.class)
.withCauseInstanceOf(IllegalStateException.class)
.withMessageContaining("Transaction must be open, but has already been closed.");
futureStrings = movieRepository.getRandomStrings(Duration.ofSeconds(1));
assertThat(futureStrings)
.succeedsWithin(Duration.ofSeconds(10))
.asList().containsExactly("a", "b");
}
@Test
void repoDelegatingToOneClassSolution(@Autowired MovieRepository movieRepository) {
CompletableFuture<Collection<String>> futureStrings = movieRepository.getRandomStrings(Duration.ofSeconds(5));
assertThat(futureStrings)
.failsWithin(Duration.ofSeconds(10))
.withThrowableOfType(ExecutionException.class)
.withCauseInstanceOf(IllegalStateException.class)
.withMessageContaining("Transaction must be open, but has already been closed.");
futureStrings = movieRepository.getRandomStrings(Duration.ofSeconds(1));
assertThat(futureStrings)
.succeedsWithin(Duration.ofSeconds(10))
.asList().containsExactly("a", "b");
}
@Test
void makeSureEverythingRunsTogether(@Autowired MovieRepository movieRepository) {
int n = 10;
CompletableFuture<?>[] futures = new CompletableFuture[n];
for (int i = 0; i < n; ++i) {
futures[i] = movieRepository.getRandomStrings2(Duration.ofMillis(1500))
.whenComplete((l, e) -> System.out.println(l));
}
assertThat(CompletableFuture.allOf(futures))
.succeedsWithin(Duration.ofSeconds(n / 2));
}
}