Skip to content

Commit a4c6bcd

Browse files
authored
test: enhance PollingConditions usage in tests (#13985)
- Use the `assert` keyword in `PollingConditions` conditions, as recommended in the [API docs](https://spockframework.org/spock/javadoc/2.3/spock/util/concurrent/PollingConditions.html) and [documentation](https://spockframework.org/spock/docs/2.3/all_in_one.html#_polling_conditions). - Increase the delay for `PollingConditions` from the default 100ms to 200ms. This adjustment tries to address flakiness observed in CI environments, providing more stability for tests.
1 parent 56957f7 commit a4c6bcd

File tree

18 files changed

+172
-171
lines changed

18 files changed

+172
-171
lines changed

grails-async/core/src/test/groovy/grails/async/FutureTaskPromiseFactorySpec.groovy

+8-8
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,18 @@ class FutureTaskPromiseFactorySpec extends Specification {
5252
list.onComplete { List l -> result = l }
5353

5454
then: 'the result is correct'
55-
new PollingConditions(timeout: 5).eventually {
56-
result
57-
result == [2, 4]
55+
new PollingConditions(timeout: 5, delay: 0.2).eventually {
56+
assert result
57+
assert result == [2, 4]
5858
}
5959

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

6464
then: 'the result is correct'
65-
new PollingConditions(timeout: 5).eventually {
66-
result == [4, 8]
65+
new PollingConditions(timeout: 5, delay: 0.2).eventually {
66+
assert result == [4, 8]
6767
}
6868
}
6969

@@ -92,9 +92,9 @@ class FutureTaskPromiseFactorySpec extends Specification {
9292

9393
then: 'the onComplete handler is invoked and the onError handler is ignored'
9494
thrown(ExecutionException)
95-
new PollingConditions(timeout: 5).eventually {
96-
!result
97-
error
95+
new PollingConditions(timeout: 5, delay: 0.2).eventually {
96+
assert !result
97+
assert error
9898
}
9999
}
100100

grails-async/core/src/test/groovy/grails/async/PromiseListSpec.groovy

+4-4
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ class PromiseListSpec extends Specification {
3737
list.onComplete { result = it }
3838

3939
then: 'then the result from onComplete is correct'
40-
new PollingConditions(timeout: 5).eventually {
41-
result == [1, 2, 3]
40+
new PollingConditions(timeout: 5, delay: 0.2).eventually {
41+
assert result == [1, 2, 3]
4242
}
4343
}
4444

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

5555
then: 'then the result from onComplete is correct'
56-
new PollingConditions(timeout: 5).eventually {
57-
result == [1, 2, 3]
56+
new PollingConditions(timeout: 5, delay: 0.2).eventually {
57+
assert result == [1, 2, 3]
5858
}
5959
}
6060

grails-async/core/src/test/groovy/grails/async/PromiseMapSpec.groovy

+21-19
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ class PromiseMapSpec extends Specification {
3333
map.onComplete { result = it }
3434

3535
then: 'an appropriately populated map is returned to the onComplete event'
36-
new PollingConditions(timeout: 5).eventually {
37-
result
38-
result['one'] == 1
39-
result['four'] == 4
40-
result['eight'] == 8
36+
new PollingConditions(timeout: 5, delay: 0.2).eventually {
37+
assert result
38+
assert result['one'] == 1
39+
assert result['four'] == 4
40+
assert result['eight'] == 8
4141
}
4242
}
4343

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

5454
then: 'an appropriately populated map is returned to the onComplete event'
55-
new PollingConditions(timeout: 5).eventually {
56-
result
57-
result['one'] == 1
58-
result['four'] == 4
59-
result['eight'] == 8
55+
new PollingConditions(timeout: 5, delay: 0.2).eventually {
56+
assert result
57+
assert result['one'] == 1
58+
assert result['four'] == 4
59+
assert result['eight'] == 8
6060
}
6161
}
6262

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

7473
then: 'an appropriately populated map is returned to the onComplete event'
75-
result
76-
result['one'] == 1
77-
result['four'] == 4
78-
result['eight'] == 8
74+
new PollingConditions(timeout: 5, delay: 0.2, initialDelay: 0.3).eventually {
75+
assert result
76+
assert result['one'] == 1
77+
assert result['four'] == 4
78+
assert result['eight'] == 8
79+
}
7980
}
8081

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

9494
then: 'An appropriately populated map is returned to the onComplete event'
95-
!result
96-
err
97-
err.message == 'java.lang.RuntimeException: bad'
95+
new PollingConditions(timeout: 5, delay: 0.2, initialDelay: 0.3).eventually {
96+
assert !result
97+
assert err
98+
assert err.message == 'java.lang.RuntimeException: bad'
99+
}
98100
}
99101

100102
@PendingFeature(reason = '''

grails-async/core/src/test/groovy/grails/async/PromiseSpec.groovy

+15-15
Original file line numberDiff line numberDiff line change
@@ -68,20 +68,20 @@ class PromiseSpec extends Specification {
6868

6969
when: 'a promise list is created from two promises'
7070
def p1 = Promises.createPromise {
71-
sleep 200
71+
sleep 200 // simulate long running task
7272
1 + 1
7373
}
7474
def p2 = Promises.createPromise {
75-
sleep 200
75+
sleep 200 // simulate long running task
7676
2 + 2
7777
}
7878
def list = Promises.createPromise(p1, p2)
7979
def result = null
8080
list.onComplete { result = it }
8181

8282
then: 'the result is correct'
83-
new PollingConditions(timeout: 5).eventually {
84-
result == [2, 4]
83+
new PollingConditions(timeout: 5, delay: 0.2).eventually {
84+
assert result == [2, 4]
8585
}
8686
}
8787

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

9797
then: 'the result is correct'
98-
new PollingConditions(timeout: 5).eventually {
99-
result == [2, 4]
98+
new PollingConditions(timeout: 5, delay: 0.2).eventually {
99+
assert result == [2, 4]
100100
}
101101

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

106106
then: 'the result is correct'
107-
new PollingConditions(timeout: 5).eventually {
108-
result == [6, 8]
107+
new PollingConditions(timeout: 5, delay: 0.2).eventually {
108+
assert result == [6, 8]
109109
}
110110
}
111111

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

121121
then: 'the onComplete handler is invoked and the onError handler is ignored'
122-
new PollingConditions(timeout: 5).eventually {
123-
result == 2
124-
hasError == false
122+
new PollingConditions(timeout: 5, delay: 0.2).eventually {
123+
assert result == 2
124+
assert hasError == false
125125
}
126126
}
127127

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

137137
then: 'the onComplete handler is invoked and the onError handler is ignored'
138-
new PollingConditions(timeout: 5).eventually {
139-
!result
140-
error
141-
error.message.contains('bad')
138+
new PollingConditions(timeout: 5, delay: 0.2).eventually {
139+
assert !result
140+
assert error
141+
assert error.message.contains('bad')
142142
}
143143
}
144144

grails-async/core/src/test/groovy/grails/async/SynchronousPromiseFactorySpec.groovy

+11-11
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,17 @@ class SynchronousPromiseFactorySpec extends Specification {
6666
list.onComplete { result = it }
6767

6868
then: 'the result is correct'
69-
new PollingConditions(timeout: 5).eventually {
70-
result == [2, 4]
69+
new PollingConditions(timeout: 5, delay: 0.2).eventually {
70+
assert result == [2, 4]
7171
}
7272

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

7777
then: 'the result is correct'
78-
new PollingConditions(timeout: 5).eventually {
79-
result == [6, 8]
78+
new PollingConditions(timeout: 5, delay: 0.2).eventually {
79+
assert result == [6, 8]
8080
}
8181
}
8282

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

9292
then: 'The onComplete handler is invoked and the onError handler is ignored'
93-
new PollingConditions(timeout: 5).eventually {
94-
result == 2
95-
hasError == false
93+
new PollingConditions(timeout: 5, delay: 0.2).eventually {
94+
assert result == 2
95+
assert hasError == false
9696
}
9797
}
9898

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

108108
then: 'the onComplete handler is invoked and the onError handler is ignored'
109-
new PollingConditions(timeout: 5).eventually {
110-
!result
111-
error
112-
error.message == 'bad'
109+
new PollingConditions(timeout: 5, delay: 0.2).eventually {
110+
assert !result
111+
assert error
112+
assert error.message == 'bad'
113113
}
114114
}
115115

grails-async/gpars/src/test/groovy/grails/async/GparsPromiseSpec.groovy

+11-11
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ class GparsPromiseSpec extends Specification {
8080
promiseList.onComplete { List v -> result = v }
8181

8282
then: 'the result is correct'
83-
new PollingConditions(timeout: 5).eventually {
84-
result == [2, 4]
83+
new PollingConditions(timeout: 5, delay: 0.2).eventually {
84+
assert result == [2, 4]
8585
}
8686

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

9292
then: 'The result is correct'
93-
new PollingConditions(timeout: 5) {
94-
result == [2,4]
93+
new PollingConditions(timeout: 5, delay: 0.2).eventually {
94+
assert result == [2,4]
9595
}
9696
}
9797

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

107107
then: 'the onComplete handler is invoked and the onError handler is ignored'
108-
new PollingConditions(timeout: 5).eventually {
109-
result == 2
110-
hasError == false
108+
new PollingConditions(timeout: 5, delay: 0.2).eventually {
109+
assert result == 2
110+
assert hasError == false
111111
}
112112
}
113113

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

123123
then: 'the onError handler is invoked and the onComplete handler is ignored'
124-
new PollingConditions(timeout: 5).eventually {
125-
!result
126-
error
127-
error.message == 'bad'
124+
new PollingConditions(timeout: 5, delay: 0.2).eventually {
125+
assert !result
126+
assert error
127+
assert error.message == 'bad'
128128
}
129129
}
130130

grails-async/rxjava/src/test/groovy/org/grails/async/factory/rxjava/RxJavaPromiseListSpec.groovy

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ class RxJavaPromiseListSpec extends Specification{
2121
list.onComplete { result = it }
2222

2323
then: 'then the result from onComplete is correct'
24-
new PollingConditions(timeout: 5).eventually {
25-
result == [1,2,3]
24+
new PollingConditions(timeout: 5, delay: 0.2).eventually {
25+
assert result == [1,2,3]
2626
}
2727
}
2828

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

3939
then: 'then the result from onComplete is correct'
40-
new PollingConditions(timeout: 5).eventually {
41-
result == [1,2,3]
40+
new PollingConditions(timeout: 5, delay: 0.2).eventually {
41+
assert result == [1,2,3]
4242
}
4343
}
4444

grails-async/rxjava/src/test/groovy/org/grails/async/factory/rxjava/RxJavaPromiseMapSpec.groovy

+19-19
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ class RxJavaPromiseMapSpec extends Specification{
1818
map.onComplete { result = it }
1919

2020
then: 'an appropriately populated map is returned to the onComplete event'
21-
new PollingConditions(timeout: 5).eventually {
22-
result
23-
result['one'] == 1
24-
result['four'] == 4
25-
result['eight'] == 8
21+
new PollingConditions(timeout: 5, delay: 0.2).eventually {
22+
assert result
23+
assert result['one'] == 1
24+
assert result['four'] == 4
25+
assert result['eight'] == 8
2626
}
2727
}
2828

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

3939
then: 'an appropriately populated map is returned to the onComplete event'
40-
new PollingConditions(timeout: 5).eventually {
41-
result
42-
result['one'] == 1
43-
result['four'] == 4
44-
result['eight'] == 8
40+
new PollingConditions(timeout: 5, delay: 0.2).eventually {
41+
assert result
42+
assert result['one'] == 1
43+
assert result['four'] == 4
44+
assert result['eight'] == 8
4545
}
4646
}
4747

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

5858
then: 'an appropriately populated map is returned to the onComplete event'
59-
new PollingConditions(timeout: 5).eventually {
60-
result
61-
result['one'] == 1
62-
result['four'] == 4
63-
result['eight'] == 8
59+
new PollingConditions(timeout: 5, delay: 0.2).eventually {
60+
assert result
61+
assert result['one'] == 1
62+
assert result['four'] == 4
63+
assert result['eight'] == 8
6464
}
6565
}
6666

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

8181
then: 'an appropriately populated map is returned to the onComplete event'
82-
new PollingConditions(timeout: 5).eventually {
83-
!result
84-
error
85-
error.message == 'bad'
82+
new PollingConditions(timeout: 5, delay: 0.2).eventually {
83+
assert !result
84+
assert error
85+
assert error.message == 'bad'
8686
}
8787
}
8888

0 commit comments

Comments
 (0)