You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2023/01/02 10:57:17 UTC

[shardingsphere] branch master updated: Revise TimeRecorderTest (#23262)

This is an automated email from the ASF dual-hosted git repository.

duanzhengqiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new fcda30fba91 Revise TimeRecorderTest (#23262)
fcda30fba91 is described below

commit fcda30fba910f43f19a76560f134b1c2faeee970
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Mon Jan 2 18:57:12 2023 +0800

    Revise TimeRecorderTest (#23262)
---
 .../agent/plugin/core/util/TimeRecorder.java       |  3 ++-
 .../agent/plugin/core/util/TimeRecorderTest.java   | 28 ++++++++++++++--------
 2 files changed, 20 insertions(+), 11 deletions(-)

diff --git a/agent/plugins/core/src/main/java/org/apache/shardingsphere/agent/plugin/core/util/TimeRecorder.java b/agent/plugins/core/src/main/java/org/apache/shardingsphere/agent/plugin/core/util/TimeRecorder.java
index a1b6eeb81bc..1b81326c896 100644
--- a/agent/plugins/core/src/main/java/org/apache/shardingsphere/agent/plugin/core/util/TimeRecorder.java
+++ b/agent/plugins/core/src/main/java/org/apache/shardingsphere/agent/plugin/core/util/TimeRecorder.java
@@ -39,7 +39,8 @@ public enum TimeRecorder {
      * @return elapsed time
      */
     public long getElapsedTime() {
-        return System.currentTimeMillis() - CURRENT_RECORDER.get();
+        Long recordMillis = CURRENT_RECORDER.get();
+        return null == recordMillis ? 0L : System.currentTimeMillis() - recordMillis;
     }
     
     /**
diff --git a/agent/plugins/core/src/test/java/org/apache/shardingsphere/agent/plugin/core/util/TimeRecorderTest.java b/agent/plugins/core/src/test/java/org/apache/shardingsphere/agent/plugin/core/util/TimeRecorderTest.java
index c27992786f2..390faf5310f 100644
--- a/agent/plugins/core/src/test/java/org/apache/shardingsphere/agent/plugin/core/util/TimeRecorderTest.java
+++ b/agent/plugins/core/src/test/java/org/apache/shardingsphere/agent/plugin/core/util/TimeRecorderTest.java
@@ -17,23 +17,31 @@
 
 package org.apache.shardingsphere.agent.plugin.core.util;
 
+import org.junit.After;
+import org.junit.Before;
 import org.junit.Test;
 
-import static org.junit.Assert.assertThrows;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
 import static org.junit.Assert.assertTrue;
 
 public final class TimeRecorderTest {
-
+    
+    @Before
+    @After
+    public void reset() {
+        TimeRecorder.INSTANCE.clean();
+    }
+    
     @Test
-    public void assertRecordedElapsedTimeIsCorrectWhenCurrentRecorderIsPresent() throws InterruptedException {
+    public void assertGetElapsedTimeWithRecorded() throws InterruptedException {
         TimeRecorder.INSTANCE.record();
-        Thread.sleep(5);
-        assertTrue(TimeRecorder.INSTANCE.getElapsedTime() >= 5);
+        Thread.sleep(5L);
+        assertTrue(TimeRecorder.INSTANCE.getElapsedTime() >= 5L);
     }
-
+    
     @Test
-    public void assertElapsedTimeThrowsNullPointerExceptionWhenCurrentRecorderIsNotPresent() {
-        TimeRecorder.INSTANCE.clean();
-        assertThrows(NullPointerException.class, TimeRecorder.INSTANCE::getElapsedTime);
+    public void assertGetElapsedTimeWithoutRecorded() {
+        assertThat(TimeRecorder.INSTANCE.getElapsedTime(), is(0L));
     }
-}
\ No newline at end of file
+}