|
1 | 1 | /* |
2 | | - * Copyright 2016-2020 the original author or authors. |
| 2 | + * Copyright 2016-2021 the original author or authors. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
|
20 | 20 | import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
21 | 21 |
|
22 | 22 | import java.util.Map; |
| 23 | +import java.util.Queue; |
23 | 24 | import java.util.concurrent.CountDownLatch; |
| 25 | +import java.util.concurrent.ExecutorService; |
| 26 | +import java.util.concurrent.Executors; |
24 | 27 | import java.util.concurrent.Future; |
| 28 | +import java.util.concurrent.LinkedBlockingQueue; |
25 | 29 | import java.util.concurrent.TimeUnit; |
26 | 30 | import java.util.concurrent.atomic.AtomicBoolean; |
27 | 31 | import java.util.concurrent.locks.Lock; |
|
35 | 39 | import org.springframework.core.task.AsyncTaskExecutor; |
36 | 40 | import org.springframework.core.task.SimpleAsyncTaskExecutor; |
37 | 41 | import org.springframework.integration.test.util.TestUtils; |
| 42 | +import org.springframework.integration.util.UUIDConverter; |
38 | 43 | import org.springframework.test.annotation.DirtiesContext; |
39 | 44 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; |
40 | 45 |
|
|
43 | 48 | * @author Artem Bilan |
44 | 49 | * @author Stefan Vassilev |
45 | 50 | * @author Alexandre Strubel |
| 51 | + * @author Unseok Kim |
46 | 52 | * |
47 | 53 | * @since 4.3 |
48 | 54 | */ |
@@ -312,4 +318,171 @@ public void testLockRenewLockNotOwned() { |
312 | 318 | .isThrownBy(() -> registry.renewLock("foo")); |
313 | 319 | } |
314 | 320 |
|
| 321 | + @Test |
| 322 | + public void concurrentObtainCapacityTest() throws InterruptedException { |
| 323 | + final int KEY_CNT = 500; |
| 324 | + final int CAPACITY_CNT = 179; |
| 325 | + final int THREAD_CNT = 4; |
| 326 | + |
| 327 | + final CountDownLatch countDownLatch = new CountDownLatch(THREAD_CNT); |
| 328 | + registry.setCacheCapacity(CAPACITY_CNT); |
| 329 | + final ExecutorService executorService = Executors.newFixedThreadPool(THREAD_CNT); |
| 330 | + |
| 331 | + for (int i = 0; i < KEY_CNT; i++) { |
| 332 | + int finalI = i; |
| 333 | + executorService.submit(() -> { |
| 334 | + countDownLatch.countDown(); |
| 335 | + try { |
| 336 | + countDownLatch.await(); |
| 337 | + } |
| 338 | + catch (InterruptedException e) { |
| 339 | + Thread.currentThread().interrupt(); |
| 340 | + } |
| 341 | + String keyId = "foo:" + finalI; |
| 342 | + Lock obtain = registry.obtain(keyId); |
| 343 | + obtain.lock(); |
| 344 | + obtain.unlock(); |
| 345 | + }); |
| 346 | + } |
| 347 | + executorService.shutdown(); |
| 348 | + executorService.awaitTermination(5, TimeUnit.SECONDS); |
| 349 | + |
| 350 | + //capacity limit test |
| 351 | + assertThat(getRegistryLocks(registry)).hasSize(CAPACITY_CNT); |
| 352 | + |
| 353 | + |
| 354 | + registry.expireUnusedOlderThan(-1000); |
| 355 | + assertThat(getRegistryLocks(registry)).isEmpty(); |
| 356 | + } |
| 357 | + |
| 358 | + @Test |
| 359 | + public void concurrentObtainRemoveOrderTest() throws InterruptedException { |
| 360 | + final int THREAD_CNT = 2; |
| 361 | + final int DUMMY_LOCK_CNT = 3; |
| 362 | + |
| 363 | + final int CAPACITY_CNT = THREAD_CNT; |
| 364 | + |
| 365 | + final CountDownLatch countDownLatch = new CountDownLatch(THREAD_CNT); |
| 366 | + registry.setCacheCapacity(CAPACITY_CNT); |
| 367 | + final ExecutorService executorService = Executors.newFixedThreadPool(THREAD_CNT); |
| 368 | + final Queue<String> remainLockCheckQueue = new LinkedBlockingQueue<>(); |
| 369 | + |
| 370 | + //Removed due to capcity limit |
| 371 | + for (int i = 0; i < DUMMY_LOCK_CNT; i++) { |
| 372 | + Lock obtainLock0 = registry.obtain("foo:" + i); |
| 373 | + obtainLock0.lock(); |
| 374 | + obtainLock0.unlock(); |
| 375 | + } |
| 376 | + |
| 377 | + for (int i = DUMMY_LOCK_CNT; i < THREAD_CNT + DUMMY_LOCK_CNT; i++) { |
| 378 | + int finalI = i; |
| 379 | + executorService.submit(() -> { |
| 380 | + countDownLatch.countDown(); |
| 381 | + try { |
| 382 | + countDownLatch.await(); |
| 383 | + } |
| 384 | + catch (InterruptedException e) { |
| 385 | + Thread.currentThread().interrupt(); |
| 386 | + } |
| 387 | + String keyId = "foo:" + finalI; |
| 388 | + remainLockCheckQueue.offer(toUUID(keyId)); |
| 389 | + Lock obtain = registry.obtain(keyId); |
| 390 | + obtain.lock(); |
| 391 | + obtain.unlock(); |
| 392 | + }); |
| 393 | + } |
| 394 | + |
| 395 | + executorService.shutdown(); |
| 396 | + executorService.awaitTermination(5, TimeUnit.SECONDS); |
| 397 | + |
| 398 | + assertThat(getRegistryLocks(registry)).containsKeys( |
| 399 | + remainLockCheckQueue.toArray(new String[remainLockCheckQueue.size()])); |
| 400 | + } |
| 401 | + |
| 402 | + @Test |
| 403 | + public void concurrentObtainAccessRemoveOrderTest() throws InterruptedException { |
| 404 | + final int THREAD_CNT = 2; |
| 405 | + final int DUMMY_LOCK_CNT = 3; |
| 406 | + |
| 407 | + final int CAPACITY_CNT = THREAD_CNT + 1; |
| 408 | + final String REMAIN_DUMMY_LOCK_KEY = "foo:1"; |
| 409 | + |
| 410 | + final CountDownLatch countDownLatch = new CountDownLatch(THREAD_CNT); |
| 411 | + registry.setCacheCapacity(CAPACITY_CNT); |
| 412 | + final ExecutorService executorService = Executors.newFixedThreadPool(THREAD_CNT); |
| 413 | + final Queue<String> remainLockCheckQueue = new LinkedBlockingQueue<>(); |
| 414 | + |
| 415 | + //Removed due to capcity limit |
| 416 | + for (int i = 0; i < DUMMY_LOCK_CNT; i++) { |
| 417 | + Lock obtainLock0 = registry.obtain("foo:" + i); |
| 418 | + obtainLock0.lock(); |
| 419 | + obtainLock0.unlock(); |
| 420 | + } |
| 421 | + |
| 422 | + Lock obtainLock0 = registry.obtain(REMAIN_DUMMY_LOCK_KEY); |
| 423 | + obtainLock0.lock(); |
| 424 | + obtainLock0.unlock(); |
| 425 | + remainLockCheckQueue.offer(toUUID(REMAIN_DUMMY_LOCK_KEY)); |
| 426 | + |
| 427 | + for (int i = DUMMY_LOCK_CNT; i < THREAD_CNT + DUMMY_LOCK_CNT; i++) { |
| 428 | + int finalI = i; |
| 429 | + executorService.submit(() -> { |
| 430 | + countDownLatch.countDown(); |
| 431 | + try { |
| 432 | + countDownLatch.await(); |
| 433 | + } |
| 434 | + catch (InterruptedException e) { |
| 435 | + Thread.currentThread().interrupt(); |
| 436 | + } |
| 437 | + String keyId = "foo:" + finalI; |
| 438 | + remainLockCheckQueue.offer(toUUID(keyId)); |
| 439 | + Lock obtain = registry.obtain(keyId); |
| 440 | + obtain.lock(); |
| 441 | + obtain.unlock(); |
| 442 | + }); |
| 443 | + } |
| 444 | + |
| 445 | + executorService.shutdown(); |
| 446 | + executorService.awaitTermination(5, TimeUnit.SECONDS); |
| 447 | + |
| 448 | + assertThat(getRegistryLocks(registry)).containsKeys( |
| 449 | + remainLockCheckQueue.toArray(new String[remainLockCheckQueue.size()])); |
| 450 | + } |
| 451 | + |
| 452 | + @Test |
| 453 | + public void setCapacityTest() { |
| 454 | + final int CAPACITY_CNT = 4; |
| 455 | + registry.setCacheCapacity(CAPACITY_CNT); |
| 456 | + |
| 457 | + registry.obtain("foo:1"); |
| 458 | + registry.obtain("foo:2"); |
| 459 | + registry.obtain("foo:3"); |
| 460 | + |
| 461 | + //capacity 4->3 |
| 462 | + registry.setCacheCapacity(CAPACITY_CNT - 1); |
| 463 | + |
| 464 | + registry.obtain("foo:4"); |
| 465 | + |
| 466 | + assertThat(getRegistryLocks(registry)).hasSize(3); |
| 467 | + assertThat(getRegistryLocks(registry)).containsKeys(toUUID("foo:2"), |
| 468 | + toUUID("foo:3"), |
| 469 | + toUUID("foo:4")); |
| 470 | + |
| 471 | + //capacity 3->4 |
| 472 | + registry.setCacheCapacity(CAPACITY_CNT); |
| 473 | + registry.obtain("foo:5"); |
| 474 | + assertThat(getRegistryLocks(registry)).hasSize(4); |
| 475 | + assertThat(getRegistryLocks(registry)).containsKeys(toUUID("foo:3"), |
| 476 | + toUUID("foo:4"), |
| 477 | + toUUID("foo:5")); |
| 478 | + } |
| 479 | + |
| 480 | + @SuppressWarnings("unchecked") |
| 481 | + private static Map<String, Lock> getRegistryLocks(JdbcLockRegistry registry) { |
| 482 | + return TestUtils.getPropertyValue(registry, "locks", Map.class); |
| 483 | + } |
| 484 | + |
| 485 | + private static String toUUID(String key) { |
| 486 | + return UUIDConverter.getUUID(key).toString(); |
| 487 | + } |
315 | 488 | } |
0 commit comments