Bug 46359 - BSF JavaScript Preprocessor cannot access sampler variable on first interation
BSF JavaScript Preprocessor cannot access sampler variable on first interation
Product: JMeter
Classification: Unclassified
Component: Main
PC Mac OS X 10.4
: P2 normal (vote)
: ---
Assigned To: JMeter issues mailing list
Depends on:
  Show dependency tree
Reported: 2008-12-07 04:14 UTC by Thomas Johnson
Modified: 2008-12-22 07:54 UTC (History)
0 users

Test Case illustrating issue (9.37 KB, application/octet-stream)
2008-12-07 04:14 UTC, Thomas Johnson

Note You need to log in before you can comment on or make changes to this bug.
Description Thomas Johnson 2008-12-07 04:14:22 UTC
Created attachment 22999 [details]
Test Case illustrating issue

The BSF JavaScript Preprocessor is unable to access the Sampler variable in the first iteration, citing the following exception:

org.apache.bsf.BSFException: JavaScript Error: Internal Error: org.mozilla.javascript.EcmaError: ReferenceError: "sampler" is not defined.

Steps to Reproduce:
1-Create a Thread Group set to run for two or more iterations
2-Add a Sampler to the Thread Group (tested with an HTTP Sampler)
3-Add a BSF PreProcessor to the Sampler
4-Use javascript as the BSF Language
5-Enter a simple script that references the "sampler" variable. Tested with OUT.println("hello"); OUT.println(sampler);

Expected Results:
 - All iterations produce console output "hello\n[sampler information]"

Actual Results:
 - First Iteration, only "hello" is output to console.
 - Log Output: 2008/12/07 12:21:51 WARN  - jmeter.modifiers.BSFPreProcessor: Problem in BSF script org.apache.bsf.BSFException: JavaScr
ipt Error: Internal Error: org.mozilla.javascript.EcmaError: ReferenceError: "sampler" is not defined. 
 - Second and further iterations "hello\n[sampler information]"

Affects Versions:
 - Trunk revision 722843
 - Release 2.3.2
Comment 1 Thomas Johnson 2008-12-07 04:52:23 UTC
In version 2.3.2, this issue affects both the Pre and Post Processors. As of the current trunk (722032) this only affects the Pre Processor.
Comment 2 Sebb 2008-12-08 07:13:15 UTC
Thanks for the report and test case.

This appears to be due to a bug in either BSF or Rhino, not JMeter.

It does not appear to be possible to define a script variable with a null value to Javascript using BSF. Furthermore, any variables which are defined after the null value will also be inaccessible.

The null variable in this case is "prev", so a work-round is to ensure that there is at least one previous sample result.

The JMeter code can be rearanged to define the "prev" variable last, but obviously that is also only a work-round.
Comment 3 Thomas Johnson 2008-12-08 08:48:03 UTC
Some research indicates that BSF used to have an issue with registering (but not declaring) a null bean back in mid-2004 (Ref 1). 

From the snippet below, a quick run of the test case shows that BSF Jexl handles the null "prev" object just fine. As BSF Jexl and JS get set up by the same code, it is most likely that the issue lies in or close to Rhino itself.

-- Test Case output --
BSF Jexl
Context: false
Variables: false
Sampler: false
Context: false
Variables: false

-- References --
(1) Commits fixing BSF issue 20355: http://markmail.org/message/ymna5ppb4556og4k
Comment 4 Sebb 2008-12-08 09:57:35 UTC
As far as I can tell, the problem is in BSF, in the Javascript engine wrapper.

I raised https://issues.apache.org/jira/browse/BSF-22 for the error.
Comment 5 Sebb 2008-12-08 11:04:44 UTC
I've added a temporary work-round in r724446 and r724445.

This is in the current nightlies (from r724447) if you want to try it out.
Comment 6 Sebb 2008-12-22 07:54:40 UTC
Please reopen if the fix does not work