summaryrefslogtreecommitdiffstats
path: root/main/src/main
diff options
context:
space:
mode:
authorJim Hahn <jrh3@att.com>2019-03-29 09:57:17 -0400
committerJim Hahn <jrh3@att.com>2019-03-29 13:19:15 -0400
commitdf456cc61db87de428120e287c941b47a455ff3b (patch)
tree49f62c0a9126302b87da1caf618c6abd8704d229 /main/src/main
parent2f00429d5531304d5403638ef0796764f79abb0b (diff)
Fix issues with timer manager junits
Attempt to fix intermittent issues with junit tests for timer manager. Eliminated duplicate method in junit test and renamed another method. Always release a semaphore, just to be sure. Logged different message depending whether timer is cancelled or expired. Locked access to the current time to prevent the timer thread from accessing it while registering a new timer. Change-Id: Icb499acb18197fd8042eb186f88b61d5c30daeb0 Issue-ID: POLICY-1542 Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'main/src/main')
-rw-r--r--main/src/main/java/org/onap/policy/pap/main/comm/TimerManager.java26
1 files changed, 18 insertions, 8 deletions
diff --git a/main/src/main/java/org/onap/policy/pap/main/comm/TimerManager.java b/main/src/main/java/org/onap/policy/pap/main/comm/TimerManager.java
index 9748b0b5..f19d7db8 100644
--- a/main/src/main/java/org/onap/policy/pap/main/comm/TimerManager.java
+++ b/main/src/main/java/org/onap/policy/pap/main/comm/TimerManager.java
@@ -118,10 +118,8 @@ public class TimerManager implements Runnable {
logger.info("{} timer registered {}", name, timer);
- if (name2timer.size() == 1) {
- // release the timer thread
- sem.release();
- }
+ // release the timer thread in case it's waiting
+ sem.release();
return timer;
}
@@ -197,7 +195,7 @@ public class TimerManager implements Runnable {
return;
}
- if (!timer.cancel()) {
+ if (!timer.cancel("expired")) {
// timer was cancelled while we were waiting
return;
}
@@ -205,7 +203,6 @@ public class TimerManager implements Runnable {
// run the timer
try {
- logger.info("{} timer expired {}", TimerManager.this.name, timer);
timer.runner.accept(timer.name);
} catch (RuntimeException e) {
logger.warn("{} timer threw an exception {}", TimerManager.this.name, timer, e);
@@ -254,10 +251,21 @@ public class TimerManager implements Runnable {
* not running
*/
public boolean cancel() {
+ return cancel("cancelled");
+ }
- AtomicBoolean wasPresent = new AtomicBoolean(false);
+ /**
+ * Cancels the timer.
+ *
+ * @param cancelMsg message to log if the timer is successfully
+ * cancelled
+ * @return {@code true} if the timer was cancelled, {@code false} if the timer was
+ * not running
+ */
+ private boolean cancel(String cancelMsg) {
synchronized (lockit) {
+ AtomicBoolean wasPresent = new AtomicBoolean(false);
name2timer.computeIfPresent(name, (key, val) -> {
@@ -266,6 +274,7 @@ public class TimerManager implements Runnable {
return null;
} else {
+ // different timer is in the map - leave it
return val;
}
});
@@ -276,7 +285,8 @@ public class TimerManager implements Runnable {
return false;
}
- logger.debug("{} timer cancelled {}", TimerManager.this.name, this);
+ logger.debug("{} timer " + cancelMsg + " {}", TimerManager.this.name, this);
+
return true;
}
}