comparison tests/OnClientTester.java @ 530:bf47aab3aeb7 onclient

Add flag to disable watchdog timer
author Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
date Fri, 27 Aug 2021 10:30:01 +0200 (2021-08-27)
parents ac6331eb7175
children 53dc4349ace9
comparison
equal deleted inserted replaced
529:ac6331eb7175 530:bf47aab3aeb7
18 int failureCount = 0; 18 int failureCount = 0;
19 private MonetConnection conn; 19 private MonetConnection conn;
20 private PrintWriter out; 20 private PrintWriter out;
21 private Statement stmt; 21 private Statement stmt;
22 private StringWriter outBuffer; 22 private StringWriter outBuffer;
23 private WatchDog watchDog; 23 private final WatchDog watchDog;
24 24
25 public static void main(String[] args) throws SQLException, NoSuchMethodException { 25 public static void main(String[] args) throws SQLException, NoSuchMethodException {
26 String jdbcUrl = null; 26 String jdbcUrl = null;
27 String requiredPrefix = null; 27 String requiredPrefix = null;
28 int verbosity = 0; 28 int verbosity = 0;
29 boolean watchDogEnabled = true;
29 30
30 for (String arg : args) { 31 for (String arg : args) {
31 if (arg.equals("-v")) 32 if (arg.equals("-v"))
32 verbosity++; 33 verbosity++;
33 else if (arg.equals("-vv")) 34 else if (arg.equals("-vv"))
34 verbosity += 2; 35 verbosity += 2;
36 else if (arg.equals("-w"))
37 watchDogEnabled = false;
35 else if (jdbcUrl == null) 38 else if (jdbcUrl == null)
36 jdbcUrl = arg; 39 jdbcUrl = arg;
37 else if (requiredPrefix == null) 40 else if (requiredPrefix == null)
38 requiredPrefix = arg; 41 requiredPrefix = arg;
39 else { 42 else {
40 System.err.println("Unexpected argument " + arg); 43 System.err.println("Unexpected argument " + arg);
41 System.exit(2); 44 System.exit(2);
42 } 45 }
43 } 46 }
44 47
45 OnClientTester tester = new OnClientTester(jdbcUrl, verbosity); 48 OnClientTester tester = new OnClientTester(jdbcUrl, verbosity, watchDogEnabled);
46 tester.runTests(requiredPrefix); 49 tester.runTests(requiredPrefix);
47 50
48 if (tester.verbosity >= VERBOSITY_ON || tester.failureCount > 0) { 51 if (tester.verbosity >= VERBOSITY_ON || tester.failureCount > 0) {
49 System.out.println(); 52 System.out.println();
50 System.out.println("Ran " + tester.testCount + " tests, " + tester.failureCount + " failed"); 53 System.out.println("Ran " + tester.testCount + " tests, " + tester.failureCount + " failed");
52 if (tester.failureCount > 0) { 55 if (tester.failureCount > 0) {
53 System.exit(1); 56 System.exit(1);
54 } 57 }
55 } 58 }
56 59
57 public OnClientTester(String jdbcUrl, int verbosity) { 60 public OnClientTester(String jdbcUrl, int verbosity, boolean watchDogEnabled) {
58 this.jdbcUrl = jdbcUrl; 61 this.jdbcUrl = jdbcUrl;
59 this.verbosity = verbosity; 62 this.verbosity = verbosity;
63 watchDog = new WatchDog();
64 if (watchDogEnabled)
65 watchDog.enable();
66 else
67 watchDog.disable();
60 } 68 }
61 69
62 private void runTests(String testPrefix) throws SQLException, NoSuchMethodException { 70 private void runTests(String testPrefix) throws SQLException, NoSuchMethodException {
63 watchDog = new WatchDog(); 71 watchDog.stop();
64 try { 72 try {
65 String initialPrefix = "test_"; 73 String initialPrefix = "test_";
66 String methodPrefix = testPrefix == null ? initialPrefix : initialPrefix + testPrefix; 74 String methodPrefix = testPrefix == null ? initialPrefix : initialPrefix + testPrefix;
67 75
68 for (Method method : this.getClass().getDeclaredMethods()) { 76 for (Method method : this.getClass().getDeclaredMethods()) {
71 String testName = methodName.substring(initialPrefix.length()); 79 String testName = methodName.substring(initialPrefix.length());
72 runTest(testName, method); 80 runTest(testName, method);
73 } 81 }
74 } 82 }
75 } finally { 83 } finally {
76 watchDog.kill(); 84 watchDog.stop();
77 watchDog = null;
78 } 85 }
79 } 86 }
80 87
81 private synchronized void runTest(String testName, Method method) throws SQLException { 88 private synchronized void runTest(String testName, Method method) throws SQLException {
82 watchDog.setContext("test " + testName); 89 watchDog.setContext("test " + testName);
366 } 373 }
367 374
368 } 375 }
369 376
370 static class WatchDog { 377 static class WatchDog {
378 private boolean enabled;
371 private long duration = 1000; 379 private long duration = 1000;
372 private long started = 0; 380 private long started = 0;
373 private String context = "no context"; 381 private String context = "no context";
374 382
375 WatchDog() { 383 WatchDog() {
376 Thread watchDog = new Thread(this::work); 384 Thread watchDog = new Thread(this::work);
377 watchDog.setName("watchdog_timer"); 385 watchDog.setName("watchdog_timer");
378 watchDog.setDaemon(true); 386 watchDog.setDaemon(true);
379 watchDog.start(); 387 watchDog.start();
388 }
389
390 synchronized void enable() {
391 this.enabled = true;
392 this.notifyAll();
393 }
394
395 synchronized void disable() {
396 this.enabled = false;
397 this.notifyAll();
380 } 398 }
381 399
382 synchronized void setContext(String context) { 400 synchronized void setContext(String context) {
383 this.context = context; 401 this.context = context;
384 } 402 }
412 final long sleepTime; 430 final long sleepTime;
413 if (started < 0) { 431 if (started < 0) {
414 // client asked us to go away 432 // client asked us to go away
415 // System.err.println("++ EXIT"); 433 // System.err.println("++ EXIT");
416 return; 434 return;
417 } else if (started == 0) { 435 } else if (!enabled || started == 0) {
418 // wait for client to start us 436 // wait for client to enable/start us
419 sleepTime = 600_000; 437 sleepTime = 600_000;
420 } else { 438 } else {
421 long deadline = started + duration; 439 long deadline = started + duration;
422 sleepTime = deadline - now; 440 sleepTime = deadline - now;
423 } 441 }