Skip to content

Commit

Permalink
Complete test coverage of SegmentRunner
Browse files Browse the repository at this point in the history
  • Loading branch information
Bj0rnen committed Jan 19, 2015
1 parent 8720806 commit e0c2bc5
Showing 1 changed file with 75 additions and 3 deletions.
78 changes: 75 additions & 3 deletions src/test/java/com/spotify/reaper/service/SegmentRunnerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ public void timeoutTest() throws InterruptedException, ReaperException {
new RepairRun.Builder("reaper", cf.getId(), DateTime.now(),
0.5));
storage.addRepairSegments(Collections.singleton(
new RepairSegment.Builder(run.getId(), new RingRange(BigInteger.ONE, BigInteger.ZERO), cf.getId())), run.getId());
new RepairSegment.Builder(run.getId(), new RingRange(BigInteger.ONE, BigInteger.ZERO),
cf.getId())), run.getId());
final long segmentId = storage.getNextFreeSegment(run.getId()).getId();

SegmentRunner.triggerRepair(storage, segmentId,
Expand All @@ -78,7 +79,8 @@ public void run() {
handler.get().handle(1, ActiveRepairService.Status.STARTED,
"Repair command 1 has started");
sleep(100);
assertEquals(RepairSegment.State.RUNNING, storage.getRepairSegment(segmentId).getState());
assertEquals(RepairSegment.State.RUNNING,
storage.getRepairSegment(segmentId).getState());
} catch (InterruptedException e) {
e.printStackTrace();
}
Expand All @@ -103,7 +105,8 @@ public void successTest() throws InterruptedException, ReaperException {
RepairRun run = storage.addRepairRun(
new RepairRun.Builder("reaper", cf.getId(), DateTime.now(), 0.5));
storage.addRepairSegments(Collections.singleton(
new RepairSegment.Builder(run.getId(), new RingRange(BigInteger.ONE, BigInteger.ZERO), cf.getId())), run.getId());
new RepairSegment.Builder(run.getId(), new RingRange(BigInteger.ONE, BigInteger.ZERO),
cf.getId())), run.getId());
final long segmentId = storage.getNextFreeSegment(run.getId()).getId();

SegmentRunner.triggerRepair(storage, segmentId,
Expand All @@ -127,14 +130,22 @@ public void run() {
System.out.println("Repair has been triggered");
try {
sleep(10);

handler.get().handle(1, ActiveRepairService.Status.STARTED,
"Repair command 1 has started");
sleep(100);
assertEquals(RepairSegment.State.RUNNING,
storage.getRepairSegment(segmentId).getState());

// report about an unrelated repair. Shouldn't affect anything.
handler.get().handle(2, ActiveRepairService.Status.SESSION_FAILED,
"Repair command 2 has failed");
handler.get().handle(1, ActiveRepairService.Status.SESSION_SUCCESS,
"Repair session succeeded in command 1");
sleep(10);

assertEquals(RepairSegment.State.RUNNING,
storage.getRepairSegment(segmentId).getState());
handler.get().handle(1, ActiveRepairService.Status.FINISHED,
"Repair command 1 has finished");
} catch (InterruptedException e) {
Expand All @@ -152,4 +163,65 @@ public void run() {

assertEquals(RepairSegment.State.DONE, storage.getRepairSegment(segmentId).getState());
}

@Test
public void failureTest() throws InterruptedException, ReaperException {
final IStorage storage = new MemoryStorage();
ColumnFamily cf =
storage.addColumnFamily(new ColumnFamily.Builder("reaper", "reaper", "reaper", 1, false));
RepairRun run = storage.addRepairRun(
new RepairRun.Builder("reaper", cf.getId(), DateTime.now(), 0.5));
storage.addRepairSegments(Collections.singleton(
new RepairSegment.Builder(run.getId(), new RingRange(BigInteger.ONE, BigInteger.ZERO),
cf.getId())), run.getId());
final long segmentId = storage.getNextFreeSegment(run.getId()).getId();

SegmentRunner.triggerRepair(storage, segmentId,
Collections.singleton(""), 500, new JmxConnectionFactory() {
@Override
public JmxProxy create(final Optional<RepairStatusHandler> handler, String host)
throws ReaperException {
JmxProxy jmx = mock(JmxProxy.class);
when(jmx.getClusterName()).thenReturn("reaper");
when(jmx.isConnectionAlive()).thenReturn(true);
when(jmx.tokenRangeToEndpoint(anyString(), any(RingRange.class)))
.thenReturn(Lists.newArrayList(""));
when(jmx.triggerRepair(any(BigInteger.class), any(BigInteger.class), anyString(),
anyString()))
.then(new Answer<Integer>() {
@Override
public Integer answer(InvocationOnMock invocation) throws Throwable {
new Thread() {
@Override
public void run() {
System.out.println("Repair has been triggered");
try {
sleep(10);
handler.get().handle(1, ActiveRepairService.Status.STARTED,
"Repair command 1 has started");
sleep(100);
assertEquals(RepairSegment.State.RUNNING,
storage.getRepairSegment(segmentId).getState());
handler.get().handle(1, ActiveRepairService.Status.SESSION_SUCCESS,
"Repair session succeeded in command 1");
sleep(10);
assertEquals(RepairSegment.State.RUNNING,
storage.getRepairSegment(segmentId).getState());
handler.get().handle(1, ActiveRepairService.Status.SESSION_FAILED,
"Repair command 1 has failed");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
return 1;
}
});

return jmx;
}
});

assertEquals(RepairSegment.State.ERROR, storage.getRepairSegment(segmentId).getState());
}
}

0 comments on commit e0c2bc5

Please sign in to comment.