-
Notifications
You must be signed in to change notification settings - Fork 471
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
thokari
wants to merge
7
commits into
spockframework:master
Choose a base branch
from
thokari:1058-concurrent-utilties-docs
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
da44202
add AsyncConditionsDocSpec
thokari dd85cff
fix grammar
thokari f495e9d
add more docs, clean up
thokari 65b2caa
improve explanations and examples
thokari ec67cd6
add missing timeout hint
thokari b3344cc
decrease timeouts in doc specs
thokari 05e6d3e
Merge branch 'master' into 1058-concurrent-utilties-docs
leonard84 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...rc/test/groovy/org/spockframework/docs/utilities/concurrent/AsyncConditionsDocSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package org.spockframework.docs.utilities.concurrent | ||
|
||
import spock.lang.Specification | ||
import spock.util.concurrent.AsyncConditions | ||
|
||
class AsyncConditionsDocSpec extends Specification { | ||
|
||
// tag::async-conditions-spec[] | ||
def "example of single passing explicit evaluation"() { | ||
def conditions = new AsyncConditions() // <1> | ||
thokari marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
when: | ||
Thread.start { // <2> | ||
conditions.evaluate { //<3> | ||
assert true | ||
} | ||
} | ||
|
||
then: | ||
conditions.await() // <4> | ||
} | ||
|
||
def "example of multiple passing implicit evaluations"() { | ||
AsyncConditions conditions = new AsyncConditions(3) // <5> | ||
|
||
when: | ||
Thread.start { | ||
conditions.evaluate { // <6> | ||
true | ||
true | ||
} | ||
conditions.evaluate { // <6> | ||
true | ||
} | ||
} | ||
|
||
and: | ||
Thread.start { | ||
conditions.evaluate { // <6> | ||
true | ||
} | ||
} | ||
|
||
then: | ||
conditions.await(2.5) // <7> | ||
} | ||
// end::async-conditions-spec[] | ||
} |
48 changes: 48 additions & 0 deletions
48
.../test/groovy/org/spockframework/docs/utilities/concurrent/BlockingVariablesDocSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package org.spockframework.docs.utilities.concurrent | ||
|
||
import spock.lang.Specification | ||
import spock.util.concurrent.BlockingVariable | ||
import spock.util.concurrent.BlockingVariables | ||
|
||
class BlockingVariablesDocSpec extends Specification { | ||
|
||
// tag::blocking-variables-spec[] | ||
def "single variable is read before it is written"() { | ||
def list = new BlockingVariable<List<Integer>>() // <1> | ||
|
||
when: | ||
Thread.start { | ||
Thread.sleep(25) // <2> | ||
println "calling set" | ||
list.set([1, 2, 3]) | ||
} | ||
|
||
then: | ||
println "calling get, blocking" | ||
list.get() == [1, 2, 3] // <3> | ||
} | ||
|
||
def "example of multiple variables"() { | ||
def vars = new BlockingVariables(2.0) // <4> | ||
|
||
when: | ||
Thread.start { | ||
Thread.sleep(50) // <5> | ||
println "setting bar and baz" | ||
vars.bar = 2 | ||
vars.baz = 3 | ||
} | ||
Thread.start { | ||
Thread.sleep(25) // <5> | ||
println "setting foo" | ||
vars.foo = 1 | ||
} | ||
|
||
then: | ||
println "before comparison, blocking" | ||
vars.foo == 1 // <6> | ||
vars.bar == 2 | ||
vars.baz == 3 | ||
} | ||
// end::blocking-variables-spec[] | ||
} |
73 changes: 73 additions & 0 deletions
73
.../test/groovy/org/spockframework/docs/utilities/concurrent/PollingConditionsDocSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package org.spockframework.docs.utilities.concurrent | ||
|
||
import org.spockframework.runtime.ConditionNotSatisfiedError | ||
import org.spockframework.runtime.SpockTimeoutError | ||
import spock.lang.Specification | ||
import spock.util.concurrent.PollingConditions | ||
|
||
class PollingConditionsDocSpec extends Specification { | ||
// tag::polling-conditions-spec[] | ||
PollingConditions conditions = new PollingConditions() // <1> | ||
|
||
volatile int num = 0 | ||
volatile String str = null | ||
|
||
def "time controls and their default values"() { | ||
expect: | ||
with(conditions) { | ||
timeout == 1 | ||
initialDelay == 0 | ||
delay == 0.1 | ||
factor == 1 | ||
} | ||
} | ||
|
||
def "succeeds if all conditions are eventually satisfied"() { | ||
|
||
when: | ||
Thread.start { | ||
num = 42 | ||
sleep(25) // <2> | ||
str = "hello" | ||
} | ||
|
||
then: | ||
conditions.eventually { // <3> | ||
num == 42 | ||
} | ||
|
||
and: | ||
conditions.eventually { // <3> | ||
str == "hello" | ||
} | ||
|
||
and: | ||
noExceptionThrown() // <4> | ||
} | ||
|
||
def "fails if any condition isn't satisfied in time"() { | ||
|
||
given: | ||
Thread.start { | ||
num = 42 | ||
sleep(25) // milliseconds <2> | ||
str = "hello" | ||
} | ||
|
||
expect: | ||
conditions.within(0.05) { // seconds <5> | ||
num == 42 | ||
} | ||
|
||
when: | ||
conditions.eventually { // <3> | ||
num == 0 | ||
str == "bye" | ||
} | ||
|
||
then: | ||
def error = thrown(SpockTimeoutError) | ||
error.cause.message.contains('num == 0') // <6> | ||
} | ||
// end::polling-conditions-spec[] | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since Spock 2.0 uses Java 8
BlockingVariable
is kind of obsolete as there isCompletableFuture
, we are still debating whether to deprecate it or remove it entirely.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And this should be mentioned in the docs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, but if the class is going to be deprecated or deleted it does not need to be documented. ;-)