-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRacingQueriesTest.java
374 lines (292 loc) · 11.2 KB
/
RacingQueriesTest.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
package dev.jedrzejczyk.r2dbclab;
import java.time.Duration;
import java.util.List;
import java.util.Objects;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import io.r2dbc.pool.ConnectionPool;
import io.r2dbc.pool.ConnectionPoolConfiguration;
import io.r2dbc.spi.Connection;
import io.r2dbc.spi.ConnectionFactory;
import io.r2dbc.spi.OutParameters;
import io.r2dbc.spi.Readable;
import io.r2dbc.spi.Result;
import io.r2dbc.spi.Row;
import io.r2dbc.spi.RowMetadata;
import io.r2dbc.spi.test.MockConnection;
import io.r2dbc.spi.test.MockConnectionFactory;
import io.r2dbc.spi.test.MockRow;
import io.r2dbc.spi.test.MockStatement;
import org.junit.jupiter.api.Test;
import org.reactivestreams.Publisher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.test.util.RaceTestUtils;
import reactor.util.function.Tuple2;
import static org.assertj.core.api.Assertions.assertThat;
public class RacingQueriesTest {
private static final Logger logger = LoggerFactory.getLogger(RacingQueriesTest.class);
// this one is ok
@Test
void testRace() throws InterruptedException {
logger.info("Started");
CountDownLatch goodLatch = new CountDownLatch(1);
CountDownLatch badLatch = new CountDownLatch(1);
ConnectionPool pooledConnectionFactory = connectionPool(slowConnectionFactory());
RaceTestUtils.race(
() -> goodActor(pooledConnectionFactory, goodLatch).subscribe(),
() -> badActor(pooledConnectionFactory, badLatch).subscribe()
);
goodLatch.await();
badLatch.await();
logger.info("Done");
}
// this one is also ok
@Test
void testRaceLoop() throws InterruptedException {
logger.info("Started");
ConnectionPool pooledConnectionFactory = connectionPool(slowConnectionFactory());
for (int i = 0; i < 100; i++) {
logger.info("Iteration: {}", i);
CountDownLatch goodLatch = new CountDownLatch(1);
CountDownLatch badLatch = new CountDownLatch(1);
RaceTestUtils.race(
() -> goodActor(pooledConnectionFactory, goodLatch).subscribe(),
() -> badActor(pooledConnectionFactory, badLatch).subscribe()
);
goodLatch.await();
badLatch.await();
}
logger.info("Done");
}
// this one is ok
@Test
void testRaceSimple() throws InterruptedException {
logger.info("Started");
CountDownLatch goodLatch = new CountDownLatch(1);
CountDownLatch badLatch = new CountDownLatch(1);
ConnectionPool pooledConnectionFactory = connectionPool(slowConnectionFactory());
RaceTestUtils.race(
() -> goodActorSimple(pooledConnectionFactory, goodLatch).subscribe(),
() -> badActorSimple(pooledConnectionFactory, badLatch).subscribe()
);
goodLatch.await();
badLatch.await();
logger.info("Done");
}
// this one is also ok
@Test
void testRaceSimpleLoop() throws InterruptedException {
logger.info("Started");
ConnectionPool pooledConnectionFactory = connectionPool(slowConnectionFactory());
for (int i = 0; i < 100; i++) {
logger.info("Iteration: {}", i);
CountDownLatch goodLatch = new CountDownLatch(1);
CountDownLatch badLatch = new CountDownLatch(1);
RaceTestUtils.race(
() -> goodActorSimple(pooledConnectionFactory, goodLatch).subscribe(),
() -> badActorSimple(pooledConnectionFactory, badLatch).subscribe()
);
goodLatch.await();
badLatch.await();
}
logger.info("Done");
}
@Test
void testGoodActor() throws InterruptedException {
logger.info("Started");
CountDownLatch latch = new CountDownLatch(1);
ConnectionPool pooledConnectionFactory = connectionPool(slowConnectionFactory());
goodActor(pooledConnectionFactory, latch).block();
latch.await();
logger.info("Done");
}
@Test
void testBadActor() throws InterruptedException {
logger.info("Started");
CountDownLatch latch = new CountDownLatch(1);
ConnectionPool pooledConnectionFactory = connectionPool(slowConnectionFactory());
badActor(pooledConnectionFactory, latch).block();
latch.await();
logger.info("Done");
}
@Test
void testGoodActorsRace() throws InterruptedException {
logger.info("Started");
CountDownLatch latch1 = new CountDownLatch(1);
CountDownLatch latch2 = new CountDownLatch(1);
ConnectionPool pooledConnectionFactory = connectionPool(slowConnectionFactory());
RaceTestUtils.race(
() -> goodActor(pooledConnectionFactory, latch1).subscribe(),
() -> goodActor(pooledConnectionFactory, latch2).subscribe());
latch1.await();
latch2.await();
logger.info("Done");
}
@Test
void testGoodActorsSimpleRace() throws InterruptedException {
logger.info("Started");
CountDownLatch latch = new CountDownLatch(2);
ConnectionPool pooledConnectionFactory = connectionPool(slowConnectionFactory());
RaceTestUtils.race(
() -> goodActorSimple(pooledConnectionFactory, latch).subscribe(),
() -> goodActorSimple(pooledConnectionFactory, latch).subscribe());
latch.await();
logger.info("Done");
}
private Mono<Void> badActorSimple(ConnectionPool pool, CountDownLatch latch) {
return actorSimple(pool, latch, true)
.zipWith(Mono.error(RuntimeException::new).delaySubscription(Duration.ofMillis(50)))
.onErrorComplete()
.map(Tuple2::getT1)
.doFinally(s -> latch.countDown());
}
private Mono<Void> goodActorSimple(ConnectionPool pool, CountDownLatch latch) {
return actorSimple(pool, latch, false);
}
private Mono<Void> actorSimple(ConnectionPool pool, CountDownLatch latch, boolean expectCancel) {
return pool.create()
.flatMap(con ->
Mono.from(con.createStatement("SELECT 1").execute())
.flatMap(result -> Mono.from(result.getRowsUpdated()))
.doOnNext(count -> latch.countDown())
.doOnCancel(() -> {
if (expectCancel) {
logger.info("Cancelled actor, releasing connection");
Mono.from(con.close()).doFinally(s -> latch.countDown()).subscribe();
} else {
logger.error("Cancelled actor, but not expected," +
" not releasing connection");
}
})
.then(Mono.from(con.close()))
);
}
private Mono<Long> goodActor(ConnectionPool pool, CountDownLatch latch) {
return actor(pool, latch, false);
}
private Mono<Long> actor(ConnectionPool pool, CountDownLatch latch, boolean expectCancel) {
return Mono.usingWhen(
pool.create()
// we need to countDown because it might be that connection is not
// yet delivered
.doOnCancel(() -> {
if (expectCancel) {
latch.countDown();
}
}),
con -> Mono.from(con.createStatement("SELECT 1").execute()),
con -> Mono.from(con.close())
// we need to do this to ensure that once we clean up, the
// latch is released
.doFinally(s -> {
if (expectCancel) {
latch.countDown();
}
})
).flatMap(result -> Mono.from(result.getRowsUpdated()))
.doFinally(s -> latch.countDown());
}
private Mono<Long> badActor(ConnectionPool pool, CountDownLatch latch) {
return actor(pool, latch, true)
.zipWith(Mono.error(RuntimeException::new).delaySubscription(Duration.ofMillis(50)))
.onErrorComplete()
.map(Tuple2::getT1)
.doFinally(s -> latch.countDown());
}
private ConnectionFactory slowConnectionFactory() {
Random random = new Random();
Supplier<Result.Segment> supplier = () ->
MockResult.row(MockRow.builder()
.identified(random.nextInt(), Integer.class,
random.nextInt()).build());
Stream<Result.Segment> segments =
Stream.concat(Stream.<Result.Segment>generate(supplier)
.limit(100), Stream.of(MockResult.updateCount(100)));
List<Result.Segment> segmentsList = segments.toList();
Flux<Result.Segment> segmentsFlux =
// swap for an instant response
// Flux.just(MockResult.updateCount(1));
Flux.fromIterable(segmentsList)
.delayElements(Duration.ofMillis(10));
MockStatement statement =
MockStatement.builder().result(new MockResult(segmentsFlux)).build();
Connection connection =
MockConnection.builder().valid(true).statement(statement).build();
ConnectionFactory cf =
MockConnectionFactory.builder().connection(connection).build();
return cf;
}
private ConnectionPool connectionPool(ConnectionFactory cf) {
ConnectionPoolConfiguration poolConfig = ConnectionPoolConfiguration.builder(cf).minIdle(1).maxSize(1)
.acquireRetry(0).build();
return new ConnectionPool(poolConfig);
}
public final class MockResult implements Result {
private final Flux<Segment> segments;
MockResult(Flux<Segment> segments) {
this.segments = Objects.requireNonNull(segments, "segments must not be null");
}
@Override
public Flux<Long> getRowsUpdated() {
return this.segments.filter(UpdateCount.class::isInstance)
.cast(UpdateCount.class)
.map(UpdateCount::value)
.collect(Collectors.summingLong(Long::longValue))
.flux();
}
@Override
public <T> Flux<T> map(BiFunction<Row, RowMetadata, ? extends T> mappingFunction) {
Objects.requireNonNull(mappingFunction, "mappingFunction must not be null");
return this.segments.filter(RowSegment.class::isInstance)
.cast(RowSegment.class)
.map(it -> mappingFunction.apply(it.row(),
it.row()
.getMetadata()));
}
@Override
public <T> Publisher<T> map(Function<? super Readable, ? extends T> mappingFunction) {
Objects.requireNonNull(mappingFunction, "f must not be null");
return this.segments.filter(it -> it instanceof RowSegment || it instanceof OutSegment)
.map(it -> {
if (it instanceof OutSegment) {
return mappingFunction.apply(((OutSegment) it).outParameters());
}
return mappingFunction.apply(((RowSegment) it).row());
});
}
@Override
public String toString() {
return "MockResult{" + "segments=" + this.segments + '}';
}
@Override
public Result filter(Predicate<Segment> filter) {
Objects.requireNonNull(filter, "mappingFunction must not be null");
return new MockResult(this.segments.filter(filter));
}
@Override
public <T> Publisher<T> flatMap(Function<Segment, ? extends Publisher<? extends T>> mappingFunction) {
Objects.requireNonNull(mappingFunction, "mappingFunction must not be null");
return this.segments.flatMap(mappingFunction);
}
public static UpdateCount updateCount(long value) {
return () -> value;
}
public static RowSegment row(Row row) {
Objects.requireNonNull(row, "row must not be null");
return () -> row;
}
public static OutSegment outParameters(OutParameters parameters) {
Objects.requireNonNull(parameters, "parameters must not be null");
return () -> parameters;
}
}
}