Skip to content

Commit 4e24cbf

Browse files
committed
Simplify the test for JDBC lock
https://build.spring.io/browse/INT-SI53X-97
1 parent 5938ab2 commit 4e24cbf

File tree

1 file changed

+42
-44
lines changed

1 file changed

+42
-44
lines changed

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

Lines changed: 42 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -132,58 +132,56 @@ public void testSecondThreadLoses() throws Exception {
132132

133133
@Test
134134
public void testBothLock() throws Exception {
135-
for (int i = 0; i < 100; i++) {
136-
final JdbcLockRegistry registry1 = this.registry;
137-
final JdbcLockRegistry registry2 = this.child.getBean(JdbcLockRegistry.class);
138-
final List<String> locked = new ArrayList<>();
139-
final CountDownLatch latch = new CountDownLatch(2);
140-
ExecutorService pool = Executors.newFixedThreadPool(2);
141-
pool.execute(() -> {
142-
Lock lock = registry1.obtain("foo");
135+
final JdbcLockRegistry registry1 = this.registry;
136+
final JdbcLockRegistry registry2 = this.child.getBean(JdbcLockRegistry.class);
137+
final List<String> locked = new ArrayList<>();
138+
final CountDownLatch latch = new CountDownLatch(2);
139+
ExecutorService pool = Executors.newFixedThreadPool(2);
140+
pool.execute(() -> {
141+
Lock lock = registry1.obtain("foo");
142+
try {
143+
lock.lockInterruptibly();
144+
locked.add("1");
145+
latch.countDown();
146+
}
147+
catch (InterruptedException e1) {
148+
Thread.currentThread().interrupt();
149+
}
150+
finally {
143151
try {
144-
lock.lockInterruptibly();
145-
locked.add("1");
146-
latch.countDown();
152+
lock.unlock();
147153
}
148-
catch (InterruptedException e1) {
149-
Thread.currentThread().interrupt();
154+
catch (Exception e2) {
155+
// ignore
150156
}
151-
finally {
152-
try {
153-
lock.unlock();
154-
}
155-
catch (Exception e2) {
156-
// ignore
157-
}
158-
}
159-
});
157+
}
158+
});
160159

161-
pool.execute(() -> {
162-
Lock lock = registry2.obtain("foo");
160+
pool.execute(() -> {
161+
Lock lock = registry2.obtain("foo");
162+
try {
163+
lock.lockInterruptibly();
164+
locked.add("2");
165+
latch.countDown();
166+
}
167+
catch (InterruptedException e1) {
168+
Thread.currentThread().interrupt();
169+
}
170+
finally {
163171
try {
164-
lock.lockInterruptibly();
165-
locked.add("2");
166-
latch.countDown();
167-
}
168-
catch (InterruptedException e1) {
169-
Thread.currentThread().interrupt();
172+
lock.unlock();
170173
}
171-
finally {
172-
try {
173-
lock.unlock();
174-
}
175-
catch (Exception e2) {
176-
// ignore
177-
}
174+
catch (Exception e2) {
175+
// ignore
178176
}
179-
});
177+
}
178+
});
180179

181-
assertThat(latch.await(10, TimeUnit.SECONDS)).isTrue();
182-
// eventually they both get the lock and release it
183-
assertThat(locked.contains("1")).isTrue();
184-
assertThat(locked.contains("2")).isTrue();
185-
pool.shutdownNow();
186-
}
180+
assertThat(latch.await(10, TimeUnit.SECONDS)).isTrue();
181+
// eventually they both get the lock and release it
182+
assertThat(locked.contains("1")).isTrue();
183+
assertThat(locked.contains("2")).isTrue();
184+
pool.shutdownNow();
187185
}
188186

189187
@Test

0 commit comments

Comments
 (0)