use of cn.taketoday.core.io.buffer.NettyDataBufferFactory in project today-infrastructure by TAKETODAY.
the class AbstractDataBufferAllocatingTests method verifyAllocations.
private void verifyAllocations() {
if (this.bufferFactory instanceof NettyDataBufferFactory) {
ByteBufAllocator allocator = ((NettyDataBufferFactory) this.bufferFactory).getByteBufAllocator();
if (allocator instanceof PooledByteBufAllocator) {
Instant start = Instant.now();
while (true) {
PooledByteBufAllocatorMetric metric = ((PooledByteBufAllocator) allocator).metric();
long total = getAllocations(metric.directArenas()) + getAllocations(metric.heapArenas());
if (total == 0) {
return;
}
if (Instant.now().isBefore(start.plus(Duration.ofSeconds(5)))) {
try {
Thread.sleep(50);
} catch (InterruptedException ex) {
// ignore
}
continue;
}
assertThat(total).as("ByteBuf Leak: " + total + " unreleased allocations").isEqualTo(0);
}
}
}
}
use of cn.taketoday.core.io.buffer.NettyDataBufferFactory in project today-framework by TAKETODAY.
the class BodyExtractorsTests method unsupportedMediaTypeShouldConsumeAndCancel.
// SPR-17054
@Test
public void unsupportedMediaTypeShouldConsumeAndCancel() {
NettyDataBufferFactory factory = new NettyDataBufferFactory(new PooledByteBufAllocator(true));
NettyDataBuffer buffer = factory.wrap(ByteBuffer.wrap("spring".getBytes(StandardCharsets.UTF_8)));
TestPublisher<DataBuffer> body = TestPublisher.create();
MockClientHttpResponse response = new MockClientHttpResponse(HttpStatus.OK);
response.getHeaders().setContentType(MediaType.APPLICATION_PDF);
response.setBody(body.flux());
BodyExtractor<Mono<User>, ReactiveHttpInputMessage> extractor = BodyExtractors.toMono(User.class);
StepVerifier.create(extractor.extract(response, this.context)).then(() -> {
body.assertWasSubscribed();
body.emit(buffer);
}).expectErrorSatisfies(throwable -> {
boolean condition = throwable instanceof UnsupportedMediaTypeException;
assertThat(condition).isTrue();
assertThatExceptionOfType(IllegalReferenceCountException.class).isThrownBy(buffer::release);
body.assertCancelled();
}).verify();
}
use of cn.taketoday.core.io.buffer.NettyDataBufferFactory in project today-framework by TAKETODAY.
the class ReactorHttpHandlerAdapter method apply.
@Override
public Mono<Void> apply(HttpServerRequest reactorRequest, HttpServerResponse reactorResponse) {
NettyDataBufferFactory bufferFactory = new NettyDataBufferFactory(reactorResponse.alloc());
try {
ServerHttpResponse response = new ReactorServerHttpResponse(reactorResponse, bufferFactory);
ReactorServerHttpRequest request = new ReactorServerHttpRequest(reactorRequest, bufferFactory);
if (request.getMethod() == HttpMethod.HEAD) {
response = new HttpHeadResponseDecorator(response);
}
if (isDebugEnabled) {
return httpHandler.handle(request, response).doOnError(ex -> log.trace("{}Failed to complete: {}", request.getLogPrefix(), ex.getMessage())).doOnSuccess(aVoid -> log.trace("{}Handling completed", request.getLogPrefix()));
}
return httpHandler.handle(request, response);
} catch (URISyntaxException ex) {
if (isDebugEnabled) {
log.debug("Failed to get request URI: {}", ex.getMessage());
}
reactorResponse.status(HttpResponseStatus.BAD_REQUEST);
return Mono.empty();
}
}
use of cn.taketoday.core.io.buffer.NettyDataBufferFactory in project today-framework by TAKETODAY.
the class AbstractDataBufferAllocatingTests method verifyAllocations.
private void verifyAllocations() {
if (this.bufferFactory instanceof NettyDataBufferFactory) {
ByteBufAllocator allocator = ((NettyDataBufferFactory) this.bufferFactory).getByteBufAllocator();
if (allocator instanceof PooledByteBufAllocator) {
Instant start = Instant.now();
while (true) {
PooledByteBufAllocatorMetric metric = ((PooledByteBufAllocator) allocator).metric();
long total = getAllocations(metric.directArenas()) + getAllocations(metric.heapArenas());
if (total == 0) {
return;
}
if (Instant.now().isBefore(start.plus(Duration.ofSeconds(5)))) {
try {
Thread.sleep(50);
} catch (InterruptedException ex) {
// ignore
}
continue;
}
assertThat(total).as("ByteBuf Leak: " + total + " unreleased allocations").isEqualTo(0);
}
}
}
}
use of cn.taketoday.core.io.buffer.NettyDataBufferFactory in project today-infrastructure by TAKETODAY.
the class ReactorHttpHandlerAdapter method apply.
@Override
public Mono<Void> apply(HttpServerRequest reactorRequest, HttpServerResponse reactorResponse) {
NettyDataBufferFactory bufferFactory = new NettyDataBufferFactory(reactorResponse.alloc());
try {
ReactorServerHttpRequest request = new ReactorServerHttpRequest(reactorRequest, bufferFactory);
ServerHttpResponse response = new ReactorServerHttpResponse(reactorResponse, bufferFactory);
if (request.getMethod() == HttpMethod.HEAD) {
response = new HttpHeadResponseDecorator(response);
}
return this.httpHandler.handle(request, response).doOnError(ex -> logger.trace("{}Failed to complete: {}", request.getLogPrefix(), ex.getMessage())).doOnSuccess(aVoid -> logger.trace("{}Handling completed", request.getLogPrefix()));
} catch (URISyntaxException ex) {
if (logger.isDebugEnabled()) {
logger.debug("Failed to get request URI: {}", ex.getMessage());
}
reactorResponse.status(HttpResponseStatus.BAD_REQUEST);
return Mono.empty();
}
}
Aggregations