use of com.yahoo.fs4.BufferTooSmallException in project vespa by vespa-engine.
the class PacketTestCase method testLargeQueryFail.
/**
* Make a query that is too large and then try to encode it
* into a small ByteBuffer
*/
@Test
public void testLargeQueryFail() {
StringBuilder queryBuffer = new StringBuilder(4008);
queryBuffer.append("/?query=");
for (int i = 0; i < 1000; i++) {
queryBuffer.append("the%20");
}
Query query = new Query(queryBuffer.toString());
assertNotNull(query);
QueryPacket queryPacket = QueryPacket.create(query);
assertNotNull(queryPacket);
ByteBuffer buffer = ByteBuffer.allocate(100);
int position = buffer.position();
int limit = buffer.limit();
try {
queryPacket.encode(buffer, 0);
fail();
} catch (BufferTooSmallException e) {
// success if exception is thrown
}
// make sure state of buffer is unchanged
assertEquals(position, buffer.position());
assertEquals(limit, buffer.limit());
}
use of com.yahoo.fs4.BufferTooSmallException in project vespa by vespa-engine.
the class PacketTestCase method requireThatPacketsCanTurnOnCompression.
@Test
public void requireThatPacketsCanTurnOnCompression() throws BufferTooSmallException {
QueryPacket queryPacket = QueryPacket.create(new Query("/?query=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
ByteBuffer buffer = ByteBuffer.allocate(1024);
int channel = 32;
queryPacket.encode(buffer, channel);
buffer.flip();
// size
assertEquals(86, buffer.getInt());
// code
assertEquals(0xda, buffer.getInt());
assertEquals(channel, buffer.getInt());
queryPacket.setCompressionLimit(88);
buffer.clear();
queryPacket.encode(buffer, channel);
buffer.flip();
// size
assertEquals(86, buffer.getInt());
// code
assertEquals(0xda, buffer.getInt());
queryPacket.setCompressionLimit(84);
buffer.clear();
queryPacket.encode(buffer, channel);
buffer.flip();
// size
assertEquals(57, buffer.getInt());
// code
assertEquals(0x060000da, buffer.getInt());
assertEquals(channel, buffer.getInt());
}
use of com.yahoo.fs4.BufferTooSmallException in project vespa by vespa-engine.
the class PacketTestCase method testSmallQueryOK.
/**
* Make sure we don't get false negatives for reasonably sized
* buffers
*/
@Test
public void testSmallQueryOK() {
Query query = new Query("/?query=foo");
assertNotNull(query);
QueryPacket queryPacket = QueryPacket.create(query);
assertNotNull(queryPacket);
ByteBuffer buffer = ByteBuffer.allocate(1024);
int position = buffer.position();
try {
queryPacket.encode(buffer, 0);
} catch (BufferTooSmallException e) {
fail();
}
// make sure state of buffer HAS changed and is according
// to contract
assertTrue(position != buffer.position());
assertTrue(buffer.position() == buffer.limit());
}
use of com.yahoo.fs4.BufferTooSmallException in project vespa by vespa-engine.
the class QueryTestCase method testEncodeSortSpec.
@Test
public void testEncodeSortSpec() throws BufferTooSmallException {
Query query = new Query("/?query=chain&sortspec=%2Ba+-b&timeout=0");
query.setWindow(2, 8);
QueryPacket packet = QueryPacket.create(query);
ByteBuffer buffer = ByteBuffer.allocate(500);
buffer.limit(0);
packet.encode(buffer, 0);
byte[] encoded = new byte[buffer.position()];
buffer.rewind();
buffer.get(encoded);
byte[] correctBuffer = new byte[] { // Header
0, // Header
0, // Header
0, // Header
55, // Header
0, // Header
0, // Header
0, // Header
-38, // Header
0, // Header
0, // Header
0, // Header
0, // Features
0, // Features
0, // Features
0, // Features
-122, // offset
2, // maxhits
8, // querytimeout
0, // querytimeout
0, // querytimeout
0, // querytimeout
1, // qflags
0, // qflags
0, // qflags
0x40, // qflags
0x03, 7, 'd', 'e', 'f', 'a', 'u', 'l', 't', // sortspec length
0, // sortspec length
0, // sortspec length
0, // sortspec length
5, // sortspec
43, // sortspec
97, // sortspec
32, // sortspec
45, // sortspec
98, // num stackitems
0, // num stackitems
0, // num stackitems
0, // num stackitems
1, 0, 0, 0, 8, 4, 0, 5, 99, 104, 97, 105, 110 };
assertEqualArrays(correctBuffer, encoded);
// Encode again to test grantEncodingBuffer
buffer = packet.grantEncodingBuffer(0);
encoded = new byte[buffer.limit()];
buffer.get(encoded);
assertEqualArrays(correctBuffer, encoded);
}
use of com.yahoo.fs4.BufferTooSmallException in project vespa by vespa-engine.
the class PacketTestCase method requireThatUncompressablePacketsArentCompressed.
@Test
public void requireThatUncompressablePacketsArentCompressed() throws BufferTooSmallException {
QueryPacket queryPacket = QueryPacket.create(new Query("/?query=aaaaaaaaaaaaaaa"));
ByteBuffer buffer = ByteBuffer.allocate(1024);
int channel = 32;
queryPacket.setCompressionLimit(10);
buffer.clear();
queryPacket.encode(buffer, channel);
buffer.flip();
// size
assertEquals(56, buffer.getInt());
// code
assertEquals(0xda, buffer.getInt());
assertEquals(channel, buffer.getInt());
}
Aggregations