From b1dbb1d16b748ae79c63d6bf7f37eab344c21c5b Mon Sep 17 00:00:00 2001 From: Peter Thomas Date: Fri, 16 Apr 2021 20:54:29 +0530 Subject: [PATCH] wontfix for #1558 and adding docs for callSingle() limitations --- README.md | 4 +++- .../java/com/intuit/karate/core/parallel/Hello.java | 13 +++++++++++++ .../core/parallel/call-single-from-config.feature | 1 + .../intuit/karate/core/parallel/karate-config.js | 2 ++ 4 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 karate-core/src/test/java/com/intuit/karate/core/parallel/Hello.java diff --git a/README.md b/README.md index 74a160b21..6f64d4c1d 100755 --- a/README.md +++ b/README.md @@ -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. @@ -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. diff --git a/karate-core/src/test/java/com/intuit/karate/core/parallel/Hello.java b/karate-core/src/test/java/com/intuit/karate/core/parallel/Hello.java new file mode 100644 index 000000000..fcb9f0a35 --- /dev/null +++ b/karate-core/src/test/java/com/intuit/karate/core/parallel/Hello.java @@ -0,0 +1,13 @@ +package com.intuit.karate.core.parallel; + +/** + * + * @author pthomas3 + */ +public class Hello { + + public static String sayHello(String message) { + return "hello " + message; + } + +} diff --git a/karate-core/src/test/java/com/intuit/karate/core/parallel/call-single-from-config.feature b/karate-core/src/test/java/com/intuit/karate/core/parallel/call-single-from-config.feature index b25d99d77..968bc7667 100644 --- a/karate-core/src/test/java/com/intuit/karate/core/parallel/call-single-from-config.feature +++ b/karate-core/src/test/java/com/intuit/karate/core/parallel/call-single-from-config.feature @@ -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') diff --git a/karate-core/src/test/java/com/intuit/karate/core/parallel/karate-config.js b/karate-core/src/test/java/com/intuit/karate/core/parallel/karate-config.js index 736239511..8d69df974 100644 --- a/karate-core/src/test/java/com/intuit/karate/core/parallel/karate-config.js +++ b/karate-core/src/test/java/com/intuit/karate/core/parallel/karate-config.js @@ -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;