Skip to content

Commit

Permalink
Avoid clearing stale weak entries from critical code segments (#2780)
Browse files Browse the repository at this point in the history
This reduces the probability of checking for mocked from from the
mock-checking code what can lead to infinitive loops.
  • Loading branch information
raphw committed Nov 11, 2022
1 parent 47045cb commit 0052e2f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
Expand Up @@ -94,8 +94,8 @@ public InlineBytecodeGenerator(
.with(Implementation.Context.Disabled.Factory.INSTANCE)
.with(MethodGraph.Compiler.ForDeclaredMethods.INSTANCE)
.ignore(isSynthetic().and(not(isConstructor())).or(isDefaultFinalizer()));
mocked = new WeakConcurrentSet<>(WeakConcurrentSet.Cleaner.INLINE);
flatMocked = new WeakConcurrentSet<>(WeakConcurrentSet.Cleaner.INLINE);
mocked = new WeakConcurrentSet<>(WeakConcurrentSet.Cleaner.MANUAL);
flatMocked = new WeakConcurrentSet<>(WeakConcurrentSet.Cleaner.MANUAL);
String identifier = RandomString.make();
subclassEngine =
new TypeCachingBytecodeGenerator(
Expand Down Expand Up @@ -299,6 +299,9 @@ private <T> void triggerRetransformation(Set<Class<?>> types, boolean flat) {
lastException = null;
}
}

mocked.expungeStaleEntries();
flatMocked.expungeStaleEntries();
}

private void assureCanReadMockito(Set<Class<?>> types) {
Expand Down
Expand Up @@ -192,13 +192,13 @@ class InlineDelegateByteBuddyMockMaker
private final BytecodeGenerator bytecodeGenerator;

private final WeakConcurrentMap<Object, MockMethodInterceptor> mocks =
new WeakConcurrentMap.WithInlinedExpunction<Object, MockMethodInterceptor>();
new WeakConcurrentMap<>(false);

private final DetachedThreadLocal<Map<Class<?>, MockMethodInterceptor>> mockedStatics =
new DetachedThreadLocal<>(DetachedThreadLocal.Cleaner.INLINE);
new DetachedThreadLocal<>(DetachedThreadLocal.Cleaner.MANUAL);

private final DetachedThreadLocal<Map<Class<?>, BiConsumer<Object, MockedConstruction.Context>>>
mockedConstruction = new DetachedThreadLocal<>(DetachedThreadLocal.Cleaner.INLINE);
mockedConstruction = new DetachedThreadLocal<>(DetachedThreadLocal.Cleaner.MANUAL);

private final ThreadLocal<Boolean> mockitoConstruction = ThreadLocal.withInitial(() -> false);

Expand Down Expand Up @@ -382,6 +382,7 @@ private <T> T doCreateMock(
if (instance instanceof MockAccess) {
((MockAccess) instance).setMockitoInterceptor(mockMethodInterceptor);
}
mocks.expungeStaleEntries();
return instance;
} catch (InstantiationException e) {
throw new MockitoException(
Expand Down Expand Up @@ -496,6 +497,7 @@ public void resetMock(Object mock, MockHandler newHandler, MockCreationSettings
if (mock instanceof MockAccess) {
((MockAccess) mock).setMockitoInterceptor(mockMethodInterceptor);
}
mocks.expungeStaleEntries();
}
}

Expand Down Expand Up @@ -570,6 +572,7 @@ public <T> StaticMockControl<T> createStaticMock(
interceptors = new WeakHashMap<>();
mockedStatics.set(interceptors);
}
mockedStatics.getBackingMap().expungeStaleEntries();

return new InlineStaticMockControl<>(type, interceptors, settings, handler);
}
Expand Down Expand Up @@ -598,6 +601,7 @@ public <T> ConstructionMockControl<T> createConstructionMock(
interceptors = new WeakHashMap<>();
mockedConstruction.set(interceptors);
}
mockedConstruction.getBackingMap().expungeStaleEntries();

return new InlineConstructionMockControl<>(
type, settingsFactory, handlerFactory, mockInitializer, interceptors);
Expand Down

0 comments on commit 0052e2f

Please sign in to comment.