Skip to content

Commit f6bb91c

Browse files
committed
Fix race conditions in JdbcLockRegDifClientTests
The default `TTL` in the `DefaultLockRepository` is 10 seconds which mislead us when other client instance has just 100 millis. So, not-adjusted client spins for expired lock wastefully leading to the synchronization barriers to fail **Cherry-pick to `5.4.x`**
1 parent b3822c2 commit f6bb91c

File tree

1 file changed

+10
-17
lines changed

1 file changed

+10
-17
lines changed

Diff for: spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/lock/JdbcLockRegistryDifferentClientTests.java

+10-17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2020 the original author or authors.
2+
* Copyright 2016-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -277,25 +277,21 @@ public void testExclusiveAccess() throws Exception {
277277
@Test
278278
public void testOutOfDateLockTaken() throws Exception {
279279
DefaultLockRepository client1 = new DefaultLockRepository(dataSource);
280-
client1.setTimeToLive(500);
280+
client1.setTimeToLive(100);
281281
client1.afterPropertiesSet();
282-
final DefaultLockRepository client2 = new DefaultLockRepository(dataSource);
282+
DefaultLockRepository client2 = new DefaultLockRepository(dataSource);
283+
client2.setTimeToLive(100);
283284
client2.afterPropertiesSet();
284285
Lock lock1 = new JdbcLockRegistry(client1).obtain("foo");
285286
final BlockingQueue<Integer> data = new LinkedBlockingQueue<>();
286-
final CountDownLatch latch1 = new CountDownLatch(1);
287-
final CountDownLatch latch2 = new CountDownLatch(1);
287+
final CountDownLatch latch = new CountDownLatch(1);
288288
lock1.lockInterruptibly();
289289
Thread.sleep(500);
290290
new SimpleAsyncTaskExecutor()
291291
.execute(() -> {
292292
Lock lock2 = new JdbcLockRegistry(client2).obtain("foo");
293293
try {
294-
latch1.countDown();
295-
StopWatch stopWatch = new StopWatch();
296-
stopWatch.start();
297294
lock2.lockInterruptibly();
298-
stopWatch.stop();
299295
data.add(1);
300296
}
301297
catch (InterruptedException e) {
@@ -304,10 +300,9 @@ public void testOutOfDateLockTaken() throws Exception {
304300
finally {
305301
lock2.unlock();
306302
}
307-
latch2.countDown();
303+
latch.countDown();
308304
});
309-
assertThat(latch1.await(10, TimeUnit.SECONDS)).isTrue();
310-
assertThat(latch2.await(10, TimeUnit.SECONDS)).isTrue();
305+
assertThat(latch.await(10, TimeUnit.SECONDS)).isTrue();
311306
data.add(2);
312307
lock1.unlock();
313308
for (int i = 0; i < 2; i++) {
@@ -322,7 +317,8 @@ public void testRenewLock() throws Exception {
322317
DefaultLockRepository client1 = new DefaultLockRepository(dataSource);
323318
client1.setTimeToLive(500);
324319
client1.afterPropertiesSet();
325-
final DefaultLockRepository client2 = new DefaultLockRepository(dataSource);
320+
DefaultLockRepository client2 = new DefaultLockRepository(dataSource);
321+
client2.setTimeToLive(500);
326322
client2.afterPropertiesSet();
327323
JdbcLockRegistry registry = new JdbcLockRegistry(client1);
328324
Lock lock1 = registry.obtain("foo");
@@ -335,10 +331,7 @@ public void testRenewLock() throws Exception {
335331
Lock lock2 = new JdbcLockRegistry(client2).obtain("foo");
336332
try {
337333
latch1.countDown();
338-
StopWatch stopWatch = new StopWatch();
339-
stopWatch.start();
340334
lock2.lockInterruptibly();
341-
stopWatch.stop();
342335
data.add(4);
343336
Thread.sleep(10);
344337
data.add(5);
@@ -356,7 +349,7 @@ public void testRenewLock() throws Exception {
356349
.execute(() -> {
357350
try {
358351
latch1.countDown();
359-
Thread.sleep(1000);
352+
Thread.sleep(500);
360353
data.add(1);
361354
Thread.sleep(100);
362355
data.add(2);

0 commit comments

Comments
 (0)