diff --git a/spock-specs/src/test/groovy/spock/util/concurrent/PollingConditionsSpec.groovy b/spock-specs/src/test/groovy/spock/util/concurrent/PollingConditionsSpec.groovy index 07bd9f1673..85dd3c1917 100644 --- a/spock-specs/src/test/groovy/spock/util/concurrent/PollingConditionsSpec.groovy +++ b/spock-specs/src/test/groovy/spock/util/concurrent/PollingConditionsSpec.groovy @@ -14,23 +14,26 @@ package spock.util.concurrent +import org.spockframework.EmbeddedSpecification import org.spockframework.runtime.ConditionNotSatisfiedError +import org.spockframework.runtime.GroovyRuntimeUtil import org.spockframework.runtime.SpockTimeoutError import spock.lang.Issue import spock.lang.PendingFeature +import spock.lang.PendingFeatureIf +import spock.lang.Requires import spock.lang.Retry -import spock.lang.Specification -class PollingConditionsSpec extends Specification { +class PollingConditionsSpec extends EmbeddedSpecification { PollingConditions conditions = new PollingConditions() - def defConditions = new PollingConditions() - - volatile int num = 0 - volatile String str = null private static def noArgClosure = { "test" } private static def throwableArgClosure = { Throwable err -> err.getClass().simpleName } + def setup() { + runner.addClassImport(PollingConditions) + } + def "defaults"() { expect: with(conditions) { @@ -41,94 +44,336 @@ class PollingConditionsSpec extends Specification { } } + @PendingFeatureIf(value = { !data.conditionMethod }, reason = "Known limitation") @Retry - def "succeeds if all conditions are eventually satisfied"() { - num = 42 - Thread.start { - sleep(500) - str = "hello" - } + def "succeeds if all conditions are eventually satisfied with condition method '#conditionMethod' and field"() { + when: + runner.runSpecBody """ + PollingConditions conditions = new PollingConditions() + + volatile int num = 0 + volatile String str = null + + def 'a feature'() { + num = 42 + Thread.start { + sleep(500) + str = "hello" + } + + expect: + conditions$conditionMethod { + num == 42 + str == "hello" + } + } + """ + + then: + noExceptionThrown() + where: + conditionMethod << [".eventually", ".call", ""] + } + + @Retry + def "succeeds if all conditions are eventually satisfied with condition method '#conditionMethod' and local variable"() { when: - conditions.eventually { - num == 42 - str == "hello" - } + runner.runSpecBody """ + volatile int num = 0 + volatile String str = null + + def 'a feature'() { + PollingConditions conditions = new PollingConditions() + num = 42 + Thread.start { + sleep(500) + str = "hello" + } + + expect: + conditions$conditionMethod { + num == 42 + str == "hello" + } + } + """ then: noExceptionThrown() + + where: + conditionMethod << [".eventually", ".call", ""] } - def "fails if any condition isn't satisfied in time"() { - num = 42 + @PendingFeatureIf(value = { !data.conditionMethod }, reason = "Known limitation") + def "fails if any condition isn't satisfied in time with condition method '#conditionMethod' and field"() { + when: + runner.runSpecBody """ + PollingConditions conditions = new PollingConditions() + + volatile int num = 0 + volatile String str = null + + def 'a feature'() { + num = 42 + expect: + conditions$conditionMethod { + num == 42 + str == "bye" + } + } + """ + + then: + thrown(SpockTimeoutError) + + where: + conditionMethod << [".eventually", ".call", ""] + } + + def "fails if any condition isn't satisfied in time with condition method '#conditionMethod' and local variable"() { when: - conditions.eventually { - num == 42 - str == "bye" - } + runner.runSpecBody """ + volatile int num = 0 + volatile String str = null + + def 'a feature'() { + PollingConditions conditions = new PollingConditions() + num = 42 + + expect: + conditions$conditionMethod { + num == 42 + str == "bye" + } + } + """ then: thrown(SpockTimeoutError) + + where: + conditionMethod << [".eventually", ".call", ""] } + @PendingFeatureIf(value = { !data.conditionMethod }, reason = "Known limitation") @Issue("https://github.com/spockframework/spock/issues/413") - def "reports failed condition of last failed attempt"() { - num = 42 - + def "reports failed condition of last failed attempt with condition method '#conditionMethod' and field"() { when: - conditions.eventually { - num == 42 - str == "bye" + runner.runSpecBody """ + PollingConditions conditions = new PollingConditions() + + volatile int num = 0 + volatile String str = null + + def 'a feature'() { + num = 42 + + expect: + conditions$conditionMethod { + num == 42 + str == "bye" + } + } + """ + + then: + SpockTimeoutError e = thrown() + with(e.cause) { + it instanceof ConditionNotSatisfiedError + condition.text == 'str == "bye"' } + where: + conditionMethod << [".eventually", ".call", ""] + } + + @Issue("https://github.com/spockframework/spock/issues/413") + def "reports failed condition of last failed attempt with condition method '#conditionMethod' and local variable"() { + when: + runner.runSpecBody """ + volatile int num = 0 + volatile String str = null + + def 'a feature'() { + PollingConditions conditions = new PollingConditions() + num = 42 + + expect: + conditions$conditionMethod { + num == 42 + str == "bye" + } + } + """ + then: SpockTimeoutError e = thrown() with(e.cause) { it instanceof ConditionNotSatisfiedError condition.text == 'str == "bye"' } + + where: + conditionMethod << [".eventually", ".call", ""] } - def "fails if condition is not met and assert keyword is used for def declared conditions object"() { - num = 50 + @Requires({ (GroovyRuntimeUtil.MAJOR_VERSION >= 3) || data.conditionMethod }) + def "fails if condition is not met and assert keyword is used for def declared conditions object with condition method '#conditionMethod' and field"() { + when: + runner.runSpecBody """ + def defConditions = new PollingConditions() + volatile int num = 0 + + def 'a feature'() { + num = 50 + expect: + defConditions$conditionMethod { + assert num == 42 + } + } + """ + + then: + thrown(SpockTimeoutError) + + where: + conditionMethod << [".eventually", ".call", ""] + } + + @Requires({ (GroovyRuntimeUtil.MAJOR_VERSION >= 3) || data.conditionMethod }) + def "fails if condition is not met and assert keyword is used for def declared conditions object with condition method '#conditionMethod' and local variable"() { when: - defConditions.eventually { - assert num == 42 - } + runner.runSpecBody """ + def defConditions = new PollingConditions() + volatile int num = 0 + + def 'a feature'() { + num = 50 + + expect: + defConditions$conditionMethod { + assert num == 42 + } + } + """ then: thrown(SpockTimeoutError) + + where: + conditionMethod << [".eventually", ".call", ""] } @PendingFeature(reason = "Known limitation") + @Requires({ (GroovyRuntimeUtil.MAJOR_VERSION >= 3) || data.conditionMethod }) @Issue("https://github.com/spockframework/spock/issues/1054") - def "fails if condition is not met and assert keyword is not used for def declared conditions object"() { - num = 50 + def "fails if condition is not met and assert keyword is not used for def declared conditions object with condition method '#conditionMethod' and field"() { + when: + runner.runSpecBody """ + def defConditions = new PollingConditions() + volatile int num = 0 + + def 'a feature'() { + num = 50 + + expect: + defConditions$conditionMethod { + num == 42 + } + } + """ + + then: + thrown(SpockTimeoutError) + + where: + conditionMethod << [".eventually", ".call", ""] + } + @PendingFeature(reason = "Known limitation") + @Requires({ (GroovyRuntimeUtil.MAJOR_VERSION >= 3) || data.conditionMethod }) + @Issue("https://github.com/spockframework/spock/issues/1054") + def "fails if condition is not met and assert keyword is not used for def declared conditions object with condition method '#conditionMethod' and local variable"() { when: - defConditions.eventually { - num == 42 - } + runner.runSpecBody """ + def defConditions = new PollingConditions() + volatile int num = 0 + + def 'a feature'() { + num = 50 + + expect: + defConditions$conditionMethod { + num == 42 + } + } + """ then: thrown(SpockTimeoutError) + + where: + conditionMethod << [".eventually", ".call", ""] } - def "can override timeout per invocation"() { - Thread.start { - Thread.sleep(250) - num = 42 - } + @PendingFeatureIf(value = { !data.conditionMethod }, reason = "Known limitation") + @Requires({ (GroovyRuntimeUtil.MAJOR_VERSION >= 3) || data.conditionMethod }) + def "can override timeout per invocation with condition method '#conditionMethod' and field"() { + when: + runner.runSpecBody """ + PollingConditions conditions = new PollingConditions() + + volatile int num = 0 + def 'a feature'() { + Thread.start { + Thread.sleep(250) + num = 42 + } + + expect: + conditions$conditionMethod(0) { + num == 42 + } + } + """ + + then: + thrown(SpockTimeoutError) + + where: + conditionMethod << [".within", ".call", ""] + } + + @Requires({ (GroovyRuntimeUtil.MAJOR_VERSION >= 3) || data.conditionMethod }) + def "can override timeout per invocation with condition method '#conditionMethod' and local variable"() { when: - conditions.within(0) { - num == 42 - } + runner.runSpecBody """ + volatile int num = 0 + + def 'a feature'() { + PollingConditions conditions = new PollingConditions() + Thread.start { + Thread.sleep(250) + num = 42 + } + + expect: + conditions$conditionMethod(0) { + num == 42 + } + } + """ then: thrown(SpockTimeoutError) + + where: + conditionMethod << [".within", ".call", ""] } @Retry