Search in sources :

Example 71 with ByteBuf

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

the class MemcacheProtocolDecodeHandlerTest method shouldDecodeInput.

/**
 * Runs through all provided inputs and makes sure even if they are sliced
 * and diced they come out whole at the end.
 *
 * <p>For now each input is tried 100 times with different chunk sizes to increase
 * the chance of finding a problem, this can be increased or decreased as
 * needed.</p>
 */
@ParameterizedTest(name = "{0}")
@MethodSource("inputProvider")
void shouldDecodeInput(final InputHolder inputHolder) {
    final EmbeddedChannel channel = new EmbeddedChannel(new MemcacheProtocolDecodeHandler());
    try {
        for (int i = 0; i < 100; i++) {
            ByteBuf copy = inputHolder.input.copy();
            writeAsRandomChunks(channel, copy);
            ByteBuf read = channel.readInbound();
            copy.resetReaderIndex();
            assertEquals(inputHolder.input, read);
            ReferenceCountUtil.release(copy);
            ReferenceCountUtil.release(read);
        }
        ReferenceCountUtil.release(inputHolder.input);
    } finally {
        channel.finishAndReleaseAll();
    }
}
Also used : EmbeddedChannel(com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 72 with ByteBuf

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

the class MemcacheProtocolTest method flexibleSyncReplicationTimeoutRightAtFloor.

@Test
void flexibleSyncReplicationTimeoutRightAtFloor() {
    // 1667 is valid, and should not log
    ByteBuf result = MemcacheProtocol.flexibleSyncReplication(ALLOC.buffer(), DurabilityLevel.MAJORITY, Duration.ofMillis(1667), context);
    try {
        // 4 bytes total for these flexible extras
        assertEquals(4, result.readableBytes());
        // sync replication id 1 and length 3
        assertEquals(0x13, result.getByte(0));
        // majority has id of 1
        assertEquals(0x01, result.getByte(1));
        // expect what you asked for (well, 90% of it)
        assertEquals(1500, result.getShort(2));
        verify(eventBus, never()).publish(any(DurabilityTimeoutCoercedEvent.class));
    } finally {
        ReferenceCountUtil.release(result);
    }
    // 1666 actually will log...
    result = MemcacheProtocol.flexibleSyncReplication(ALLOC.buffer(), DurabilityLevel.MAJORITY, Duration.ofMillis(1666), context);
    try {
        // 4 bytes total for these flexible extras
        assertEquals(4, result.readableBytes());
        // sync replication id 1 and length 3
        assertEquals(0x13, result.getByte(0));
        // majority has id of 1
        assertEquals(0x01, result.getByte(1));
        // expect what you asked for
        assertEquals(1500, result.getShort(2));
        verify(eventBus, times(1)).publish(any(DurabilityTimeoutCoercedEvent.class));
    } finally {
        ReferenceCountUtil.release(result);
    }
}
Also used : DurabilityTimeoutCoercedEvent(com.couchbase.client.core.cnc.events.io.DurabilityTimeoutCoercedEvent) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Example 73 with ByteBuf

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

the class MemcacheProtocolVerificationHandlerTest method shouldVerifyCorrectResponses.

/**
 * Verifies good responses are passed through.
 *
 * @param inputHolder the good input packets.
 */
@ParameterizedTest(name = "{0}")
@MethodSource
void shouldVerifyCorrectResponses(final InputHolder inputHolder) {
    EndpointContext ctx = mock(EndpointContext.class);
    final EmbeddedChannel channel = new EmbeddedChannel(new MemcacheProtocolVerificationHandler(ctx));
    try {
        channel.writeInbound(inputHolder.input);
        ByteBuf written = channel.readInbound();
        assertEquals(inputHolder.input, written);
    } finally {
        channel.finishAndReleaseAll();
    }
}
Also used : EndpointContext(com.couchbase.client.core.endpoint.EndpointContext) EmbeddedChannel(com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 74 with ByteBuf

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

the class MemcacheProtocolVerificationHandlerTest method shouldVerifyCorrectRequests.

/**
 * Verifies good requests are passed through.
 *
 * @param inputHolder the good input packets.
 */
@ParameterizedTest(name = "{0}")
@MethodSource
void shouldVerifyCorrectRequests(final InputHolder inputHolder) {
    EndpointContext ctx = mock(EndpointContext.class);
    final EmbeddedChannel channel = new EmbeddedChannel(new MemcacheProtocolVerificationHandler(ctx));
    try {
        channel.writeOutbound(inputHolder.input);
        ByteBuf written = channel.readOutbound();
        assertEquals(inputHolder.input, written);
    } finally {
        channel.finishAndReleaseAll();
    }
}
Also used : EndpointContext(com.couchbase.client.core.endpoint.EndpointContext) EmbeddedChannel(com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 75 with ByteBuf

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

the class SaslListMechanismsHandlerTest method ignoresUnknownSaslMech.

@Test
void ignoresUnknownSaslMech() {
    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_unknown_mech_response.txt", ErrorMapLoadingHandlerTest.class));
    channel.writeInbound(response);
    channel.runPendingTasks();
    assertTrue(connectFuture.isSuccess());
    Set<SaslMechanism> saslMechanisms = channel.attr(ChannelAttributes.SASL_MECHS_KEY).get();
    assertEquals(saslMechanisms, EnumSet.of(SaslMechanism.SCRAM_SHA1, SaslMechanism.SCRAM_SHA256, SaslMechanism.SCRAM_SHA512));
    UnknownSaslMechanismDetectedEvent event = (UnknownSaslMechanismDetectedEvent) eventBus.publishedEvents().get(0);
    assertEquals("FLAUN", event.mechanism());
}
Also used : ChannelFuture(com.couchbase.client.core.deps.io.netty.channel.ChannelFuture) UnknownSaslMechanismDetectedEvent(com.couchbase.client.core.cnc.events.io.UnknownSaslMechanismDetectedEvent) InetSocketAddress(java.net.InetSocketAddress) SaslMechanism(com.couchbase.client.core.env.SaslMechanism) 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