Skip to content
This repository was archived by the owner on May 30, 2024. It is now read-only.

Commit 3466fa9

Browse files
author
Dan Richelson
committed
Move start wait param into builder. Restore original constructor signatures.
1 parent ba7e423 commit 3466fa9

File tree

3 files changed

+32
-21
lines changed

3 files changed

+32
-21
lines changed

src/main/java/com/launchdarkly/client/LDClient.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,9 @@ public class LDClient implements Closeable {
3434
* cases, you should use this constructor.
3535
*
3636
* @param apiKey the API key for your account
37-
* @param waitForMillis when set to greater than zero allows callers to block until the client
38-
* has connected to LaunchDarkly and is properly initialized
3937
*/
40-
public LDClient(String apiKey, long waitForMillis) {
41-
this(apiKey, LDConfig.DEFAULT, waitForMillis);
38+
public LDClient(String apiKey) {
39+
this(apiKey, LDConfig.DEFAULT);
4240
}
4341

4442
/**
@@ -47,10 +45,8 @@ public LDClient(String apiKey, long waitForMillis) {
4745
*
4846
* @param apiKey the API key for your account
4947
* @param config a client configuration object
50-
* @param waitForMillis when set to greater than zero allows callers to block until the client
51-
* has connected to LaunchDarkly and is properly initialized
5248
*/
53-
public LDClient(String apiKey, LDConfig config, long waitForMillis) {
49+
public LDClient(String apiKey, LDConfig config) {
5450
this.config = config;
5551
this.requestor = createFeatureRequestor(apiKey, config);
5652
this.eventProcessor = createEventProcessor(apiKey, config);
@@ -75,10 +71,10 @@ public LDClient(String apiKey, LDConfig config, long waitForMillis) {
7571

7672
Future<Void> startFuture = updateProcessor.start();
7773

78-
if (waitForMillis > 0L) {
79-
logger.info("Waiting up to " + waitForMillis + " milliseconds for LaunchDarkly client to start...");
74+
if (config.startWaitMillis > 0L) {
75+
logger.info("Waiting up to " + config.startWaitMillis + " milliseconds for LaunchDarkly client to start...");
8076
try {
81-
startFuture.get(waitForMillis, TimeUnit.MILLISECONDS);
77+
startFuture.get(config.startWaitMillis, TimeUnit.MILLISECONDS);
8278
} catch (TimeoutException e) {
8379
logger.error("Timeout encountered waiting for LaunchDarkly client initialization");
8480
} catch (Exception e) {

src/main/java/com/launchdarkly/client/LDConfig.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public final class LDConfig {
2222
private static final int DEFAULT_SOCKET_TIMEOUT = 10000;
2323
private static final int DEFAULT_FLUSH_INTERVAL = 5;
2424
private static final long DEFAULT_POLLING_INTERVAL_MILLIS = 1000L;
25+
private static final long DEFAULT_START_WAIT_MILLIS = 0L;
2526
private static final Logger logger = LoggerFactory.getLogger(LDConfig.class);
2627

2728
protected static final LDConfig DEFAULT = new Builder().build();
@@ -40,6 +41,7 @@ public final class LDConfig {
4041
final boolean useLdd;
4142
final boolean offline;
4243
final long pollingIntervalMillis;
44+
final long startWaitMillis;
4345

4446
protected LDConfig(Builder builder) {
4547
this.baseURI = builder.baseURI;
@@ -60,6 +62,7 @@ protected LDConfig(Builder builder) {
6062
} else {
6163
this.pollingIntervalMillis = builder.pollingIntervalMillis;
6264
}
65+
this.startWaitMillis = builder.startWaitMillis;
6366
}
6467

6568
/**
@@ -90,6 +93,7 @@ public static class Builder {
9093
private boolean offline = false;
9194
private long pollingIntervalMillis = DEFAULT_POLLING_INTERVAL_MILLIS;
9295
private FeatureStore featureStore = new InMemoryFeatureStore();
96+
public long startWaitMillis = DEFAULT_START_WAIT_MILLIS;
9397

9498
/**
9599
* Creates a builder with all configuration parameters set to the default
@@ -319,6 +323,18 @@ public Builder pollingIntervalMillis(long pollingIntervalMillis) {
319323
return this;
320324
}
321325

326+
/**
327+
* Set how long the constructor will block awaiting a successful connection to LaunchDarkly.
328+
* Default value of 0 will not block and cause the constructor to return immediately.
329+
*
330+
* @param startWaitMillis
331+
* @return the builder
332+
*/
333+
public Builder startWaitMillis(long startWaitMillis) {
334+
this.startWaitMillis = startWaitMillis;
335+
return this;
336+
}
337+
322338
HttpHost proxyHost() {
323339
if (this.proxyHost == null && this.proxyPort == -1 && this.proxyScheme == null) {
324340
return null;

src/test/java/com/launchdarkly/client/LDClientTest.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public void testOffline() throws IOException {
3737
.offline(true)
3838
.build();
3939

40-
client = createMockClient(config, 0L);
40+
client = createMockClient(config);
4141
replayAll();
4242

4343
assertDefaultValueIsReturned();
@@ -51,7 +51,7 @@ public void testUseLdd() throws IOException {
5151
.useLdd(true)
5252
.build();
5353

54-
client = createMockClient(config, 0L);
54+
client = createMockClient(config);
5555
// Asserting 2 things here: no pollingProcessor or streamingProcessor activity
5656
// and sending of event:
5757
expect(eventProcessor.sendEvent(anyObject(Event.class))).andReturn(true);
@@ -73,7 +73,7 @@ public void testStreamingNoWait() throws IOException {
7373
expect(eventProcessor.sendEvent(anyObject(Event.class))).andReturn(true);
7474
replayAll();
7575

76-
client = createMockClient(config, 0L);
76+
client = createMockClient(config);
7777
assertDefaultValueIsReturned();
7878

7979
verifyAll();
@@ -82,14 +82,15 @@ public void testStreamingNoWait() throws IOException {
8282
@Test
8383
public void testStreamingWait() throws Exception {
8484
LDConfig config = new LDConfig.Builder()
85+
.startWaitMillis(10L)
8586
.stream(true)
8687
.build();
8788

8889
expect(streamProcessor.start()).andReturn(initFuture);
8990
expect(initFuture.get(10L, TimeUnit.MILLISECONDS)).andThrow(new TimeoutException());
9091
replayAll();
9192

92-
client = createMockClient(config, 10L);
93+
client = createMockClient(config);
9394
verifyAll();
9495
}
9596

@@ -104,7 +105,7 @@ public void testPollingNoWait() throws IOException {
104105
expect(eventProcessor.sendEvent(anyObject(Event.class))).andReturn(true);
105106
replayAll();
106107

107-
client = createMockClient(config, 0L);
108+
client = createMockClient(config);
108109
assertDefaultValueIsReturned();
109110

110111
verifyAll();
@@ -113,6 +114,7 @@ public void testPollingNoWait() throws IOException {
113114
@Test
114115
public void testPollingWait() throws Exception {
115116
LDConfig config = new LDConfig.Builder()
117+
.startWaitMillis(10L)
116118
.stream(false)
117119
.build();
118120

@@ -122,7 +124,7 @@ public void testPollingWait() throws Exception {
122124
expect(pollingProcessor.initialized()).andReturn(false);
123125
replayAll();
124126

125-
client = createMockClient(config, 10L);
127+
client = createMockClient(config);
126128
assertDefaultValueIsReturned();
127129
verifyAll();
128130
}
@@ -132,11 +134,8 @@ private void assertDefaultValueIsReturned() {
132134
assertEquals(true, result);
133135
}
134136

135-
private LDClient createMockClient(
136-
LDConfig config,
137-
Long waitForMillis
138-
) {
139-
return new LDClient("API_KEY", config, waitForMillis) {
137+
private LDClient createMockClient(LDConfig config) {
138+
return new LDClient("API_KEY", config) {
140139
@Override
141140
protected FeatureRequestor createFeatureRequestor(String apiKey, LDConfig config) {
142141
return requestor;

0 commit comments

Comments
 (0)