aboutsummaryrefslogtreecommitdiffstats
path: root/jrt/tests/com/yahoo/jrt/PacketTest.java
blob: 970d926b70aee13f7796b309a616bcb9a52e150b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jrt;

import java.nio.ByteBuffer;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class PacketTest {

    @org.junit.Test
    public void testRequestPacket() {

        Values params = new Values();
        params.add(new Int32Value(123));

        Packet packet = new RequestPacket(Packet.FLAG_NOREPLY, 42,
                                          "foobar", params);
        PacketInfo info = packet.getPacketInfo();

        ByteBuffer buf = ByteBuffer.allocate(info.packetLength());
        info.encodePacket(packet, buf);
        buf.flip();

        int bytes = 12 + 4 + 6 + params.bytes();
        ByteBuffer ref = ByteBuffer.allocate(bytes);
        ref.putInt(bytes - 4);    // plen
        ref.putShort((short)2);   // flags (no reply)
        ref.putShort((short)100); // pcode (request)
        ref.putInt(42);           // reqId
        ref.putInt(6);            // method name length
        ref.put((byte)'f').put((byte)'o').put((byte)'o')
            .put((byte)'b').put((byte)'a').put((byte)'r');
        params.encode(ref);
        assertEquals(0, ref.remaining());
        ref.flip();
        assertTrue(buf.equals(ref));

        PacketInfo info2 = PacketInfo.getPacketInfo(buf);
        assertTrue(info2 != null);
        assertEquals(info2.packetLength(), buf.remaining());
        Packet packet2 = info2.decodePacket(buf);
        assertEquals(0, buf.remaining());

        assertEquals(packet2.requestId(), 42);
        assertEquals(((RequestPacket)packet2).methodName(), "foobar");
        Values params2 = ((RequestPacket)packet2).parameters();
        assertEquals(params2.size(), 1);
        assertEquals(params2.get(0).type(), Value.INT32);
        assertEquals(params2.get(0).asInt32(), 123);
    }

    @org.junit.Test
    public void testReplyPacket() {
        Values ret = new Values();
        ret.add(new Int32Value(123));

        Packet packet = new ReplyPacket(0, 42, ret);
        PacketInfo info = packet.getPacketInfo();

        ByteBuffer buf = ByteBuffer.allocate(info.packetLength());
        info.encodePacket(packet, buf);
        buf.flip();

        int bytes = 12 + ret.bytes();
        ByteBuffer ref = ByteBuffer.allocate(bytes);
        ref.putInt(bytes - 4);    // plen
        ref.putShort((short)0);   // flags
        ref.putShort((short)101); // pcode (reply)
        ref.putInt(42);           // reqId
        ret.encode(ref);
        assertEquals(0, ref.remaining());
        ref.flip();
        assertTrue(buf.equals(ref));

        PacketInfo info2 = PacketInfo.getPacketInfo(buf);
        assertTrue(info2 != null);
        assertEquals(info2.packetLength(), buf.remaining());
        Packet packet2 = info2.decodePacket(buf);
        assertEquals(0, buf.remaining());

        assertEquals(packet2.requestId(), 42);
        Values ret2 = ((ReplyPacket)packet2).returnValues();
        assertEquals(ret2.size(), 1);
        assertEquals(ret2.get(0).type(), Value.INT32);
        assertEquals(ret2.get(0).asInt32(), 123);
    }

    @org.junit.Test
    public void testErrorPacket() {
        String errStr = "NSM";
        Packet packet =
            new ErrorPacket(0, 42, ErrorCode.NO_SUCH_METHOD, errStr);
        PacketInfo info = packet.getPacketInfo();

        ByteBuffer buf = ByteBuffer.allocate(info.packetLength());
        info.encodePacket(packet, buf);
        buf.flip();

        int bytes = 12 + 4 + 4 + 3;
        ByteBuffer ref = ByteBuffer.allocate(bytes);
        ref.putInt(bytes - 4);    // plen
        ref.putShort((short)0);   // flags
        ref.putShort((short)102); // pcode (error)
        ref.putInt(42);           // reqId
        ref.putInt(ErrorCode.NO_SUCH_METHOD);
        ref.putInt(3); // length of errorMessage
        ref.put((byte)'N').put((byte)'S').put((byte)'M');
        assertEquals(0, ref.remaining());
        ref.flip();
        assertTrue(buf.equals(ref));

        PacketInfo info2 = PacketInfo.getPacketInfo(buf);
        assertTrue(info2 != null);
        assertEquals(info2.packetLength(), buf.remaining());
        Packet packet2 = info2.decodePacket(buf);
        assertEquals(0, buf.remaining());

        assertEquals(packet2.requestId(), 42);
        assertEquals(ErrorCode.NO_SUCH_METHOD,
                     ((ErrorPacket)packet2).errorCode());
        assertEquals(errStr, ((ErrorPacket)packet2).errorMessage());
    }

}