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

Add documentation for concurrency helpers #1173

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add AsyncConditionsDocSpec
  • Loading branch information
thokari committed May 31, 2020
commit da44202e48b2647ff0dbf203407fd4bcf9359f84
29 changes: 29 additions & 0 deletions docs/utilities.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,32 @@ include::{sourcedir}/utilities/MutableClockDocSpec.groovy[tag=age-filter-spec]

There are many more ways to modify `MutableClock` just have a look at the JavaDocs, or the test code `spock.util.time.MutableClockSpec`.

[[async-conditions]]
== Evaluating conditions asynchronously with `AsyncConditions`

The utility class `AsyncConditions` can be helpful when working with asynchronous assertions. These are assertions made
from a different thread than the one running the test, for example when working with callback functions.
The individual assertions are collected, and in the asserting block, e.g. `then`, the `await` method is called to
verify the individual results. If any of the results is not available when `await` is called, it will also fail.
The amount of seconds to wait for can be specified as a parameter, see the example below. The default is `1.0` seconds.
Additionally, the number of expected evaluations must be provided initially, the default value is `1`.

=== Example

.Test
[source,groovy,indent=0]
----
include::{sourcedir}/utilities/concurrent/AsyncConditionsDocSpec.groovy[tag=async-conditions-spec]
----
<1> create a default `AsyncConditions` object (expecting a single evaluation)
<2> A new thread is created, and code is passed to be run from it. This could also happen implicitly, when working with
a method expecting a callback parameter.
<3> pass any code that wants to do assertions to the `evaluate` method
<4> finally, call the `await` method

<5> create an `AsyncConditions` object (expecting three evaluations)
<6> call `evaluate` multiple times
<7> call `await` in the end, specifying 2.5 seconds as the timeout

For more information have a look at the test code `spock.util.concurrent.AsyncConditionsSpec`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As it is rather uncommon to use AsyncConditions in production code, it might be helpful to show its usage via a Mock/Spy as this would be closer to its real world usage.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Production code? I'm not sure I understand, could you elaborate on what you have in mind here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, you somehow need to trigger these conditions.evaluate calls. The current examples show the direct usage in the test code via Thread.start. While that is a nice bare bones example, it does not really help the user to see how he can use that to test his actual production code.
One way, is that you'd define a Spy and then do something like service.foo() >> { conditions.evaluate { ... }; callRealMethod()}.


Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.spockframework.docs.utilities.concurrent

import spock.lang.Specification
import spock.util.concurrent.AsyncConditions

import java.util.concurrent.TimeUnit

class AsyncConditionsDocSpec extends Specification {

// tag::async-conditions-spec[]
def "example for single passing evaluation"() {
def conditions = new AsyncConditions() // <1>

when:
Thread.start { // <2>
conditions.evaluate { //<3>
assert true
}
}

then:
conditions.await() // <4>
}

def "example for multiple passing evaluations"() {
def conditions = new AsyncConditions(3) // <5>

when:
Thread.start {
conditions.evaluate { // <6>
assert true
assert true
}
conditions.evaluate { // <6>
assert true
}
}

and:
Thread.start {
conditions.evaluate { // <6>
assert true
}
}

then:
conditions.await(2.5) // <7>
}
}
// end::async-conditions-spec[]