Skip to content

Commit

Permalink
java functions can work but with caveats #1558
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrthomas committed Jun 21, 2021
1 parent 905f57f commit 846d7a8
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1209,8 +1209,12 @@ private Object recurseAndDetachAndDeepClone(String name, Object o, Set<Object> s
if (o instanceof Value) {
Value value = (Value) o;
try {
if (value.canExecute() && value.isMetaObject()) { // js function
return new JsFunction(value);
if (value.canExecute()) {
if (value.isMetaObject()) { // js function
return new JsFunction(value);
} else { // java function
return new JsExecutable(value);
}
} else {
return value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ private Map<String, Object> initMagicVariables() {
}
if (scenario.isOutlineExample() && !this.isDynamicBackground()) { // init examples row magic variables
Map<String, Object> exampleData = scenario.getExampleData();
exampleData.forEach((k, v) -> map.put(k, v));
map.putAll(exampleData);
map.put("__row", exampleData);
map.put("__num", scenario.getExampleIndex());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@ public JsExecutable(Value value) {
this.value = value;
}

private static final Object LOCK = new Object();

@Override
public Object execute(Value... arguments) {
if (logger.isTraceEnabled()) {
logger.trace("begin execute: {}", value);
synchronized (LOCK) {
logger.warn("[*** execute ***] global lock on java function possibly from callonce / callSingle: {}", value);

This comment has been minimized.

Copy link
@joelpramos

joelpramos Jun 21, 2021

Contributor

I think the warning is misleading. It won't be from call once at all times, it could just come from a normal call method. My second question is whether the LOCK is truly needed here (I still think here the best name for this class is HostExecutable)

This comment has been minimized.

Copy link
@ptrthomas

ptrthomas Jun 22, 2021

Author Member

a lot of this code is desperation. by all means do whatever you want here :)

return value.execute(arguments);
}
return value.execute(arguments);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ Scenario: one
* match response == { one: '#string' }
* def result = karate.callSingle('call-single-from-feature.feature')
* match result.response == { message: 'from feature' }
# use java class instance from callSingle in config

* match HelloConfigSingle.sayHello('world') == 'hello world'
# use java method instance from callSingle in config
# * match sayHello('world') == 'hello world'
* match HelloOnce.sayHello('world') == 'hello world'
* match sayHello('world') == 'hello world'

Scenario: two
* path 'two'
Expand All @@ -28,10 +28,18 @@ Scenario: two
* def result = karate.callSingle('call-single-from-feature.feature')
* match result.response == { message: 'from feature' }

* match HelloConfigSingle.sayHello('world') == 'hello world'
* match HelloOnce.sayHello('world') == 'hello world'
* match sayHello('world') == 'hello world'

Scenario: three
* path 'three'
* method get
* status 200
* match response == { three: '#string' }
* def result = karate.callSingle('call-single-from-feature.feature')
* match result.response == { message: 'from feature' }
* match result.response == { message: 'from feature' }

* match HelloConfigSingle.sayHello('world') == 'hello world'
* match HelloOnce.sayHello('world') == 'hello world'
* match sayHello('world') == 'hello world'

0 comments on commit 846d7a8

Please sign in to comment.