|
5 | 5 |
|
6 | 6 | import edu.stanford.nlp.pipeline.CoreNLPProtos;
|
7 | 7 |
|
| 8 | +import java.io.ByteArrayInputStream; |
| 9 | +import java.io.ByteArrayOutputStream; |
| 10 | +import java.io.DataInputStream; |
| 11 | +import java.io.DataOutputStream; |
| 12 | +import java.io.IOException; |
| 13 | + |
8 | 14 | public class ProcessSemgrexRequestTest {
|
9 | 15 | /**
|
10 | 16 | * Build a fake request. The same query will be repeated N times
|
@@ -85,7 +91,7 @@ public void testSimpleRequest() {
|
85 | 91 | }
|
86 | 92 |
|
87 | 93 | @Test
|
88 |
| - public void testMultiSemgrex() { |
| 94 | + public void testTwoSemgrex() { |
89 | 95 | CoreNLPProtos.SemgrexRequest request = buildFakeRequest(1, 2);
|
90 | 96 | CoreNLPProtos.SemgrexResponse response = ProcessSemgrexRequest.processRequest(request);
|
91 | 97 |
|
@@ -123,12 +129,86 @@ public void testEmptyRequest() {
|
123 | 129 | }
|
124 | 130 |
|
125 | 131 | @Test
|
126 |
| - public void testMultiRequest() { |
| 132 | + public void testTwoGraphs() { |
127 | 133 | CoreNLPProtos.SemgrexRequest request = buildFakeRequest(2, 1);
|
128 | 134 | CoreNLPProtos.SemgrexResponse response = ProcessSemgrexRequest.processRequest(request);
|
129 | 135 |
|
130 | 136 | Assert.assertEquals("Expected exactly 2 replies", 2, response.getResultList().size());
|
131 | 137 | checkResult(response.getResultList().get(0), 1);
|
132 | 138 | checkResult(response.getResultList().get(1), 1);
|
133 | 139 | }
|
| 140 | + |
| 141 | + public byte[] buildRepeatedRequest(int count, boolean closingLength) throws IOException { |
| 142 | + ByteArrayOutputStream singleBout = new ByteArrayOutputStream(); |
| 143 | + CoreNLPProtos.SemgrexRequest singleRequest = buildFakeRequest(1, 1); |
| 144 | + singleRequest.writeTo(singleBout); |
| 145 | + byte[] singleBytes = singleBout.toByteArray(); |
| 146 | + |
| 147 | + ByteArrayOutputStream bout = new ByteArrayOutputStream(); |
| 148 | + DataOutputStream dout = new DataOutputStream(bout); |
| 149 | + for (int i = 0; i < count; ++i) { |
| 150 | + dout.writeInt(singleBytes.length); |
| 151 | + dout.write(singleBytes, 0, singleBytes.length); |
| 152 | + } |
| 153 | + if (closingLength) { |
| 154 | + dout.writeInt(0); |
| 155 | + } |
| 156 | + dout.close(); |
| 157 | + |
| 158 | + return bout.toByteArray(); |
| 159 | + } |
| 160 | + |
| 161 | + public void checkRepeatedResults(byte[] arr, int count) throws IOException { |
| 162 | + ByteArrayInputStream bin = new ByteArrayInputStream(arr); |
| 163 | + DataInputStream din = new DataInputStream(bin); |
| 164 | + for (int i = 0; i < count; ++i) { |
| 165 | + int len = din.readInt(); |
| 166 | + byte[] responseBytes = new byte[len]; |
| 167 | + din.read(responseBytes, 0, len); |
| 168 | + CoreNLPProtos.SemgrexResponse response = CoreNLPProtos.SemgrexResponse.parseFrom(responseBytes); |
| 169 | + checkResult(response.getResultList().get(0), 1); |
| 170 | + } |
| 171 | + int len = din.readInt(); |
| 172 | + Assert.assertEquals("Repeated results should be over", 0, len); |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * Test that the multiple request pathway works with 1 request |
| 177 | + */ |
| 178 | + @Test |
| 179 | + public void testSingleMultiRequest() throws IOException { |
| 180 | + byte[] request = buildRepeatedRequest(1, true); |
| 181 | + ByteArrayInputStream bin = new ByteArrayInputStream(request); |
| 182 | + ByteArrayOutputStream bout = new ByteArrayOutputStream(); |
| 183 | + |
| 184 | + ProcessSemgrexRequest.processMultipleInputs(bin, bout); |
| 185 | + checkRepeatedResults(bout.toByteArray(), 1); |
| 186 | + } |
| 187 | + |
| 188 | + /** |
| 189 | + * Test that the multiple request pathway works with 2 requests |
| 190 | + */ |
| 191 | + @Test |
| 192 | + public void testDoubleMultiRequest() throws IOException { |
| 193 | + byte[] request = buildRepeatedRequest(2, true); |
| 194 | + ByteArrayInputStream bin = new ByteArrayInputStream(request); |
| 195 | + ByteArrayOutputStream bout = new ByteArrayOutputStream(); |
| 196 | + |
| 197 | + ProcessSemgrexRequest.processMultipleInputs(bin, bout); |
| 198 | + checkRepeatedResults(bout.toByteArray(), 2); |
| 199 | + } |
| 200 | + |
| 201 | + /** |
| 202 | + * Test that the multiple request pathway works even when the |
| 203 | + * input stream hits EOF |
| 204 | + */ |
| 205 | + @Test |
| 206 | + public void testUnclosedMultiRequest() throws IOException { |
| 207 | + byte[] request = buildRepeatedRequest(1, false); |
| 208 | + ByteArrayInputStream bin = new ByteArrayInputStream(request); |
| 209 | + ByteArrayOutputStream bout = new ByteArrayOutputStream(); |
| 210 | + |
| 211 | + ProcessSemgrexRequest.processMultipleInputs(bin, bout); |
| 212 | + checkRepeatedResults(bout.toByteArray(), 1); |
| 213 | + } |
134 | 214 | }
|
0 commit comments