BUG-9141: Fix Stateful07TopologySessionListener failing test
[bgpcep.git] / pcep / topology-provider / src / main / java / org / opendaylight / bgpcep / pcep / topology / provider / PCEPRequest.java
index 8109d25a9e99db78e0e940ebd94b3b96b7ec3903..4f8e15d8d56a543555497185269dd5f82cb5f9cd 100755 (executable)
@@ -12,22 +12,28 @@ import com.google.common.util.concurrent.ListenableFuture;
 import com.google.common.util.concurrent.SettableFuture;
 import java.util.Timer;
 import java.util.concurrent.TimeUnit;
+import javax.annotation.concurrent.GuardedBy;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.OperationResult;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.lsp.metadata.Metadata;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 final class PCEPRequest {
+
+    private static final Logger LOG = LoggerFactory.getLogger(PCEPRequest.class);
+
+    private static final long MINIMUM_ELAPSED_TIME = 1;
+
     enum State {
         UNSENT,
         UNACKED,
         DONE,
     }
 
-    private static final Logger LOG = LoggerFactory.getLogger(PCEPRequest.class);
     private final SettableFuture<OperationResult> future;
     private final Metadata metadata;
     private volatile State state;
+    @GuardedBy("this")
     private final Stopwatch stopwatch;
     private final Timer timer;
 
@@ -40,33 +46,33 @@ final class PCEPRequest {
     }
 
     protected ListenableFuture<OperationResult> getFuture() {
-        return future;
+        return this.future;
     }
 
     public Metadata getMetadata() {
-        return metadata;
+        return this.metadata;
     }
 
     public State getState() {
-        return state;
+        return this.state;
     }
 
     Timer getTimer() {
-        return timer;
+        return this.timer;
     }
 
     synchronized void done(final OperationResult result) {
-        if (state != State.DONE) {
-            LOG.debug("Request went from {} to {}", state, State.DONE);
-            state = State.DONE;
-            timer.cancel();
-            future.set(result);
+        if (this.state != State.DONE) {
+            LOG.debug("Request went from {} to {}", this.state, State.DONE);
+            this.state = State.DONE;
+            this.timer.cancel();
+            this.future.set(result);
         }
     }
 
     synchronized void done() {
         OperationResult result;
-        switch (state) {
+        switch (this.state) {
         case UNSENT:
             result = OperationResults.UNSENT;
             break;
@@ -82,13 +88,18 @@ final class PCEPRequest {
     }
 
     synchronized void sent() {
-        if (state == State.UNSENT) {
-            LOG.debug("Request went from {} to {}", state, State.UNACKED);
-            state = State.UNACKED;
+        if (this.state == State.UNSENT) {
+            LOG.debug("Request went from {} to {}", this.state, State.UNACKED);
+            this.state = State.UNACKED;
         }
     }
 
-    long getElapsedMillis() {
-        return this.stopwatch.elapsed(TimeUnit.MILLISECONDS);
+    synchronized long getElapsedMillis() {
+        final long elapsedNanos = this.stopwatch.elapsed().toNanos();
+        final long elapsedMillis = TimeUnit.NANOSECONDS.toMillis(elapsedNanos);
+        if (elapsedMillis == 0 && elapsedNanos > 0) {
+            return  MINIMUM_ELAPSED_TIME;
+        }
+        return elapsedMillis;
     }
 }