comparison tests/OnClientTester.java @ 551:5ce6a942aff3 onclient

Last minute API fix: 'int offset' -> 'long linesToSkip' Int -> long because there might be many lines Offset -> linesToSkip to save users some work. The COPY INTO statement has an OFFSET modifier which is 1-based but also allows 0. This means both 0 and 1 mean 'upload the whole file' and any N > 1 means skip N-1 lines and upload the rest. To avoid having to deal with this over and over in every implementation of MonetConnection.UploadHandler#handleUpload(), parameter 'offset' has been replaced with 'linesToSkip' where the adjustment has already been performed.
author Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
date Tue, 14 Sep 2021 10:52:04 +0200 (2021-09-14)
parents d1f6678f92c5
children 87feb93330a6
comparison
equal deleted inserted replaced
550:c5cf3f00c4c5 551:5ce6a942aff3
162 prepare(); 162 prepare();
163 UploadHandler handler = new UploadHandler() { 163 UploadHandler handler = new UploadHandler() {
164 final String data = "1|one\n2|two\n3|three\n"; 164 final String data = "1|one\n2|two\n3|three\n";
165 165
166 @Override 166 @Override
167 public void handleUpload(MonetConnection.Upload handle, String name, boolean textMode, int offset) throws IOException { 167 public void handleUpload(MonetConnection.Upload handle, String name, boolean textMode, long linesToSkip) throws IOException {
168 // ignoring linesToSkip as it's not used in this test
168 ByteArrayInputStream s = new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8)); 169 ByteArrayInputStream s = new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8));
169 handle.uploadFrom(s); 170 handle.uploadFrom(s);
170 } 171 }
171 }; 172 };
172 conn.setUploadHandler(handler); 173 conn.setUploadHandler(handler);
178 prepare(); 179 prepare();
179 UploadHandler handler = new UploadHandler() { 180 UploadHandler handler = new UploadHandler() {
180 final String data = "1|one\n2|two\n3|three\n"; 181 final String data = "1|one\n2|two\n3|three\n";
181 182
182 @Override 183 @Override
183 public void handleUpload(MonetConnection.Upload handle, String name, boolean textMode, int offset) throws IOException { 184 public void handleUpload(MonetConnection.Upload handle, String name, boolean textMode, long linesToSkip) throws IOException {
185 // ignoring linesToSkip as it's not used in this test
184 StringReader r = new StringReader(data); 186 StringReader r = new StringReader(data);
185 handle.uploadFrom(r); 187 handle.uploadFrom(r);
186 } 188 }
187 }; 189 };
188 conn.setUploadHandler(handler); 190 conn.setUploadHandler(handler);
194 prepare(); 196 prepare();
195 UploadHandler handler = new UploadHandler() { 197 UploadHandler handler = new UploadHandler() {
196 final String data = "1|one\n2|two\n3|three\n"; 198 final String data = "1|one\n2|two\n3|three\n";
197 199
198 @Override 200 @Override
199 public void handleUpload(MonetConnection.Upload handle, String name, boolean textMode, int offset) throws IOException { 201 public void handleUpload(MonetConnection.Upload handle, String name, boolean textMode, long linesToSkip) throws IOException {
200 BufferedReader r = new BufferedReader(new StringReader(data)); 202 BufferedReader r = new BufferedReader(new StringReader(data));
201 handle.uploadFrom(r, offset); 203 handle.uploadFrom(r, linesToSkip);
202 } 204 }
203 }; 205 };
204 conn.setUploadHandler(handler); 206 conn.setUploadHandler(handler);
205 update("COPY OFFSET 2 INTO foo FROM 'banana' ON CLIENT", 2); 207 update("COPY OFFSET 2 INTO foo FROM 'banana' ON CLIENT", 2);
206 queryInt("SELECT i FROM foo WHERE t = 'three'", 3); 208 queryInt("SELECT i FROM foo WHERE t = 'three'", 3);
220 // Here we send empty lines only, to check if the server detects is properly instead 222 // Here we send empty lines only, to check if the server detects is properly instead
221 // of simply complaining about an incomplete file. 223 // of simply complaining about an incomplete file.
222 prepare(); 224 prepare();
223 UploadHandler handler = new UploadHandler() { 225 UploadHandler handler = new UploadHandler() {
224 @Override 226 @Override
225 public void handleUpload(MonetConnection.Upload handle, String name, boolean textMode, int offset) throws IOException { 227 public void handleUpload(MonetConnection.Upload handle, String name, boolean textMode, long linesToSkip) throws IOException {
228 // ignoring linesToSkip as it's not used in this test
226 PrintStream stream = handle.getStream(); 229 PrintStream stream = handle.getStream();
227 for (int i = 1; i <= 20_000; i++) 230 for (int i = 1; i <= 20_000; i++)
228 stream.println(); 231 stream.println();
229 stream.flush(); 232 stream.flush();
230 throw new IOException("exception after all"); 233 throw new IOException("exception after all");
246 // Exception closes the connection 249 // Exception closes the connection
247 assertEq("connection is closed", conn.isClosed(), true); 250 assertEq("connection is closed", conn.isClosed(), true);
248 } 251 }
249 252
250 static class MyUploadHandler implements UploadHandler { 253 static class MyUploadHandler implements UploadHandler {
251 private final int rows; 254 private final long rows;
252 private final int errorAt; 255 private final long errorAt;
253 private final String errorMessage; 256 private final String errorMessage;
254 private boolean encounteredWriteError; 257 private boolean encounteredWriteError;
255 258
256 private int chunkSize = 100; // small number to trigger more bugs 259 private int chunkSize = 100; // small number to trigger more bugs
257 260
258 MyUploadHandler(int rows, int errorAt, String errorMessage) { 261 MyUploadHandler(long rows, long errorAt, String errorMessage) {
259 this.rows = rows; 262 this.rows = rows;
260 this.errorAt = errorAt; 263 this.errorAt = errorAt;
261 this.errorMessage = errorMessage; 264 this.errorMessage = errorMessage;
262 } 265 }
263 266
264 MyUploadHandler(int rows) { 267 MyUploadHandler(long rows) {
265 this(rows, -1, null); 268 this(rows, -1, null);
266 } 269 }
267 270
268 MyUploadHandler(String errorMessage) { 271 MyUploadHandler(String errorMessage) {
269 this(0, -1, errorMessage); 272 this(0, -1, errorMessage);
272 public void setChunkSize(int chunkSize) { 275 public void setChunkSize(int chunkSize) {
273 this.chunkSize = chunkSize; 276 this.chunkSize = chunkSize;
274 } 277 }
275 278
276 @Override 279 @Override
277 public void handleUpload(MonetConnection.Upload handle, String name, boolean textMode, int offset) throws IOException { 280 public void handleUpload(MonetConnection.Upload handle, String name, boolean textMode, long linesToSkip) throws IOException {
278 int toSkip = offset > 0 ? offset - 1 : 0;
279 if (errorAt == -1 && errorMessage != null) { 281 if (errorAt == -1 && errorMessage != null) {
280 handle.sendError(errorMessage); 282 handle.sendError(errorMessage);
281 return; 283 return;
282 } 284 }
283 handle.setChunkSize(chunkSize); 285 handle.setChunkSize(chunkSize);
284 PrintStream stream = handle.getStream(); 286 PrintStream stream = handle.getStream();
285 for (int i = toSkip; i < rows; i++) { 287 for (long i = linesToSkip; i < rows; i++) {
286 if (i == errorAt) { 288 if (i == errorAt) {
287 throw new IOException(errorMessage); 289 throw new IOException(errorMessage);
288 } 290 }
289 stream.printf("%d|%d%n", i + 1, i + 1); 291 stream.printf("%d|%d%n", i + 1, i + 1);
290 if (i % 25 == 0 && stream.checkError()) { 292 if (i % 25 == 0 && stream.checkError()) {