/
EventuallyTest.kt
208 lines (197 loc) · 7.05 KB
/
EventuallyTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package com.sksamuel.kotest.assertions.timing
import io.kotest.assertions.fail
import io.kotest.assertions.nondeterministicListener
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.assertions.timing.eventually
import io.kotest.assertions.until.fibonacci
import io.kotest.assertions.until.fixed
import io.kotest.assertions.until.until
import io.kotest.assertions.until.untilListener
import io.kotest.core.spec.style.WordSpec
import io.kotest.matchers.ints.shouldBeLessThan
import io.kotest.matchers.longs.shouldBeGreaterThanOrEqual
import io.kotest.matchers.nulls.shouldNotBeNull
import io.kotest.matchers.shouldBe
import io.kotest.matchers.string.shouldContain
import kotlinx.coroutines.delay
import java.io.FileNotFoundException
import java.io.IOException
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
import kotlin.time.ExperimentalTime
import kotlin.time.TimeSource
import kotlin.time.days
import kotlin.time.milliseconds
import kotlin.time.seconds
@OptIn(ExperimentalTime::class)
class EventuallyTest : WordSpec() {
init {
"eventually" should {
"pass working tests" {
eventually(5.days) {
System.currentTimeMillis()
}
}
"pass tests that completed within the time allowed" {
val end = System.currentTimeMillis() + 250
eventually(1.seconds) {
if (System.currentTimeMillis() < end)
throw RuntimeException("foo")
}
}
"fail tests that do not complete within the time allowed" {
shouldThrow<AssertionError> {
eventually(150.milliseconds) {
throw RuntimeException("foo")
}
}
}
"return the result computed inside" {
val result = eventually(2.seconds) {
1
}
result shouldBe 1
}
"pass tests that completed within the time allowed, AssertionError" {
val end = System.currentTimeMillis() + 250
eventually(5.days) {
if (System.currentTimeMillis() < end)
assert(false)
}
}
"pass tests that completed within the time allowed, custom exception" {
val end = System.currentTimeMillis() + 250
eventually(5.seconds, FileNotFoundException::class) {
if (System.currentTimeMillis() < end)
throw FileNotFoundException()
}
}
"fail tests throw unexpected exception type" {
shouldThrow<NullPointerException> {
eventually(2.seconds, exceptionClass = IOException::class) {
(null as String?)!!.length
}
}
}
"pass tests that throws FileNotFoundException for some time" {
val end = System.currentTimeMillis() + 150
eventually(duration = 5.days) {
}
eventually(5.days) {
if (System.currentTimeMillis() < end)
throw FileNotFoundException("foo")
}
}
"handle kotlin assertion errors" {
var thrown = false
eventually(100.milliseconds) {
if (!thrown) {
thrown = true
throw AssertionError("boom")
}
}
}
"handle java assertion errors" {
var thrown = false
eventually(100.milliseconds) {
if (!thrown) {
thrown = true
throw java.lang.AssertionError("boom")
}
}
}
"display the first and last underlying failures" {
var count = 0
val message = shouldThrow<AssertionError> {
eventually(100.milliseconds) {
if (count == 0) {
count = 1
fail("first")
} else {
fail("last")
}
}
}.message
message.shouldContain("Eventually block failed after 100ms; attempted \\d+ time\\(s\\); 25.0ms delay between attempts".toRegex())
message.shouldContain("The first error was caused by: first")
message.shouldContain("The last error was caused by: last")
}
"allow suspendable functions" {
eventually(100.milliseconds) {
delay(25)
System.currentTimeMillis()
}
}
"allow configuring interval delay" {
var count = 0
eventually(200.milliseconds, 40.milliseconds.fixed()) {
count += 1
}
count.shouldBeLessThan(6)
}
"handle shouldNotBeNull" {
val mark = TimeSource.Monotonic.markNow()
shouldThrow<java.lang.AssertionError> {
eventually(50.milliseconds) {
val str: String? = null
str.shouldNotBeNull()
}
}
mark.elapsedNow().toLongMilliseconds().shouldBeGreaterThanOrEqual(50)
}
"eventually with boolean predicate" {
eventually(5.seconds) {
System.currentTimeMillis() > 0
}
}
"eventually with boolean predicate and interval" {
eventually(5.seconds, 1.seconds.fixed()) {
System.currentTimeMillis() > 0
}
}
"eventually with T predicate" {
var t = ""
eventually(5.seconds, predicate = { t == "xxxx" }) {
t += "x"
}
}
"eventually with T predicate and interval" {
var t = ""
val result = eventually(5.seconds, 250.milliseconds.fixed(), { t == "xxxxxxxxxxx" }) {
t += "x"
t
}
result shouldBe "xxxxxxxxxxx"
}
"eventually with T predicate, interval, and listener" {
var t = ""
val latch = CountDownLatch(5)
val listener = nondeterministicListener<String> { latch.countDown() }
val result = eventually(5.seconds, 250.milliseconds.fixed(), listener, predicate = { t == "xxxxxxxxxxx" }) {
t += "x"
t
}
latch.await(15, TimeUnit.SECONDS) shouldBe true
result shouldBe "xxxxxxxxxxx"
}
"fail tests that fail a predicate" {
shouldThrow<AssertionError> {
eventually(1.seconds, { it == 2 }) {
1
}
}
}
"support fibonacci intervals" {
var t = ""
val latch = CountDownLatch(5)
val listener = nondeterministicListener<String> { latch.countDown() }
val result = eventually(10.seconds, 200.milliseconds.fibonacci(), listener, predicate = { t == "xxxxxx" }) {
t += "x"
t
}
latch.await(10, TimeUnit.SECONDS) shouldBe true
result shouldBe "xxxxxx"
}
}
}
}