BwProvisionerSimple extends BwProvisioner { @Override public boolean allocateBwForVm(Vm vm, long bw) { deallocateBwForVm(vm); if (getAvailableBw() >= bw) { setAvailableBw(getAvailableBw() - bw); getBwTable().put(vm.getUid(), bw); vm.setCurrentAllocatedBw(getAllocatedBwForVm(vm)); return true; } vm.setCurrentAllocatedBw(getAllocatedBwForVm(vm)); return false; } BwProvisionerSimple(long bw); @Override boolean allocateBwForVm(Vm vm, long bw); @Override long getAllocatedBwForVm(Vm vm); @Override void deallocateBwForVm(Vm vm); @Override void deallocateBwForAllVms(); @Override boolean isSuitableForVm(Vm vm, long bw);  }
@Test public void testAllocateBwforVm() { Vm vm1 = new Vm(0, 0, 0, 0, 0, BW / 2, 0, "", null); Vm vm2 = new Vm(1, 0, 0, 0, 0, BW, 0, "", null); assertTrue(bwProvisioner.isSuitableForVm(vm1, BW / 2)); assertTrue(bwProvisioner.allocateBwForVm(vm1, BW / 2)); assertEquals(BW / 2, bwProvisioner.getAvailableBw()); assertFalse(bwProvisioner.isSuitableForVm(vm2, BW)); assertFalse(bwProvisioner.allocateBwForVm(vm2, BW)); assertEquals(BW / 2, bwProvisioner.getAvailableBw()); assertTrue(bwProvisioner.isSuitableForVm(vm2, BW / 4)); assertTrue(bwProvisioner.allocateBwForVm(vm2, BW / 4)); assertEquals(BW * 1 / 4, bwProvisioner.getAvailableBw()); assertTrue(bwProvisioner.isSuitableForVm(vm2, BW / 2)); assertTrue(bwProvisioner.allocateBwForVm(vm2, BW / 2)); assertEquals(0, bwProvisioner.getAvailableBw()); }