/
HttpPushFactoryTest.java
369 lines (325 loc) · 17.5 KB
/
HttpPushFactoryTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
* Copyright (c) 2019 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.connectivity.service.messaging.httppush;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import java.time.Duration;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Queue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.eclipse.ditto.base.service.config.DittoServiceConfig;
import org.eclipse.ditto.base.service.config.http.DefaultHttpProxyConfig;
import org.eclipse.ditto.base.service.config.http.HttpProxyConfig;
import org.eclipse.ditto.connectivity.model.Connection;
import org.eclipse.ditto.connectivity.model.ConnectionType;
import org.eclipse.ditto.connectivity.model.ConnectivityModelFactory;
import org.eclipse.ditto.connectivity.model.ConnectivityStatus;
import org.eclipse.ditto.connectivity.service.config.DefaultConnectionConfig;
import org.eclipse.ditto.connectivity.service.config.HttpPushConfig;
import org.eclipse.ditto.connectivity.service.messaging.AbstractBaseClientActorTest;
import org.eclipse.ditto.connectivity.service.messaging.TestConstants;
import org.eclipse.ditto.connectivity.service.messaging.monitoring.logs.ConnectionLogger;
import org.eclipse.ditto.connectivity.service.messaging.tunnel.SshTunnelState;
import org.eclipse.ditto.internal.utils.config.DefaultScopedConfig;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import akka.NotUsed;
import akka.actor.ActorSystem;
import akka.http.impl.engine.client.ProxyConnectionFailedException;
import akka.http.javadsl.Http;
import akka.http.javadsl.ServerBinding;
import akka.http.javadsl.model.HttpMethods;
import akka.http.javadsl.model.HttpRequest;
import akka.http.javadsl.model.HttpResponse;
import akka.http.javadsl.model.StatusCodes;
import akka.http.javadsl.model.headers.Authorization;
import akka.japi.Pair;
import akka.stream.KillSwitches;
import akka.stream.OverflowStrategy;
import akka.stream.javadsl.Flow;
import akka.stream.javadsl.Keep;
import akka.stream.javadsl.Sink;
import akka.stream.javadsl.SinkQueueWithCancel;
import akka.stream.javadsl.Source;
import akka.stream.javadsl.SourceQueueWithComplete;
import akka.testkit.javadsl.TestKit;
import scala.util.Failure;
import scala.util.Try;
/**
* Tests {@link HttpPushFactory}.
*/
public final class HttpPushFactoryTest {
private final Queue<CompletableFuture<Void>> killSwitchTrigger = new ConcurrentLinkedQueue<>();
private ActorSystem actorSystem;
private ServerBinding binding;
private Connection connection;
private BlockingQueue<HttpRequest> requestQueue;
private BlockingQueue<CompletableFuture<HttpResponse>> responseQueue;
private DefaultConnectionConfig connectionConfig;
public HttpPushFactoryTest() {
killSwitchTrigger.offer(new CompletableFuture<>());
}
@Before
public void createActorSystem() {
actorSystem = ActorSystem.create(getClass().getSimpleName(), TestConstants.CONFIG);
connectionConfig = DefaultConnectionConfig.of(
DittoServiceConfig.of(
DefaultScopedConfig.dittoScoped(TestConstants.CONFIG), "connectivity"));
newBinding();
connection = createHttpPushConnection(binding);
}
@After
public void stopActorSystem() {
if (actorSystem != null) {
TestKit.shutdownActorSystem(actorSystem);
}
}
@Test
public void appendPathToUri() {
connection = connection.toBuilder()
.uri("http://127.0.0.1:" + binding.localAddress().getPort() + "/path/prefix/")
.build();
final HttpPushFactory underTest = HttpPushFactory.of(connection, connectionConfig.getHttpPushConfig(),
mock(ConnectionLogger.class), SshTunnelState::disabled);
final HttpRequest request = underTest.newRequest(HttpPublishTarget.of("PUT:/path/appendage/"));
assertThat(request.method()).isEqualTo(HttpMethods.PUT);
assertThat(request.getUri().getPathString()).isEqualTo("/path/prefix/path/appendage/");
}
@Test
public void basicAuth() throws Exception {
// GIVEN: the connection has plain credentials in the URI
connection = connection.toBuilder()
.uri("http://username:password@127.0.0.1:" + binding.localAddress().getPort() + "/path/prefix/")
.build();
final HttpPushFactory underTest = HttpPushFactory.of(connection, connectionConfig.getHttpPushConfig(),
mock(ConnectionLogger.class), SshTunnelState::disabled);
final Pair<SourceQueueWithComplete<HttpRequest>, SinkQueueWithCancel<Try<HttpResponse>>> pair =
newSourceSinkQueues(underTest);
final SourceQueueWithComplete<HttpRequest> sourceQueue = pair.first();
final SinkQueueWithCancel<Try<HttpResponse>> sinkQueue = pair.second();
final HttpRequest request = underTest.newRequest(HttpPublishTarget.of("PUT:/path/appendage/"));
final HttpResponse response = HttpResponse.create().withStatus(StatusCodes.OK);
// WHEN: request-response cycle is carried out
responseQueue.offer(CompletableFuture.completedFuture(response));
sourceQueue.offer(request);
final HttpRequest actualRequest = requestQueue.take();
// THEN: actual received request has a basic auth header
assertThat(actualRequest.getHeader(Authorization.class))
.contains(Authorization.basic("username", "password"));
}
@Test
public void withHttpProxyConfig() throws Exception {
// GIVEN: the connection's URI points to an unreachable host
connection = connection.toBuilder()
.uri("http://no.such.host.example:42/path/prefix/")
.build();
// GIVEN: the HTTP-push factory has the proxy configured to the test server binding
final HttpPushFactory underTest = HttpPushFactory.of(connection, new HttpPushConfig() {
@Override
public int getMaxQueueSize() {
return 0;
}
@Override
public Duration getRequestTimeout() {
return Duration.ofSeconds(10);
}
@Override
public HttpProxyConfig getHttpProxyConfig() {
return getEnabledProxyConfig(binding);
}
}, mock(ConnectionLogger.class), SshTunnelState::disabled);
final Pair<SourceQueueWithComplete<HttpRequest>, SinkQueueWithCancel<Try<HttpResponse>>> pair =
newSourceSinkQueues(underTest);
final SourceQueueWithComplete<HttpRequest> sourceQueue = pair.first();
final SinkQueueWithCancel<Try<HttpResponse>> sinkQueue = pair.second();
final HttpRequest request = underTest.newRequest(HttpPublishTarget.of("PUT:/path/appendage/"));
// WHEN: request is made
sourceQueue.offer(request);
// THEN: CONNECT request is made to the Akka HTTP test server.
// THEN: Akka HTTP server rejects CONNECT request, creating a failed response
final Optional<Try<HttpResponse>> optionalTryResponse = sinkQueue.pull().toCompletableFuture().join();
assertThat(optionalTryResponse).isNotEmpty();
final Try<HttpResponse> tryResponse = optionalTryResponse.get();
assertThat(tryResponse).isInstanceOf(Failure.class);
assertThat(tryResponse.failed().get()).isInstanceOf(ProxyConnectionFailedException.class);
assertThat(tryResponse.failed().get().getMessage())
.contains("proxy rejected to open a connection to no.such.host.example:42 with status code: 400");
}
@Test
public void handleFailure() throws Exception {
new TestKit(actorSystem) {{
// GIVEN: An HTTP-push connection is established against localhost.
final HttpPushFactory underTest =
HttpPushFactory.of(connection, connectionConfig.getHttpPushConfig(), mock(ConnectionLogger.class), SshTunnelState::disabled);
final Pair<SourceQueueWithComplete<HttpRequest>, SinkQueueWithCancel<Try<HttpResponse>>> pair =
newSourceSinkQueues(underTest);
final SourceQueueWithComplete<HttpRequest> sourceQueue = pair.first();
final SinkQueueWithCancel<Try<HttpResponse>> sinkQueue = pair.second();
final HttpRequest request1 = underTest.newRequest(HttpPublishTarget.of("1"));
final HttpRequest request2 = underTest.newRequest(HttpPublishTarget.of("2"));
final HttpRequest request3 = underTest.newRequest(HttpPublishTarget.of("3"));
final HttpResponse response1 = HttpResponse.create().withStatus(StatusCodes.IM_A_TEAPOT);
final HttpResponse response3 = HttpResponse.create().withStatus(StatusCodes.BLOCKED_BY_PARENTAL_CONTROLS);
// GIVEN: The connection is working.
responseQueue.offer(CompletableFuture.completedFuture(response1));
sourceQueue.offer(request1);
assertThat(requestQueue.take().getUri()).isEqualTo(request1.getUri());
final Try<HttpResponse> responseOrError1 = pullResponse(sinkQueue);
assertThat(responseOrError1.isSuccess()).isTrue();
assertThat(responseOrError1.get().status()).isEqualTo(response1.status());
// WHEN: In-flight request is killed
// THEN: Akka HTTP responds with status 500
responseQueue.offer(new CompletableFuture<>());
sourceQueue.offer(request2);
assertThat(requestQueue.take().getUri()).isEqualTo(request2.getUri());
shutdownAllServerStreams();
final Try<HttpResponse> responseOrError2 = pullResponse(sinkQueue);
assertThat(responseOrError2.isSuccess()).isTrue();
assertThat(responseOrError2.get().status()).isEqualTo(StatusCodes.INTERNAL_SERVER_ERROR);
// WHEN: HTTP server becomes available again.
// THEN: A new request resumes and the previously failed request is discarded.
responseQueue.offer(CompletableFuture.completedFuture(response3));
sourceQueue.offer(request3);
assertThat(requestQueue.take().getUri()).isEqualTo(request3.getUri());
final Try<HttpResponse> responseOrError3 = pullResponse(sinkQueue);
assertThat(responseOrError3.isSuccess()).isTrue();
assertThat(responseOrError3.get().status()).isEqualTo(response3.status());
}};
}
@Test
public void sendRequestsInParallel() throws Exception {
// GIVEN: the connection has the specific config parallelism=4
connection = connection.toBuilder()
.uri("http://127.0.0.1:" + binding.localAddress().getPort())
.specificConfig(Map.of("parallelism", "4")) // must be a power of 2!
.build();
final HttpPushFactory underTest = HttpPushFactory.of(connection, connectionConfig.getHttpPushConfig(),
mock(ConnectionLogger.class), SshTunnelState::disabled);
final Pair<SourceQueueWithComplete<HttpRequest>, SinkQueueWithCancel<Try<HttpResponse>>> pair =
newSourceSinkQueues(underTest);
final SourceQueueWithComplete<HttpRequest> sourceQueue = pair.first();
final SinkQueueWithCancel<Try<HttpResponse>> sinkQueue = pair.second();
final HttpRequest request = underTest.newRequest(HttpPublishTarget.of("PUT:/path/appendage/"));
final HttpResponse response = HttpResponse.create().withStatus(StatusCodes.OK);
// WHEN: 3 requests are sent in parallel
sourceQueue.offer(request);
sourceQueue.offer(request);
sourceQueue.offer(request);
// THEN: all 3 requests are sent to the server before the first response comes back
assertThat(requestQueue.poll(10L, TimeUnit.SECONDS)).isNotNull();
assertThat(requestQueue.poll(10L, TimeUnit.SECONDS)).isNotNull();
assertThat(requestQueue.poll(10L, TimeUnit.SECONDS)).isNotNull();
responseQueue.offer(CompletableFuture.completedFuture(response));
responseQueue.offer(CompletableFuture.completedFuture(response));
responseQueue.offer(CompletableFuture.completedFuture(response));
sinkQueue.pull();
sinkQueue.pull();
sinkQueue.pull();
}
@Test
public void emitFailureOnTimeout() {
connection = connection.toBuilder().uri("http://127.0.0.1:" + binding.localAddress().getPort()).build();
final HttpPushFactory underTest = HttpPushFactory.of(connection, connectionConfig.getHttpPushConfig(),
mock(ConnectionLogger.class), SshTunnelState::disabled);
// GIVEN: the HTTP client flow is configured with very short timeout
final Pair<SourceQueueWithComplete<HttpRequest>, SinkQueueWithCancel<Try<HttpResponse>>> pair =
newSourceSinkQueues(underTest.createFlow(actorSystem, actorSystem.log(), Duration.ofMillis(10)));
final SourceQueueWithComplete<HttpRequest> sourceQueue = pair.first();
final SinkQueueWithCancel<Try<HttpResponse>> sinkQueue = pair.second();
final HttpRequest request = underTest.newRequest(HttpPublishTarget.of("PUT:/path/appendage/"));
for (int i = 0; i < 3; ++i) {
// WHEN: A request is made
sourceQueue.offer(request);
// THEN: A timeout failure is emitted
final Try<HttpResponse> tryResponse = sinkQueue.pull().toCompletableFuture().join().orElseThrow();
assertThat(tryResponse.isFailure()).describedAs("expect timeout response").isTrue();
assertThat(tryResponse.failed().get()).isInstanceOf(TimeoutException.class);
// Offer a response to unblock the test server stream.
responseQueue.offer(CompletableFuture.completedFuture(HttpResponse.create()));
}
sinkQueue.cancel();
}
private void newBinding() {
requestQueue = new LinkedBlockingQueue<>();
responseQueue = new LinkedBlockingQueue<>();
final Flow<HttpRequest, HttpResponse, NotUsed> handler =
Flow.fromGraph(KillSwitches.<HttpRequest>single())
.mapAsync(1, request -> {
requestQueue.offer(request);
return responseQueue.take();
})
.mapMaterializedValue(killSwitch -> {
Objects.requireNonNull(killSwitchTrigger.peek())
.thenAccept(unused -> killSwitch.shutdown());
return NotUsed.getInstance();
});
binding = Http.get(actorSystem)
.newServerAt("127.0.0.1", 0)
.bindFlow(handler)
.toCompletableFuture()
.join();
}
private void shutdownAllServerStreams() {
killSwitchTrigger.offer(new CompletableFuture<>());
Objects.requireNonNull(killSwitchTrigger.poll()).complete(null);
}
private Pair<SourceQueueWithComplete<HttpRequest>, SinkQueueWithCancel<Try<HttpResponse>>> newSourceSinkQueues(
final Flow<Pair<HttpRequest, Object>, Pair<Try<HttpResponse>, Object>, ?> flow) {
return Source.<HttpRequest>queue(10, OverflowStrategy.dropNew())
.map(r -> Pair.create(r, null))
.viaMat(flow, Keep.left())
.map(Pair::first)
.toMat(Sink.queue(), Keep.both())
.run(actorSystem);
}
private Pair<SourceQueueWithComplete<HttpRequest>, SinkQueueWithCancel<Try<HttpResponse>>> newSourceSinkQueues(
final HttpPushFactory underTest) {
final Duration requestTimeout = Duration.ofMinutes(2);
return newSourceSinkQueues(underTest.createFlow(actorSystem, actorSystem.log(), requestTimeout));
}
private static Try<HttpResponse> pullResponse(final SinkQueueWithCancel<Try<HttpResponse>> responseQueue) {
return responseQueue.pull()
.toCompletableFuture()
.join()
.orElseThrow(() -> new AssertionError("Response expected"));
}
private static Connection createHttpPushConnection(final ServerBinding binding) {
return ConnectivityModelFactory.newConnectionBuilder(TestConstants.createRandomConnectionId(),
ConnectionType.HTTP_PUSH,
ConnectivityStatus.OPEN,
"http://127.0.0.1:" + binding.localAddress().getPort())
.targets(singletonList(AbstractBaseClientActorTest.HTTP_TARGET))
.build();
}
private static HttpProxyConfig getEnabledProxyConfig(final ServerBinding binding) {
final Config config = ConfigFactory.parseString("proxy {\n" +
" enabled = true\n" +
" hostname = \"127.0.0.1\"\n" +
" port = " + binding.localAddress().getPort() + "\n" +
" username = \"username\"\n" +
" password = \"password\"\n" +
"}");
return DefaultHttpProxyConfig.ofProxy(config);
}
}