Skip to content

Commit 0265af6

Browse files
fix(client): don't close client on withOptions usage when original is gc'd
1 parent 24abc88 commit 0265af6

File tree

2 files changed

+41
-8
lines changed

2 files changed

+41
-8
lines changed

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ private constructor(
8383
apiKey = clientOptions.apiKey
8484
}
8585

86-
fun httpClient(httpClient: HttpClient) = apply { this.httpClient = httpClient }
86+
fun httpClient(httpClient: HttpClient) = apply {
87+
this.httpClient = PhantomReachableClosingHttpClient(httpClient)
88+
}
8789

8890
fun checkJacksonVersionCompatibility(checkJacksonVersionCompatibility: Boolean) = apply {
8991
this.checkJacksonVersionCompatibility = checkJacksonVersionCompatibility
@@ -226,13 +228,11 @@ private constructor(
226228

227229
return ClientOptions(
228230
httpClient,
229-
PhantomReachableClosingHttpClient(
230-
RetryingHttpClient.builder()
231-
.httpClient(httpClient)
232-
.clock(clock)
233-
.maxRetries(maxRetries)
234-
.build()
235-
),
231+
RetryingHttpClient.builder()
232+
.httpClient(httpClient)
233+
.clock(clock)
234+
.maxRetries(maxRetries)
235+
.build(),
236236
checkJacksonVersionCompatibility,
237237
jsonMapper,
238238
clock,
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// File generated from our OpenAPI spec by Stainless.
2+
3+
package org.onebusaway.core
4+
5+
import org.assertj.core.api.Assertions.assertThat
6+
import org.junit.jupiter.api.Test
7+
import org.junit.jupiter.api.extension.ExtendWith
8+
import org.mockito.junit.jupiter.MockitoExtension
9+
import org.mockito.kotlin.mock
10+
import org.mockito.kotlin.never
11+
import org.mockito.kotlin.verify
12+
import org.onebusaway.core.http.HttpClient
13+
14+
@ExtendWith(MockitoExtension::class)
15+
internal class ClientOptionsTest {
16+
17+
@Test
18+
fun toBuilder_whenOriginalClientOptionsGarbageCollected_doesNotCloseOriginalClient() {
19+
val httpClient = mock<HttpClient>()
20+
var clientOptions =
21+
ClientOptions.builder().httpClient(httpClient).apiKey("My API Key").build()
22+
verify(httpClient, never()).close()
23+
24+
// Overwrite the `clientOptions` variable so that the original `ClientOptions` is GC'd.
25+
clientOptions = clientOptions.toBuilder().build()
26+
System.gc()
27+
Thread.sleep(100)
28+
29+
verify(httpClient, never()).close()
30+
// This exists so that `clientOptions` is still reachable.
31+
assertThat(clientOptions).isEqualTo(clientOptions)
32+
}
33+
}

0 commit comments

Comments
 (0)