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

Enhance PollingConditions usage in tests #13985

Merged
merged 1 commit into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,18 @@ class FutureTaskPromiseFactorySpec extends Specification {
list.onComplete { List l -> result = l }

then: 'the result is correct'
new PollingConditions(timeout: 5).eventually {
result
result == [2, 4]
new PollingConditions(timeout: 5, delay: 0.2).eventually {
assert result
assert result == [2, 4]
}

when: 'a promise list is created from two closures'
list = Promises.createPromise({ 2 + 2 }, { 4 + 4 })
list.onComplete { result = it }

then: 'the result is correct'
new PollingConditions(timeout: 5).eventually {
result == [4, 8]
new PollingConditions(timeout: 5, delay: 0.2).eventually {
assert result == [4, 8]
}
}

Expand Down Expand Up @@ -92,9 +92,9 @@ class FutureTaskPromiseFactorySpec extends Specification {

then: 'the onComplete handler is invoked and the onError handler is ignored'
thrown(ExecutionException)
new PollingConditions(timeout: 5).eventually {
!result
error
new PollingConditions(timeout: 5, delay: 0.2).eventually {
assert !result
assert error
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ class PromiseListSpec extends Specification {
list.onComplete { result = it }

then: 'then the result from onComplete is correct'
new PollingConditions(timeout: 5).eventually {
result == [1, 2, 3]
new PollingConditions(timeout: 5, delay: 0.2).eventually {
assert result == [1, 2, 3]
}
}

Expand All @@ -53,8 +53,8 @@ class PromiseListSpec extends Specification {
list.onComplete { result = it }

then: 'then the result from onComplete is correct'
new PollingConditions(timeout: 5).eventually {
result == [1, 2, 3]
new PollingConditions(timeout: 5, delay: 0.2).eventually {
assert result == [1, 2, 3]
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ class PromiseMapSpec extends Specification {
map.onComplete { result = it }

then: 'an appropriately populated map is returned to the onComplete event'
new PollingConditions(timeout: 5).eventually {
result
result['one'] == 1
result['four'] == 4
result['eight'] == 8
new PollingConditions(timeout: 5, delay: 0.2).eventually {
assert result
assert result['one'] == 1
assert result['four'] == 4
assert result['eight'] == 8
}
}

Expand All @@ -52,11 +52,11 @@ class PromiseMapSpec extends Specification {
map.onComplete { result = it }

then: 'an appropriately populated map is returned to the onComplete event'
new PollingConditions(timeout: 5).eventually {
result
result['one'] == 1
result['four'] == 4
result['eight'] == 8
new PollingConditions(timeout: 5, delay: 0.2).eventually {
assert result
assert result['one'] == 1
assert result['four'] == 4
assert result['eight'] == 8
}
}

Expand All @@ -69,13 +69,14 @@ class PromiseMapSpec extends Specification {
map['eight'] = { 4 * 2 }
def result = null
map.onComplete { result = it }
sleep 300

then: 'an appropriately populated map is returned to the onComplete event'
result
result['one'] == 1
result['four'] == 4
result['eight'] == 8
new PollingConditions(timeout: 5, delay: 0.2, initialDelay: 0.3).eventually {
assert result
assert result['one'] == 1
assert result['four'] == 4
assert result['eight'] == 8
}
}

void 'Test that a PromiseMap triggers onError for an exception and ignores onComplete'() {
Expand All @@ -89,12 +90,13 @@ class PromiseMapSpec extends Specification {
Throwable err = null
map.onComplete { result = it }
map.onError { err = it }
sleep 300

then: 'An appropriately populated map is returned to the onComplete event'
!result
err
err.message == 'java.lang.RuntimeException: bad'
new PollingConditions(timeout: 5, delay: 0.2, initialDelay: 0.3).eventually {
assert !result
assert err
assert err.message == 'java.lang.RuntimeException: bad'
}
}

@PendingFeature(reason = '''
Expand Down
30 changes: 15 additions & 15 deletions grails-async/core/src/test/groovy/grails/async/PromiseSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,20 @@ class PromiseSpec extends Specification {

when: 'a promise list is created from two promises'
def p1 = Promises.createPromise {
sleep 200
sleep 200 // simulate long running task
1 + 1
}
def p2 = Promises.createPromise {
sleep 200
sleep 200 // simulate long running task
2 + 2
}
def list = Promises.createPromise(p1, p2)
def result = null
list.onComplete { result = it }

then: 'the result is correct'
new PollingConditions(timeout: 5).eventually {
result == [2, 4]
new PollingConditions(timeout: 5, delay: 0.2).eventually {
assert result == [2, 4]
}
}

Expand All @@ -95,17 +95,17 @@ class PromiseSpec extends Specification {
list.onComplete { result = it }

then: 'the result is correct'
new PollingConditions(timeout: 5).eventually {
result == [2, 4]
new PollingConditions(timeout: 5, delay: 0.2).eventually {
assert result == [2, 4]
}

when: 'a promise list is created from two closures'
list = Promises.createPromise({ 3 + 3 }, { 4 + 4 })
list.onComplete { result = it }

then: 'the result is correct'
new PollingConditions(timeout: 5).eventually {
result == [6, 8]
new PollingConditions(timeout: 5, delay: 0.2).eventually {
assert result == [6, 8]
}
}

Expand All @@ -119,9 +119,9 @@ class PromiseSpec extends Specification {
promise.onError { hasError = true }

then: 'the onComplete handler is invoked and the onError handler is ignored'
new PollingConditions(timeout: 5).eventually {
result == 2
hasError == false
new PollingConditions(timeout: 5, delay: 0.2).eventually {
assert result == 2
assert hasError == false
}
}

Expand All @@ -135,10 +135,10 @@ class PromiseSpec extends Specification {
promise.onError { error = it }

then: 'the onComplete handler is invoked and the onError handler is ignored'
new PollingConditions(timeout: 5).eventually {
!result
error
error.message.contains('bad')
new PollingConditions(timeout: 5, delay: 0.2).eventually {
assert !result
assert error
assert error.message.contains('bad')
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,17 @@ class SynchronousPromiseFactorySpec extends Specification {
list.onComplete { result = it }

then: 'the result is correct'
new PollingConditions(timeout: 5).eventually {
result == [2, 4]
new PollingConditions(timeout: 5, delay: 0.2).eventually {
assert result == [2, 4]
}

when: 'a promise list is created from two closures'
list = Promises.createPromise({ 3 + 3 }, { 4 + 4 })
list.onComplete { result = it }

then: 'the result is correct'
new PollingConditions(timeout: 5).eventually {
result == [6, 8]
new PollingConditions(timeout: 5, delay: 0.2).eventually {
assert result == [6, 8]
}
}

Expand All @@ -90,9 +90,9 @@ class SynchronousPromiseFactorySpec extends Specification {
promise.onError { hasError = true }

then: 'The onComplete handler is invoked and the onError handler is ignored'
new PollingConditions(timeout: 5).eventually {
result == 2
hasError == false
new PollingConditions(timeout: 5, delay: 0.2).eventually {
assert result == 2
assert hasError == false
}
}

Expand All @@ -106,10 +106,10 @@ class SynchronousPromiseFactorySpec extends Specification {
promise.onError { error = it }

then: 'the onComplete handler is invoked and the onError handler is ignored'
new PollingConditions(timeout: 5).eventually {
!result
error
error.message == 'bad'
new PollingConditions(timeout: 5, delay: 0.2).eventually {
assert !result
assert error
assert error.message == 'bad'
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ class GparsPromiseSpec extends Specification {
promiseList.onComplete { List v -> result = v }

then: 'the result is correct'
new PollingConditions(timeout: 5).eventually {
result == [2, 4]
new PollingConditions(timeout: 5, delay: 0.2).eventually {
assert result == [2, 4]
}

when: 'a promise list is created from two closures'
Expand All @@ -90,8 +90,8 @@ class GparsPromiseSpec extends Specification {
promiseList.onComplete { List v -> result = v }

then: 'The result is correct'
new PollingConditions(timeout: 5) {
result == [2,4]
new PollingConditions(timeout: 5, delay: 0.2).eventually {
assert result == [2,4]
}
}

Expand All @@ -105,9 +105,9 @@ class GparsPromiseSpec extends Specification {
promise.onError { hasError = true }

then: 'the onComplete handler is invoked and the onError handler is ignored'
new PollingConditions(timeout: 5).eventually {
result == 2
hasError == false
new PollingConditions(timeout: 5, delay: 0.2).eventually {
assert result == 2
assert hasError == false
}
}

Expand All @@ -121,10 +121,10 @@ class GparsPromiseSpec extends Specification {
promise.onError { error = it }

then: 'the onError handler is invoked and the onComplete handler is ignored'
new PollingConditions(timeout: 5).eventually {
!result
error
error.message == 'bad'
new PollingConditions(timeout: 5, delay: 0.2).eventually {
assert !result
assert error
assert error.message == 'bad'
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class RxJavaPromiseListSpec extends Specification{
list.onComplete { result = it }

then: 'then the result from onComplete is correct'
new PollingConditions(timeout: 5).eventually {
result == [1,2,3]
new PollingConditions(timeout: 5, delay: 0.2).eventually {
assert result == [1,2,3]
}
}

Expand All @@ -37,8 +37,8 @@ class RxJavaPromiseListSpec extends Specification{
list.onComplete { result = it }

then: 'then the result from onComplete is correct'
new PollingConditions(timeout: 5).eventually {
result == [1,2,3]
new PollingConditions(timeout: 5, delay: 0.2).eventually {
assert result == [1,2,3]
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ class RxJavaPromiseMapSpec extends Specification{
map.onComplete { result = it }

then: 'an appropriately populated map is returned to the onComplete event'
new PollingConditions(timeout: 5).eventually {
result
result['one'] == 1
result['four'] == 4
result['eight'] == 8
new PollingConditions(timeout: 5, delay: 0.2).eventually {
assert result
assert result['one'] == 1
assert result['four'] == 4
assert result['eight'] == 8
}
}

Expand All @@ -37,11 +37,11 @@ class RxJavaPromiseMapSpec extends Specification{
map.onComplete { result = it }

then: 'an appropriately populated map is returned to the onComplete event'
new PollingConditions(timeout: 5).eventually {
result
result['one'] == 1
result['four'] == 4
result['eight'] == 8
new PollingConditions(timeout: 5, delay: 0.2).eventually {
assert result
assert result['one'] == 1
assert result['four'] == 4
assert result['eight'] == 8
}
}

Expand All @@ -56,11 +56,11 @@ class RxJavaPromiseMapSpec extends Specification{
map.onComplete { result = it }

then: 'an appropriately populated map is returned to the onComplete event'
new PollingConditions(timeout: 5).eventually {
result
result['one'] == 1
result['four'] == 4
result['eight'] == 8
new PollingConditions(timeout: 5, delay: 0.2).eventually {
assert result
assert result['one'] == 1
assert result['four'] == 4
assert result['eight'] == 8
}
}

Expand All @@ -79,10 +79,10 @@ class RxJavaPromiseMapSpec extends Specification{
map.onError { error = it }

then: 'an appropriately populated map is returned to the onComplete event'
new PollingConditions(timeout: 5).eventually {
!result
error
error.message == 'bad'
new PollingConditions(timeout: 5, delay: 0.2).eventually {
assert !result
assert error
assert error.message == 'bad'
}
}

Expand Down
Loading
Loading