public class ScheduleWithCoLocationHintTest {
@Test
public void scheduleAllSharedAndCoLocated() {
try {
JobVertexID jid1 = new JobVertexID();
JobVertexID jid2 = new JobVertexID();
Scheduler scheduler = new Scheduler();
scheduler.newInstanceAvailable(getRandomInstance(2));
scheduler.newInstanceAvailable(getRandomInstance(2));
scheduler.newInstanceAvailable(getRandomInstance(2));
assertEquals(6, scheduler.getNumberOfAvailableSlots());
SlotSharingGroup sharingGroup = new SlotSharingGroup();
CoLocationGroup ccg = new CoLocationGroup();
CoLocationConstraint c1 = new CoLocationConstraint(ccg);
CoLocationConstraint c2 = new CoLocationConstraint(ccg);
CoLocationConstraint c3 = new CoLocationConstraint(ccg);
CoLocationConstraint c4 = new CoLocationConstraint(ccg);
CoLocationConstraint c5 = new CoLocationConstraint(ccg);
CoLocationConstraint c6 = new CoLocationConstraint(ccg);
// schedule 4 tasks from the first vertex group
AllocatedSlot s1 = scheduler.scheduleImmediately(new ScheduledUnit(getTestVertex(jid1, 0, 6), sharingGroup, c1));
AllocatedSlot s2 = scheduler.scheduleImmediately(new ScheduledUnit(getTestVertex(jid1, 1, 6), sharingGroup, c2));
AllocatedSlot s3 = scheduler.scheduleImmediately(new ScheduledUnit(getTestVertex(jid1, 2, 6), sharingGroup, c3));
AllocatedSlot s4 = scheduler.scheduleImmediately(new ScheduledUnit(getTestVertex(jid1, 3, 6), sharingGroup, c4));
AllocatedSlot s5 = scheduler.scheduleImmediately(new ScheduledUnit(getTestVertex(jid2, 0, 6), sharingGroup, c1));
AllocatedSlot s6 = scheduler.scheduleImmediately(new ScheduledUnit(getTestVertex(jid2, 1, 6), sharingGroup, c2));
AllocatedSlot s7 = scheduler.scheduleImmediately(new ScheduledUnit(getTestVertex(jid2, 2, 6), sharingGroup, c3));
AllocatedSlot s8 = scheduler.scheduleImmediately(new ScheduledUnit(getTestVertex(jid1, 4, 6), sharingGroup, c5));
AllocatedSlot s9 = scheduler.scheduleImmediately(new ScheduledUnit(getTestVertex(jid1, 5, 6), sharingGroup, c6));
AllocatedSlot s10 = scheduler.scheduleImmediately(new ScheduledUnit(getTestVertex(jid2, 3, 6), sharingGroup, c4));
AllocatedSlot s11 = scheduler.scheduleImmediately(new ScheduledUnit(getTestVertex(jid2, 4, 6), sharingGroup, c5));
AllocatedSlot s12 = scheduler.scheduleImmediately(new ScheduledUnit(getTestVertex(jid2, 5, 6), sharingGroup, c6));
assertNotNull(s1);
assertNotNull(s2);
assertNotNull(s3);
assertNotNull(s4);
assertNotNull(s5);
assertNotNull(s6);
assertNotNull(s7);
assertNotNull(s8);
assertNotNull(s9);
assertNotNull(s10);
assertNotNull(s11);
assertNotNull(s12);
// check that each slot got exactly two tasks
assertEquals(2, ((SubSlot) s1).getSharedSlot().getNumberOfAllocatedSubSlots());
assertEquals(2, ((SubSlot) s2).getSharedSlot().getNumberOfAllocatedSubSlots());
assertEquals(2, ((SubSlot) s3).getSharedSlot().getNumberOfAllocatedSubSlots());
assertEquals(2, ((SubSlot) s4).getSharedSlot().getNumberOfAllocatedSubSlots());
assertEquals(2, ((SubSlot) s5).getSharedSlot().getNumberOfAllocatedSubSlots());
assertEquals(2, ((SubSlot) s6).getSharedSlot().getNumberOfAllocatedSubSlots());
assertEquals(2, ((SubSlot) s7).getSharedSlot().getNumberOfAllocatedSubSlots());
assertEquals(2, ((SubSlot) s8).getSharedSlot().getNumberOfAllocatedSubSlots());
assertEquals(2, ((SubSlot) s9).getSharedSlot().getNumberOfAllocatedSubSlots());
assertEquals(2, ((SubSlot) s10).getSharedSlot().getNumberOfAllocatedSubSlots());
assertEquals(2, ((SubSlot) s11).getSharedSlot().getNumberOfAllocatedSubSlots());
assertEquals(2, ((SubSlot) s12).getSharedSlot().getNumberOfAllocatedSubSlots());
assertEquals(s1.getInstance(), s5.getInstance());
assertEquals(s2.getInstance(), s6.getInstance());
assertEquals(s3.getInstance(), s7.getInstance());
assertEquals(s4.getInstance(), s10.getInstance());
assertEquals(s8.getInstance(), s11.getInstance());
assertEquals(s9.getInstance(), s12.getInstance());
assertEquals(c1.getLocation(), s1.getInstance());
assertEquals(c2.getLocation(), s2.getInstance());
assertEquals(c3.getLocation(), s3.getInstance());
assertEquals(c4.getLocation(), s4.getInstance());
assertEquals(c5.getLocation(), s8.getInstance());
assertEquals(c6.getLocation(), s9.getInstance());
// check the scheduler's bookkeeping
assertEquals(0, scheduler.getNumberOfAvailableSlots());
// the first assignments are unconstrained, co.-scheduling is constrained
assertEquals(6, scheduler.getNumberOfLocalizedAssignments());
assertEquals(0, scheduler.getNumberOfNonLocalizedAssignments());
assertEquals(6, scheduler.getNumberOfUnconstrainedAssignments());
// release some slots, be sure that new available ones come up
s1.releaseSlot();
s2.releaseSlot();
s3.releaseSlot();
s4.releaseSlot();
s7.releaseSlot();
s10.releaseSlot();
s11.releaseSlot();
s12.releaseSlot();
assertTrue(scheduler.getNumberOfAvailableSlots() >= 1);
AllocatedSlot single = scheduler.scheduleImmediately(new ScheduledUnit(getTestVertex(new JobVertexID(), 0, 1)));
assertNotNull(single);
s1.releaseSlot();
s2.releaseSlot();
s3.releaseSlot();