Exemplo n.º 1
0
        public async Task SaveReadSmallerHistoryWithDifferentSizesOfWindow()
        {
            using (var testDirectory = new DisposableDirectory(FileSystem))
            {
                var pinSizeHistory = await PinSizeHistory.LoadOrCreateNewAsync(FileSystem, _clock, testDirectory.Path);

                AddIntoPinSizeHistory(pinSizeHistory, 1, 1, 1, 7, 2, 7, 13, 11);

                await pinSizeHistory.SaveAsync(FileSystem);

                pinSizeHistory = await PinSizeHistory.LoadOrCreateNewAsync(FileSystem, _clock, testDirectory.Path, 5);

                var history3 = pinSizeHistory.ReadHistory(3);
                AssertEqualHistory(new long[] { 11, 13, 7 }, history3.Window);

                var history5 = pinSizeHistory.ReadHistory(5);
                AssertEqualHistory(new long[] { 11, 13, 7, 2, 7 }, history5.Window);

                var history7 = pinSizeHistory.ReadHistory(7);
                AssertEqualHistory(new long[] { 11, 13, 7, 2, 7 }, history7.Window);

                await pinSizeHistory.SaveAsync(FileSystem);

                pinSizeHistory = await PinSizeHistory.LoadOrCreateNewAsync(FileSystem, _clock, testDirectory.Path, 8);

                history3 = pinSizeHistory.ReadHistory(3);
                AssertEqualHistory(new long[] { 11, 13, 7 }, history3.Window);

                history5 = pinSizeHistory.ReadHistory(5);
                AssertEqualHistory(new long[] { 11, 13, 7, 2, 7 }, history5.Window);

                history7 = pinSizeHistory.ReadHistory(7);
                AssertEqualHistory(new long[] { 11, 13, 7, 2, 7 }, history7.Window);
            }
        }
Exemplo n.º 2
0
        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);
                }
            }
        }
Exemplo n.º 3
0
 private void AddIntoPinSizeHistory(PinSizeHistory pinSizeHistory, params long[] values)
 {
     foreach (var value in values)
     {
         _clock.Increment();
         pinSizeHistory.Add(value);
     }
 }
Exemplo n.º 4
0
 public void NoHistoryInitially()
 {
     using (var testDirectory = new DisposableDirectory(FileSystem))
     {
         var pinSizeHistory = PinSizeHistory.LoadOrCreateNew(FileSystem, _clock, testDirectory.Path);
         var history        = pinSizeHistory.ReadHistory(1);
         AssertEmptyHistory(history.Window);
     }
 }
Exemplo n.º 5
0
        public void ReadHistoryWithDifferentSizesOfWindow()
        {
            using (var testDirectory = new DisposableDirectory(FileSystem))
            {
                var pinSizeHistory = PinSizeHistory.LoadOrCreateNew(FileSystem, _clock, testDirectory.Path);
                AddIntoPinSizeHistory(pinSizeHistory, 7, 2, 7, 13, 11);

                var history3 = pinSizeHistory.ReadHistory(3);
                AssertEqualHistory(new long[] { 11, 13, 7 }, history3.Window);

                var history5 = pinSizeHistory.ReadHistory(5);
                AssertEqualHistory(new long[] { 11, 13, 7, 2, 7 }, history5.Window);

                var history7 = pinSizeHistory.ReadHistory(7);
                AssertEqualHistory(new long[] { 11, 13, 7, 2, 7 }, history7.Window);
            }
        }
Exemplo n.º 6
0
        public async Task HistoryTimestampIsIncreasingMonotonically()
        {
            using (var testDirectory = new DisposableDirectory(FileSystem))
            {
                var pinSizeHistory = PinSizeHistory.LoadOrCreateNew(FileSystem, _clock, testDirectory.Path);
                var emptyHistory   = pinSizeHistory.ReadHistory(1);
                AssertEmptyHistory(emptyHistory.Window);

                AddIntoPinSizeHistory(pinSizeHistory, 7);

                var history1 = pinSizeHistory.ReadHistory(1);
                AssertEqualHistory(new long[] { 7 }, history1.Window);
                Assert.True(history1.TimestampInTick > emptyHistory.TimestampInTick);

                await pinSizeHistory.SaveAsync(FileSystem);

                pinSizeHistory = PinSizeHistory.LoadOrCreateNew(FileSystem, _clock, testDirectory.Path);

                var history1AfterSave = pinSizeHistory.ReadHistory(1);
                AssertEqualHistory(new long[] { 7 }, history1AfterSave.Window);
                Assert.Equal(history1.TimestampInTick, history1AfterSave.TimestampInTick);
            }
        }