|
20 | 20 | import org.apache.dubbo.remoting.zookeeper.curator5.Curator5ZookeeperClient; |
21 | 21 | import org.apache.dubbo.remoting.zookeeper.curator5.ZookeeperClient; |
22 | 22 | import org.apache.dubbo.remoting.zookeeper.curator5.ZookeeperClientManager; |
| 23 | +import org.apache.dubbo.rpc.model.ApplicationModel; |
23 | 24 |
|
24 | 25 | import java.util.List; |
| 26 | +import java.util.Map; |
25 | 27 |
|
26 | 28 | import org.junit.jupiter.api.AfterAll; |
27 | 29 | import org.junit.jupiter.api.Assertions; |
@@ -244,6 +246,98 @@ void testSameHostWithDifferentUser() { |
244 | 246 | assertThat(client1, not(client2)); |
245 | 247 | } |
246 | 248 |
|
| 249 | + @Test |
| 250 | + void testGetInstanceConcurrentRaceCondition() throws Exception { |
| 251 | + // Clear static managerMap via reflection to start fresh |
| 252 | + java.lang.reflect.Field managerMapField = ZookeeperClientManager.class.getDeclaredField("managerMap"); |
| 253 | + managerMapField.setAccessible(true); |
| 254 | + @SuppressWarnings("unchecked") |
| 255 | + Map<?, ?> managerMap = (Map<?, ?>) managerMapField.get(null); |
| 256 | + managerMap.clear(); |
| 257 | + |
| 258 | + ApplicationModel appModel = ApplicationModel.defaultModel(); |
| 259 | + int threadCount = 20; |
| 260 | + java.util.concurrent.CountDownLatch startGate = new java.util.concurrent.CountDownLatch(1); |
| 261 | + java.util.concurrent.CountDownLatch endGate = new java.util.concurrent.CountDownLatch(threadCount); |
| 262 | + java.util.Set<ZookeeperClientManager> uniqueInstances = |
| 263 | + java.util.Collections.synchronizedSet(new java.util.HashSet<>()); |
| 264 | + |
| 265 | + for (int i = 0; i < threadCount; i++) { |
| 266 | + new Thread(() -> { |
| 267 | + try { |
| 268 | + startGate.await(); |
| 269 | + ZookeeperClientManager instance = ZookeeperClientManager.getInstance(appModel); |
| 270 | + uniqueInstances.add(instance); |
| 271 | + } catch (Exception e) { |
| 272 | + e.printStackTrace(); |
| 273 | + } finally { |
| 274 | + endGate.countDown(); |
| 275 | + } |
| 276 | + }).start(); |
| 277 | + } |
| 278 | + |
| 279 | + startGate.countDown(); |
| 280 | + endGate.await(); |
| 281 | + |
| 282 | + // With the buggy code, multiple different instances may be created. |
| 283 | + // With the fix (computeIfAbsent), exactly 1 instance should exist. |
| 284 | + Assertions.assertEquals(1, uniqueInstances.size(), |
| 285 | + "Expected exactly 1 ZookeeperClientManager instance, but got " + uniqueInstances.size() |
| 286 | + + ". This indicates a TOCTOU race condition in getInstance()."); |
| 287 | + |
| 288 | + // Also verify only 1 entry in the static map |
| 289 | + Assertions.assertEquals(1, managerMap.size(), |
| 290 | + "Expected exactly 1 entry in managerMap"); |
| 291 | + |
| 292 | + managerMap.clear(); |
| 293 | + } |
| 294 | + |
| 295 | + @Test |
| 296 | + void testGetInstanceConcurrentRaceCondition() throws Exception { |
| 297 | + // Clear static managerMap via reflection to start fresh |
| 298 | + java.lang.reflect.Field managerMapField = ZookeeperClientManager.class.getDeclaredField("managerMap"); |
| 299 | + managerMapField.setAccessible(true); |
| 300 | + @SuppressWarnings("unchecked") |
| 301 | + Map<?, ?> managerMap = (Map<?, ?>) managerMapField.get(null); |
| 302 | + managerMap.clear(); |
| 303 | + |
| 304 | + ApplicationModel appModel = ApplicationModel.defaultModel(); |
| 305 | + int threadCount = 20; |
| 306 | + java.util.concurrent.CountDownLatch startGate = new java.util.concurrent.CountDownLatch(1); |
| 307 | + java.util.concurrent.CountDownLatch endGate = new java.util.concurrent.CountDownLatch(threadCount); |
| 308 | + java.util.Set<ZookeeperClientManager> uniqueInstances = |
| 309 | + java.util.Collections.synchronizedSet(new java.util.HashSet<>()); |
| 310 | + |
| 311 | + for (int i = 0; i < threadCount; i++) { |
| 312 | + new Thread(() -> { |
| 313 | + try { |
| 314 | + startGate.await(); |
| 315 | + ZookeeperClientManager instance = ZookeeperClientManager.getInstance(appModel); |
| 316 | + uniqueInstances.add(instance); |
| 317 | + } catch (Exception e) { |
| 318 | + e.printStackTrace(); |
| 319 | + } finally { |
| 320 | + endGate.countDown(); |
| 321 | + } |
| 322 | + }).start(); |
| 323 | + } |
| 324 | + |
| 325 | + startGate.countDown(); |
| 326 | + endGate.await(); |
| 327 | + |
| 328 | + // With the buggy code, multiple different instances may be created. |
| 329 | + // With the fix (computeIfAbsent), exactly 1 instance should exist. |
| 330 | + Assertions.assertEquals(1, uniqueInstances.size(), |
| 331 | + "Expected exactly 1 ZookeeperClientManager instance, but got " + uniqueInstances.size() |
| 332 | + + ". This indicates a TOCTOU race condition in getInstance()."); |
| 333 | + |
| 334 | + // Also verify only 1 entry in the static map |
| 335 | + Assertions.assertEquals(1, managerMap.size(), |
| 336 | + "Expected exactly 1 entry in managerMap"); |
| 337 | + |
| 338 | + managerMap.clear(); |
| 339 | + } |
| 340 | + |
247 | 341 | @AfterAll |
248 | 342 | public static void afterAll() { |
249 | 343 | mockedCurator5ZookeeperClientConstruction.close(); |
|
0 commit comments