use of com.couchbase.client.core.msg.manager.BucketConfigStreamingRequest in project couchbase-jvm-clients by couchbase.
the class ManagerMessageHandlerTest method returnsNewConfigsWhenChunked.
/**
* Configs can come in all shapes and sizes chunked, but only after the 4 newlines are pushed by the cluster
* the config should be propagated into the flux.
*/
@Test
void returnsNewConfigsWhenChunked() throws Exception {
CoreContext ctx = new CoreContext(mock(Core.class), 1, ENV, PasswordAuthenticator.create(USER, PASS));
BaseEndpoint endpoint = mock(BaseEndpoint.class);
EndpointContext endpointContext = mock(EndpointContext.class);
when(endpointContext.environment()).thenReturn(ENV);
when(endpoint.context()).thenReturn(endpointContext);
EmbeddedChannel channel = new EmbeddedChannel(new ManagerMessageHandler(endpoint, ctx));
BucketConfigStreamingRequest request = new BucketConfigStreamingRequest(Duration.ofSeconds(1), ctx, BestEffortRetryStrategy.INSTANCE, "bucket", ctx.authenticator());
channel.write(request);
HttpRequest outboundHeader = channel.readOutbound();
assertEquals(HttpMethod.GET, outboundHeader.method());
assertEquals("/pools/default/bs/bucket", outboundHeader.uri());
assertEquals(HttpVersion.HTTP_1_1, outboundHeader.protocolVersion());
CompletableFuture<BucketConfigStreamingResponse> response = request.response();
assertFalse(response.isDone());
HttpResponse inboundResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
channel.writeInbound(inboundResponse);
BucketConfigStreamingResponse completedResponse = request.response().get();
final List<String> configsPushed = Collections.synchronizedList(new ArrayList<>());
final AtomicBoolean terminated = new AtomicBoolean(false);
Thread listener = new Thread(() -> completedResponse.configs().subscribe(configsPushed::add, (e) -> {
}, () -> terminated.set(true)));
listener.setDaemon(true);
listener.start();
ByteBuf fullContent = Unpooled.copiedBuffer(readResource("terse_stream_two_configs.json", ManagerMessageHandlerTest.class), StandardCharsets.UTF_8);
while (fullContent.readableBytes() > 0) {
int len = new Random().nextInt(fullContent.readableBytes() + 1);
if (len == 0) {
continue;
}
channel.writeInbound(new DefaultHttpContent(fullContent.readBytes(len)));
}
waitUntilCondition(() -> configsPushed.size() >= 1);
for (String config : configsPushed) {
assertTrue(config.startsWith("{"));
assertTrue(config.endsWith("}"));
}
assertFalse(terminated.get());
channel.writeInbound(new DefaultLastHttpContent());
waitUntilCondition(terminated::get);
ReferenceCountUtil.release(fullContent);
channel.close().awaitUninterruptibly();
}
use of com.couchbase.client.core.msg.manager.BucketConfigStreamingRequest in project couchbase-jvm-clients by couchbase.
the class ClusterManagerBucketRefresherTest method shouldReconnectIfStreamCloses.
/**
* Unsubscription is driven purely from up the stack, so if the config stream should close for some
* reason the refresher needs to try to establish a new connection.
*/
@Test
void shouldReconnectIfStreamCloses() {
final AtomicReference<BucketConfigStreamingResponse> responseRef = new AtomicReference<>();
final AtomicInteger streamingRequestAttempts = new AtomicInteger();
doAnswer(i -> {
streamingRequestAttempts.incrementAndGet();
BucketConfigStreamingRequest request = i.getArgument(0);
HttpResponse httpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
BucketConfigStreamingResponse response = request.decode(httpResponse, null);
responseRef.set(response);
request.succeed(response);
return null;
}).when(core).send(any(BucketConfigStreamingRequest.class));
refresher.register("bucketName").block();
// Let's pretend the successfully opened stream closes for whatever reason
responseRef.get().completeStream();
waitUntilCondition(() -> streamingRequestAttempts.get() >= 2);
}
use of com.couchbase.client.core.msg.manager.BucketConfigStreamingRequest in project couchbase-jvm-clients by couchbase.
the class ClusterManagerBucketRefresherTest method shouldReconnectIfStreamErrors.
/**
* Unsubscription is driven purely from up the stack, so if the config stream should error for some
* reason the refresher needs to try to establish a new connection.
*/
@Test
void shouldReconnectIfStreamErrors() {
final AtomicReference<BucketConfigStreamingResponse> responseRef = new AtomicReference<>();
final AtomicInteger streamingRequestAttempts = new AtomicInteger();
doAnswer(i -> {
streamingRequestAttempts.incrementAndGet();
BucketConfigStreamingRequest request = i.getArgument(0);
HttpResponse httpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
BucketConfigStreamingResponse response = request.decode(httpResponse, null);
responseRef.set(response);
request.succeed(response);
return null;
}).when(core).send(any(BucketConfigStreamingRequest.class));
refresher.register("bucketName").block();
// Let's pretend the successfully opened stream fails for whatever reason
responseRef.get().failStream(new RuntimeException("Something Happened"));
waitUntilCondition(() -> streamingRequestAttempts.get() >= 2);
}
Aggregations