aboutsummaryrefslogtreecommitdiffstats
path: root/jrt/tests/com/yahoo/jrt/Test.java
blob: ed2566a7ec211bdf4d190fd11c870e611dd99803 (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jrt;

import java.util.Arrays;

public class Test {

    @org.junit.Test
    public void testNothing() {}

    /**
     * Supervisor extension with some extra statistics used for
     * testing.
     **/
    public static class Orb extends Supervisor {
        public volatile int  readRequestCount  = 0;
        public volatile int  readReplyCount    = 0;
        public volatile int  readErrorCount    = 0;
        public volatile long readBytes         = 0;
        public volatile int  writeRequestCount = 0;
        public volatile int  writeReplyCount   = 0;
        public volatile int  writeErrorCount   = 0;
        public volatile long writeBytes        = 0;

        public Orb(Transport t) {
            super(t);
        }

        public boolean checkReadCounts(int request, int reply, int error) {
            return (request == readRequestCount &&
                    reply == readReplyCount &&
                    error == readErrorCount);
        }

        public boolean checkWriteCounts(int request, int reply, int error) {
            return (request == writeRequestCount &&
                    reply == writeReplyCount &&
                    error == writeErrorCount);
        }

        public void readPacket(PacketInfo info) {
            if (info.packetCode() == Packet.PCODE_REQUEST) {
                readRequestCount++;
            } else if (info.packetCode() == Packet.PCODE_REPLY) {
                readReplyCount++;
            } else if (info.packetCode() == Packet.PCODE_ERROR) {
                readErrorCount++;
            }
            readBytes += info.packetLength();
            super.readPacket(info);
        }

        public void writePacket(PacketInfo info) {
            if (info.packetCode() == Packet.PCODE_REQUEST) {
                writeRequestCount++;
            } else if (info.packetCode() == Packet.PCODE_REPLY) {
                writeReplyCount++;
            } else if (info.packetCode() == Packet.PCODE_ERROR) {
                writeErrorCount++;
            }
            writeBytes += info.packetLength();
            super.writePacket(info);
        }
    }

    /**
     * A simple object used to wait for the completion of an
     * asynchronous request.
     **/
    public static class Waiter implements RequestWaiter {
        private boolean done = false;
        public boolean isDone() {
            return done;
        }
        public synchronized void handleRequestDone(Request req) {
            done = true;
            notify();
        }
        public synchronized void waitDone() {
            while (!isDone()) {
                try { wait(); } catch (InterruptedException e) {}
            }
        }
    }

    /**
     * A simple object used to make one thread wait until another
     * thread tells it to continue.
     **/
    public static class Barrier {
        private boolean broken = false;
        public synchronized void reset() {
            broken = false;
        }
        public synchronized void breakIt() {
            broken = true;
            notify();
        }
        public synchronized void waitFor() {
            while (!broken) {
                try { wait(); } catch (InterruptedException e) {}
            }
        }
    }

    /**
     * A simple object used to pass a single object from one thread to
     * another.
     **/
    public static class Receptor {
        private Object obj = null;
        public synchronized void reset() {
            obj = null;
        }
        public synchronized Object get() {
            while (obj == null) {
                try { wait(); } catch (InterruptedException e) {}
            }
            return obj;
        }
        public synchronized void put(Object obj) {
            this.obj = obj;
            notify();
        }
    }


    public static boolean equals(byte[][] a, byte[][] b) {
        if (a == null || b == null) {
            return false;
        }
        if (a.length != b.length) {
            return false;
        }
        for (int i = 0; i < a.length; i++) {
            if (!Arrays.equals(a[i], b[i])) {
                return false;
            }
        }
        return true;
    }

    public static boolean equals(Value a, Value b) {
        if (a == null || b == null) {
            return false;
        }
        if (a.type() != b.type()) {
            return false;
        }
        switch (a.type()) {
        case Value.INT8:         return (a.asInt8() == b.asInt8());
        case Value.INT8_ARRAY:   return Arrays.equals(a.asInt8Array(),
                                                      b.asInt8Array());
        case Value.INT16:        return (a.asInt16() == b.asInt16());
        case Value.INT16_ARRAY:  return Arrays.equals(a.asInt16Array(),
                                                      b.asInt16Array());
        case Value.INT32:        return (a.asInt32() == b.asInt32());
        case Value.INT32_ARRAY:  return Arrays.equals(a.asInt32Array(),
                                                      b.asInt32Array());
        case Value.INT64:        return (a.asInt64() == b.asInt64());
        case Value.INT64_ARRAY:  return Arrays.equals(a.asInt64Array(),
                                                      b.asInt64Array());
        case Value.FLOAT:        return (a.asFloat() == b.asFloat());
        case Value.FLOAT_ARRAY:  return Arrays.equals(a.asFloatArray(),
                                                      b.asFloatArray());
        case Value.DOUBLE:       return (a.asDouble() == b.asDouble());
        case Value.DOUBLE_ARRAY: return Arrays.equals(a.asDoubleArray(),
                                                      b.asDoubleArray());
        case Value.DATA:         return Arrays.equals(a.asData(), b.asData());
        case Value.DATA_ARRAY:   return equals(a.asDataArray(),
                                               b.asDataArray());
        case Value.STRING:       return a.asString().equals(b.asString());
        case Value.STRING_ARRAY: return Arrays.equals(a.asStringArray(),
                                                      b.asStringArray());
        default: return false;
        }
    }

    public static boolean equals(Values a, Values b) {
        if (a == null || b == null) {
            return false;
        }
        if (a.size() != b.size()) {
            return false;
        }
        for (int i = 0; i < a.size(); i++) {
            if (!equals(a.get(i), b.get(i))) {
                return false;
            }
        }
        return true;
    }

}