public async Task TestCalibration( long initialSize, long initialHardLimit, long?intermediateNewSize, long[] intermediateNewHistory, long finalNewSize, long[] finalNewHistory, int windowSize, double calibrateCoefficient, bool shouldBeCalibrated, long?newHardLimit) { using (var root = new DisposableDirectory(FileSystem)) { long size = initialSize; PinSizeHistory pinSizeHistory = await PinSizeHistory.LoadOrCreateNewAsync(FileSystem, _clock, root.Path); Func <long> currentSizeFunc = () => Volatile.Read(ref size); Func <int, PinSizeHistory.ReadHistoryResult> readHistoryFunc = ws => pinSizeHistory.ReadHistory(ws); var elasticRule = CreateElasticRule(root.Path, initialHardLimit, currentSizeFunc, readHistoryFunc, windowSize, calibrateCoefficient); elasticRule.CanBeCalibrated.Should().BeTrue("Quota should be able to be calibrated"); if (initialHardLimit >= initialSize) { elasticRule.CurrentQuota.Hard.Should().Be(initialHardLimit, "Hard quota should be equal to as initial"); } else { elasticRule.CurrentQuota.Hard.Should().Be((long)(initialSize / ElasticSizeRule.CurrentSizeHardLimitRatio), "Hard quota should be around the initial"); } if (intermediateNewSize.HasValue && intermediateNewHistory != null) { Volatile.Write(ref size, intermediateNewSize.Value); await elasticRule.CalibrateAsync().ShouldBeSuccess(); _clock.Increment(); } Volatile.Write(ref size, finalNewSize); foreach (var h in finalNewHistory) { pinSizeHistory.Add(h); _clock.Increment(); } var result = await elasticRule.CalibrateAsync(); result.Succeeded.Should().Be(shouldBeCalibrated, $"this error [{result.ErrorMessage}] should not happen"); if (newHardLimit.HasValue) { elasticRule.CurrentQuota.Hard.Should().Be(newHardLimit.Value); } } }
private void AddIntoPinSizeHistory(PinSizeHistory pinSizeHistory, params long[] values) { foreach (var value in values) { _clock.Increment(); pinSizeHistory.Add(value); } }