/*
 * The Alluxio Open Foundation licenses this work under the Apache License, version 2.0
 * (the "License"). You may not use this work except in compliance with the License, which is
 * available at www.apache.org/licenses/LICENSE-2.0
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied, as more fully set forth in the License.
 *
 * See the NOTICE file distributed with this work for information regarding copyright ownership.
 */

package alluxio.heartbeat;

import alluxio.clock.ManualClock;
import alluxio.time.Sleeper;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.slf4j.Logger;

import java.time.Duration;

/**
 * Unit tests for {@link SleepingTimer}.
 */
public final class SleepingTimerTest {
  private static final String THREAD_NAME = "sleepingtimer-test-thread-name";
  private static final long INTERVAL_MS = 500;
  private Logger mMockLogger;
  private ManualClock mFakeClock;
  private Sleeper mMockSleeper;

  @Before
  public void before() {
    mMockLogger = Mockito.mock(Logger.class);
    mFakeClock = new ManualClock();
    mMockSleeper = Mockito.mock(Sleeper.class);
  }

  @Test
  public void warnWhenExecutionTakesLongerThanInterval() throws Exception {
    SleepingTimer timer =
        new SleepingTimer(THREAD_NAME, INTERVAL_MS, mMockLogger, mFakeClock, mMockSleeper);

    timer.tick();
    mFakeClock.addTimeMs(5 * INTERVAL_MS);
    timer.tick();

    Mockito.verify(mMockLogger).warn(Mockito.anyString(), Mockito.anyString(), Mockito.anyLong(),
        Mockito.anyLong());
  }

  @Test
  public void sleepForSpecifiedInterval() throws Exception {
    final SleepingTimer timer =
        new SleepingTimer(THREAD_NAME, INTERVAL_MS, mMockLogger, mFakeClock, mMockSleeper);
    timer.tick(); // first tick won't sleep
    Mockito.verify(mMockSleeper, Mockito.times(0)).sleep(Mockito.any(Duration.class));
    timer.tick();
    Mockito.verify(mMockSleeper).sleep(Duration.ofMillis(INTERVAL_MS));
  }

  /**
   * Tests that the sleeping timer will attempt to run at the same interval, independently of how
   * long the execution between ticks takes. For example, if the interval is 100ms and execution
   * takes 80ms, the timer should sleep for only 20ms to maintain the regular interval of 100ms.
   */
  @Test
  public void maintainInterval() throws Exception {
    SleepingTimer stimer =
        new SleepingTimer(THREAD_NAME, INTERVAL_MS, mMockLogger, mFakeClock, mMockSleeper);

    stimer.tick();
    mFakeClock.addTimeMs(INTERVAL_MS / 3);
    stimer.tick();
    Mockito.verify(mMockSleeper).sleep(Duration.ofMillis(INTERVAL_MS - (INTERVAL_MS / 3)));
  }
}

@@ -595,24 +595,234 @@
 e.Sleeper;%0A%0A
+import static org.mockito.Matchers.any;%0Aimport static org.mockito.Matchers.anyString;%0Aimport static org.mockito.Mockito.mock;%0Aimport static org.mockito.Mockito.times;%0Aimport static org.mockito.Mockito.verify;%0A%0A
 import org.j
@@ -1298,32 +1298,24 @@
 ockLogger = 
-Mockito.
 mock(Logger.
@@ -1377,24 +1377,16 @@
 eeper = 
-Mockito.
 mock(Sle
@@ -1686,32 +1686,24 @@
 ick();%0A%0A    
-Mockito.
 verify(mMock
@@ -1715,24 +1715,16 @@
 r).warn(
-Mockito.
 anyStrin
@@ -1728,24 +1728,16 @@
 ring(), 
-Mockito.
 anyStrin
@@ -2030,32 +2030,24 @@
 t sleep%0A    
-Mockito.
 verify(mMock
@@ -2055,24 +2055,16 @@
 leeper, 
-Mockito.
 times(0)
@@ -2071,24 +2071,16 @@
 ).sleep(
-Mockito.
 any(Dura
@@ -2111,32 +2111,24 @@
 tick();%0A    
-Mockito.
 verify(mMock
@@ -2749,24 +2749,16 @@
 ();%0A    
-Mockito.
 verify(m

Use static imports for standard test utilities for class SleepingTimerTest (#7684)