Cleanup in BufTest to make it more extensible in the future
This commit is contained in:
parent
b5ac0fdbe6
commit
99ad2cc120
@ -27,8 +27,12 @@ import java.util.concurrent.Executors;
|
|||||||
import java.util.concurrent.Future;
|
import java.util.concurrent.Future;
|
||||||
import java.util.concurrent.SynchronousQueue;
|
import java.util.concurrent.SynchronousQueue;
|
||||||
|
|
||||||
import static org.hamcrest.Matchers.*;
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
import static org.junit.Assert.*;
|
import static org.hamcrest.Matchers.containsString;
|
||||||
|
import static org.junit.Assert.assertArrayEquals;
|
||||||
|
import static org.junit.Assert.assertSame;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
public abstract class BufTest {
|
public abstract class BufTest {
|
||||||
protected abstract Allocator createAllocator();
|
protected abstract Allocator createAllocator();
|
||||||
@ -39,7 +43,11 @@ public abstract class BufTest {
|
|||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
allocator = createAllocator();
|
allocator = createAllocator();
|
||||||
buf = allocator.allocate(8);
|
buf = allocate(8);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Buf allocate(int size) {
|
||||||
|
return allocator.allocate(size);
|
||||||
}
|
}
|
||||||
|
|
||||||
@After
|
@After
|
||||||
@ -91,7 +99,7 @@ public abstract class BufTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void acquireOnClosedBufferMustThrow() {
|
public void acquireOnClosedBufferMustThrow() {
|
||||||
var buf = allocator.allocate(8);
|
var buf = allocate(8);
|
||||||
buf.close();
|
buf.close();
|
||||||
try {
|
try {
|
||||||
buf.acquire();
|
buf.acquire();
|
||||||
@ -112,7 +120,7 @@ public abstract class BufTest {
|
|||||||
});
|
});
|
||||||
executor.shutdown();
|
executor.shutdown();
|
||||||
|
|
||||||
try (Buf buf = allocator.allocate(8)) {
|
try (Buf buf = allocate(8)) {
|
||||||
buf.writeByte((byte) 42);
|
buf.writeByte((byte) 42);
|
||||||
assertTrue(queue.offer(buf.send()));
|
assertTrue(queue.offer(buf.send()));
|
||||||
}
|
}
|
||||||
@ -131,7 +139,7 @@ public abstract class BufTest {
|
|||||||
});
|
});
|
||||||
executor.shutdown();
|
executor.shutdown();
|
||||||
|
|
||||||
try (Buf buf = allocator.allocate(8)) {
|
try (Buf buf = allocate(8)) {
|
||||||
assertSame(buf, buf.writeByte((byte) 42));
|
assertSame(buf, buf.writeByte((byte) 42));
|
||||||
queue.put(buf.send());
|
queue.put(buf.send());
|
||||||
}
|
}
|
||||||
@ -142,7 +150,7 @@ public abstract class BufTest {
|
|||||||
@Test
|
@Test
|
||||||
public void mustThrowWhenAllocatingZeroSizedBuffer() {
|
public void mustThrowWhenAllocatingZeroSizedBuffer() {
|
||||||
try {
|
try {
|
||||||
allocator.allocate(0);
|
allocate(0);
|
||||||
fail("Expected to throw an IllegalArgumentException.");
|
fail("Expected to throw an IllegalArgumentException.");
|
||||||
} catch (IllegalArgumentException ignore) {
|
} catch (IllegalArgumentException ignore) {
|
||||||
}
|
}
|
||||||
@ -151,7 +159,7 @@ public abstract class BufTest {
|
|||||||
@Test
|
@Test
|
||||||
public void mustThrowWhenAllocatingNegativeSizedBuffer() {
|
public void mustThrowWhenAllocatingNegativeSizedBuffer() {
|
||||||
try {
|
try {
|
||||||
allocator.allocate(-1);
|
allocate(-1);
|
||||||
fail("Expected to throw an IllegalArgumentException.");
|
fail("Expected to throw an IllegalArgumentException.");
|
||||||
} catch (IllegalArgumentException ignore) {
|
} catch (IllegalArgumentException ignore) {
|
||||||
}
|
}
|
||||||
@ -160,7 +168,7 @@ public abstract class BufTest {
|
|||||||
@Test
|
@Test
|
||||||
public void mustThrowWhenAllocatingOverSizedBuffer() {
|
public void mustThrowWhenAllocatingOverSizedBuffer() {
|
||||||
try {
|
try {
|
||||||
allocator.allocate(Integer.MAX_VALUE);
|
allocate(Integer.MAX_VALUE);
|
||||||
fail("Expected to throw an IllegalArgumentException.");
|
fail("Expected to throw an IllegalArgumentException.");
|
||||||
} catch (IllegalArgumentException ignore) {
|
} catch (IllegalArgumentException ignore) {
|
||||||
}
|
}
|
||||||
@ -169,7 +177,7 @@ public abstract class BufTest {
|
|||||||
@Test
|
@Test
|
||||||
public void mustAllowAllocatingMaxArraySizedBuffer() {
|
public void mustAllowAllocatingMaxArraySizedBuffer() {
|
||||||
try {
|
try {
|
||||||
allocator.allocate(Integer.MAX_VALUE - 8).close();
|
allocate(Integer.MAX_VALUE - 8).close();
|
||||||
} catch (OutOfMemoryError oome) {
|
} catch (OutOfMemoryError oome) {
|
||||||
// Mark test as ignored if this happens.
|
// Mark test as ignored if this happens.
|
||||||
throw new AssumptionViolatedException("JVM does not have enough memory for this test.", oome);
|
throw new AssumptionViolatedException("JVM does not have enough memory for this test.", oome);
|
||||||
@ -222,14 +230,14 @@ public abstract class BufTest {
|
|||||||
@Test
|
@Test
|
||||||
public void capacityMustBeAllocatedSize() {
|
public void capacityMustBeAllocatedSize() {
|
||||||
assertEquals(8, buf.capacity());
|
assertEquals(8, buf.capacity());
|
||||||
try (Buf b = allocator.allocate(13)) {
|
try (Buf b = allocate(13)) {
|
||||||
assertEquals(13, b.capacity());
|
assertEquals(13, b.capacity());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void fill() {
|
public void fill() {
|
||||||
try (Buf buf = allocator.allocate(16)) {
|
try (Buf buf = allocate(16)) {
|
||||||
assertSame(buf, buf.fill((byte) 0xA5));
|
assertSame(buf, buf.fill((byte) 0xA5));
|
||||||
buf.writerIndex(16);
|
buf.writerIndex(16);
|
||||||
assertEquals(0xA5A5A5A5_A5A5A5A5L, buf.readLong());
|
assertEquals(0xA5A5A5A5_A5A5A5A5L, buf.readLong());
|
||||||
@ -239,7 +247,7 @@ public abstract class BufTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void readerWriterIndexUpdates() {
|
public void readerWriterIndexUpdates() {
|
||||||
try (Buf buf = allocator.allocate(22)) {
|
try (Buf buf = allocate(22)) {
|
||||||
assertEquals(0, buf.writerIndex());
|
assertEquals(0, buf.writerIndex());
|
||||||
assertSame(buf, buf.writerIndex(1));
|
assertSame(buf, buf.writerIndex(1));
|
||||||
assertEquals(1, buf.writerIndex());
|
assertEquals(1, buf.writerIndex());
|
||||||
@ -271,7 +279,7 @@ public abstract class BufTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void readAndWriteBoundsChecksWithIndexUpdates() {
|
public void readAndWriteBoundsChecksWithIndexUpdates() {
|
||||||
try (Buf buf = allocator.allocate(8)) {
|
try (Buf buf = allocate(8)) {
|
||||||
buf.writeLong(0);
|
buf.writeLong(0);
|
||||||
|
|
||||||
buf.readLong(); // Fine.
|
buf.readLong(); // Fine.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user