Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

background executed for each row even for dynamic scenario outline #1793

Closed
ptrthomas opened this issue Oct 6, 2021 · 14 comments
Closed

background executed for each row even for dynamic scenario outline #1793

ptrthomas opened this issue Oct 6, 2021 · 14 comments
Assignees
Milestone

Comments

@ptrthomas
Copy link
Member

here's a single feature to replicate. thanks to @naganjaneyuluyazali who reported this in #1792

Feature:

Background:
* print 'in background'
* def data = [{ foo: 'one' }, { foo: 'two' }]

Scenario Outline:
* print __row

Examples:
| data |

and the print is seen 3 times:

Running com.intuit.karate.junit4.dev.DevRunner
16:02:01.310 [main] DEBUG com.intuit.karate.Suite - [config] classpath:karate-base.js
16:02:01.313 [main] DEBUG com.intuit.karate.Suite - [config] classpath:com/intuit/karate/junit4/dev/karate-config.js
16:02:02.009 [main] INFO  com.intuit.karate - [print] in background 
16:02:02.054 [main] INFO  com.intuit.karate - [print] in background 
16:02:02.055 [main] DEBUG c.intuit.karate.core.ScenarioEngine - over-writing existing variable 'data' with new value: [{ foo: 'one' }, { foo: 'two' }]
16:02:02.056 [main] INFO  com.intuit.karate - [print] {
  "foo": "one"
}
 
16:02:02.066 [main] INFO  com.intuit.karate - [print] in background 
16:02:02.066 [main] DEBUG c.intuit.karate.core.ScenarioEngine - over-writing existing variable 'data' with new value: [{ foo: 'one' }, { foo: 'two' }]
16:02:02.067 [main] INFO  com.intuit.karate - [print] {
  "foo": "two"
}

@joelpramos this is a surprise to me, I thought we were not re-doing Background in this case

@joelpramos
Copy link
Contributor

I once fixed it but for some reason had to go back to this behaviour which keeps consistency with the “normal” scenarios. Can’t remember where we discussed this (maybe in PR code review) but my gut feeling / wishful thinking is that it was done in the battle against Graal variables shared across context in multi threads.

Give it a shot at reverting that line and unit test in this commit:
df792f3

@ptrthomas
Copy link
Member Author

ptrthomas commented Oct 6, 2021

@joelpramos thanks, yes I kind of remember that. will take a look if someone does not pick this up for hacktoberfest

perhaps a solution or workaround may be to use callonce. so here below, the in fun print occurs only once:

just a thought, maybe we should intro a @nobackground tag, hmmm

EDIT: don't think we will go down that route but better tag is @background=false etc.

Feature:

Background:
* print 'in background'
* def fun = function(){ karate.log('in fun'); return [{ foo: 'one' }, { foo: 'two' }] }
* def data = callonce fun

Scenario Outline:
* print 'in row', __row

Examples:
| data |

@naganjaneyuluyazali
Copy link

I just tried workaround mentioned - but it's printing more than once.

04:41:08.248 [main] DEBUG com.intuit.karate.Suite - [config] classpath:karate-config.js
04:41:10.318 [main] INFO com.intuit.karate - [print] in background
04:41:10.334 [main] INFO com.intuit.karate - >> lock acquired, begin callonce: fun
04:41:10.334 [main] INFO com.intuit.karate - in fun
04:41:10.349 [main] INFO com.intuit.karate - << lock released, cached callonce: fun
04:41:10.365 [main] INFO com.intuit.karate - [print] in background
04:41:10.365 [main] DEBUG c.intuit.karate.core.ScenarioEngine - over-writing existing variable 'fun' with new value: function(){ karate.log('in fun'); return [{ foo: 'one' }, { foo: 'two' }] }
04:41:10.365 [main] DEBUG c.intuit.karate.core.ScenarioEngine - over-writing existing variable 'data' with new value: callonce fun
04:41:10.381 [main] INFO com.intuit.karate - [print] in row {
"foo": "one"
}

04:41:10.459 [main] INFO com.intuit.karate - [print] in background
04:41:10.459 [main] DEBUG c.intuit.karate.core.ScenarioEngine - over-writing existing variable 'fun' with new value: function(){ karate.log('in fun'); return [{ foo: 'one' }, { foo: 'two' }] }
04:41:10.459 [main] DEBUG c.intuit.karate.core.ScenarioEngine - over-writing existing variable 'data' with new value: callonce fun
04:41:10.459 [main] INFO com.intuit.karate - [print] in row {
"foo": "two"
}

@ptrthomas
Copy link
Member Author

ptrthomas commented Oct 6, 2021

@naganjaneyuluyazali works for me on the develop branch :) anyway this is an open bug, thanks.

@ptrthomas
Copy link
Member Author

@naganjaneyuluyazali maybe you will be interested to contribute code to fix this :)

@naganjaneyuluyazali
Copy link

I'm very good at breaking the things as Quality guy :) but not yet fixing the code.

@ptrthomas
Copy link
Member Author

@naganjaneyuluyazali and I think you missed my point. the print of in fun occurs only once. so the point is if you want some JS or Java code run only once, use a callonce. is that clear ?

@naganjaneyuluyazali
Copy link

okay got it ...intially I thought the temp fix you provided is for not re-doing Background .
Is there any workaround or trick to stop re-doing background :)

@ptrthomas
Copy link
Member Author

@naganjaneyuluyazali not until the bug is fixed :)

@naganjaneyuluyazali
Copy link

Can I be part of your karate developers community

@ptrthomas
Copy link
Member Author

@joelpramos
Copy link
Contributor

@naganjaneyuluyazali can you validate with the latest in develop?

@naganjaneyuluyazali
Copy link

It's working now ..thank you nd developers for the fix.

@ptrthomas
Copy link
Member Author

1.2.0 released

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants