2020-12-14 14:07:11 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2020 The Netty Project
|
|
|
|
*
|
|
|
|
* The Netty Project licenses this file to you under the Apache License,
|
|
|
|
* version 2.0 (the "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at:
|
|
|
|
*
|
|
|
|
* https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*/
|
2021-05-20 18:57:31 +02:00
|
|
|
package io.netty.buffer.api.tests;
|
2020-12-14 14:07:11 +01:00
|
|
|
|
2021-05-20 18:57:31 +02:00
|
|
|
import io.netty.buffer.api.Buffer;
|
|
|
|
import io.netty.buffer.api.BufferAllocator;
|
|
|
|
import io.netty.buffer.api.BufferRef;
|
|
|
|
import io.netty.buffer.api.Send;
|
2020-12-14 14:07:11 +01:00
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
|
|
import static org.junit.jupiter.api.Assertions.*;
|
|
|
|
|
2021-02-12 18:22:07 +01:00
|
|
|
class BufferRefTest {
|
2020-12-14 14:07:11 +01:00
|
|
|
@Test
|
|
|
|
public void closingBufRefMustCloseOwnedBuf() {
|
2021-02-12 18:22:07 +01:00
|
|
|
try (BufferAllocator allocator = BufferAllocator.heap()) {
|
|
|
|
BufferRef ref;
|
|
|
|
try (Buffer b = allocator.allocate(8)) {
|
|
|
|
ref = new BufferRef(b);
|
2020-12-14 14:07:11 +01:00
|
|
|
}
|
|
|
|
ref.contents().writeInt(42);
|
|
|
|
assertThat(ref.contents().readInt()).isEqualTo(42);
|
|
|
|
ref.close();
|
|
|
|
assertThrows(IllegalStateException.class, () -> ref.contents().writeInt(32));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void closingBufRefMustCloseOwnedBufFromSend() {
|
2021-02-12 18:22:07 +01:00
|
|
|
try (BufferAllocator allocator = BufferAllocator.heap();
|
|
|
|
Buffer buf = allocator.allocate(8)) {
|
|
|
|
BufferRef ref = new BufferRef(buf.send());
|
2020-12-14 14:07:11 +01:00
|
|
|
ref.contents().writeInt(42);
|
|
|
|
assertThat(ref.contents().readInt()).isEqualTo(42);
|
|
|
|
ref.close();
|
|
|
|
assertThrows(IllegalStateException.class, () -> ref.contents().writeInt(32));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void mustCloseOwnedBufferWhenReplaced() {
|
2021-02-12 18:22:07 +01:00
|
|
|
try (BufferAllocator allocator = BufferAllocator.heap()) {
|
|
|
|
Buffer orig;
|
|
|
|
BufferRef ref;
|
|
|
|
try (Buffer buf = allocator.allocate(8)) {
|
|
|
|
ref = new BufferRef(orig = buf);
|
2020-12-14 14:07:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
orig.writeInt(42);
|
|
|
|
assertThat(ref.contents().readInt()).isEqualTo(42);
|
|
|
|
|
2021-02-12 18:22:07 +01:00
|
|
|
try (Buffer buf = allocator.allocate(8)) {
|
2020-12-16 17:06:22 +01:00
|
|
|
ref.replace(buf); // Pass replacement directly.
|
2020-12-14 14:07:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
assertThrows(IllegalStateException.class, () -> orig.writeInt(32));
|
|
|
|
ref.contents().writeInt(42);
|
|
|
|
assertThat(ref.contents().readInt()).isEqualTo(42);
|
|
|
|
ref.close();
|
|
|
|
assertThrows(IllegalStateException.class, () -> ref.contents().writeInt(32));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void mustCloseOwnedBufferWhenReplacedFromSend() {
|
2021-02-12 18:22:07 +01:00
|
|
|
try (BufferAllocator allocator = BufferAllocator.heap()) {
|
|
|
|
Buffer orig;
|
|
|
|
BufferRef ref;
|
|
|
|
try (Buffer buf = allocator.allocate(8)) {
|
|
|
|
ref = new BufferRef(orig = buf);
|
2020-12-14 14:07:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
orig.writeInt(42);
|
|
|
|
assertThat(ref.contents().readInt()).isEqualTo(42);
|
|
|
|
|
2021-02-12 18:22:07 +01:00
|
|
|
try (Buffer buf = allocator.allocate(8)) {
|
2020-12-16 17:06:22 +01:00
|
|
|
ref.replace(buf.send()); // Pass replacement via send().
|
2020-12-14 14:07:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
assertThrows(IllegalStateException.class, () -> orig.writeInt(32));
|
|
|
|
ref.contents().writeInt(42);
|
|
|
|
assertThat(ref.contents().readInt()).isEqualTo(42);
|
|
|
|
ref.close();
|
|
|
|
assertThrows(IllegalStateException.class, () -> ref.contents().writeInt(32));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void sendingRefMustSendBuffer() {
|
2021-02-12 18:22:07 +01:00
|
|
|
try (BufferAllocator allocator = BufferAllocator.heap();
|
|
|
|
BufferRef refA = new BufferRef(allocator.allocate(8).send())) {
|
2020-12-14 14:07:11 +01:00
|
|
|
refA.contents().writeInt(42);
|
2021-02-12 18:22:07 +01:00
|
|
|
Send<BufferRef> send = refA.send();
|
2020-12-14 14:07:11 +01:00
|
|
|
assertThrows(IllegalStateException.class, () -> refA.contents().readInt());
|
2021-02-12 18:22:07 +01:00
|
|
|
try (BufferRef refB = send.receive()) {
|
2020-12-14 14:07:11 +01:00
|
|
|
assertThat(refB.contents().readInt()).isEqualTo(42);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|