use of com.couchbase.client.core.cnc.events.io.UnsolicitedFeaturesReturnedEvent in project couchbase-jvm-clients by couchbase.
the class FeatureNegotiatingHandler method extractFeaturesFromBody.
/**
* Helper method to safely extract the negotiated server features from the
* body of the memcache payload.
*
* @param response the response to extract from.
* @return the list of server features, may be empty but never null.
*/
private Set<ServerFeature> extractFeaturesFromBody(final ByteBuf response) {
Optional<ByteBuf> body = MemcacheProtocol.body(response);
Set<ServerFeature> negotiated = EnumSet.noneOf(ServerFeature.class);
List<ServerFeature> unsolicited = new ArrayList<>();
if (!body.isPresent()) {
return negotiated;
}
while (body.get().isReadable()) {
try {
short featureRaw = body.get().readShort();
ServerFeature feature = ServerFeature.from(featureRaw);
if (features.contains(feature)) {
negotiated.add(feature);
} else {
unsolicited.add(feature);
}
} catch (Exception ex) {
interceptedConnectPromise.tryFailure(new CouchbaseException("Error while parsing negotiated server features.", ex));
}
}
if (!unsolicited.isEmpty()) {
endpointContext.environment().eventBus().publish(new UnsolicitedFeaturesReturnedEvent(ioContext, unsolicited));
}
return negotiated;
}
use of com.couchbase.client.core.cnc.events.io.UnsolicitedFeaturesReturnedEvent in project couchbase-jvm-clients by couchbase.
the class FeatureNegotiatingHandlerTest method decodeAndIgnoreNonAskedForFeaturesInResponse.
/**
* Should the server return non-asked-for features, ignore them.
*/
@Test
void decodeAndIgnoreNonAskedForFeaturesInResponse() {
Set<ServerFeature> toNegotiate = EnumSet.of(ServerFeature.SNAPPY, ServerFeature.TRACING);
FeatureNegotiatingHandler handler = new FeatureNegotiatingHandler(endpointContext, toNegotiate);
channel.pipeline().addLast(handler);
assertEquals(handler, channel.pipeline().get(FeatureNegotiatingHandler.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.HELLO.opcode(), true, false, true);
assertNotNull(channel.pipeline().get(FeatureNegotiatingHandler.class));
ByteBuf response = decodeHexDump(readResource("success_hello_response.txt", FeatureNegotiatingHandlerTest.class));
channel.writeInbound(response);
channel.runPendingTasks();
assertTrue(connectFuture.isSuccess());
assertEquals(2, eventBus.publishedEvents().size());
UnsolicitedFeaturesReturnedEvent unsolicitedEvent = (UnsolicitedFeaturesReturnedEvent) eventBus.publishedEvents().get(0);
assertEquals("Received unsolicited features during HELLO [TCPNODELAY, XATTR, XERROR, SELECT_BUCKET]", unsolicitedEvent.description());
assertEquals(Event.Severity.WARN, unsolicitedEvent.severity());
FeaturesNegotiatedEvent event = (FeaturesNegotiatedEvent) eventBus.publishedEvents().get(1);
assertEquals("Negotiated [SNAPPY, TRACING]", event.description());
Set<ServerFeature> serverFeatures = channel.attr(ChannelAttributes.SERVER_FEATURE_KEY).get();
assertEquals(toNegotiate, serverFeatures);
assertNull(channel.pipeline().get(FeatureNegotiatingHandler.class));
ReferenceCountUtil.release(writtenRequest);
}
Aggregations