From 50e8bf5f73677a87c4ec131a8e0357458369cbfb Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Mon, 8 Oct 2018 12:23:06 -0400 Subject: Add coverage to test-transaction Fixed bug in test-transaction, wherein it was sometimes using the controller instead of the controller name as the key into the map. Changed test to use latch instead of sleep. Added a little more register/unregister testing. Change-Id: I2647b44cf164038d211063bd499c25af70c2d9d7 Issue-ID: POLICY-1148 Signed-off-by: Jim Hahn --- .../drools/testtransaction/TestTransaction.java | 54 ++++++++++++++-------- 1 file changed, 36 insertions(+), 18 deletions(-) (limited to 'feature-test-transaction/src/main/java') diff --git a/feature-test-transaction/src/main/java/org/onap/policy/drools/testtransaction/TestTransaction.java b/feature-test-transaction/src/main/java/org/onap/policy/drools/testtransaction/TestTransaction.java index c778bf67..452825c0 100644 --- a/feature-test-transaction/src/main/java/org/onap/policy/drools/testtransaction/TestTransaction.java +++ b/feature-test-transaction/src/main/java/org/onap/policy/drools/testtransaction/TestTransaction.java @@ -65,29 +65,29 @@ class TTImpl implements TestTransaction { @Override public synchronized void register(PolicyController controller) { - if (this.controllers.containsValue(controller)) { - final TTControllerTask controllerTask = this.controllers.get(controller.getName()); - if (controllerTask.isAlive()) { - return; - } - - // continue : unregister, register operation + TTControllerTask controllerTask = this.controllers.get(controller.getName()); + if (controllerTask != null && controllerTask.isAlive()) { + return; } - final TTControllerTask controllerTask = new TTControllerTask(controller); + // continue : unregister, register operation + + controllerTask = makeControllerTask(controller); this.controllers.put(controller.getName(), controllerTask); } @Override public synchronized void unregister(PolicyController controller) { - if (!this.controllers.containsValue(controller)) { - return; + final TTControllerTask controllerTask = this.controllers.remove(controller.getName()); + if (controllerTask != null) { + controllerTask.stop(); } + } - final TTControllerTask controllerTask = this.controllers.get(controller.getName()); - controllerTask.stop(); + // these may be overridden by junit tests - this.controllers.remove(controller.getName()); + protected TTControllerTask makeControllerTask(PolicyController controller) { + return new TTControllerTask(controller); } } @@ -103,7 +103,7 @@ class TTControllerTask implements Runnable { protected final PolicyController controller; protected volatile boolean alive = true; - protected final Thread thread = new Thread(this); + protected final Thread thread = makeThread(this); public TTControllerTask(PolicyController controller) { this.controller = controller; @@ -123,7 +123,7 @@ class TTControllerTask implements Runnable { this.alive = false; this.thread.interrupt(); try { - this.thread.join(1000); + joinThread(1000); } catch (final InterruptedException e) { logger.error("TestTransaction thread threw", e); this.thread.interrupt(); @@ -163,13 +163,13 @@ class TTControllerTask implements Runnable { return; } - if (!Thread.currentThread().isInterrupted()) { - Thread.sleep(TestTransaction.DEFAULT_TT_TASK_SLEEP); + if (!getCurrentThread().isInterrupted()) { + doSleep(TestTransaction.DEFAULT_TT_TASK_SLEEP); } } } catch (final InterruptedException e) { logger.info("{}: stopping ...", this, e); - Thread.currentThread().interrupt(); + getCurrentThread().interrupt(); } catch (final IllegalArgumentException e) { logger.error( "{}: controller {} has not been enabled for testing: ", @@ -245,4 +245,22 @@ class TTControllerTask implements Runnable { builder.append("]"); return builder.toString(); } + + // these may be overridden by junit tests + + protected Thread makeThread(Runnable action) { + return new Thread(action); + } + + protected void joinThread(long waitTimeMs) throws InterruptedException { + this.thread.join(waitTimeMs); + } + + protected void doSleep(long sleepMs) throws InterruptedException { + Thread.sleep(sleepMs); + } + + protected Thread getCurrentThread() { + return Thread.currentThread(); + } } -- cgit 1.2.3-korg