use of com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel in project couchbase-jvm-clients by couchbase.
the class PasswordAuthenticatorTest method shouldOnlyNegotiatePlainWhenTlsEnabled.
/**
* Regression test for JVMCBC-890.
*/
@Test
void shouldOnlyNegotiatePlainWhenTlsEnabled() {
PasswordAuthenticator authenticator = PasswordAuthenticator.create("user", "pass");
CoreEnvironment tlsEnvironment = CoreEnvironment.builder().securityConfig(SecurityConfig.enableTls(true).trustManagerFactory(InsecureTrustManagerFactory.INSTANCE)).build();
try {
EndpointContext ctx = mock(EndpointContext.class);
when(ctx.environment()).thenReturn(tlsEnvironment);
EmbeddedChannel channel = new EmbeddedChannel();
authenticator.authKeyValueConnection(ctx, channel.pipeline());
SaslAuthenticationHandler handler = channel.pipeline().get(SaslAuthenticationHandler.class);
assertEquals(EnumSet.of(SaslMechanism.PLAIN), handler.allowedMechanisms());
} finally {
tlsEnvironment.shutdown();
}
}
use of com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel in project couchbase-jvm-clients by couchbase.
the class BaseEndpointTest method disconnectOverridesConnectCompletion.
/**
* This test makes sure that even if we connect successfully, if there has been a
* disconnect signal in the meantime we need to properly close it all and not end
* in a connect-disconnect limbo.
*/
@Test
void disconnectOverridesConnectCompletion() {
final CompletableFuture<Channel> cf = new CompletableFuture<>();
InstrumentedEndpoint endpoint = InstrumentedEndpoint.create(eventLoopGroup, ctx, () -> Mono.fromFuture(cf));
endpoint.connect();
waitUntilCondition(() -> endpoint.state() == EndpointState.CONNECTING);
endpoint.disconnect();
EmbeddedChannel channel = new EmbeddedChannel();
cf.complete(channel);
waitUntilCondition(() -> endpoint.state() == EndpointState.DISCONNECTED);
assertTrue(eventBus.publishedEvents().size() >= 2);
assertTrue(eventBus.publishedEvents().get(0) instanceof EndpointConnectionIgnoredEvent);
assertTrue(eventBus.publishedEvents().get(1) instanceof EndpointDisconnectedEvent);
assertEquals("Endpoint disconnected successfully", eventBus.publishedEvents().get(1).description());
}
use of com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel in project couchbase-jvm-clients by couchbase.
the class BaseEndpointTest method retryOnFailureUntilEventuallyConnected.
/**
* The {@link #retryOnTimeoutUntilEventuallyConnected()} tests that a netty
* channel future does not return at all and we reconnect, this one tests that
* if netty returns with a failure we keep reconnecting until it succeeds.
*/
@Test
void retryOnFailureUntilEventuallyConnected() {
final AtomicInteger invocationAttempt = new AtomicInteger();
InstrumentedEndpoint endpoint = InstrumentedEndpoint.create(eventLoopGroup, ctx, () -> invocationAttempt.incrementAndGet() > 3 ? Mono.just(new EmbeddedChannel()) : Mono.error(new ChannelException("Could not connect for some reason")));
endpoint.connect();
waitUntilCondition(() -> endpoint.state() == EndpointState.CONNECTED);
assertEquals(4, eventBus.publishedEvents().size());
int warnings = 0;
int debug = 0;
for (Event event : eventBus.publishedEvents()) {
if (event.severity() == Event.Severity.WARN) {
assertTrue(event instanceof EndpointConnectionFailedEvent);
warnings++;
} else if (event.severity() == Event.Severity.DEBUG) {
assertTrue(event instanceof EndpointConnectedEvent);
debug++;
} else {
throw new RuntimeException("Unexpected Event" + event);
}
}
assertEquals(3, warnings);
assertEquals(1, debug);
}
use of com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel in project couchbase-jvm-clients by couchbase.
the class BaseEndpointTest method retryOnTimeoutUntilEventuallyConnected.
/**
* This test fakes a situation where the channel future from netty would simply not return
* at all and time out, and the client would resubscribe. Then at some future attempt the
* future returns fine and we should end up in a connected state and ready to go.
*/
@Test
void retryOnTimeoutUntilEventuallyConnected() {
SimpleEventBus eventBus = new SimpleEventBus(true);
CoreEnvironment env = CoreEnvironment.builder().eventBus(eventBus).timeoutConfig(TimeoutConfig.connectTimeout(Duration.ofMillis(10))).build();
CoreContext coreContext = new CoreContext(mock(Core.class), 1, env, authenticator);
ServiceContext ctx = new ServiceContext(coreContext, LOCALHOST, 1234, ServiceType.KV, Optional.empty());
try {
final CompletableFuture<Channel> cf = new CompletableFuture<>();
InstrumentedEndpoint endpoint = InstrumentedEndpoint.create(eventLoopGroup, ctx, () -> Mono.fromFuture(cf));
endpoint.connect();
waitUntilCondition(() -> eventBus.publishedEvents().size() >= 3);
EmbeddedChannel channel = new EmbeddedChannel();
cf.complete(channel);
waitUntilCondition(() -> endpoint.state() == EndpointState.CONNECTED);
assertTrue(eventBus.publishedEvents().size() >= 3);
boolean failedFound = false;
boolean successFound = false;
for (Event event : eventBus.publishedEvents()) {
if (event instanceof EndpointConnectionFailedEvent) {
assertEquals(Event.Severity.WARN, event.severity());
assertEquals(Duration.ofMillis(10), event.duration());
failedFound = true;
} else if (event instanceof EndpointConnectedEvent) {
assertEquals(Event.Severity.DEBUG, event.severity());
assertTrue(event.duration().toNanos() > 0);
successFound = true;
}
}
assertTrue(failedFound);
assertTrue(successFound);
} finally {
env.shutdown();
}
}
use of com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel 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();
}
}
Aggregations