/* * Ensure that we randomly select the lesser-used volumes with appropriate * frequency. */ /// <exception cref="System.Exception"/> public virtual void DoRandomizedTest(float preferencePercent, int lowSpaceVolumes , int highSpaceVolumes) { Random random = new Random(123L); AvailableSpaceVolumeChoosingPolicy <FsVolumeSpi> policy = new AvailableSpaceVolumeChoosingPolicy <FsVolumeSpi>(random); IList <FsVolumeSpi> volumes = new AList <FsVolumeSpi>(); // Volumes with 1MB free space for (int i = 0; i < lowSpaceVolumes; i++) { FsVolumeSpi volume = Org.Mockito.Mockito.Mock <FsVolumeSpi>(); Org.Mockito.Mockito.When(volume.GetAvailable()).ThenReturn(1024L * 1024L); volumes.AddItem(volume); } // Volumes with 3MB free space for (int i_1 = 0; i_1 < highSpaceVolumes; i_1++) { FsVolumeSpi volume = Org.Mockito.Mockito.Mock <FsVolumeSpi>(); Org.Mockito.Mockito.When(volume.GetAvailable()).ThenReturn(1024L * 1024L * 3); volumes.AddItem(volume); } InitPolicy(policy, preferencePercent); long lowAvailableSpaceVolumeSelected = 0; long highAvailableSpaceVolumeSelected = 0; for (int i_2 = 0; i_2 < RandomizedIterations; i_2++) { FsVolumeSpi volume = policy.ChooseVolume(volumes, 100); for (int j = 0; j < volumes.Count; j++) { // Note how many times the first low available volume was selected if (volume == volumes[j] && j == 0) { lowAvailableSpaceVolumeSelected++; } // Note how many times the first high available volume was selected if (volume == volumes[j] && j == lowSpaceVolumes) { highAvailableSpaceVolumeSelected++; break; } } } // Calculate the expected ratio of how often low available space volumes // were selected vs. high available space volumes. float expectedSelectionRatio = preferencePercent / (1 - preferencePercent); GenericTestUtils.AssertValueNear((long)(lowAvailableSpaceVolumeSelected * expectedSelectionRatio ), highAvailableSpaceVolumeSelected, RandomizedAllowedError); }