Skip to content

Commit

Permalink
wontfix for #1558 and adding docs for callSingle() limitations
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrthomas committed Apr 16, 2021
1 parent f29ad40 commit b1dbb1d
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 1 deletion.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,7 @@ function fn() {
}
```

> Here above, you see the [`karate.log()`](#karate-log), [`karate.env`](#karate-env) and [`karate.configure()`](#karate-configure) "helpers" being used. Note that the `karate-config.js` is re-processed for *every* `Scenario` and in rare cases, you may want to initialize (e.g. auth tokens) only once for all of your tests. This can be achieved using [`karate.callSingle()`](#karate-callsingle).
> Here above, you see the [`karate.log()`](#karate-log), [`karate.env`](#karate-env) and [`karate.configure()`](#karate-configure) "helpers" being used. Note that the `karate-config.js` is re-processed for *every* `Scenario` and in rare cases, you may want to initialize (e.g. auth tokens) only once for all of your tests. This can be achieved using [`karate.callSingle()`](#karatecallsingle).
A common requirement is to pass dynamic parameter values via the command line, and you can use the [`karate.properties['some.name']`](#karate-properties) syntax for getting a system property passed via JVM options in the form `-Dsome.name=foo`. Refer to the section on [dynamic port numbers](#dynamic-port-numbers) for an example.

Expand Down Expand Up @@ -3913,6 +3913,8 @@ Refer to this example:

You *can* use `karate.callSingle()` directly in a `*.feature` file, but it logically fits better in the global "bootstrap". Ideally it should return "pure JSON" and note that you always get a "deep clone" of the cached result object.

IMPORTANT: There are some restrictions when using `karate.callSingle()` especially within [`karate-config.js`](#karate-configjs). Ideally you should return only *pure* JSON data (or a primitive string, number etc.). Keep in mind that the reason this exists is to "cache" data, and *not* behavior. So if you return complex objects such as a custom Java instance or a JS function that depends on complex objects, this [will cause issues when you run in parallel](https://github.com/intuit/karate/issues/1558).

#### `configure callSingleCache`
When re-running tests in development mode and when your test suite depends on say an `Authorization` header set by [`karate.callSingle()`](#karatecallsingle), you can cache the results locally to a file, which is very convenient when your "auth token" is valid for a period of a few minutes - which typically is the case. This means that as long as the token "on file" is valid, you can save time by not having to make the one or two HTTP calls needed to "sign-in" or create "throw-away" users in your SSO store.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.intuit.karate.core.parallel;

/**
*
* @author pthomas3
*/
public class Hello {

public static String sayHello(String message) {
return "hello " + message;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ Scenario:
* match response == { message: 'from config' }

* def functionFromCallSingleFromConfig = function(){ return 'resultFromFunctionFromCallSingleFromConfig' }
* def Hello = Java.type('com.intuit.karate.core.parallel.Hello')
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ function fn() {
};
var result = karate.callSingle('call-single-from-config.feature', config);
config.message = result.response.message;
// this will throw the [Multi threaded access requested by thread xxx but is not allowed for language(s) js.] error
// config.Hello = result.Hello;
var result2 = karate.callSingle('call-single-from-config2.feature', result);
config.message2 = result2.message;
return config;
Expand Down

0 comments on commit b1dbb1d

Please sign in to comment.