Skip to content

Commit f16754f

Browse files
fix(client): don't close client on withOptions usage when original is gc'd
1 parent 65d0eb9 commit f16754f

File tree

2 files changed

+41
-8
lines changed

2 files changed

+41
-8
lines changed

finch-java-core/src/main/kotlin/com/tryfinch/api/core/ClientOptions.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,9 @@ private constructor(
110110
webhookSecret = clientOptions.webhookSecret
111111
}
112112

113-
fun httpClient(httpClient: HttpClient) = apply { this.httpClient = httpClient }
113+
fun httpClient(httpClient: HttpClient) = apply {
114+
this.httpClient = PhantomReachableClosingHttpClient(httpClient)
115+
}
114116

115117
fun checkJacksonVersionCompatibility(checkJacksonVersionCompatibility: Boolean) = apply {
116118
this.checkJacksonVersionCompatibility = checkJacksonVersionCompatibility
@@ -290,13 +292,11 @@ private constructor(
290292

291293
return ClientOptions(
292294
httpClient,
293-
PhantomReachableClosingHttpClient(
294-
RetryingHttpClient.builder()
295-
.httpClient(httpClient)
296-
.clock(clock)
297-
.maxRetries(maxRetries)
298-
.build()
299-
),
295+
RetryingHttpClient.builder()
296+
.httpClient(httpClient)
297+
.clock(clock)
298+
.maxRetries(maxRetries)
299+
.build(),
300300
checkJacksonVersionCompatibility,
301301
jsonMapper,
302302
streamHandlerExecutor
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 com.tryfinch.api.core
4+
5+
import com.tryfinch.api.core.http.HttpClient
6+
import org.assertj.core.api.Assertions.assertThat
7+
import org.junit.jupiter.api.Test
8+
import org.junit.jupiter.api.extension.ExtendWith
9+
import org.mockito.junit.jupiter.MockitoExtension
10+
import org.mockito.kotlin.mock
11+
import org.mockito.kotlin.never
12+
import org.mockito.kotlin.verify
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).accessToken("My Access Token").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)