/*
 * Copyright Terracotta, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.terracotta.lease.service.monitor;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Spy;
import org.mockito.runners.MockitoJUnitRunner;
import org.terracotta.lease.TestTimeSource;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

@RunWith(MockitoJUnitRunner.class)
public class LeaseMonitorThreadTest {
  @Spy
  private TestTimeSource timeSource = new TestTimeSource();

  @Test
  public void callsCheckLeasesPeriodically() throws Exception {
    LeaseState leaseState = mock(LeaseState.class);
    LeaseMonitorThread leaseMonitorThread = new LeaseMonitorThread(timeSource, leaseState);
    assertEquals("LeaseMonitorThread", leaseMonitorThread.getName());
    assertTrue(leaseMonitorThread.isDaemon());
    leaseMonitorThread.start();

    verify(timeSource, timeout(1000L).times(1)).sleep(200L);
    verify(leaseState, times(1)).checkLeases();

    timeSource.tickMillis(200L);

    verify(timeSource, timeout(1000L).times(2)).sleep(200L);
    verify(leaseState, times(2)).checkLeases();

    leaseMonitorThread.interrupt();
    leaseMonitorThread.join(1000L);
    assertFalse(leaseMonitorThread.isAlive());
  }
}

@@ -1638,32 +1638,34 @@
 urce, timeout(10
+_0
 00L).times(1)).s
@@ -1668,32 +1668,32 @@
 )).sleep(200L);%0A
-
     verify(lease
@@ -1792,16 +1792,18 @@
 meout(10
+_0
 00L).tim
@@ -1907,16 +1907,16 @@
 rupt();%0A
-
     leas
@@ -1937,16 +1937,18 @@
 .join(10
+_0
 00L);%0A  

Increase timeouts on test because it is occasionally failing