// allocate something
JobID jobID = new JobID();
Configuration conf = new Configuration();
InstanceRequestMap instanceRequestMap = new InstanceRequestMap();
instanceRequestMap.setNumberOfInstances(cm.getDefaultInstanceType(), 2);
cm.requestInstance(jobID, conf, instanceRequestMap, null);
ClusterManagerTestUtils.waitForInstances(jobID, testInstanceListener, 3, 1000);
List<AllocatedResource> allocatedResources = testInstanceListener.getAllocatedResourcesForJob(jobID);
assertEquals(2, allocatedResources.size());
Iterator<AllocatedResource> it = allocatedResources.iterator();
Set<AllocationID> allocationIDs = new HashSet<AllocationID>();
while (it.hasNext()) {
AllocatedResource allocatedResource = it.next();
if (ConfigConstants.DEFAULT_INSTANCE_TYPE.equals(allocatedResource.getInstance().getType().getIdentifier())) {
fail("Allocated unexpected instance of type "
+ allocatedResource.getInstance().getType().getIdentifier());
}
if (allocationIDs.contains(allocatedResource.getAllocationID())) {
fail("Discovered allocation ID " + allocatedResource.getAllocationID() + " at least twice");
} else {
allocationIDs.add(allocatedResource.getAllocationID());
}
}
// Try to allocate more resources which must result in an error
try {
InstanceRequestMap instancem = new InstanceRequestMap();
instancem.setNumberOfInstances(cm.getDefaultInstanceType(), 1);
cm.requestInstance(jobID, conf, instancem, null);
fail("ClusterManager allowed to request more instances than actually available");
} catch (InstanceException ie) {
// Exception is expected and correct behavior here
}
// Release all allocated resources
it = allocatedResources.iterator();
while (it.hasNext()) {
final AllocatedResource allocatedResource = it.next();
cm.releaseAllocatedResource(jobID, conf, allocatedResource);
}
// Now further allocations should be possible
InstanceRequestMap instancem = new InstanceRequestMap();
instancem.setNumberOfInstances(cm.getDefaultInstanceType(), 1);
cm.requestInstance(jobID, conf, instancem, null);
cm.shutdown();
}