Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoids clearing stale weak entries from critical code segments. #2780

Merged
merged 1 commit into from Nov 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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