use of com.netflix.simianarmy.Monkey 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);
}
use of com.netflix.simianarmy.Monkey in project SimianArmy by Netflix.
the class TestBasicCalendar method testMonkeyTime.
@Test
void testMonkeyTime() {
Calendar test = Calendar.getInstance();
Monkey monkey = new TestMonkey();
// using leap day b/c it is not a holiday & not a weekend
test.set(Calendar.YEAR, 2012);
test.set(Calendar.MONTH, Calendar.FEBRUARY);
test.set(Calendar.DAY_OF_MONTH, 29);
// 8am leap day
test.set(Calendar.HOUR_OF_DAY, 8);
setNow(test);
Assert.assertFalse(isMonkeyTime(monkey));
// 10am leap day
test.set(Calendar.HOUR_OF_DAY, 10);
setNow(test);
Assert.assertTrue(isMonkeyTime(monkey));
// 5pm leap day
test.set(Calendar.HOUR_OF_DAY, 17);
setNow(test);
Assert.assertFalse(isMonkeyTime(monkey));
// set to the following Saturday so we can test we dont run on weekends
// even though within "business hours"
test.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
test.set(Calendar.HOUR_OF_DAY, 10);
setNow(test);
Assert.assertFalse(isMonkeyTime(monkey));
// test config overrides
PROPS.setProperty("simianarmy.calendar.isMonkeyTime", Boolean.toString(true));
Assert.assertTrue(isMonkeyTime(monkey));
PROPS.setProperty("simianarmy.calendar.isMonkeyTime", Boolean.toString(false));
Assert.assertFalse(isMonkeyTime(monkey));
}
use of com.netflix.simianarmy.Monkey 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);
}
use of com.netflix.simianarmy.Monkey in project SimianArmy by Netflix.
the class TestBasicChaosMonkey method testFullProbability.
@Test
public void testFullProbability() {
TestChaosMonkeyContext ctx = new TestChaosMonkeyContext("fullProbability.properties") {
@Override
public MonkeyScheduler scheduler() {
return new MonkeyScheduler() {
@Override
public int frequency() {
return 1;
}
@Override
public TimeUnit frequencyUnit() {
return TimeUnit.DAYS;
}
@Override
public void start(Monkey monkey, Runnable run) {
Assert.assertEquals(monkey.type().name(), monkey.type().name(), "starting monkey");
run.run();
}
@Override
public void stop(Monkey monkey) {
Assert.assertEquals(monkey.type().name(), monkey.type().name(), "stopping monkey");
}
};
}
};
ChaosMonkey chaos = new BasicChaosMonkey(ctx);
chaos.start();
chaos.stop();
List<InstanceGroup> selectedOn = ctx.selectedOn();
List<String> terminated = ctx.terminated();
Assert.assertEquals(selectedOn.size(), 4);
Assert.assertEquals(selectedOn.get(0).type(), TestChaosMonkeyContext.CrawlerTypes.TYPE_A);
Assert.assertEquals(selectedOn.get(0).name(), "name0");
Assert.assertEquals(selectedOn.get(1).type(), TestChaosMonkeyContext.CrawlerTypes.TYPE_A);
Assert.assertEquals(selectedOn.get(1).name(), "name1");
Assert.assertEquals(selectedOn.get(2).type(), TestChaosMonkeyContext.CrawlerTypes.TYPE_B);
Assert.assertEquals(selectedOn.get(2).name(), "name2");
Assert.assertEquals(selectedOn.get(3).type(), TestChaosMonkeyContext.CrawlerTypes.TYPE_B);
Assert.assertEquals(selectedOn.get(3).name(), "name3");
Assert.assertEquals(terminated.size(), 4);
}
Aggregations