|
| 1 | +/* |
| 2 | + * Copyright 2002-2019 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.core.io.buffer; |
| 18 | + |
| 19 | +import io.netty.buffer.PooledByteBufAllocator; |
| 20 | +import io.netty.buffer.UnpooledByteBufAllocator; |
| 21 | +import org.junit.jupiter.api.Nested; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | + |
| 24 | +import static org.assertj.core.api.Assertions.assertThat; |
| 25 | +import static org.assertj.core.api.Assertions.assertThatIllegalStateException; |
| 26 | + |
| 27 | +/** |
| 28 | + * @author Arjen Poutsma |
| 29 | + * @author Sam Brannen |
| 30 | + */ |
| 31 | +public class PooledDataBufferNestedTests { |
| 32 | + |
| 33 | + @Nested |
| 34 | + class UnpooledByteBufAllocatorWithPreferDirectTrueTests implements PooledDataBufferTestingTrait { |
| 35 | + |
| 36 | + @Override |
| 37 | + public DataBufferFactory createDataBufferFactory() { |
| 38 | + return new NettyDataBufferFactory(new UnpooledByteBufAllocator(true)); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + @Nested |
| 43 | + class UnpooledByteBufAllocatorWithPreferDirectFalseTests implements PooledDataBufferTestingTrait { |
| 44 | + |
| 45 | + @Override |
| 46 | + public DataBufferFactory createDataBufferFactory() { |
| 47 | + return new NettyDataBufferFactory(new UnpooledByteBufAllocator(true)); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + @Nested |
| 52 | + class PooledByteBufAllocatorWithPreferDirectTrueTests implements PooledDataBufferTestingTrait { |
| 53 | + |
| 54 | + @Override |
| 55 | + public DataBufferFactory createDataBufferFactory() { |
| 56 | + return new NettyDataBufferFactory(new PooledByteBufAllocator(true)); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + @Nested |
| 61 | + class PooledByteBufAllocatorWithPreferDirectFalseTests implements PooledDataBufferTestingTrait { |
| 62 | + |
| 63 | + @Override |
| 64 | + public DataBufferFactory createDataBufferFactory() { |
| 65 | + return new NettyDataBufferFactory(new PooledByteBufAllocator(true)); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + interface PooledDataBufferTestingTrait { |
| 70 | + |
| 71 | + DataBufferFactory createDataBufferFactory(); |
| 72 | + |
| 73 | + default PooledDataBuffer createDataBuffer(int capacity) { |
| 74 | + return (PooledDataBuffer) createDataBufferFactory().allocateBuffer(capacity); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + default void retainAndRelease() { |
| 79 | + PooledDataBuffer buffer = createDataBuffer(1); |
| 80 | + buffer.write((byte) 'a'); |
| 81 | + |
| 82 | + buffer.retain(); |
| 83 | + assertThat(buffer.release()).isFalse(); |
| 84 | + assertThat(buffer.release()).isTrue(); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + default void tooManyReleases() { |
| 89 | + PooledDataBuffer buffer = createDataBuffer(1); |
| 90 | + buffer.write((byte) 'a'); |
| 91 | + |
| 92 | + buffer.release(); |
| 93 | + assertThatIllegalStateException().isThrownBy(buffer::release); |
| 94 | + } |
| 95 | + |
| 96 | + } |
| 97 | + |
| 98 | +} |
0 commit comments