|
4 | 4 | import org.junit.jupiter.api.Tag; |
5 | 5 | import org.junit.jupiter.api.Test; |
6 | 6 |
|
| 7 | +import java.lang.reflect.Field; |
| 8 | +import java.util.WeakHashMap; |
7 | 9 | import java.util.concurrent.CountDownLatch; |
8 | 10 | import java.util.concurrent.TimeUnit; |
9 | 11 |
|
10 | 12 | import static io.lettuce.TestTags.UNIT_TEST; |
| 13 | +import java.util.concurrent.atomic.AtomicInteger; |
11 | 14 |
|
12 | 15 | @Tag(UNIT_TEST) |
13 | 16 | public class SharedLockUnitTests { |
@@ -58,4 +61,162 @@ public void safety_on_reentrant_lock_exclusive_on_writers() throws InterruptedEx |
58 | 61 | Assertions.assertTrue(await); |
59 | 62 | } |
60 | 63 |
|
| 64 | + @Test |
| 65 | + public void writerCountsAreIndependentPerSharedLockInstance() { |
| 66 | + final SharedLock lock1 = new SharedLock(); |
| 67 | + final SharedLock lock2 = new SharedLock(); |
| 68 | + |
| 69 | + // Increment writers on lock1 |
| 70 | + lock1.incrementWriters(); |
| 71 | + |
| 72 | + // lock2 should still be able to get exclusive lock (its writer count is 0) |
| 73 | + String result = lock2.doExclusive(() -> "exclusive-on-lock2"); |
| 74 | + Assertions.assertEquals("exclusive-on-lock2", result); |
| 75 | + |
| 76 | + // Cleanup |
| 77 | + lock1.decrementWriters(); |
| 78 | + |
| 79 | + // Now lock1 should also work |
| 80 | + result = lock1.doExclusive(() -> "exclusive-on-lock1"); |
| 81 | + Assertions.assertEquals("exclusive-on-lock1", result); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + @SuppressWarnings("unchecked") |
| 86 | + public void entryShouldBeRemovedWhenWriterCountReachesZero() throws Exception { |
| 87 | + final SharedLock sharedLock = new SharedLock(); |
| 88 | + |
| 89 | + // Access the static THREAD_WRITERS field |
| 90 | + Field threadWritersField = SharedLock.class.getDeclaredField("THREAD_WRITERS"); |
| 91 | + threadWritersField.setAccessible(true); |
| 92 | + ThreadLocal<WeakHashMap<SharedLock, Integer>> threadLocal = (ThreadLocal<WeakHashMap<SharedLock, Integer>>) threadWritersField |
| 93 | + .get(null); |
| 94 | + |
| 95 | + WeakHashMap<SharedLock, Integer> map = threadLocal.get(); |
| 96 | + |
| 97 | + // Initially, the map should not contain this SharedLock |
| 98 | + Assertions.assertFalse(map.containsKey(sharedLock), "Map should not contain SharedLock initially"); |
| 99 | + |
| 100 | + // Increment should add an entry |
| 101 | + sharedLock.incrementWriters(); |
| 102 | + Assertions.assertTrue(map.containsKey(sharedLock), "Map should contain SharedLock after increment"); |
| 103 | + Assertions.assertEquals(1, map.get(sharedLock), "Writer count should be 1"); |
| 104 | + |
| 105 | + // Decrement to zero should remove the entry |
| 106 | + sharedLock.decrementWriters(); |
| 107 | + Assertions.assertFalse(map.containsKey(sharedLock), "Map entry should be removed when count reaches zero"); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + @SuppressWarnings("unchecked") |
| 112 | + public void nestedWriterCountsShouldWorkCorrectly() throws Exception { |
| 113 | + final SharedLock sharedLock = new SharedLock(); |
| 114 | + |
| 115 | + // Access the static THREAD_WRITERS field |
| 116 | + Field threadWritersField = SharedLock.class.getDeclaredField("THREAD_WRITERS"); |
| 117 | + threadWritersField.setAccessible(true); |
| 118 | + ThreadLocal<WeakHashMap<SharedLock, Integer>> threadLocal = (ThreadLocal<WeakHashMap<SharedLock, Integer>>) threadWritersField |
| 119 | + .get(null); |
| 120 | + |
| 121 | + WeakHashMap<SharedLock, Integer> map = threadLocal.get(); |
| 122 | + |
| 123 | + // Nested increments |
| 124 | + sharedLock.incrementWriters(); |
| 125 | + Assertions.assertEquals(1, map.get(sharedLock)); |
| 126 | + |
| 127 | + sharedLock.incrementWriters(); |
| 128 | + Assertions.assertEquals(2, map.get(sharedLock)); |
| 129 | + |
| 130 | + sharedLock.incrementWriters(); |
| 131 | + Assertions.assertEquals(3, map.get(sharedLock)); |
| 132 | + |
| 133 | + // Decrements - entry should NOT be removed until count reaches 0 |
| 134 | + sharedLock.decrementWriters(); |
| 135 | + Assertions.assertEquals(2, map.get(sharedLock)); |
| 136 | + |
| 137 | + sharedLock.decrementWriters(); |
| 138 | + Assertions.assertEquals(1, map.get(sharedLock)); |
| 139 | + |
| 140 | + // Final decrement to zero - entry should be removed |
| 141 | + sharedLock.decrementWriters(); |
| 142 | + Assertions.assertFalse(map.containsKey(sharedLock), "Entry should be removed when count reaches zero"); |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + public void multipleThreadsShouldNotInterfere() throws InterruptedException { |
| 147 | + final SharedLock sharedLock = new SharedLock(); |
| 148 | + final int threadCount = 100; |
| 149 | + final CountDownLatch startLatch = new CountDownLatch(1); |
| 150 | + final CountDownLatch completionLatch = new CountDownLatch(threadCount); |
| 151 | + final AtomicInteger errorCount = new AtomicInteger(0); |
| 152 | + |
| 153 | + // Create multiple threads that will use the SharedLock |
| 154 | + for (int i = 0; i < threadCount; i++) { |
| 155 | + new Thread(() -> { |
| 156 | + try { |
| 157 | + startLatch.await(); |
| 158 | + |
| 159 | + // Each thread increments and decrements multiple times |
| 160 | + for (int j = 0; j < 10; j++) { |
| 161 | + sharedLock.incrementWriters(); |
| 162 | + sharedLock.decrementWriters(); |
| 163 | + } |
| 164 | + } catch (Exception e) { |
| 165 | + errorCount.incrementAndGet(); |
| 166 | + } finally { |
| 167 | + completionLatch.countDown(); |
| 168 | + } |
| 169 | + }).start(); |
| 170 | + } |
| 171 | + |
| 172 | + // Start all threads at once |
| 173 | + startLatch.countDown(); |
| 174 | + |
| 175 | + // Wait for all threads to complete |
| 176 | + boolean completed = completionLatch.await(10, TimeUnit.SECONDS); |
| 177 | + Assertions.assertTrue(completed, "All threads should complete within timeout"); |
| 178 | + Assertions.assertEquals(0, errorCount.get(), "No errors should occur during concurrent operations"); |
| 179 | + |
| 180 | + // After all threads complete, the SharedLock should be in a clean state |
| 181 | + String result = sharedLock.doExclusive(() -> "success"); |
| 182 | + Assertions.assertEquals("success", result); |
| 183 | + } |
| 184 | + |
| 185 | + @Test |
| 186 | + @SuppressWarnings("unchecked") |
| 187 | + public void singleThreadLocalEntryPerThread() throws Exception { |
| 188 | + // Access the static THREAD_WRITERS field |
| 189 | + Field threadWritersField = SharedLock.class.getDeclaredField("THREAD_WRITERS"); |
| 190 | + threadWritersField.setAccessible(true); |
| 191 | + ThreadLocal<WeakHashMap<SharedLock, Integer>> threadLocal = (ThreadLocal<WeakHashMap<SharedLock, Integer>>) threadWritersField |
| 192 | + .get(null); |
| 193 | + |
| 194 | + // Get the map for this thread |
| 195 | + WeakHashMap<SharedLock, Integer> map1 = threadLocal.get(); |
| 196 | + WeakHashMap<SharedLock, Integer> map2 = threadLocal.get(); |
| 197 | + |
| 198 | + // Should be the SAME map instance |
| 199 | + Assertions.assertSame(map1, map2, "ThreadLocal should return the same map instance"); |
| 200 | + |
| 201 | + // Create multiple SharedLocks |
| 202 | + SharedLock lock1 = new SharedLock(); |
| 203 | + SharedLock lock2 = new SharedLock(); |
| 204 | + SharedLock lock3 = new SharedLock(); |
| 205 | + |
| 206 | + lock1.incrementWriters(); |
| 207 | + lock2.incrementWriters(); |
| 208 | + lock3.incrementWriters(); |
| 209 | + |
| 210 | + // All should be in the SAME map |
| 211 | + WeakHashMap<SharedLock, Integer> map = threadLocal.get(); |
| 212 | + Assertions.assertTrue(map.containsKey(lock1)); |
| 213 | + Assertions.assertTrue(map.containsKey(lock2)); |
| 214 | + Assertions.assertTrue(map.containsKey(lock3)); |
| 215 | + |
| 216 | + // Cleanup |
| 217 | + lock1.decrementWriters(); |
| 218 | + lock2.decrementWriters(); |
| 219 | + lock3.decrementWriters(); |
| 220 | + } |
| 221 | + |
61 | 222 | } |
0 commit comments