Search in sources :

Example 76 with ByteBuf

use of com.couchbase.client.core.deps.io.netty.buffer.ByteBuf in project couchbase-jvm-clients by couchbase.

the class SaslListMechanismsHandlerTest method failConnectIfStatusNotSuccess.

@Test
void failConnectIfStatusNotSuccess() {
    SaslListMechanismsHandler handler = new SaslListMechanismsHandler(endpointContext);
    channel.pipeline().addLast(handler);
    assertEquals(handler, channel.pipeline().get(SaslListMechanismsHandler.class));
    ChannelFuture connectFuture = channel.connect(new InetSocketAddress("1.2.3.4", 1234));
    assertFalse(connectFuture.isDone());
    channel.pipeline().fireChannelActive();
    channel.runPendingTasks();
    ByteBuf writtenRequest = channel.readOutbound();
    verifyRequest(writtenRequest, MemcacheProtocol.Opcode.SASL_LIST_MECHS.opcode(), false, false, false);
    assertNotNull(channel.pipeline().get(SaslListMechanismsHandler.class));
    ReferenceCountUtil.release(writtenRequest);
    ByteBuf response = decodeHexDump(readResource("error_sasl_list_mechs_response.txt", ErrorMapLoadingHandlerTest.class));
    channel.writeInbound(response);
    channel.runPendingTasks();
    assertFalse(connectFuture.isSuccess());
    AuthenticationFailureException cause = (AuthenticationFailureException) connectFuture.cause();
    assertTrue(cause.getMessage().contains("Received non-success status from server"));
}
Also used : ChannelFuture(com.couchbase.client.core.deps.io.netty.channel.ChannelFuture) InetSocketAddress(java.net.InetSocketAddress) AuthenticationFailureException(com.couchbase.client.core.error.AuthenticationFailureException) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Example 77 with ByteBuf

use of com.couchbase.client.core.deps.io.netty.buffer.ByteBuf 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();
}
Also used : BucketConfigStreamingResponse(com.couchbase.client.core.msg.manager.BucketConfigStreamingResponse) HttpVersion(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpVersion) ReferenceCountUtil(com.couchbase.client.core.deps.io.netty.util.ReferenceCountUtil) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BestEffortRetryStrategy(com.couchbase.client.core.retry.BestEffortRetryStrategy) Random(java.util.Random) CompletableFuture(java.util.concurrent.CompletableFuture) DefaultHttpContent(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultHttpContent) HttpResponse(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpResponse) EmbeddedChannel(com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel) ArrayList(java.util.ArrayList) BucketConfigStreamingRequest(com.couchbase.client.core.msg.manager.BucketConfigStreamingRequest) AfterAll(org.junit.jupiter.api.AfterAll) DefaultLastHttpContent(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultLastHttpContent) PasswordAuthenticator(com.couchbase.client.core.env.PasswordAuthenticator) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) BeforeAll(org.junit.jupiter.api.BeforeAll) CoreContext(com.couchbase.client.core.CoreContext) ResourceLeakDetector(com.couchbase.client.core.deps.io.netty.util.ResourceLeakDetector) Duration(java.time.Duration) EndpointContext(com.couchbase.client.core.endpoint.EndpointContext) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) HttpResponseStatus(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpResponseStatus) MockingDetails(org.mockito.MockingDetails) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Util.waitUntilCondition(com.couchbase.client.test.Util.waitUntilCondition) Unpooled(com.couchbase.client.core.deps.io.netty.buffer.Unpooled) HttpContent(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpContent) HttpMethod(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpMethod) BaseEndpoint(com.couchbase.client.core.endpoint.BaseEndpoint) CoreEnvironment(com.couchbase.client.core.env.CoreEnvironment) Mockito.when(org.mockito.Mockito.when) StandardCharsets(java.nio.charset.StandardCharsets) DefaultHttpResponse(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultHttpResponse) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) List(java.util.List) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) HttpRequest(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpRequest) Util.readResource(com.couchbase.client.test.Util.readResource) Core(com.couchbase.client.core.Core) Collections(java.util.Collections) IoConfig(com.couchbase.client.core.env.IoConfig) Mockito.mock(org.mockito.Mockito.mock) CoreContext(com.couchbase.client.core.CoreContext) BucketConfigStreamingResponse(com.couchbase.client.core.msg.manager.BucketConfigStreamingResponse) EndpointContext(com.couchbase.client.core.endpoint.EndpointContext) DefaultHttpContent(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultHttpContent) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) Random(java.util.Random) DefaultLastHttpContent(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultLastHttpContent) Core(com.couchbase.client.core.Core) HttpRequest(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpRequest) EmbeddedChannel(com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel) HttpResponse(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpResponse) DefaultHttpResponse(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultHttpResponse) BucketConfigStreamingRequest(com.couchbase.client.core.msg.manager.BucketConfigStreamingRequest) BaseEndpoint(com.couchbase.client.core.endpoint.BaseEndpoint) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BaseEndpoint(com.couchbase.client.core.endpoint.BaseEndpoint) DefaultHttpResponse(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultHttpResponse) Test(org.junit.jupiter.api.Test)

Example 78 with ByteBuf

use of com.couchbase.client.core.deps.io.netty.buffer.ByteBuf in project couchbase-jvm-clients by couchbase.

the class DecompressionTest method decompressesGetAndLock.

@Test
void decompressesGetAndLock() {
    ByteBuf response = decodeHexDump(readResource("compressed_get_and_lock_response.txt", DecompressionTest.class));
    GetAndLockRequest request = new GetAndLockRequest("mydoc", Duration.ofSeconds(1), mock(CoreContext.class), CollectionIdentifier.fromDefault("bucket"), BestEffortRetryStrategy.INSTANCE, Duration.ofSeconds(1), null);
    GetAndLockResponse decoded = request.decode(response, null);
    assertEquals(readResource("dummy.json", DecompressionTest.class), new String(decoded.content(), UTF_8));
}
Also used : CoreContext(com.couchbase.client.core.CoreContext) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Example 79 with ByteBuf

use of com.couchbase.client.core.deps.io.netty.buffer.ByteBuf in project couchbase-jvm-clients by couchbase.

the class GetRequestTest method decodeSuccessfulResponse.

@Test
void decodeSuccessfulResponse() {
    ByteBuf response = decodeHexDump(readResource("get_response_success.txt", GetRequestTest.class));
    GetRequest request = new GetRequest(null, TIMEOUT, CTX, null, RETRY, null);
    GetResponse decoded = request.decode(response, null);
    byte[] expected = ("{\"callsign\":\"AIRCALIN\",\"country\":\"France\"," + "\"iata\":\"SB\",\"icao\":\"ACI\",\"id\":139," + "\"name\":\"Air Caledonie International\",\"type\":\"airline\"}").getBytes(UTF_8);
    assertEquals(ResponseStatus.SUCCESS, decoded.status());
    assertArrayEquals(expected, decoded.content());
    assertEquals("2000000", Integer.toHexString(decoded.flags()));
}
Also used : ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Example 80 with ByteBuf

use of com.couchbase.client.core.deps.io.netty.buffer.ByteBuf in project couchbase-jvm-clients by couchbase.

the class GetRequestTest method decodeNotFoundResponse.

@Test
void decodeNotFoundResponse() {
    ByteBuf response = decodeHexDump(readResource("get_response_not_found.txt", GetRequestTest.class));
    GetRequest request = new GetRequest(null, TIMEOUT, CTX, null, RETRY, null);
    GetResponse decoded = request.decode(response, null);
    assertEquals(ResponseStatus.NOT_FOUND, decoded.status());
    assertNull(decoded.content());
    assertEquals(0, decoded.flags());
}
Also used : ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Aggregations

ByteBuf (com.couchbase.client.core.deps.io.netty.buffer.ByteBuf)107 Test (org.junit.jupiter.api.Test)54 InetSocketAddress (java.net.InetSocketAddress)17 EmbeddedChannel (com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel)12 ChannelFuture (com.couchbase.client.core.deps.io.netty.channel.ChannelFuture)11 Duration (java.time.Duration)10 CouchbaseException (com.couchbase.client.core.error.CouchbaseException)9 CoreContext (com.couchbase.client.core.CoreContext)8 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)8 CompressionConfig (com.couchbase.client.core.env.CompressionConfig)6 ResponseStatus (com.couchbase.client.core.msg.ResponseStatus)6 GetRequest (com.couchbase.client.core.msg.kv.GetRequest)6 ChannelHandlerContext (com.couchbase.client.core.deps.io.netty.channel.ChannelHandlerContext)5 AuthenticationFailureException (com.couchbase.client.core.error.AuthenticationFailureException)5 ArrayList (java.util.ArrayList)5 DurabilityTimeoutCoercedEvent (com.couchbase.client.core.cnc.events.io.DurabilityTimeoutCoercedEvent)4 FeaturesNegotiatedEvent (com.couchbase.client.core.cnc.events.io.FeaturesNegotiatedEvent)4 CompositeByteBuf (com.couchbase.client.core.deps.io.netty.buffer.CompositeByteBuf)4 FullHttpRequest (com.couchbase.client.core.deps.io.netty.handler.codec.http.FullHttpRequest)4 BaseEndpoint (com.couchbase.client.core.endpoint.BaseEndpoint)4