Re: [Cucumber] [JVM]Asynchronous step execution with cucumber-jvm

86 views
Skip to first unread message

Thomas Sundberg

unread,
Feb 10, 2016, 1:04:26 AM2/10/16
to cu...@googlegroups.com
On 9 February 2016 at 20:58, Aniruddha chatterjee
<anirudd...@gmail.com> wrote:
> Consider the following cucumber scenario:-
>
> Scenario: Test payment
> Given I login to terminal
> When POS token is generated asynchronously
> Then user generates mobile token
> And payment is successful
>
> The step "POS token is generated asynchronously" needs to execute
> asynchronously and should not block the execution of downstream steps after
> it. I was able to get it done with FutureTask in Java. However in case of
> failures I am not able to assert the failures. Below is the code snippet
>
> @When("^POS token is generated asynchronously$")
> public void gs_Consumer() throws Throwable {
>
> HashMap<String, Object> m = DataContainer.getDataMap();
>
> ExecutorService executor = Executors.newFixedThreadPool(2);
> FutureTask<Object> futureTask1 = null;
>
> futureTask1 = new FutureTask<Object>(new Callable<Object>() {
>
> public Object call() throws Exception {
>
> DataContainer.setDataMap(m);
>
> try {
> retrieve_consumer_information();
> } catch (Throwable e) {
> DataContainer.getDataMap().put("exception", e);
> throw new Exception(e);
> }
> return null;
> }
> });
>
> executor.execute(futureTask1);
>
> DataContainer.getDataMap().put("response", futureTask1);
> // Shutdown the ExecutorService
> executor.shutdown();
> }
>
> Then I get the response in the After method since I cannot do a
> futureTask1.get() inside this method as it will block the execution from
> executing the other downstream steps.
>
> public void afterAsynchMethod() {
>
> try {
> ((FutureTask<Object>)
> DataContainer.getDataMap().get("response")).get();
> } catch (InterruptedException | ExecutionException e) {
> // TODO Auto-generated catch block
> Assert.fail(e.getMessage());
> }
>
> }
>
> Now if the exception happens in the After method the scenario still is not
> reflected as a failed scenario. How do I fail the scenario in this case or
> any other ways of doing this?
>

This is probably not the answer you would like to hear.

You are trying to solve a complicated technical issue here. You are
trying to handle asynchronous communication. What you would like to do
is describe the wanted behaviour that a product owner can validate.
And implement that wanted behaviour.

My approach would be to implement the wanted behaviour using fake
services that are predictable. These predictable fake services should
fail during certain conditions giving you the ability to describe, and
implement, your internal behaviour to handle the failures properly. I
would also implement a fake service that doesn't fail and therefore
get a possibility to implement the happy path scenario.

Faking the external services would give me the chance to handle things
like timeout without waiting the stipulated timeout period. It would
give me a fast and reliable suite of scenarios where I am in control.

Checking the communication with the real, external, service is
something I wouldn't try to do using Cucumber and Gherkin. I would
implement a minimal number of tests, written in say JUnit, that
actually uses the real service. But notice that I say minimal number.
These tests would only verify that I have implemented the protocol to
the external service properly. They would probably be slow and
potential flaky. But I would isolate the slowness and flakiness in one
or two tests. I would not implement a complete test suite for the
external services, that I might not even own. I would assume that the
external services are bug free and follow their contract. If bug still
occurs, I would prove the bug with a test of the external service,
change my failing fakes and file bug report to the service provider.

This is very different compared to your question. But the approach you
seem to be aiming towards is a potential dangerous way that I would
advice against.

Cheers
Thomas


--
Thomas Sundberg
M. Sc. in Computer Science

Mobile: +46 70 767 33 15
Blog: http://thomassundberg.wordpress.com/
Twitter: @thomassundberg

Better software through faster feedback
Reply all
Reply to author
Forward
0 new messages