Search in sources :

Example 1 with TestMonkeyContext

use of com.netflix.simianarmy.TestMonkeyContext in project SimianArmy by Netflix.

the class TestBasicScheduler method testRunner.

@Test
public void testRunner() throws InterruptedException {
    BasicScheduler sched = new BasicScheduler(200, TimeUnit.MILLISECONDS, 1);
    Monkey mockMonkey = mock(Monkey.class);
    when(mockMonkey.context()).thenReturn(new TestMonkeyContext(Enums.MONKEY));
    when(mockMonkey.type()).thenReturn(Enums.MONKEY).thenReturn(Enums.MONKEY);
    final AtomicLong counter = new AtomicLong(0L);
    sched.start(mockMonkey, new Runnable() {

        @Override
        public void run() {
            counter.incrementAndGet();
        }
    });
    Thread.sleep(100);
    Assert.assertEquals(counter.get(), 1);
    Thread.sleep(200);
    Assert.assertEquals(counter.get(), 2);
    sched.stop(mockMonkey);
    Thread.sleep(200);
    Assert.assertEquals(counter.get(), 2);
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) TestMonkeyContext(com.netflix.simianarmy.TestMonkeyContext) Monkey(com.netflix.simianarmy.Monkey) Test(org.testng.annotations.Test)

Example 2 with TestMonkeyContext

use of com.netflix.simianarmy.TestMonkeyContext in project SimianArmy by Netflix.

the class TestBasicScheduler method testDelayedStart.

@Test
public void testDelayedStart() throws Exception {
    BasicScheduler sched = new BasicScheduler(1, TimeUnit.HOURS, 1);
    TestMonkeyContext context = new TestMonkeyContext(Enums.MONKEY);
    Monkey mockMonkey = mock(Monkey.class);
    when(mockMonkey.context()).thenReturn(context).thenReturn(context);
    when(mockMonkey.type()).thenReturn(Enums.MONKEY).thenReturn(Enums.MONKEY);
    // first monkey has no previous events, so it runs practically immediately
    FutureTask<Void> task = new FutureTask<Void>(Callables.<Void>returning(null));
    sched.start(mockMonkey, task);
    // make sure that the task gets completed within 100ms
    task.get(100L, TimeUnit.MILLISECONDS);
    sched.stop(mockMonkey);
    // create an event 5 min ago
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.MINUTE, -5);
    BasicRecorderEvent evt = new BasicRecorderEvent(Enums.MONKEY, EventEnums.EVENT, "region", "test-id", cal.getTime().getTime());
    context.recorder().recordEvent(evt);
    // this time when it runs it will not run immediately since it should be scheduled for 55m from now.
    task = new FutureTask<Void>(Callables.<Void>returning(null));
    sched.start(mockMonkey, task);
    try {
        task.get(100, TimeUnit.MILLISECONDS);
        Assert.fail("The task shouldn't have been completed in 100ms");
    } catch (TimeoutException e) {
    // NOPMD - This is an expected exception
    }
    sched.stop(mockMonkey);
}
Also used : TestMonkeyContext(com.netflix.simianarmy.TestMonkeyContext) FutureTask(java.util.concurrent.FutureTask) Monkey(com.netflix.simianarmy.Monkey) Calendar(java.util.Calendar) TimeoutException(java.util.concurrent.TimeoutException) Test(org.testng.annotations.Test)

Aggregations

Monkey (com.netflix.simianarmy.Monkey)2 TestMonkeyContext (com.netflix.simianarmy.TestMonkeyContext)2 Test (org.testng.annotations.Test)2 Calendar (java.util.Calendar)1 FutureTask (java.util.concurrent.FutureTask)1 TimeoutException (java.util.concurrent.TimeoutException)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1