View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * Tests for {@link DefaultProjectRealmCache}.
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          // First put should succeed and return a record with our realm
55          ProjectRealmCache.CacheRecord record1 = cache.put(key, realm1, filter);
56          assertNotNull(record1);
57          assertSame(realm1, record1.getRealm());
58  
59          // Second put with the same key should return the existing record
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          // Create a shared extension realm for the cache key
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(); // synchronize all threads to maximize contention
88                      return cache.put(key, projectRealm, filter);
89                  }));
90              }
91  
92              // All threads should complete without throwing IllegalStateException
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                 // All threads should get back the same cached record
101                 assertSame(firstRecord, record, "All concurrent puts should return the same cached record");
102             }
103 
104             // Cache should contain exactly one entry
105             assertEquals(1, cache.cache.size(), "Cache should contain exactly one entry");
106         } finally {
107             executor.shutdown();
108         }
109     }
110 }