Skip to content

Commit 2fbb6fb

Browse files
committed
Fix JdbcLockRegDiffClientTests race condition
https://build.spring.io/browse/INT-MJATS41-1242 When different `DefaultLockRepository` instances use the same client id, there is a possibility that they will update the same row in the table. This way we have a chance that not only one obtains a lock and try to add a value to the collection. * Remove the test-case for the same client id as non-stable and even dangerous by the the distributed lock purpose * Use `tryLock(Long.MAX_VALUE)` to really ensure the wait behavior during the concurrent loop * Use `20` for thread pool to align with the tasks amount **Cherry-picked to 4.3.x**
1 parent c48a239 commit 2fbb6fb

File tree

1 file changed

+6
-13
lines changed

1 file changed

+6
-13
lines changed

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

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -191,26 +191,19 @@ public void testBothLock() throws Exception {
191191

192192
@Test
193193
public void testOnlyOneLock() throws Exception {
194-
testOnlyOneLock(null);
195-
testOnlyOneLock("AABBCCDD");
196-
}
197-
198-
private void testOnlyOneLock(String id) throws Exception {
199194
for (int i = 0; i < 100; i++) {
200195
final BlockingQueue<String> locked = new LinkedBlockingQueue<>();
201196
final CountDownLatch latch = new CountDownLatch(20);
202-
ExecutorService pool = Executors.newFixedThreadPool(6);
203-
ArrayList<Callable<Boolean>> tasks = new ArrayList<Callable<Boolean>>();
204-
final DefaultLockRepository client = (id == null) ?
205-
new DefaultLockRepository(this.dataSource) :
206-
new DefaultLockRepository(this.dataSource, id);
207-
client.afterPropertiesSet();
208-
this.context.getAutowireCapableBeanFactory().autowireBean(client);
197+
ExecutorService pool = Executors.newFixedThreadPool(20);
198+
ArrayList<Callable<Boolean>> tasks = new ArrayList<>();
199+
209200
for (int j = 0; j < 20; j++) {
210201
Callable<Boolean> task = () -> {
202+
DefaultLockRepository client = new DefaultLockRepository(this.dataSource);
203+
client.afterPropertiesSet();
211204
Lock lock = new JdbcLockRegistry(client).obtain("foo");
212205
try {
213-
if (locked.isEmpty() && lock.tryLock()) {
206+
if (locked.isEmpty() && lock.tryLock(Long.MAX_VALUE, TimeUnit.MILLISECONDS)) {
214207
if (locked.isEmpty()) {
215208
locked.add("done");
216209
return true;

0 commit comments

Comments
 (0)