package alluxio.heartbeat;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;
import org.slf4j.Logger;

import alluxio.util.CommonUtils;

/**
 * Created by shayne on 5/17/16.
 *
 * Unit tests for {@link SleepingTimer}.
 *
 * 1. Sleep more than the interval of SleepingTimer and see if the SleepingTimer warns correctly 2.
 * Tick continuously for several time and see if the time interval is correct 3. Sleep less than the
 * interval of SleepingTimer and see if the time interval is correct
 */

@RunWith(PowerMockRunner.class)
@PrepareForTest({SleepingTimer.class})
public class SleepingTimerTest {
  private static final String THREAD_NAME = "sleepingtimer-test-thread-name";
  private static final long INTERVAL_MS = 500;

  @Test
  public void test() throws Exception {
    SleepingTimer stimer = new SleepingTimer(THREAD_NAME, INTERVAL_MS);

    Logger logger = Mockito.mock(Logger.class);
    long PreTickMs = 0;

    Whitebox.setInternalState(SleepingTimer.class, "LOG", logger);

    Whitebox.setInternalState(stimer, "mPreviousTickMs", PreTickMs);


    stimer.tick();
    CommonUtils.sleepMs(5 * INTERVAL_MS);
    stimer.tick();

    Mockito.verify(logger).warn(Mockito.anyString(), Mockito.anyString(), Mockito.anyLong(),
        Mockito.anyLong());

    long timeBeforeMs = PreTickMs;
    stimer.tick();
    stimer.tick();
    stimer.tick();
    long timeIntervalMs = System.currentTimeMillis() - timeBeforeMs;
    Assert.assertTrue(timeIntervalMs >= 3 * INTERVAL_MS);



    timeBeforeMs = PreTickMs;
    CommonUtils.sleepMs(INTERVAL_MS / 2);
    stimer.tick();
    timeIntervalMs = System.currentTimeMillis() - timeBeforeMs;
    Assert.assertTrue(timeIntervalMs >= INTERVAL_MS);

  }
}

@@ -1,8 +1,522 @@
+/*%0A * The Alluxio Open Foundation licenses this work under the Apache License, version 2.0%0A * (the %E2%80%9CLicense%E2%80%9D). You may not use this work except in compliance with the License, which is%0A * available at www.apache.org/licenses/LICENSE-2.0%0A *%0A * This software is distributed on an %22AS IS%22 basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,%0A * either express or implied, as more fully set forth in the License.%0A *%0A * See the NOTICE file distributed with this work for information regarding copyright ownership.%0A */%0A%0A
 package 
@@ -870,44 +870,8 @@
 /**%0A
- * Created by shayne on 5/17/16.%0A *%0A
  * U
@@ -906,19 +906,55 @@
 gTimer%7D.
-%0A *
+ It tests three scenarios listed below:
 %0A * 1. S
@@ -1046,14 +1046,14 @@
 ctly
- 2.
 %0A *
+ 2.
  Tic
@@ -1123,16 +1123,19 @@
  correct
+%0A *
  3. Slee
@@ -1149,19 +1149,16 @@
 than the
-%0A *
  interva
@@ -1712,17 +1712,16 @@
 ogger);%0A
-%0A
     Whit
@@ -1782,17 +1782,103 @@
 ckMs);%0A%0A
+    //first case, SleepingTimer should warn when excution time is longer than interval
 %0A
-
     stim
@@ -2068,24 +2068,95 @@
 nyLong());%0A%0A
+    //second case, tick three times and check the interval correctness%0A
     long tim
@@ -2363,17 +2363,92 @@
 L_MS);%0A%0A
-%0A
+    //third case, sleep less than interval and check theinterval correctness
 %0A    tim
@@ -2587,32 +2587,32 @@
 - timeBeforeMs;%0A
+
     Assert.asser
@@ -2649,15 +2649,14 @@
 AL_MS);%0A
-%0A
   %7D%0A%7D%0A

update for code review