-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
373 additions
and
321 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,48 +20,34 @@ | |
import static org.mockito.Mockito.verifyNoInteractions; | ||
import static org.mockito.Mockito.verifyNoMoreInteractions; | ||
import static org.mockito.Mockito.when; | ||
import static org.mockito.quality.Strictness.STRICT_STUBS; | ||
|
||
import java.util.Random; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.Executor; | ||
import java.util.concurrent.Future; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.mockito.Mock; | ||
import org.mockito.testng.MockitoSettings; | ||
import org.mockito.testng.MockitoTestNGListener; | ||
import org.testng.annotations.BeforeMethod; | ||
import org.testng.annotations.Listeners; | ||
import org.mockito.Mockito; | ||
import org.testng.annotations.Test; | ||
|
||
import com.google.common.primitives.Ints; | ||
|
||
/** | ||
* @author [email protected] (Ben Manes) | ||
*/ | ||
@Test(singleThreaded = true) | ||
@Listeners(MockitoTestNGListener.class) | ||
@MockitoSettings(strictness = STRICT_STUBS) | ||
public final class PacerTest { | ||
private static final long ONE_MINUTE_IN_NANOS = TimeUnit.MINUTES.toNanos(1); | ||
private static final Random random = new Random(); | ||
private static final long NOW = random.nextLong(); | ||
|
||
@Mock Scheduler scheduler; | ||
@Mock Executor executor; | ||
@Mock Runnable command; | ||
@Mock Future<?> future; | ||
|
||
Pacer pacer; | ||
|
||
@BeforeMethod | ||
public void beforeMethod() { | ||
pacer = new Pacer(scheduler); | ||
} | ||
|
||
@Test | ||
public void schedule_initialize() { | ||
Scheduler scheduler = Mockito.mock(); | ||
Executor executor = Mockito.mock(); | ||
Runnable command = Mockito.mock(); | ||
Future<?> future = Mockito.mock(); | ||
var pacer = new Pacer(scheduler); | ||
|
||
long delay = random.nextInt(Ints.saturatedCast(Pacer.TOLERANCE)); | ||
when(scheduler.schedule(executor, command, Pacer.TOLERANCE, TimeUnit.NANOSECONDS)) | ||
.then(invocation -> future); | ||
|
@@ -74,6 +60,12 @@ public void schedule_initialize() { | |
|
||
@Test | ||
public void schedule_initialize_recurse() { | ||
Scheduler scheduler = Mockito.mock(); | ||
Executor executor = Mockito.mock(); | ||
Runnable command = Mockito.mock(); | ||
Future<?> future = Mockito.mock(); | ||
var pacer = new Pacer(scheduler); | ||
|
||
long delay = random.nextInt(Ints.saturatedCast(Pacer.TOLERANCE)); | ||
when(scheduler.schedule(executor, command, Pacer.TOLERANCE, TimeUnit.NANOSECONDS)) | ||
.then(invocation -> { | ||
|
@@ -92,6 +84,12 @@ public void schedule_initialize_recurse() { | |
|
||
@Test | ||
public void schedule_cancel_schedule() { | ||
Scheduler scheduler = Mockito.mock(); | ||
Executor executor = Mockito.mock(); | ||
Runnable command = Mockito.mock(); | ||
Future<?> future = Mockito.mock(); | ||
var pacer = new Pacer(scheduler); | ||
|
||
long fireTime = NOW + Pacer.TOLERANCE; | ||
long delay = random.nextInt(Ints.saturatedCast(Pacer.TOLERANCE)); | ||
when(scheduler.schedule(executor, command, Pacer.TOLERANCE, TimeUnit.NANOSECONDS)) | ||
|
@@ -116,6 +114,12 @@ public void schedule_cancel_schedule() { | |
|
||
@Test | ||
public void scheduled_afterNextFireTime_skip() { | ||
Scheduler scheduler = Mockito.mock(); | ||
Executor executor = Mockito.mock(); | ||
Runnable command = Mockito.mock(); | ||
Future<?> future = Mockito.mock(); | ||
var pacer = new Pacer(scheduler); | ||
|
||
pacer.nextFireTime = NOW + ONE_MINUTE_IN_NANOS; | ||
pacer.future = future; | ||
|
||
|
@@ -130,6 +134,12 @@ public void scheduled_afterNextFireTime_skip() { | |
|
||
@Test | ||
public void schedule_beforeNextFireTime_skip() { | ||
Scheduler scheduler = Mockito.mock(); | ||
Executor executor = Mockito.mock(); | ||
Runnable command = Mockito.mock(); | ||
Future<?> future = Mockito.mock(); | ||
var pacer = new Pacer(scheduler); | ||
|
||
pacer.nextFireTime = NOW + ONE_MINUTE_IN_NANOS; | ||
pacer.future = future; | ||
|
||
|
@@ -146,6 +156,12 @@ public void schedule_beforeNextFireTime_skip() { | |
|
||
@Test | ||
public void schedule_beforeNextFireTime_minimumDelay() { | ||
Scheduler scheduler = Mockito.mock(); | ||
Executor executor = Mockito.mock(); | ||
Runnable command = Mockito.mock(); | ||
Future<?> future = Mockito.mock(); | ||
var pacer = new Pacer(scheduler); | ||
|
||
pacer.nextFireTime = NOW + ONE_MINUTE_IN_NANOS; | ||
pacer.future = future; | ||
|
||
|
@@ -167,6 +183,12 @@ public void schedule_beforeNextFireTime_minimumDelay() { | |
|
||
@Test | ||
public void schedule_beforeNextFireTime_customDelay() { | ||
Scheduler scheduler = Mockito.mock(); | ||
Executor executor = Mockito.mock(); | ||
Runnable command = Mockito.mock(); | ||
Future<?> future = Mockito.mock(); | ||
var pacer = new Pacer(scheduler); | ||
|
||
pacer.nextFireTime = NOW + ONE_MINUTE_IN_NANOS; | ||
pacer.future = future; | ||
|
||
|
@@ -188,6 +210,8 @@ public void schedule_beforeNextFireTime_customDelay() { | |
|
||
@Test | ||
public void cancel_initialize() { | ||
var pacer = new Pacer(Mockito.mock()); | ||
|
||
pacer.cancel(); | ||
assertThat(pacer.nextFireTime).isEqualTo(0); | ||
assertThat(pacer.isScheduled()).isFalse(); | ||
|
@@ -196,6 +220,9 @@ public void cancel_initialize() { | |
|
||
@Test | ||
public void cancel_scheduled() { | ||
Future<?> future = Mockito.mock(); | ||
var pacer = new Pacer(Mockito.mock()); | ||
|
||
pacer.nextFireTime = NOW + ONE_MINUTE_IN_NANOS; | ||
pacer.future = future; | ||
|
||
|
@@ -208,18 +235,24 @@ public void cancel_scheduled() { | |
|
||
@Test | ||
public void isScheduled_nullFuture() { | ||
var pacer = new Pacer(Mockito.mock()); | ||
|
||
pacer.future = null; | ||
assertThat(pacer.isScheduled()).isFalse(); | ||
} | ||
|
||
@Test | ||
public void isScheduled_doneFuture() { | ||
var pacer = new Pacer(Mockito.mock()); | ||
|
||
pacer.future = DisabledFuture.instance(); | ||
assertThat(pacer.isScheduled()).isFalse(); | ||
} | ||
|
||
@Test | ||
public void isScheduled_inFlight() { | ||
var pacer = new Pacer(Mockito.mock()); | ||
|
||
pacer.future = new CompletableFuture<>(); | ||
assertThat(pacer.isScheduled()).isTrue(); | ||
} | ||
|
Oops, something went wrong.