public void Sampler_should_throttle_calls()
        {
            var monitorType = "test";
            var throttling = 2;
            var total = 3;

            var settings = SetupThrottling(new Dictionary<string, int> {{monitorType, throttling}});
            var endpoint = SetupEndpoint(monitorType);
            var manualReset = new ManualResetEventSlim();
            var sampler = SetupSampler(endpoint, manualReset);

            var throttlingSampler = new ThrottlingSampler(sampler.Object, settings.Object);
            var token = new CancellationToken();

            var tasks = Enumerable.Range(0, total)
                .Select(n => throttlingSampler.CheckHealth(endpoint, token))
                .ToArray();
            var allFinishedTask = Task.WhenAll(tasks);

            Assert.False(allFinishedTask.Wait(250), "No tasks should finish yet");
            sampler.Verify(s => s.CheckHealth(endpoint, It.IsAny<CancellationToken>()), Times.Exactly(throttling));

            manualReset.Set();
            allFinishedTask.Wait();
            sampler.Verify(s => s.CheckHealth(endpoint, It.IsAny<CancellationToken>()), Times.Exactly(total));
        }
        public async Task Sampler_should_throttle_calls()
        {
            var monitorType = "test";
            var throttling  = 2;
            var total       = 3;

            var settings = SetupThrottling(new Dictionary <string, int> {
                { monitorType, throttling }
            });
            var endpoint    = SetupEndpoint(monitorType);
            var manualReset = new ManualResetEventSlim();
            var sampler     = SetupSampler(endpoint, manualReset);

            var throttlingSampler = new ThrottlingSampler(sampler.Object, settings.Object);
            var token             = new CancellationToken();

            var tasks = Enumerable.Range(0, total)
                        .Select(n => throttlingSampler.CheckHealthAsync(endpoint, token))
                        .ToArray();
            var allFinishedTask = Task.WhenAll(tasks);

            await Task.Delay(250);

            Assert.False(allFinishedTask.IsCompleted, "No tasks should finish yet");
            sampler.Verify(s => s.CheckHealthAsync(endpoint, It.IsAny <CancellationToken>()), Times.Exactly(throttling));

            manualReset.Set();
            await allFinishedTask;

            sampler.Verify(s => s.CheckHealthAsync(endpoint, It.IsAny <CancellationToken>()), Times.Exactly(total));
        }
Beispiel #3
0
        public void Sampler_should_not_throttle_calls_if_throttling_is_not_specified()
        {
            var monitorType = "test";
            var total       = 3;

            var settings    = SetupThrottling(new Dictionary <string, int>());
            var endpoint    = SetupEndpoint(monitorType);
            var manualReset = new ManualResetEventSlim();
            var sampler     = SetupSampler(endpoint, manualReset);

            var throttlingSampler = new ThrottlingSampler(sampler.Object, settings.Object);
            var token             = new CancellationToken();

            var tasks = Enumerable.Range(0, total)
                        .Select(n => throttlingSampler.CheckHealth(endpoint, token))
                        .ToArray();
            var allFinishedTask = Task.WhenAll(tasks);

            Assert.False(allFinishedTask.Wait(250), "No tasks should finish yet");
            sampler.Verify(s => s.CheckHealth(endpoint, It.IsAny <CancellationToken>()), Times.Exactly(total));

            manualReset.Set();
            allFinishedTask.Wait();
        }