Scheduler scheduler = new Scheduler();
scheduler.newInstanceAvailable(getRandomInstance(3));
scheduler.newInstanceAvailable(getRandomInstance(2));
// schedule some individual vertices
AllocatedSlot sA1 = scheduler.scheduleImmediately(new ScheduledUnit(getTestVertex(jidA, 0, 2)));
AllocatedSlot sA2 = scheduler.scheduleImmediately(new ScheduledUnit(getTestVertex(jidA, 1, 2)));
assertNotNull(sA1);
assertNotNull(sA2);
// schedule some vertices in the sharing group
AllocatedSlot s1_0 = scheduler.scheduleImmediately(new ScheduledUnit(getTestVertex(jid1, 0, 4), sharingGroup));
AllocatedSlot s1_1 = scheduler.scheduleImmediately(new ScheduledUnit(getTestVertex(jid1, 1, 4), sharingGroup));
AllocatedSlot s2_0 = scheduler.scheduleImmediately(new ScheduledUnit(getTestVertex(jid2, 0, 4), sharingGroup));
AllocatedSlot s2_1 = scheduler.scheduleImmediately(new ScheduledUnit(getTestVertex(jid2, 1, 4), sharingGroup));
assertNotNull(s1_0);
assertNotNull(s1_1);
assertNotNull(s2_0);
assertNotNull(s2_1);
// schedule another isolated vertex
AllocatedSlot sB1 = scheduler.scheduleImmediately(new ScheduledUnit(getTestVertex(jidB, 1, 3)));
assertNotNull(sB1);
// should not be able to schedule more vertices
try {
scheduler.scheduleImmediately(new ScheduledUnit(getTestVertex(jid1, 2, 4), sharingGroup));
fail("Scheduler accepted too many tasks at the same time");
}
catch (NoResourceAvailableException e) {
// good!
}
catch (Exception e) {
fail("Wrong exception.");
}
try {
scheduler.scheduleImmediately(new ScheduledUnit(getTestVertex(jid2, 2, 4), sharingGroup));
fail("Scheduler accepted too many tasks at the same time");
}
catch (NoResourceAvailableException e) {
// good!
}
catch (Exception e) {
fail("Wrong exception.");
}
try {
scheduler.scheduleImmediately(new ScheduledUnit(getTestVertex(jidB, 0, 3)));
fail("Scheduler accepted too many tasks at the same time");
}
catch (NoResourceAvailableException e) {
// good!
}
catch (Exception e) {
fail("Wrong exception.");
}
try {
scheduler.scheduleImmediately(new ScheduledUnit(getTestVertex(jidC, 0, 1)));
fail("Scheduler accepted too many tasks at the same time");
}
catch (NoResourceAvailableException e) {
// good!
}
catch (Exception e) {
fail("Wrong exception.");
}
// release some isolated task and check that the sharing group may grow
sA1.releaseSlot();
AllocatedSlot s1_2 = scheduler.scheduleImmediately(new ScheduledUnit(getTestVertex(jid1, 2, 4), sharingGroup));
AllocatedSlot s2_2 = scheduler.scheduleImmediately(new ScheduledUnit(getTestVertex(jid2, 3, 4), sharingGroup));
assertNotNull(s1_2);
assertNotNull(s2_2);
// release three of the previously allocated sub slots, which guarantees to return one shared slot
s1_0.releaseSlot();
s1_1.releaseSlot();
s2_0.releaseSlot();
assertEquals(1, scheduler.getNumberOfAvailableSlots());
// schedule one more no-shared task
AllocatedSlot sB0 = scheduler.scheduleImmediately(new ScheduledUnit(getTestVertex(jidB, 0, 3)));
assertNotNull(sB0);
// release the last of the original shared slots and allocate one more non-shared slot
s2_1.releaseSlot();
AllocatedSlot sB2 = scheduler.scheduleImmediately(new ScheduledUnit(getTestVertex(jidB, 2, 3)));
assertNotNull(sB2);
// release on non-shared and add some shared slots
sA2.releaseSlot();
AllocatedSlot s1_3 = scheduler.scheduleImmediately(new ScheduledUnit(getTestVertex(jid1, 3, 4), sharingGroup));
AllocatedSlot s2_3 = scheduler.scheduleImmediately(new ScheduledUnit(getTestVertex(jid2, 2, 4), sharingGroup));
assertNotNull(s1_3);
assertNotNull(s2_3);
// release all shared and allocate all in non-shared
s1_2.releaseSlot();
s2_2.releaseSlot();
s1_3.releaseSlot();
s2_3.releaseSlot();
AllocatedSlot sC0 = scheduler.scheduleImmediately(new ScheduledUnit(getTestVertex(jidC, 1, 2)));
AllocatedSlot sC1 = scheduler.scheduleImmediately(new ScheduledUnit(getTestVertex(jidC, 0, 2)));
assertNotNull(sC0);
assertNotNull(sC1);
sB0.releaseSlot();
sB1.releaseSlot();
sB2.releaseSlot();
sC0.releaseSlot();
sC1.releaseSlot();
// test that everything is released
assertEquals(5, scheduler.getNumberOfAvailableSlots());
// check the scheduler's bookkeeping