summaryrefslogtreecommitdiffstats
path: root/container-search/src/test/java/com/yahoo/fs4/PacketQueryTracerTestCase.java
blob: 27d5e6778717e2ac7c6b7e6b6f36897fb2ba478a (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.fs4;

import static org.junit.Assert.*;

import java.io.IOException;
import java.io.StringWriter;
import java.nio.ByteBuffer;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.yahoo.fs4.mplex.FS4Channel;
import com.yahoo.fs4.mplex.InvalidChannelException;
import com.yahoo.search.Query;

/**
 * Ensure hex dumping of packets seems to work.
 *
 * @author <a href="mailto:steinar@yahoo-inc.com">Steinar Knutsen</a>
 */
public class PacketQueryTracerTestCase {
    FS4Channel channel;
    BasicPacket packet;
    PacketListener tracer;

    static class MockChannel extends FS4Channel {

        @Override
        public void setQuery(Query query) {
            super.setQuery(query);
        }

        @Override
        public Query getQuery() {
            return super.getQuery();
        }

        @Override
        public Integer getChannelId() {
            return 1;
        }

        @Override
        public void close() {
        }

        @Override
        public boolean sendPacket(BasicPacket packet)
                throws InvalidChannelException, IOException {
            return true;
        }

        @Override
        public BasicPacket[] receivePackets(long timeout, int packetCount)
                throws InvalidChannelException, ChannelTimeoutException {
            return null;
        }

        @Override
        public BasicPacket nextPacket(long timeout)
                throws InterruptedException, InvalidChannelException {
            return null;
        }

        @Override
        protected void addPacket(BasicPacket packet)
                throws InterruptedException, InvalidChannelException {
        }

        @Override
        public boolean isValid() {
            return true;
        }

        @Override
        public String toString() {
            return "MockChannel";
        }
    }

    @Before
    public void setUp() throws Exception {
        channel = new MockChannel();
        channel.setQuery(new Query("/?query=a&tracelevel=11"));
        packet = new PingPacket();
        tracer = new PacketQueryTracer();
    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public final void testPacketSent() throws IOException {
        byte[] simulatedPacket = new byte[] { 1, 2, 3 };
        tracer.packetReceived(channel, packet, ByteBuffer.wrap(simulatedPacket));
        StringWriter w = new StringWriter();
        channel.getQuery().getContext(false).render(w);
        assertTrue(w.getBuffer().toString().indexOf("PingPacket: 010203") != -1);
    }

    @Test
    public final void testPacketReceived() throws IOException {
        byte[] simulatedPacket = new byte[] { 1, 2, 3 };
        tracer.packetReceived(channel, packet, ByteBuffer.wrap(simulatedPacket));
        StringWriter w = new StringWriter();
        channel.getQuery().getContext(false).render(w);
        assertTrue(w.getBuffer().toString().indexOf("PingPacket: 010203") != -1);
    }

}