Search in sources :

Example 16 with ByteBuf

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

the class SaslListMechanismsHandlerTest method propagatesChannelActiveAfterSendingInitialRequest.

/**
 * This test makes sure that after the initial request is sent, the channel active signal is
 * propagated so that we do not regress bootstrap pipelining functionality.
 */
@Test
void propagatesChannelActiveAfterSendingInitialRequest() {
    final SaslListMechanismsHandler handler = new SaslListMechanismsHandler(endpointContext);
    final AtomicBoolean channelActiveFired = new AtomicBoolean();
    channel.pipeline().addLast(handler).addLast(new SimpleChannelInboundHandler<ByteBuf>() {

        @Override
        protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) {
        }

        @Override
        public void channelActive(ChannelHandlerContext ctx) {
            channelActiveFired.set(true);
        }
    });
    assertEquals(handler, channel.pipeline().get(SaslListMechanismsHandler.class));
    channel.connect(new InetSocketAddress("1.2.3.4", 1234));
    channel.pipeline().fireChannelActive();
    channel.runPendingTasks();
    waitUntilCondition(channelActiveFired::get);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) InetSocketAddress(java.net.InetSocketAddress) ChannelHandlerContext(com.couchbase.client.core.deps.io.netty.channel.ChannelHandlerContext) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Example 17 with ByteBuf

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

the class SaslListMechanismsHandlerTest method encodeAndSendListMechsRequest.

/**
 * This test verifies that the client sends the proper KV command to the server
 * when requesting the SASL mechanisms.
 */
@Test
void encodeAndSendListMechsRequest() {
    SaslListMechanismsHandler handler = new SaslListMechanismsHandler(endpointContext);
    channel.pipeline().addLast(handler);
    assertEquals(handler, channel.pipeline().get(SaslListMechanismsHandler.class));
    channel.connect(new InetSocketAddress("1.2.3.4", 1234));
    channel.pipeline().fireChannelActive();
    channel.runPendingTasks();
    ByteBuf writtenRequest = channel.readOutbound();
    verifyRequest(writtenRequest, MemcacheProtocol.Opcode.SASL_LIST_MECHS.opcode(), false, false, false);
    ReferenceCountUtil.release(writtenRequest);
}
Also used : InetSocketAddress(java.net.InetSocketAddress) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Example 18 with ByteBuf

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

the class SaslListMechanismsHandlerTest method failConnectIfSaslMechsListEmpty.

@Test
void failConnectIfSaslMechsListEmpty() {
    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("success_sasl_list_mechs_empty_response.txt", ErrorMapLoadingHandlerTest.class));
    channel.writeInbound(response);
    channel.runPendingTasks();
    assertFalse(connectFuture.isSuccess());
    AuthenticationFailureException cause = (AuthenticationFailureException) connectFuture.cause();
    assertTrue(cause.getMessage().contains("Received empty mechanism list 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 19 with ByteBuf

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

the class JsonStreamParserTest method exampleUsage.

@Test
void exampleUsage() {
    final List<String> matches = new ArrayList<>();
    JsonStreamParser parser = JsonStreamParser.builder().doOnValue("/name", v -> matches.add("Hello " + v.readString())).doOnValue("/pets/-/name", v -> matches.add("I see you have a pet named " + v.readString())).doOnValue("/blob", v -> matches.add("Here's a blob of JSON: " + new String(v.readBytes(), UTF_8))).build();
    byte[] json = "{'name':'Jon','pets':[{'name':'Odie'},{'name':'Garfield'}],'blob':{'magicWord':'xyzzy'}}".replace("'", "\"").getBytes(UTF_8);
    int half = json.length / 2;
    ByteBuf firstChunk = Unpooled.copiedBuffer(json, 0, half);
    ByteBuf secondChunk = Unpooled.copiedBuffer(json, half, json.length - half);
    try {
        // parser takes ownership of the buffer and will release it.
        parser.feed(firstChunk);
        assertEquals(Arrays.asList("Hello Jon", "I see you have a pet named Odie"), matches);
        matches.clear();
        parser.feed(secondChunk);
        assertEquals(Arrays.asList("I see you have a pet named Garfield", "Here's a blob of JSON: {\"magicWord\":\"xyzzy\"}"), matches);
        matches.clear();
    } finally {
        // Must always call close() when done to release the buffers owned by the parser.
        parser.close();
    }
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Arrays(java.util.Arrays) Unpooled(com.couchbase.client.core.deps.io.netty.buffer.Unpooled) UTF_8(java.nio.charset.StandardCharsets.UTF_8) IOException(java.io.IOException) Math.min(java.lang.Math.min) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) ResourceLeakDetector(com.couchbase.client.core.deps.io.netty.util.ResourceLeakDetector) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ArrayList(java.util.ArrayList) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Example 20 with ByteBuf

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

the class CompressionTest method doesCompressLongAppend.

@Test
void doesCompressLongAppend() {
    AppendRequest request = new AppendRequest(timeout, coreContext, cid, retryStrategy, key, longContent, cas, durability, null);
    ByteBuf encoded = request.encode(allocator, 0, ctx(true));
    assertEquals(MemcacheProtocol.Datatype.SNAPPY.datatype(), datatype(encoded));
    assertTrue(body(encoded).get().readableBytes() < longContent.length);
    ReferenceCountUtil.release(encoded);
}
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