1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.project;
20
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.List;
24 import java.util.concurrent.CyclicBarrier;
25 import java.util.concurrent.ExecutorService;
26 import java.util.concurrent.Executors;
27 import java.util.concurrent.Future;
28
29 import org.codehaus.plexus.classworlds.ClassWorld;
30 import org.codehaus.plexus.classworlds.realm.ClassRealm;
31 import org.eclipse.aether.graph.DependencyFilter;
32 import org.junit.jupiter.api.Test;
33
34 import static org.junit.jupiter.api.Assertions.assertEquals;
35 import static org.junit.jupiter.api.Assertions.assertNotNull;
36 import static org.junit.jupiter.api.Assertions.assertSame;
37 import static org.mockito.Mockito.mock;
38
39
40
41
42 class DefaultProjectRealmCacheTest {
43
44 @Test
45 void testPutReturnsSameRecordForDuplicateKey() throws Exception {
46 DefaultProjectRealmCache cache = new DefaultProjectRealmCache();
47 ClassWorld world = new ClassWorld();
48 ClassRealm realm1 = world.newRealm("test-realm-1");
49 ClassRealm realm2 = world.newRealm("test-realm-2");
50 DependencyFilter filter = mock(DependencyFilter.class);
51
52 ProjectRealmCache.Key key = cache.createKey(Collections.singletonList(realm1));
53
54
55 ProjectRealmCache.CacheRecord record1 = cache.put(key, realm1, filter);
56 assertNotNull(record1);
57 assertSame(realm1, record1.getRealm());
58
59
60 ProjectRealmCache.Key sameKey = cache.createKey(Collections.singletonList(realm1));
61 ProjectRealmCache.CacheRecord record2 = cache.put(sameKey, realm2, filter);
62 assertNotNull(record2);
63 assertSame(record1, record2, "Duplicate put should return the existing cached record");
64 assertSame(realm1, record2.getRealm(), "Duplicate put should return the first realm, not the second");
65 }
66
67 @Test
68 void testConcurrentPutWithSameKeyDoesNotThrow() throws Exception {
69 DefaultProjectRealmCache cache = new DefaultProjectRealmCache();
70 ClassWorld world = new ClassWorld();
71
72
73 ClassRealm sharedExtensionRealm = world.newRealm("shared-extension");
74 DependencyFilter filter = mock(DependencyFilter.class);
75
76 int threadCount = 8;
77 CyclicBarrier barrier = new CyclicBarrier(threadCount);
78 ExecutorService executor = Executors.newFixedThreadPool(threadCount);
79
80 try {
81 List<Future<ProjectRealmCache.CacheRecord>> futures = new ArrayList<>();
82 for (int i = 0; i < threadCount; i++) {
83 final int index = i;
84 futures.add(executor.submit(() -> {
85 ClassRealm projectRealm = world.newRealm("project-realm-" + index);
86 ProjectRealmCache.Key key = cache.createKey(Collections.singletonList(sharedExtensionRealm));
87 barrier.await();
88 return cache.put(key, projectRealm, filter);
89 }));
90 }
91
92
93 ProjectRealmCache.CacheRecord firstRecord = null;
94 for (Future<ProjectRealmCache.CacheRecord> future : futures) {
95 ProjectRealmCache.CacheRecord record = future.get();
96 assertNotNull(record);
97 if (firstRecord == null) {
98 firstRecord = record;
99 }
100
101 assertSame(firstRecord, record, "All concurrent puts should return the same cached record");
102 }
103
104
105 assertEquals(1, cache.cache.size(), "Cache should contain exactly one entry");
106 } finally {
107 executor.shutdown();
108 }
109 }
110 }