### Question:
RoundRobinVolumesPolicy implements BlockVolumeChoosingPolicy { @Override public synchronized FSVolume chooseVolume(List<FSVolume> volumes, long blockSize) throws IOException { if(volumes.size() < 1) { throw new DiskOutOfSpaceException("No more available volumes"); } if(curVolume >= volumes.size()) { curVolume = 0; } int startVolume = curVolume; long maxAvailable = 0; while (true) { FSVolume volume = volumes.get(curVolume); curVolume = (curVolume + 1) % volumes.size(); long availableVolumeSize = volume.getAvailable(); if (availableVolumeSize > blockSize) { return volume; } if (availableVolumeSize > maxAvailable) { maxAvailable = availableVolumeSize; } if (curVolume == startVolume) { throw new DiskOutOfSpaceException( "Insufficient space for an additional block. Volume with the most available space has " + maxAvailable + " bytes free, configured block size is " + blockSize); } } }  @Override synchronized FSVolume chooseVolume(List<FSVolume> volumes, long blockSize);  }### Answer:
@Test public void testRR() throws Exception { final List<FSVolume> volumes = new ArrayList<FSVolume>(); volumes.add(Mockito.mock(FSVolume.class)); Mockito.when(volumes.get(0).getAvailable()).thenReturn(100L); volumes.add(Mockito.mock(FSVolume.class)); Mockito.when(volumes.get(1).getAvailable()).thenReturn(200L); RoundRobinVolumesPolicy policy = ReflectionUtils.newInstance( RoundRobinVolumesPolicy.class, null); Assert.assertEquals(volumes.get(0), policy.chooseVolume(volumes, 0)); Assert.assertEquals(volumes.get(1), policy.chooseVolume(volumes, 0)); Assert.assertEquals(volumes.get(0), policy.chooseVolume(volumes, 0)); Assert.assertEquals(volumes.get(1), policy.chooseVolume(volumes, 0)); Assert.assertEquals(volumes.get(1), policy.chooseVolume(volumes, 150)); try { policy.chooseVolume(volumes, Long.MAX_VALUE); Assert.fail(); } catch (IOException e) { } }
@Test public void testRRPolicyExceptionMessage() throws Exception { final List<FSVolume> volumes = new ArrayList<FSVolume>(); volumes.add(Mockito.mock(FSVolume.class)); Mockito.when(volumes.get(0).getAvailable()).thenReturn(500L); volumes.add(Mockito.mock(FSVolume.class)); Mockito.when(volumes.get(1).getAvailable()).thenReturn(600L); RoundRobinVolumesPolicy policy = new RoundRobinVolumesPolicy(); int blockSize = 700; try { policy.chooseVolume(volumes, blockSize); Assert.fail("expected to throw DiskOutOfSpaceException"); } catch (DiskOutOfSpaceException e) { Assert .assertEquals( "Not returnig the expected message", "Insufficient space for an additional block. Volume with the most available space has 600 bytes free, configured block size is " + blockSize, e .getMessage()); } }