|
| 1 | +/* |
| 2 | + * Copyright 2023 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.integration.jdbc.oracle; |
| 18 | + |
| 19 | +import java.util.concurrent.CountDownLatch; |
| 20 | +import java.util.concurrent.Future; |
| 21 | +import java.util.concurrent.TimeUnit; |
| 22 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 23 | +import java.util.concurrent.locks.Lock; |
| 24 | + |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | + |
| 27 | +import org.springframework.beans.factory.annotation.Autowired; |
| 28 | +import org.springframework.context.annotation.Bean; |
| 29 | +import org.springframework.context.annotation.Configuration; |
| 30 | +import org.springframework.core.task.AsyncTaskExecutor; |
| 31 | +import org.springframework.core.task.SimpleAsyncTaskExecutor; |
| 32 | +import org.springframework.integration.jdbc.lock.DefaultLockRepository; |
| 33 | +import org.springframework.integration.jdbc.lock.JdbcLockRegistry; |
| 34 | +import org.springframework.integration.jdbc.lock.LockRepository; |
| 35 | +import org.springframework.jdbc.support.JdbcTransactionManager; |
| 36 | +import org.springframework.test.annotation.DirtiesContext; |
| 37 | +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; |
| 38 | +import org.springframework.transaction.PlatformTransactionManager; |
| 39 | + |
| 40 | +import static org.assertj.core.api.Assertions.assertThat; |
| 41 | +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
| 42 | +import static org.assertj.core.api.Assertions.assertThatNoException; |
| 43 | + |
| 44 | +/** |
| 45 | + * @author Artem Bilan |
| 46 | + * |
| 47 | + * @since 6.0.8 |
| 48 | + */ |
| 49 | +@SpringJUnitConfig |
| 50 | +@DirtiesContext |
| 51 | +public class OracleLockRegistryTests implements OracleContainerTest { |
| 52 | + |
| 53 | + @Autowired |
| 54 | + AsyncTaskExecutor taskExecutor; |
| 55 | + |
| 56 | + @Autowired |
| 57 | + JdbcLockRegistry registry; |
| 58 | + |
| 59 | + @Test |
| 60 | + public void twoThreadsSameLock() throws Exception { |
| 61 | + final Lock lock1 = this.registry.obtain("foo"); |
| 62 | + final AtomicBoolean locked = new AtomicBoolean(); |
| 63 | + final CountDownLatch latch1 = new CountDownLatch(1); |
| 64 | + final CountDownLatch latch2 = new CountDownLatch(1); |
| 65 | + final CountDownLatch latch3 = new CountDownLatch(1); |
| 66 | + lock1.lockInterruptibly(); |
| 67 | + this.taskExecutor.execute(() -> { |
| 68 | + Lock lock2 = this.registry.obtain("foo"); |
| 69 | + try { |
| 70 | + latch1.countDown(); |
| 71 | + lock2.lockInterruptibly(); |
| 72 | + latch2.await(10, TimeUnit.SECONDS); |
| 73 | + locked.set(true); |
| 74 | + } |
| 75 | + catch (InterruptedException e) { |
| 76 | + Thread.currentThread().interrupt(); |
| 77 | + } |
| 78 | + finally { |
| 79 | + lock2.unlock(); |
| 80 | + latch3.countDown(); |
| 81 | + } |
| 82 | + }); |
| 83 | + assertThat(latch1.await(10, TimeUnit.SECONDS)).isTrue(); |
| 84 | + assertThat(locked.get()).isFalse(); |
| 85 | + lock1.unlock(); |
| 86 | + latch2.countDown(); |
| 87 | + assertThat(latch3.await(10, TimeUnit.SECONDS)).isTrue(); |
| 88 | + assertThat(locked.get()).isTrue(); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void twoThreadsSecondFailsToGetLock() throws Exception { |
| 93 | + final Lock lock1 = this.registry.obtain("foo"); |
| 94 | + lock1.lockInterruptibly(); |
| 95 | + final AtomicBoolean locked = new AtomicBoolean(); |
| 96 | + final CountDownLatch latch = new CountDownLatch(1); |
| 97 | + Future<Object> result = taskExecutor.submit(() -> { |
| 98 | + Lock lock2 = this.registry.obtain("foo"); |
| 99 | + locked.set(lock2.tryLock(200, TimeUnit.MILLISECONDS)); |
| 100 | + latch.countDown(); |
| 101 | + try { |
| 102 | + lock2.unlock(); |
| 103 | + } |
| 104 | + catch (Exception e) { |
| 105 | + return e; |
| 106 | + } |
| 107 | + return null; |
| 108 | + }); |
| 109 | + assertThat(latch.await(10, TimeUnit.SECONDS)).isTrue(); |
| 110 | + assertThat(locked.get()).isFalse(); |
| 111 | + lock1.unlock(); |
| 112 | + Object ise = result.get(10, TimeUnit.SECONDS); |
| 113 | + assertThat(ise).isInstanceOf(IllegalMonitorStateException.class); |
| 114 | + assertThat(((Exception) ise).getMessage()).contains("own"); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + public void lockRenewed() { |
| 119 | + Lock lock = this.registry.obtain("foo"); |
| 120 | + |
| 121 | + assertThat(lock.tryLock()).isTrue(); |
| 122 | + |
| 123 | + assertThatNoException() |
| 124 | + .isThrownBy(() -> this.registry.renewLock("foo")); |
| 125 | + |
| 126 | + lock.unlock(); |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + public void lockRenewExceptionNotOwned() { |
| 131 | + this.registry.obtain("foo"); |
| 132 | + |
| 133 | + assertThatExceptionOfType(IllegalMonitorStateException.class) |
| 134 | + .isThrownBy(() -> this.registry.renewLock("foo")); |
| 135 | + } |
| 136 | + |
| 137 | + @Configuration |
| 138 | + public static class Config { |
| 139 | + |
| 140 | + @Bean |
| 141 | + AsyncTaskExecutor taskExecutor() { |
| 142 | + return new SimpleAsyncTaskExecutor(); |
| 143 | + } |
| 144 | + |
| 145 | + @Bean |
| 146 | + public PlatformTransactionManager transactionManager() { |
| 147 | + return new JdbcTransactionManager(OracleContainerTest.dataSource()); |
| 148 | + } |
| 149 | + |
| 150 | + @Bean |
| 151 | + public DefaultLockRepository defaultLockRepository() { |
| 152 | + return new DefaultLockRepository(OracleContainerTest.dataSource()); |
| 153 | + } |
| 154 | + |
| 155 | + @Bean |
| 156 | + public JdbcLockRegistry jdbcLockRegistry(LockRepository lockRepository) { |
| 157 | + return new JdbcLockRegistry(lockRepository); |
| 158 | + } |
| 159 | + |
| 160 | + } |
| 161 | + |
| 162 | +} |
| 163 | + |
0 commit comments