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

ScriptProfile: Recover from closed context for JS Scripting #4437

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,18 @@ public void deactivate() {
return result == null ? null : result.toString();
} catch (ScriptException e) {
throw new TransformationException("Failed to execute script.", e);
} catch (IllegalStateException e) {
String message = e.getMessage();
if (message != null && message.equals("The Context is already closed.")) {
florian-h05 marked this conversation as resolved.
Show resolved Hide resolved
logger.warn(
"Script engine context {} is already closed, this should not happen. Recreating script engine.",
scriptUid);
scriptCache.remove(scriptUid);
return transform(function, source);
} else {
// rethrow
throw e;
}
}
} finally {
scriptRecord.lock.unlock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import org.openhab.core.config.core.ConfigDescriptionRegistry;
import org.openhab.core.test.java.JavaTest;
import org.openhab.core.transform.Transformation;
import org.openhab.core.transform.TransformationException;
import org.openhab.core.transform.TransformationRegistry;
Expand All @@ -49,7 +50,7 @@
@NonNullByDefault
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
public class ScriptTransformationServiceTest {
public class ScriptTransformationServiceTest extends JavaTest {
private static final String SCRIPT_LANGUAGE = "customDsl";
private static final String SCRIPT_UID = "scriptUid." + SCRIPT_LANGUAGE;
private static final String INVALID_SCRIPT_UID = "invalidScriptUid";
Expand Down Expand Up @@ -189,6 +190,21 @@ public void scriptExceptionResultsInTransformationException() throws ScriptExcep
assertThat(e.getCause().getMessage(), is("exception"));
}

@Test
public void recoversFromClosedScriptContext() throws ScriptException, TransformationException {
when(scriptEngine.eval(SCRIPT)).thenThrow(new IllegalStateException("The Context is already closed."))
.thenReturn(SCRIPT_OUTPUT);
setupInterceptedLogger(ScriptTransformationService.class, LogLevel.WARN);

String returnValue = Objects.requireNonNull(service.transform(SCRIPT_UID, "input"));

assertThat(returnValue, is(SCRIPT_OUTPUT));

stopInterceptedLogger(ScriptTransformationService.class);
assertLogMessage(ScriptTransformationService.class, LogLevel.WARN, "Script engine context " + SCRIPT_UID
+ " is already closed, this should not happen. Recreating script engine.");
}

@Test
public void inlineScriptProperlyProcessed() throws TransformationException, ScriptException {
service.transform(INLINE_SCRIPT, "input");
Expand Down
Loading