Inheritance: IConcurrencyThrottle
    private async Task Cancel()
    {
        var cts = new CancellationTokenSource();

        var infos = new List <WorkUnit>
        {
            new(2000),
            new(2000),
            new(2000),
            new(2000),
            new(1000, onFinished : cts.Cancel),
            new(1000),
            new(1000),
            new(1000),
            new(1000),
            new(1000),
        };

        var startTime = DateTime.Now;

        await Assert.ThrowsAsync <OperationCanceledException>(
            () => ConcurrencyThrottle.Do(infos, i => i.Do(), cts.Token));

        var duration = (DateTime.Now - startTime).TotalSeconds;

        Assert.True(duration < 3);
        var called = infos.Count(wu => wu.WasCalled);

        if (called > 7)
        {
            throw new AssertActualExpectedException("<=6", called, "Called more than expected");
        }
    }
    private async Task Success()
    {
        var infos = new List <WorkUnit>
        {
            new(1000),
            new(1000),
            new(1000),
            new(1000),
            new(1000),
            new(1000),
            new(1000),
            new(1000),
            new(1000),
            new(1000),
        };

        var startTime = DateTime.Now;

        await ConcurrencyThrottle.Do(infos, i => i.Do(), CancellationToken.None);

        var duration = (DateTime.Now - startTime).TotalSeconds;

        Assert.True(duration < 3);
        Assert.All(infos, info => Assert.True(info.WasCalled));
    }
    private async Task ManyExceptions()
    {
        async Task Test()
        {
            await Task.Delay(100);

            throw new TestException();
        }

        await Test().ContinueWith(async t =>
        {
            await t;
        });

        var cts = new CancellationTokenSource();

        var infos = new List <WorkUnit>
        {
            new(1000, onFinished : () => throw new TestException()),
            new(1000, onFinished : () => throw new TestException()),
            new(1000, onFinished : () => throw new TestException()),
            new(1000, onFinished : () => throw new TestException()),
            new(1000, onFinished : () => throw new TestException()),
            new(1000),
            new(1000),
            new(1000),
            new(1000),
            new(1000),
        };

        var startTime = DateTime.Now;

        await Assert.ThrowsAsync <TestException>(() => ConcurrencyThrottle.Do(infos, i => i.Do(), cts.Token));

        var duration = (DateTime.Now - startTime).TotalSeconds;

        Assert.True(duration < 3);
        Assert.All(infos, info => Assert.True(info.WasCalled));
    }
Exemplo n.º 4
0
        public void ThrottleTest()
        {
            var throttle = new ConcurrencyThrottle {
                MaxValue = 5, Enabled = true
            };
            var maxReg     = new MaxRegister();
            var threadPool = new SmartThreadPool();

            var state = new DoWorkState {
                Throttle = throttle, MaxRegister = maxReg
            };
            var workItemResults = new List <IWaitableResult>();

            for (var i = 0; i < 100; i++)
            {
                workItemResults.Add(threadPool.QueueWorkItem(DoWork, state));
            }

            SmartThreadPool.WaitAll(workItemResults.ToArray());

            Assert.IsTrue(maxReg.MaxValue <= 5);
        }
Exemplo n.º 5
0
        private async Task UpdateAsync(CancellationToken cancellationToken, IProgress <FileSyncStats>?progress)
        {
            var task = ConcurrencyThrottle.Do(_allActions, a => a.DoAsync(cancellationToken), cancellationToken);

            await task.WithUpdates(TimeSpan.FromMilliseconds(50), () => ProgressCallback(progress));
        }
 internal ThottlePostThreadQueueStrategy(ConcurrencyThrottle throttle)
 {
     _throttle = throttle;
 }