Skip to content

Commit 9c57ef5

Browse files
feat(client): expose sleeper option
fix(client): ensure single timer is created per client
1 parent 96fbb1e commit 9c57ef5

File tree

8 files changed

+139
-34
lines changed

8 files changed

+139
-34
lines changed

onebusaway-sdk-java-client-okhttp/src/main/kotlin/org/onebusaway/client/okhttp/OnebusawaySdkOkHttpClient.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import kotlin.jvm.optionals.getOrNull
1414
import org.onebusaway.client.OnebusawaySdkClient
1515
import org.onebusaway.client.OnebusawaySdkClientImpl
1616
import org.onebusaway.core.ClientOptions
17+
import org.onebusaway.core.Sleeper
1718
import org.onebusaway.core.Timeout
1819
import org.onebusaway.core.http.Headers
1920
import org.onebusaway.core.http.HttpClient
@@ -120,6 +121,17 @@ class OnebusawaySdkOkHttpClient private constructor() {
120121
*/
121122
fun jsonMapper(jsonMapper: JsonMapper) = apply { clientOptions.jsonMapper(jsonMapper) }
122123

124+
/**
125+
* The interface to use for delaying execution, like during retries.
126+
*
127+
* This is primarily useful for using fake delays in tests.
128+
*
129+
* Defaults to real execution delays.
130+
*
131+
* This class takes ownership of the sleeper and closes it when closed.
132+
*/
133+
fun sleeper(sleeper: Sleeper) = apply { clientOptions.sleeper(sleeper) }
134+
123135
/**
124136
* The clock to use for operations that require timing, like retries.
125137
*

onebusaway-sdk-java-client-okhttp/src/main/kotlin/org/onebusaway/client/okhttp/OnebusawaySdkOkHttpClientAsync.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import kotlin.jvm.optionals.getOrNull
1414
import org.onebusaway.client.OnebusawaySdkClientAsync
1515
import org.onebusaway.client.OnebusawaySdkClientAsyncImpl
1616
import org.onebusaway.core.ClientOptions
17+
import org.onebusaway.core.Sleeper
1718
import org.onebusaway.core.Timeout
1819
import org.onebusaway.core.http.Headers
1920
import org.onebusaway.core.http.HttpClient
@@ -120,6 +121,17 @@ class OnebusawaySdkOkHttpClientAsync private constructor() {
120121
*/
121122
fun jsonMapper(jsonMapper: JsonMapper) = apply { clientOptions.jsonMapper(jsonMapper) }
122123

124+
/**
125+
* The interface to use for delaying execution, like during retries.
126+
*
127+
* This is primarily useful for using fake delays in tests.
128+
*
129+
* Defaults to real execution delays.
130+
*
131+
* This class takes ownership of the sleeper and closes it when closed.
132+
*/
133+
fun sleeper(sleeper: Sleeper) = apply { clientOptions.sleeper(sleeper) }
134+
123135
/**
124136
* The clock to use for operations that require timing, like retries.
125137
*

onebusaway-sdk-java-core/src/main/kotlin/org/onebusaway/core/ClientOptions.kt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@ private constructor(
4040
* needs to be overridden.
4141
*/
4242
@get:JvmName("jsonMapper") val jsonMapper: JsonMapper,
43+
/**
44+
* The interface to use for delaying execution, like during retries.
45+
*
46+
* This is primarily useful for using fake delays in tests.
47+
*
48+
* Defaults to real execution delays.
49+
*
50+
* This class takes ownership of the sleeper and closes it when closed.
51+
*/
52+
@get:JvmName("sleeper") val sleeper: Sleeper,
4353
/**
4454
* The clock to use for operations that require timing, like retries.
4555
*
@@ -130,6 +140,7 @@ private constructor(
130140
private var httpClient: HttpClient? = null
131141
private var checkJacksonVersionCompatibility: Boolean = true
132142
private var jsonMapper: JsonMapper = jsonMapper()
143+
private var sleeper: Sleeper? = null
133144
private var clock: Clock = Clock.systemUTC()
134145
private var baseUrl: String? = null
135146
private var headers: Headers.Builder = Headers.builder()
@@ -144,6 +155,7 @@ private constructor(
144155
httpClient = clientOptions.originalHttpClient
145156
checkJacksonVersionCompatibility = clientOptions.checkJacksonVersionCompatibility
146157
jsonMapper = clientOptions.jsonMapper
158+
sleeper = clientOptions.sleeper
147159
clock = clientOptions.clock
148160
baseUrl = clientOptions.baseUrl
149161
headers = clientOptions.headers.toBuilder()
@@ -184,6 +196,17 @@ private constructor(
184196
*/
185197
fun jsonMapper(jsonMapper: JsonMapper) = apply { this.jsonMapper = jsonMapper }
186198

199+
/**
200+
* The interface to use for delaying execution, like during retries.
201+
*
202+
* This is primarily useful for using fake delays in tests.
203+
*
204+
* Defaults to real execution delays.
205+
*
206+
* This class takes ownership of the sleeper and closes it when closed.
207+
*/
208+
fun sleeper(sleeper: Sleeper) = apply { this.sleeper = PhantomReachableSleeper(sleeper) }
209+
187210
/**
188211
* The clock to use for operations that require timing, like retries.
189212
*
@@ -367,6 +390,7 @@ private constructor(
367390
*/
368391
fun build(): ClientOptions {
369392
val httpClient = checkRequired("httpClient", httpClient)
393+
val sleeper = sleeper ?: PhantomReachableSleeper(DefaultSleeper())
370394
val apiKey = checkRequired("apiKey", apiKey)
371395

372396
val headers = Headers.builder()
@@ -390,11 +414,13 @@ private constructor(
390414
httpClient,
391415
RetryingHttpClient.builder()
392416
.httpClient(httpClient)
417+
.sleeper(sleeper)
393418
.clock(clock)
394419
.maxRetries(maxRetries)
395420
.build(),
396421
checkJacksonVersionCompatibility,
397422
jsonMapper,
423+
sleeper,
398424
clock,
399425
baseUrl,
400426
headers.build(),
@@ -419,5 +445,6 @@ private constructor(
419445
*/
420446
fun close() {
421447
httpClient.close()
448+
sleeper.close()
422449
}
423450
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.onebusaway.core
2+
3+
import java.time.Duration
4+
import java.util.Timer
5+
import java.util.TimerTask
6+
import java.util.concurrent.CompletableFuture
7+
8+
class DefaultSleeper : Sleeper {
9+
10+
private val timer = Timer("DefaultSleeper", true)
11+
12+
override fun sleep(duration: Duration) = Thread.sleep(duration.toMillis())
13+
14+
override fun sleepAsync(duration: Duration): CompletableFuture<Void> {
15+
val future = CompletableFuture<Void>()
16+
timer.schedule(
17+
object : TimerTask() {
18+
override fun run() {
19+
future.complete(null)
20+
}
21+
},
22+
duration.toMillis(),
23+
)
24+
return future
25+
}
26+
27+
override fun close() = timer.cancel()
28+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.onebusaway.core
2+
3+
import java.time.Duration
4+
import java.util.concurrent.CompletableFuture
5+
6+
/**
7+
* A delegating wrapper around a [Sleeper] that closes it once it's only phantom reachable.
8+
*
9+
* This class ensures the [Sleeper] is closed even if the user forgets to do it.
10+
*/
11+
internal class PhantomReachableSleeper(private val sleeper: Sleeper) : Sleeper {
12+
13+
init {
14+
closeWhenPhantomReachable(this, sleeper)
15+
}
16+
17+
override fun sleep(duration: Duration) = sleeper.sleep(duration)
18+
19+
override fun sleepAsync(duration: Duration): CompletableFuture<Void> =
20+
sleeper.sleepAsync(duration)
21+
22+
override fun close() = sleeper.close()
23+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.onebusaway.core
2+
3+
import java.time.Duration
4+
import java.util.concurrent.CompletableFuture
5+
6+
/**
7+
* An interface for delaying execution for a specified amount of time.
8+
*
9+
* Useful for testing and cleaning up resources.
10+
*/
11+
interface Sleeper : AutoCloseable {
12+
13+
/** Synchronously pauses execution for the given [duration]. */
14+
fun sleep(duration: Duration)
15+
16+
/** Asynchronously pauses execution for the given [duration]. */
17+
fun sleepAsync(duration: Duration): CompletableFuture<Void>
18+
19+
/** Overridden from [AutoCloseable] to not have a checked exception in its signature. */
20+
override fun close()
21+
}

onebusaway-sdk-java-core/src/main/kotlin/org/onebusaway/core/http/RetryingHttpClient.kt

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ import java.time.OffsetDateTime
77
import java.time.format.DateTimeFormatter
88
import java.time.format.DateTimeParseException
99
import java.time.temporal.ChronoUnit
10-
import java.util.Timer
11-
import java.util.TimerTask
1210
import java.util.UUID
1311
import java.util.concurrent.CompletableFuture
1412
import java.util.concurrent.ThreadLocalRandom
1513
import java.util.concurrent.TimeUnit
1614
import java.util.function.Function
1715
import kotlin.math.min
1816
import kotlin.math.pow
17+
import org.onebusaway.core.DefaultSleeper
1918
import org.onebusaway.core.RequestOptions
19+
import org.onebusaway.core.Sleeper
2020
import org.onebusaway.core.checkRequired
2121
import org.onebusaway.errors.OnebusawaySdkIoException
2222
import org.onebusaway.errors.OnebusawaySdkRetryableException
@@ -130,7 +130,10 @@ private constructor(
130130
return executeWithRetries(modifiedRequest, requestOptions)
131131
}
132132

133-
override fun close() = httpClient.close()
133+
override fun close() {
134+
httpClient.close()
135+
sleeper.close()
136+
}
134137

135138
private fun isRetryable(request: HttpRequest): Boolean =
136139
// Some requests, such as when a request body is being streamed, cannot be retried because
@@ -235,33 +238,14 @@ private constructor(
235238
class Builder internal constructor() {
236239

237240
private var httpClient: HttpClient? = null
238-
private var sleeper: Sleeper =
239-
object : Sleeper {
240-
241-
private val timer = Timer("RetryingHttpClient", true)
242-
243-
override fun sleep(duration: Duration) = Thread.sleep(duration.toMillis())
244-
245-
override fun sleepAsync(duration: Duration): CompletableFuture<Void> {
246-
val future = CompletableFuture<Void>()
247-
timer.schedule(
248-
object : TimerTask() {
249-
override fun run() {
250-
future.complete(null)
251-
}
252-
},
253-
duration.toMillis(),
254-
)
255-
return future
256-
}
257-
}
241+
private var sleeper: Sleeper? = null
258242
private var clock: Clock = Clock.systemUTC()
259243
private var maxRetries: Int = 2
260244
private var idempotencyHeader: String? = null
261245

262246
fun httpClient(httpClient: HttpClient) = apply { this.httpClient = httpClient }
263247

264-
@JvmSynthetic internal fun sleeper(sleeper: Sleeper) = apply { this.sleeper = sleeper }
248+
fun sleeper(sleeper: Sleeper) = apply { this.sleeper = sleeper }
265249

266250
fun clock(clock: Clock) = apply { this.clock = clock }
267251

@@ -272,17 +256,10 @@ private constructor(
272256
fun build(): HttpClient =
273257
RetryingHttpClient(
274258
checkRequired("httpClient", httpClient),
275-
sleeper,
259+
sleeper ?: DefaultSleeper(),
276260
clock,
277261
maxRetries,
278262
idempotencyHeader,
279263
)
280264
}
281-
282-
internal interface Sleeper {
283-
284-
fun sleep(duration: Duration)
285-
286-
fun sleepAsync(duration: Duration): CompletableFuture<Void>
287-
}
288265
}

onebusaway-sdk-java-core/src/test/kotlin/org/onebusaway/core/http/RetryingHttpClientTest.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import org.junit.jupiter.params.ParameterizedTest
1414
import org.junit.jupiter.params.provider.ValueSource
1515
import org.onebusaway.client.okhttp.OkHttpClient
1616
import org.onebusaway.core.RequestOptions
17+
import org.onebusaway.core.Sleeper
1718
import org.onebusaway.errors.OnebusawaySdkRetryableException
1819

1920
@WireMockTest
@@ -294,12 +295,14 @@ internal class RetryingHttpClientTest {
294295
.httpClient(failingHttpClient)
295296
.maxRetries(2)
296297
.sleeper(
297-
object : RetryingHttpClient.Sleeper {
298+
object : Sleeper {
298299

299300
override fun sleep(duration: Duration) {}
300301

301302
override fun sleepAsync(duration: Duration): CompletableFuture<Void> =
302303
CompletableFuture.completedFuture(null)
304+
305+
override fun close() {}
303306
}
304307
)
305308
.build()
@@ -333,12 +336,14 @@ internal class RetryingHttpClientTest {
333336
.httpClient(httpClient)
334337
// Use a no-op `Sleeper` to make the test fast.
335338
.sleeper(
336-
object : RetryingHttpClient.Sleeper {
339+
object : Sleeper {
337340

338341
override fun sleep(duration: Duration) {}
339342

340343
override fun sleepAsync(duration: Duration): CompletableFuture<Void> =
341344
CompletableFuture.completedFuture(null)
345+
346+
override fun close() {}
342347
}
343348
)
344349

0 commit comments

Comments
 (0)