Skip to content

Commit 6ec5364

Browse files
committed
fix: initialization and defaults for timeout
1 parent 94edea8 commit 6ec5364

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

client/src/main/java/io/micrometer/prometheus/rsocket/PrometheusRSocketClient.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public class PrometheusRSocketClient {
6565
private volatile boolean requestedDisconnect = false;
6666
private RSocket sendingSocket;
6767

68-
private Duration timeout = Duration.ofSeconds(5);
68+
private Duration timeout;
6969

7070
/**
7171
* Creates a {@link PrometheusRSocketClient}.
@@ -97,6 +97,7 @@ private PrometheusRSocketClient(MeterRegistryAndScrape<?> registryAndScrape,
9797
Duration timeout,
9898
Runnable onKeyReceived) {
9999
this.registryAndScrape = registryAndScrape;
100+
this.timeout = timeout;
100101

101102
RSocketConnector.create()
102103
.reconnect(new Retry() {
@@ -301,7 +302,7 @@ public static class Builder {
301302
private Retry retry = Retry.backoff(Long.MAX_VALUE, Duration.ofSeconds(10))
302303
.maxBackoff(Duration.ofMinutes(10));
303304

304-
private Duration timeout;
305+
private Duration timeout = Duration.ofSeconds(5);
305306

306307
private Runnable onKeyReceived = () -> {
307308
};
@@ -350,6 +351,17 @@ public Builder doOnKeyReceived(Runnable onKeyReceived) {
350351
* @return the {@link PrometheusRSocketClient}
351352
*/
352353
public PrometheusRSocketClient connect() {
354+
return connect(timeout);
355+
}
356+
357+
/**
358+
* Connects the {@link PrometheusRSocketClient}.
359+
*
360+
* @param timeout the timeout for the client to connect
361+
*
362+
* @return the {@link PrometheusRSocketClient}
363+
*/
364+
public PrometheusRSocketClient connect(Duration timeout) {
353365
LOGGER.debug("Connecting to RSocket Proxy...");
354366
return new PrometheusRSocketClient(
355367
registryAndScrape,
@@ -384,6 +396,7 @@ public PrometheusRSocketClient connectBlockingly(Duration timeout) {
384396
registryAndScrape,
385397
clientTransport,
386398
retry,
399+
timeout,
387400
() -> {
388401
LOGGER.info("Connected to RSocket Proxy!");
389402
onKeyReceived.run();

client/src/test/java/io/micrometer/prometheus/rsocket/PrometheusRSocketClientTests.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,17 +207,17 @@ public Mono<Payload> requestResponse(Payload payload) {
207207
keyReceived.set(true);
208208
});
209209

210-
Field timeoutField = PrometheusRSocketClient.class.getDeclaredField("timeout");
211-
timeoutField.setAccessible(true);
212-
Duration timeout = (Duration)timeoutField.get(null);
213-
assertThat(timeout.toSeconds()).isEqualTo(10L);
214-
215210
CompletableFuture<PrometheusRSocketClient> clientFuture = supplyAsync(clientBuilder::connectBlockingly, newSingleThreadExecutor());
216211
runAsync(keyReceivedLatch::countDown, newDelayedExecutor());
217212
assertThat(keyReceived).as("Public key should not be received (not connected)").isFalse();
218213
PrometheusRSocketClient client = clientFuture.get();
219214
assertThat(keyReceived).as("Public key should be received(connected)").isTrue();
220215

216+
Field timeoutField = PrometheusRSocketClient.class.getDeclaredField("timeout");
217+
timeoutField.setAccessible(true);
218+
Duration timeout = (Duration)timeoutField.get(client);
219+
assertThat(timeout.toSeconds()).isEqualTo(10L);
220+
221221
CompletableFuture<Void> closeFuture = runAsync(client::pushAndCloseBlockingly, newSingleThreadExecutor());
222222
runAsync(pushLatch::countDown, newDelayedExecutor());
223223
assertThat(pushed).as("Data should not be pushed").isFalse();

0 commit comments

Comments
 (0)