-
Notifications
You must be signed in to change notification settings - Fork 51
fix(QTDI-2284): fix junit resources cleanup #1179
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
base: master
Are you sure you want to change the base?
Changes from all commits
c4befc3
56323ee
96d903f
8cde4c2
2bbd210
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -87,12 +87,10 @@ public class BaseComponentsHandler implements ComponentsHandler { | |||||
| protected static final Local<State> STATE = loadStateHolder(); | ||||||
|
|
||||||
| private static Local<State> loadStateHolder() { | ||||||
| switch (System.getProperty("talend.component.junit.handler.state", "thread").toLowerCase(ROOT)) { | ||||||
| case "static": | ||||||
| return new Local.StaticImpl<>(); | ||||||
| default: | ||||||
| return new Local.ThreadLocalImpl<>(); | ||||||
| } | ||||||
| final String handlerState = System.getProperty("talend.component.junit.handler.state", "thread"); | ||||||
| return "static".equals(handlerState.toLowerCase(ROOT)) | ||||||
| ? new Local.StaticImpl<>() | ||||||
| : new Local.ThreadLocalImpl<>(); | ||||||
| } | ||||||
|
|
||||||
| private final ThreadLocal<PreState> initState = ThreadLocal.withInitial(PreState::new); | ||||||
|
|
@@ -111,13 +109,14 @@ public <T> T injectServices(final T instance) { | |||||
| .orElseThrow(() -> new IllegalArgumentException("cant find plugin '" + plugin + "'")) | ||||||
| .get(ComponentManager.AllServices.class) | ||||||
| .getServices(); | ||||||
| Injector.class.cast(services.get(Injector.class)).inject(instance); | ||||||
| ((Injector) services.get(Injector.class)).inject(instance); | ||||||
| return instance; | ||||||
| } | ||||||
|
|
||||||
| public BaseComponentsHandler withIsolatedPackage(final String packageName, final String... packages) { | ||||||
| isolatedPackages = | ||||||
| Stream.concat(Stream.of(packageName), Stream.of(packages)).filter(Objects::nonNull).collect(toList()); | ||||||
| isolatedPackages = Stream.concat(Stream.of(packageName), Stream.of(packages)) | ||||||
| .filter(Objects::nonNull) | ||||||
| .collect(toList()); | ||||||
| if (isolatedPackages.isEmpty()) { | ||||||
| isolatedPackages = null; | ||||||
| } | ||||||
|
|
@@ -140,14 +139,10 @@ protected boolean isContainerClass(final Filter filter, final String name) { | |||||
| public void close() { | ||||||
| try { | ||||||
| final State state = STATE.get(); | ||||||
| if (state.jsonb != null) { | ||||||
| try { | ||||||
| state.jsonb.close(); | ||||||
| } catch (final Exception e) { | ||||||
| // no-op: not important | ||||||
| } | ||||||
| if (state != null) { | ||||||
| state.cleanUp(); | ||||||
| STATE.remove(); | ||||||
| } | ||||||
| STATE.remove(); | ||||||
| initState.remove(); | ||||||
| } finally { | ||||||
| super.close(); | ||||||
|
|
@@ -170,7 +165,7 @@ public Outputs collect(final Processor processor, final ControllableInputFactory | |||||
| * Collects all outputs of a processor. | ||||||
| * | ||||||
| * @param processor the processor to run while there are inputs. | ||||||
| * @param inputs the input factory, when an input will return null it will stop the | ||||||
| * @param inputs The input factory, when an input returns null, it will stop the | ||||||
| * processing. | ||||||
| * @param bundleSize the bundle size to use. | ||||||
| * @return a map where the key is the output name and the value a stream of the | ||||||
|
|
@@ -203,10 +198,10 @@ public <T> Stream<T> collect(final Class<T> recordType, final Mapper mapper, fin | |||||
|
|
||||||
| /** | ||||||
| * Collects data emitted from this mapper. If the split creates more than one | ||||||
| * mapper, it will create as much threads as mappers otherwise it will use the | ||||||
| * mapper, it will create as many threads as mappers, otherwise it will use the | ||||||
| * caller thread. | ||||||
| * | ||||||
| * IMPORTANT: don't forget to consume all the stream to ensure the underlying | ||||||
| * IMPORTANT: don't forget to consume the stream entirely to ensure the underlying | ||||||
| * { @see org.talend.sdk.component.runtime.input.Input} is closed. | ||||||
| * | ||||||
| * @param recordType the record type to use to type the returned type. | ||||||
|
|
@@ -268,7 +263,7 @@ public <T> Stream<T> collect(final Class<T> recordType, final Mapper mapper, fin | |||||
| latch.countDown(); | ||||||
| } | ||||||
| })) | ||||||
| .collect(toList()); | ||||||
| .toList(); | ||||||
| es.shutdown(); | ||||||
|
|
||||||
| final int timeout = Integer.getInteger("talend.component.junit.timeout", 5); | ||||||
|
|
@@ -601,6 +596,16 @@ synchronized RecordBuilderFactory recordBuilderFactory() { | |||||
| } | ||||||
| return recordBuilderFactory; | ||||||
| } | ||||||
|
|
||||||
| synchronized void cleanUp() { | ||||||
| if (jsonb != null) { | ||||||
| try { | ||||||
| jsonb.close(); | ||||||
| } catch (final Exception e) { | ||||||
| // no-op: not important | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| public static class EmbeddedComponentManager extends ComponentManager { | ||||||
|
|
@@ -653,7 +658,7 @@ public <T> List<T> get(final Class<T> type, final String name) { | |||||
| } | ||||||
| } | ||||||
|
|
||||||
| interface Local<T> { | ||||||
| protected interface Local<T> { | ||||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Intellij detected that the interface Local is used with a
|
||||||
| protected interface Local<T> { | |
| interface Local<T> { |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| /** | ||
| * Copyright (C) 2006-2026 Talend Inc. - www.talend.com | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.talend.sdk.component.junit; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
| import static org.junit.jupiter.api.Assertions.assertNull; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class BaseComponentHandlerTest { | ||
|
|
||
| @Test | ||
| void canCloseTheEmbeddedManagerTwice() { | ||
| final MyBaseComponentsHandler myBaseComponentsHandler = new MyBaseComponentsHandler("org.talend.sdk.component.junit"); | ||
| final BaseComponentsHandler.EmbeddedComponentManager start = myBaseComponentsHandler.start(); | ||
| try { | ||
| assertNotNull(BaseComponentsHandler.STATE.get()); | ||
| start.close(); | ||
| assertNull(BaseComponentsHandler.STATE.get()); | ||
| start.close(); | ||
| assertNull(BaseComponentsHandler.STATE.get()); | ||
| } finally { | ||
| start.close(); | ||
| } | ||
| } | ||
|
|
||
| private static class MyBaseComponentsHandler extends BaseComponentsHandler { | ||
| public MyBaseComponentsHandler(final String packageName) { | ||
| this.packageName = packageName; | ||
| } | ||
| } | ||
|
|
||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Intellij asked me to do so
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Useless change