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

#1409 - fix Karate mock server slow to start up under certain circumstances #1410

Merged
merged 1 commit into from
Dec 21, 2020

Conversation

theathlete3141
Copy link

Fix for issue #1409 tested using the examples provided there.

I assume this fix works because it initialises the JSEngine a bit earlier than it may do otherwise

@theathlete3141
Copy link
Author

Probably not relevant to this particular issue, but whilst looking into this I was a little confused by ScenarioEngine.set and ScenarioEngine.get.

If I add logging to ScenarioEngine.java

private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(ScenarioEngine.class);

public static ScenarioEngine get() {
	ScenarioEngine se = THREAD_LOCAL.get();
	LOGGER.info("ScenarioEngine.get() " + se);
	return se;
}

public static void set(ScenarioEngine se) {
	ScenarioEngine before = THREAD_LOCAL.get();
	THREAD_LOCAL.set(se);
	ScenarioEngine after = THREAD_LOCAL.get();
	LOGGER.info("ScenarioEngine.set() " + before + " -> " + after);
}

Then in the MockHandler initialiser (in the case where feature.isBackgroundPresent() is true) I see:

[main] INFO  c.intuit.karate.core.ScenarioEngine - ScenarioEngine.set() null -> com.intuit.karate.core.ScenarioEngine@5d342959

Then a few occurrences like:

[GatlingSystem-akka.actor.default-dispatcher-16] INFO  c.intuit.karate.core.ScenarioEngine - ScenarioEngine.set() null -> com.intuit.karate.core.ScenarioEngine@33bc4ef3
[GatlingSystem-akka.actor.default-dispatcher-16] INFO  c.intuit.karate.core.ScenarioEngine - ScenarioEngine.get() com.intuit.karate.core.ScenarioEngine@33bc4ef3
[GatlingSystem-akka.actor.default-dispatcher-16] INFO  c.intuit.karate.core.ScenarioEngine - ScenarioEngine.get() com.intuit.karate.core.ScenarioEngine@33bc4ef3

(I haven't checked where these were called from)
And then from MockHandler handle(Request req):

[armeria-common-worker-nio-4-4] INFO  c.intuit.karate.core.ScenarioEngine - ScenarioEngine.get() null
[armeria-common-worker-nio-4-4] INFO  c.intuit.karate.core.ScenarioEngine - ScenarioEngine.set() null -> com.intuit.karate.core.ScenarioEngine@751ff4d3
...
[armeria-common-worker-nio-4-4] INFO  c.intuit.karate.core.ScenarioEngine - ScenarioEngine.set() com.intuit.karate.core.ScenarioEngine@751ff4d3 -> null

Which are due to getting the previous engine, temporarily replacing, and then setting the previous engine back again when finished.

Because of the use of ThreadLocal to store ScenarioEngine, and the fact that the MockHandler request method is called from different threads, the initial ScenarioEngine which may have been set during the MockHandler initialiser is never used again.

If I run the karate-core tests i.e.

cd karate-core
mvn install

Then the only instances I can see where ScenarioEngine prevEngine = ScenarioEngine.get() is non-null occurs during MockHandlerTest. However when I run this test on it's own in IntelliJ then prevEngine is null each time. Presumably it must behave slightly different when ran from IntelliJ vs the command line.

I can see that HttpMockHandlerRunner.java testProceed (which I run via IntelliJ as it isn't run during mvn install) has two HttpServers, each with a MockHandler, but as the first calls the second on a different thread to which the first is called then prevEngine is still null

So although I see the comment "highly unlikely but support mocks calling other mocks in the same jvm", is this additional complexity necessary/used/tested?

@ptrthomas ptrthomas merged commit bb5e088 into karatelabs:develop Dec 21, 2020
@ptrthomas
Copy link
Member

@theathlete3141 agreed. please go ahead and remove the additional complexity we can always revisit later

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants