|
4 | 4 | import com.google.gson.JsonObject;
|
5 | 5 | import com.launchdarkly.sdk.LDUser;
|
6 | 6 | import com.launchdarkly.sdk.LDValue;
|
| 7 | +import com.launchdarkly.sdk.server.interfaces.DataSourceStatusProvider; |
7 | 8 |
|
8 | 9 | import org.junit.Test;
|
9 | 10 |
|
10 | 11 | import java.net.URI;
|
| 12 | +import java.time.Duration; |
| 13 | +import java.util.concurrent.BlockingQueue; |
| 14 | +import java.util.concurrent.LinkedBlockingQueue; |
11 | 15 |
|
12 | 16 | import static com.launchdarkly.sdk.server.Components.externalUpdatesOnly;
|
13 | 17 | import static com.launchdarkly.sdk.server.Components.noEvents;
|
@@ -112,19 +116,54 @@ public void clientStartsInStreamingMode() throws Exception {
|
112 | 116 | }
|
113 | 117 | }
|
114 | 118 |
|
| 119 | + @Test |
| 120 | + public void clientStartsInStreamingModeAfterRecoverableError() throws Exception { |
| 121 | + MockResponse errorResp = new MockResponse().setResponseCode(503); |
| 122 | + |
| 123 | + String streamData = "event: put\n" + |
| 124 | + "data: {\"data\":" + makeAllDataJson() + "}\n\n"; |
| 125 | + MockResponse streamResp = TestHttpUtil.eventStreamResponse(streamData); |
| 126 | + |
| 127 | + try (MockWebServer server = makeStartedServer(errorResp, streamResp)) { |
| 128 | + LDConfig config = new LDConfig.Builder() |
| 129 | + .dataSource(baseStreamingConfig(server)) |
| 130 | + .events(noEvents()) |
| 131 | + .build(); |
| 132 | + |
| 133 | + try (LDClient client = new LDClient(sdkKey, config)) { |
| 134 | + assertTrue(client.isInitialized()); |
| 135 | + assertTrue(client.boolVariation(flagKey, user, false)); |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + |
115 | 140 | @Test
|
116 | 141 | public void clientFailsInStreamingModeWith401Error() throws Exception {
|
117 | 142 | MockResponse resp = new MockResponse().setResponseCode(401);
|
118 | 143 |
|
119 |
| - try (MockWebServer server = makeStartedServer(resp)) { |
| 144 | + try (MockWebServer server = makeStartedServer(resp, resp, resp)) { |
120 | 145 | LDConfig config = new LDConfig.Builder()
|
121 |
| - .dataSource(baseStreamingConfig(server)) |
| 146 | + .dataSource(baseStreamingConfig(server).initialReconnectDelay(Duration.ZERO)) |
122 | 147 | .events(noEvents())
|
123 | 148 | .build();
|
124 | 149 |
|
125 | 150 | try (LDClient client = new LDClient(sdkKey, config)) {
|
126 | 151 | assertFalse(client.isInitialized());
|
127 | 152 | assertFalse(client.boolVariation(flagKey, user, false));
|
| 153 | + |
| 154 | + BlockingQueue<DataSourceStatusProvider.Status> statuses = new LinkedBlockingQueue<>(); |
| 155 | + client.getDataSourceStatusProvider().addStatusListener(statuses::add); |
| 156 | + |
| 157 | + Thread.sleep(100); // make sure it didn't retry the connection |
| 158 | + assertThat(client.getDataSourceStatusProvider().getStatus().getState(), |
| 159 | + equalTo(DataSourceStatusProvider.State.OFF)); |
| 160 | + while (!statuses.isEmpty()) { |
| 161 | + // The status listener may or may not have been registered early enough to receive |
| 162 | + // the OFF notification, but we should at least not see any *other* statuses. |
| 163 | + assertThat(statuses.take().getState(), equalTo(DataSourceStatusProvider.State.OFF)); |
| 164 | + } |
| 165 | + assertThat(statuses.isEmpty(), equalTo(true)); |
| 166 | + assertThat(server.getRequestCount(), equalTo(1)); // no retries |
128 | 167 | }
|
129 | 168 | }
|
130 | 169 | }
|
|
0 commit comments