Search in sources :

Example 6 with BufferTooSmallException

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());
}
Also used : BufferTooSmallException(com.yahoo.fs4.BufferTooSmallException) Query(com.yahoo.search.Query) ByteBuffer(java.nio.ByteBuffer) QueryPacket(com.yahoo.fs4.QueryPacket) Test(org.junit.Test)

Example 7 with BufferTooSmallException

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());
}
Also used : Query(com.yahoo.search.Query) ByteBuffer(java.nio.ByteBuffer) QueryPacket(com.yahoo.fs4.QueryPacket) Test(org.junit.Test)

Example 8 with BufferTooSmallException

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());
}
Also used : BufferTooSmallException(com.yahoo.fs4.BufferTooSmallException) Query(com.yahoo.search.Query) ByteBuffer(java.nio.ByteBuffer) QueryPacket(com.yahoo.fs4.QueryPacket) Test(org.junit.Test)

Example 9 with BufferTooSmallException

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);
}
Also used : Query(com.yahoo.search.Query) ByteBuffer(java.nio.ByteBuffer) QueryPacket(com.yahoo.fs4.QueryPacket) Test(org.junit.Test)

Example 10 with BufferTooSmallException

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());
}
Also used : Query(com.yahoo.search.Query) ByteBuffer(java.nio.ByteBuffer) QueryPacket(com.yahoo.fs4.QueryPacket) Test(org.junit.Test)

Aggregations

ByteBuffer (java.nio.ByteBuffer)15 Test (org.junit.Test)12 QueryPacket (com.yahoo.fs4.QueryPacket)6 Query (com.yahoo.search.Query)6 DecodedPacket (com.yahoo.fs4.PacketDecoder.DecodedPacket)5 BufferTooSmallException (com.yahoo.fs4.BufferTooSmallException)4 PacketDecoder (com.yahoo.fs4.PacketDecoder)4 BasicPacket (com.yahoo.fs4.BasicPacket)2 QueryResultPacket (com.yahoo.fs4.QueryResultPacket)2 ErrorPacket (com.yahoo.fs4.ErrorPacket)1 GetDocSumsPacket (com.yahoo.fs4.GetDocSumsPacket)1 PacketWrapper (com.yahoo.prelude.fastsearch.PacketWrapper)1 PhraseItem (com.yahoo.prelude.query.PhraseItem)1 PhraseSegmentItem (com.yahoo.prelude.query.PhraseSegmentItem)1 WordItem (com.yahoo.prelude.query.WordItem)1