Skip to content

Commit

Permalink
wip update code comments #2009 #1883
Browse files Browse the repository at this point in the history
note that this commit reverts the change made in #1835
  • Loading branch information
ptrthomas committed Aug 2, 2022
1 parent e9054ca commit 5bbebb1
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public MockHandler(String prefix, List<Feature> features, Map<String, Object> ar
}
}
corsEnabled = corsEnabled || runtime.engine.getConfig().isCorsEnabled();
globals.putAll(runtime.engine.detachVariables());
globals.putAll(runtime.engine.shallowCloneVariables());
runtime.logger.info("mock server initialized: {}", feature);
this.features.put(feature, runtime);
}
Expand Down Expand Up @@ -187,7 +187,7 @@ public synchronized Response handle(Request req) { // note the [synchronized]
responseStatus = engine.vars.remove(ScenarioEngine.RESPONSE_STATUS);
responseHeaders = engine.vars.remove(ScenarioEngine.RESPONSE_HEADERS);
responseDelay = engine.vars.remove(RESPONSE_DELAY);
globals.putAll(engine.detachVariables());
globals.putAll(engine.shallowCloneVariables());
Response res = new Response(200);
if (result.isFailed()) {
response = new Variable(result.getError().getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,7 @@ private static Object callSingleResult(ScenarioEngine engine, Object o) throws E
engine.logger.warn("callSingle() cached result is an exception");
throw (Exception) o;
}
// if we don't clone, an attach operation would update the tree within the cached value
// causing future cache hit + attach attempts to fail !
// shallow clone so that threads see the same data snapshot
o = JsonUtils.shallowCopy(o);
return JsValue.fromJava(o);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1061,11 +1061,10 @@ public void init() { // not in constructor because it has to be on Runnable.run(
}
}

protected Map<String, Variable> detachVariables() {
Set<Object> seen = Collections.newSetFromMap(new IdentityHashMap());
Map<String, Variable> detached = new HashMap(vars.size());
vars.forEach((k, v) -> detached.put(k, v.copy(false))); // shallow clone
return detached;
protected Map<String, Variable> shallowCloneVariables() {
Map<String, Variable> copy = new HashMap(vars.size());
vars.forEach((k, v) -> copy.put(k, v.copy(false))); // shallow clone
return copy;
}

protected <T> Map<String, T> getOrEvalAsMap(Variable var, Object... args) {
Expand Down Expand Up @@ -1800,11 +1799,9 @@ public Variable call(boolean callOnce, String exp, boolean sharedScope) {
Variable resultVariables = this.getCallFeatureVariables(result);
if (sharedScope) {
if (resultVariables.isMap()) {
setVariables(resultVariables.getValue());
} else if (resultVariables.isList()) {
((List) resultVariables.getValue()).forEach(r -> {
setVariables((Map) r);
});
synchronized (JsValue.LOCK) {
setVariables(resultVariables.getValue());
}
}
if (result.getValue() instanceof FeatureResult) {
setConfig(((FeatureResult) result.getValue()).getConfig());
Expand All @@ -1828,9 +1825,8 @@ private Variable callOnceResult(ScenarioCall.Result result, boolean sharedScope)
logger.warn("callonce: ignoring non-map value from result.value: {}", result.value);
}
}
init(); // this will attach and also insert magic variables
init(); // this will insert magic variables
// re-apply config from time of snapshot
// and note that setConfig() will attach functions such as configured "headers"
setConfig(new Config(result.config));
return Variable.NULL; // since we already reset the vars above we return null
// else the call() routine would try to do it again
Expand Down Expand Up @@ -1867,14 +1863,11 @@ private Variable callOnce(String cacheKey, Variable called, Variable arg, boolea
Variable resultValue = call(called, arg, sharedScope);
Variable resultVariables = this.getCallFeatureVariables(resultValue);
// we clone result (and config) here, to snapshot state at the point the callonce was invoked
// detaching is important (see JsFunction) so that we can keep the source-code aside
// and use it to re-create functions in a new JS context - and work around graal-js limitations
Map<String, Variable> clonedVars = called.isFeature() && sharedScope ? detachVariables() : null;
Map<String, Variable> clonedVars = called.isFeature() && sharedScope ? shallowCloneVariables() : null;
result = new ScenarioCall.Result(resultVariables.copy(false), new Config(config), clonedVars);
CACHE.put(cacheKey, result);
logger.info("<< lock released, cached callonce: {}", cacheKey);
// another routine will apply globally if needed
// wrap and attach if being used immediately in a Scenario
return callOnceResult(result, sharedScope);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ public ScenarioRuntime(FeatureRuntime featureRuntime, Scenario scenario, Scenari
engine.requestBuilder = background.engine.requestBuilder.copy(client);
}
result.addStepResults(background.result.getStepResults());
Map<String, Variable> detached = background.engine.detachVariables();
detached.forEach((k, v) -> engine.vars.put(k, v));
Map<String, Variable> copy = background.engine.shallowCloneVariables();
copy.forEach((k, v) -> engine.vars.put(k, v));
}
dryRun = featureRuntime.suite.dryRun;
tags = scenario.getTagsEffective();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ static void beforeAll() {
server = HttpServer.handler(mock).build();
}

// TODO graal22
@Test
void testParallel() {
Results results = Runner.path("classpath:com/intuit/karate/core/parallel/parallel.feature")
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,5 @@ function fn() {
config.message2 = result2.message;
var result3 = karate.callSingle('call-single-from-config3.js');
config.sayHello = result3.sayHello;
// attempt at reproducing #1835
karate.call('call-from-config3.feature');
return config;
}

0 comments on commit 5bbebb1

Please sign in to comment.