use of org.apache.cassandra.io.util.DataInputPlus in project cassandra by apache.
the class BytesReadTrackerTest method internalTestBytesRead.
public void internalTestBytesRead(boolean inputStream) throws Exception {
byte[] testData;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(baos);
try {
// boolean
out.writeBoolean(true);
// byte
out.writeByte(0x1);
// char
out.writeChar('a');
// short
out.writeShort(1);
// int
out.writeInt(1);
// long
out.writeLong(1L);
// float
out.writeFloat(1.0f);
// double
out.writeDouble(1.0d);
// String
out.writeUTF("abc");
testData = baos.toByteArray();
} finally {
out.close();
}
DataInputPlus.DataInputStreamPlus in = new DataInputPlus.DataInputStreamPlus(new ByteArrayInputStream(testData));
BytesReadTracker tracker = inputStream ? new TrackedInputStream(in) : new TrackedDataInputPlus(in);
DataInputPlus reader = inputStream ? new DataInputPlus.DataInputStreamPlus((TrackedInputStream) tracker) : (DataInputPlus) tracker;
try {
// boolean = 1byte
boolean bool = reader.readBoolean();
assertTrue(bool);
assertEquals(1, tracker.getBytesRead());
// byte = 1byte
byte b = reader.readByte();
assertEquals(b, 0x1);
assertEquals(2, tracker.getBytesRead());
// char = 2byte
char c = reader.readChar();
assertEquals('a', c);
assertEquals(4, tracker.getBytesRead());
// short = 2bytes
short s = reader.readShort();
assertEquals(1, s);
assertEquals((short) 6, tracker.getBytesRead());
// int = 4bytes
int i = reader.readInt();
assertEquals(1, i);
assertEquals(10, tracker.getBytesRead());
// long = 8bytes
long l = reader.readLong();
assertEquals(1L, l);
assertEquals(18, tracker.getBytesRead());
// float = 4bytes
float f = reader.readFloat();
assertEquals(1.0f, f, 0);
assertEquals(22, tracker.getBytesRead());
// double = 8bytes
double d = reader.readDouble();
assertEquals(1.0d, d, 0);
assertEquals(30, tracker.getBytesRead());
// String("abc") = 2(string size) + 3 = 5 bytes
String str = reader.readUTF();
assertEquals("abc", str);
assertEquals(35, tracker.getBytesRead());
assertEquals(testData.length, tracker.getBytesRead());
} finally {
in.close();
}
tracker.reset(0);
assertEquals(0, tracker.getBytesRead());
}
use of org.apache.cassandra.io.util.DataInputPlus in project cassandra by apache.
the class ConnectionTest method testInsufficientSpace.
@Test
public void testInsufficientSpace() throws Throwable {
test(new Settings(null).outbound(settings -> settings.withApplicationReserveSendQueueCapacityInBytes(1 << 15, new ResourceLimits.Concurrent(1 << 16)).withApplicationSendQueueCapacityInBytes(1 << 16)), (inbound, outbound, endpoint) -> {
CountDownLatch done = new CountDownLatch(1);
Message<?> message = Message.out(Verb._TEST_1, new Object());
MessagingService.instance().callbacks.addWithExpiration(new RequestCallback() {
@Override
public void onFailure(InetAddressAndPort from, RequestFailureReason failureReason) {
done.countDown();
}
@Override
public boolean invokeOnFailure() {
return true;
}
@Override
public void onResponse(Message msg) {
throw new IllegalStateException();
}
}, message, endpoint);
AtomicInteger delivered = new AtomicInteger();
unsafeSetSerializer(Verb._TEST_1, () -> new IVersionedSerializer<Object>() {
public void serialize(Object o, DataOutputPlus out, int version) throws IOException {
for (int i = 0; i <= 4 << 16; i += 8L) out.writeLong(1L);
}
public Object deserialize(DataInputPlus in, int version) throws IOException {
in.skipBytesFully(4 << 16);
return null;
}
public long serializedSize(Object o, int version) {
return 4 << 16;
}
});
unsafeSetHandler(Verb._TEST_1, () -> msg -> delivered.incrementAndGet());
outbound.enqueue(message);
Assert.assertTrue(done.await(10, SECONDS));
Assert.assertEquals(0, delivered.get());
check(outbound).submitted(1).sent(0, 0).pending(0, 0).overload(1, message.serializedSize(current_version)).expired(0, 0).error(0, 0).check();
check(inbound).received(0, 0).processed(0, 0).pending(0, 0).expired(0, 0).error(0, 0).check();
});
}
use of org.apache.cassandra.io.util.DataInputPlus in project cassandra by apache.
the class ConnectionTest method testSendLarge.
@Test
public void testSendLarge() throws Throwable {
test((inbound, outbound, endpoint) -> {
int version = outbound.settings().acceptVersions.max;
int count = 10;
CountDownLatch deliveryDone = new CountDownLatch(1);
CountDownLatch receiveDone = new CountDownLatch(count);
unsafeSetSerializer(Verb._TEST_1, () -> new IVersionedSerializer<Object>() {
public void serialize(Object noPayload, DataOutputPlus out, int version) throws IOException {
for (int i = 0; i < LARGE_MESSAGE_THRESHOLD + 1; ++i) out.writeByte(i);
}
public Object deserialize(DataInputPlus in, int version) throws IOException {
in.skipBytesFully(LARGE_MESSAGE_THRESHOLD + 1);
return noPayload;
}
public long serializedSize(Object noPayload, int version) {
return LARGE_MESSAGE_THRESHOLD + 1;
}
});
unsafeSetHandler(Verb._TEST_1, () -> msg -> receiveDone.countDown());
Message<?> message = Message.builder(Verb._TEST_1, new Object()).withExpiresAt(nanoTime() + SECONDS.toNanos(30L)).build();
for (int i = 0; i < count; ++i) outbound.enqueue(message);
Assert.assertTrue(receiveDone.await(10, SECONDS));
outbound.unsafeRunOnDelivery(deliveryDone::countDown);
Assert.assertTrue(deliveryDone.await(10, SECONDS));
check(outbound).submitted(10).sent(10, 10 * message.serializedSize(version)).pending(0, 0).overload(0, 0).expired(0, 0).error(0, 0).check();
check(inbound).received(10, 10 * message.serializedSize(version)).processed(10, 10 * message.serializedSize(version)).pending(0, 0).expired(0, 0).error(0, 0).check();
});
}
Aggregations