LimitedDataBufferList adds or raises error

Closes gh-26060
This commit is contained in:
Rossen Stoyanchev 2020-11-10 19:59:36 +00:00
parent fc5e3c335f
commit 2b1f229998
3 changed files with 28 additions and 9 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2019 the original author or authors. * Copyright 2002-2020 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -54,11 +54,8 @@ public class LimitedDataBufferList extends ArrayList<DataBuffer> {
@Override @Override
public boolean add(DataBuffer buffer) { public boolean add(DataBuffer buffer) {
boolean result = super.add(buffer); updateCount(buffer.readableByteCount());
if (result) { return super.add(buffer);
updateCount(buffer.readableByteCount());
}
return result;
} }
@Override @Override

View File

@ -35,6 +35,8 @@ import java.util.List;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.PooledByteBufAllocator;
import org.junit.jupiter.api.Test;
import org.mockito.stubbing.Answer; import org.mockito.stubbing.Answer;
import org.reactivestreams.Subscription; import org.reactivestreams.Subscription;
import reactor.core.publisher.BaseSubscriber; import reactor.core.publisher.BaseSubscriber;
@ -834,6 +836,22 @@ class DataBufferUtilsTests extends AbstractDataBufferAllocatingTests {
.verifyError(DataBufferLimitException.class); .verifyError(DataBufferLimitException.class);
} }
@Test // gh-26060
void joinWithLimitDoesNotOverRelease() {
NettyDataBufferFactory bufferFactory = new NettyDataBufferFactory(PooledByteBufAllocator.DEFAULT);
byte[] bytes = "foo-bar-baz".getBytes(StandardCharsets.UTF_8);
NettyDataBuffer buffer = bufferFactory.allocateBuffer(bytes.length);
buffer.getNativeBuffer().retain(); // should be at 2 now
buffer.write(bytes);
Mono<DataBuffer> result = DataBufferUtils.join(Flux.just(buffer), 8);
StepVerifier.create(result).verifyError(DataBufferLimitException.class);
assertThat(buffer.getNativeBuffer().refCnt()).isEqualTo(1);
buffer.release();
}
@ParameterizedDataBufferAllocatingTest @ParameterizedDataBufferAllocatingTest
void joinErrors(String displayName, DataBufferFactory bufferFactory) { void joinErrors(String displayName, DataBufferFactory bufferFactory) {
super.bufferFactory = bufferFactory; super.bufferFactory = bufferFactory;

View File

@ -17,9 +17,11 @@ package org.springframework.core.io.buffer;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
/** /**
* Unit tests for {@link LimitedDataBufferList}. * Unit tests for {@link LimitedDataBufferList}.
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
@ -29,8 +31,10 @@ public class LimitedDataBufferListTests {
@Test @Test
void limitEnforced() { void limitEnforced() {
Assertions.assertThatThrownBy(() -> new LimitedDataBufferList(5).add(toDataBuffer("123456"))) LimitedDataBufferList list = new LimitedDataBufferList(5);
.isInstanceOf(DataBufferLimitException.class);
assertThatThrownBy(() -> list.add(toDataBuffer("123456"))).isInstanceOf(DataBufferLimitException.class);
assertThat(list).isEmpty();
} }
@Test @Test