Github user alexandrelimassantana commented on a diff in the pull request: https://github.com/apache/cloudstack/pull/1445#discussion_r56808020 --- Diff: utils/src/test/java/com/cloud/utils/TestProfiler.java --- @@ -19,84 +19,79 @@ package com.cloud.utils; -import org.apache.log4j.Logger; import org.junit.Assert; +import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; import com.cloud.utils.testcase.Log4jEnabledTestCase; +@RunWith(PowerMockRunner.class) +@PrepareForTest({Profiler.class}) public class TestProfiler extends Log4jEnabledTestCase { - protected final static Logger s_logger = Logger.getLogger(TestProfiler.class); - private static final long MILLIS_FACTOR = 1000l; - private static final int MARGIN = 100; - // Profiler uses System.nanoTime which is not reliable on the millisecond - // A sleep of 1000 milliseconds CAN be measured as 999 milliseconds - // Therefore make the second larger, by 10% of the margin - private static final long ONE_SECOND = 1000l + (MARGIN / 10); - private static final double EXPONENT = 3d; + private static final long SLEEP_TIME_NANO = 1000000000L; + private static Profiler pf; + + @Before + public void setUp() { + pf = new Profiler(); + PowerMockito.mockStatic(System.class); + PowerMockito.when(System.nanoTime()).thenReturn(0L, SLEEP_TIME_NANO); --- End diff -- The statement on line 43 is saying that on the first call to nanoTime it will return 0 and the second call will return 10^9 ( 1 second in nanoseconds ). when start() calls nanoTime() first, it gets 0 and when stop() calls it again, the result will be 10^9. That is how it simulates the passage of 1s without actually making the thread sleep for 1 second. The tests previously written about this class are not intended to test the capability of the System class to capture time nor the Thread.sleep() method ( which was causing the test to fail as I stated because of the low priority the JVM's proccess have on different OS ). As it is right now, it stills test the same functionalities... Just without the sleep and System direct calls.
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---