Search in sources :

Example 1 with BufferTooSmallException

use of com.yahoo.fs4.BufferTooSmallException in project vespa by vespa-engine.

the class GetDocSumsPacketTestCase method assertPacket.

private static void assertPacket(boolean sendQuery, Result result, byte[] expected) throws BufferTooSmallException {
    GetDocSumsPacket packet = GetDocSumsPacket.create(result, "default", sendQuery);
    ByteBuffer buf = ByteBuffer.allocate(1024);
    packet.encode(buf);
    buf.flip();
    byte[] actual = new byte[buf.remaining()];
    buf.get(actual);
    // assertEquals(Arrays.toString(expected), Arrays.toString(actual));
    assertEquals("Equal length", expected.length, actual.length);
    for (int i = 0; i < expected.length; ++i) {
        if (expected[i] == IGNORE) {
            actual[i] = IGNORE;
        }
    }
    assertArrayEquals(expected, actual);
}
Also used : GetDocSumsPacket(com.yahoo.fs4.GetDocSumsPacket) ByteBuffer(java.nio.ByteBuffer)

Example 2 with BufferTooSmallException

use of com.yahoo.fs4.BufferTooSmallException in project vespa by vespa-engine.

the class PacketDecoderTestCase method testPartialWithMoreRoom.

/**
 * In this testcase we have a partial packet and room for
 * more data
 */
@Test
public void testPartialWithMoreRoom() throws BufferTooSmallException {
    ByteBuffer data = ByteBuffer.allocate(len);
    data.put(queryResultPacketData, 0, 10);
    data.flip();
    PacketDecoder.DecodedPacket p = PacketDecoder.extractPacket(data);
    assertTrue(p == null);
}
Also used : DecodedPacket(com.yahoo.fs4.PacketDecoder.DecodedPacket) PacketDecoder(com.yahoo.fs4.PacketDecoder) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 3 with BufferTooSmallException

use of com.yahoo.fs4.BufferTooSmallException in project vespa by vespa-engine.

the class PacketDecoderTestCase method testOneAndAHalfPackets.

/**
 * In this testcase we have one and a half packet
 */
@Test
public void testOneAndAHalfPackets() throws BufferTooSmallException {
    int half = len / 2;
    ByteBuffer data = ByteBuffer.allocate(len + half);
    data.put(queryResultPacketData);
    data.put(queryResultPacketData, 0, half);
    assertEquals((len + half), data.position());
    data.flip();
    // the first packet we should be able to extract just fine
    BasicPacket p1 = PacketDecoder.extractPacket(data).packet;
    assertTrue(p1 instanceof QueryResultPacket);
    PacketDecoder.DecodedPacket p2 = PacketDecoder.extractPacket(data);
    assertTrue(p2 == null);
    // at this point the buffer should be ready for more
    // reading so position should be at the end and limit
    // should be at capacity
    assertEquals(half, data.position());
    assertEquals(data.capacity(), data.limit());
}
Also used : QueryResultPacket(com.yahoo.fs4.QueryResultPacket) BasicPacket(com.yahoo.fs4.BasicPacket) DecodedPacket(com.yahoo.fs4.PacketDecoder.DecodedPacket) PacketDecoder(com.yahoo.fs4.PacketDecoder) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 4 with BufferTooSmallException

use of com.yahoo.fs4.BufferTooSmallException in project vespa by vespa-engine.

the class PacketDecoderTestCase method testOnePacket.

/**
 * In this testcase we have exactly one packet which fills the
 * entire buffer
 */
@Test
public void testOnePacket() throws BufferTooSmallException {
    ByteBuffer data = ByteBuffer.allocate(len);
    data.put(queryResultPacketData);
    data.flip();
    // not really necessary for testing, but these help visualize
    // the state the buffer should be in so a reader of this test
    // will not have to
    assertEquals(0, data.position());
    assertEquals(len, data.limit());
    assertEquals(len, data.capacity());
    assertEquals(data.limit(), data.capacity());
    PacketDecoder.DecodedPacket p = PacketDecoder.extractPacket(data);
    assertTrue(p.packet instanceof QueryResultPacket);
    // now the buffer should have position == capacity == limit
    assertEquals(len, data.position());
    assertEquals(len, data.limit());
    assertEquals(len, data.capacity());
    // next call to decode on same bufer should result
    // in null and buffer should be reset for writing.
    p = PacketDecoder.extractPacket(data);
    assertTrue(p == null);
    // make sure the buffer is now ready for reading
    assertEquals(0, data.position());
    assertEquals(len, data.limit());
    assertEquals(len, data.capacity());
}
Also used : QueryResultPacket(com.yahoo.fs4.QueryResultPacket) DecodedPacket(com.yahoo.fs4.PacketDecoder.DecodedPacket) PacketDecoder(com.yahoo.fs4.PacketDecoder) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 5 with BufferTooSmallException

use of com.yahoo.fs4.BufferTooSmallException in project vespa by vespa-engine.

the class PacketDecoderTestCase method testThreeBytesPacket.

/**
 * In this testcase we only have 3 bytes so we can't
 * even determine the size of the packet.
 */
@Test
public void testThreeBytesPacket() throws BufferTooSmallException {
    ByteBuffer data = ByteBuffer.allocate(len);
    data.put(queryResultPacketData, 0, 3);
    data.flip();
    // packetLength() should return -1 since we don't even have
    // the size of the packet
    assertEquals(-1, PacketDecoder.packetLength(data));
    // since we can't determine the size we don't get a packet.
    // the buffer should now be at offset 3 so we can read more
    // data and limit should be set to capacity
    PacketDecoder.DecodedPacket p = PacketDecoder.extractPacket(data);
    assertTrue(p == null);
    assertEquals(3, data.position());
    assertEquals(len, data.limit());
    assertEquals(len, data.capacity());
}
Also used : DecodedPacket(com.yahoo.fs4.PacketDecoder.DecodedPacket) PacketDecoder(com.yahoo.fs4.PacketDecoder) ByteBuffer(java.nio.ByteBuffer) 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