Wait() public method

public Wait ( CancellationToken, cancellationToken = default ) : Task,
cancellationToken CancellationToken,
return Task,
示例#1
0
    public async Task RunTest()
    {
        var defer  = new Defer();
        var number = 0;

        async Task AsyncFunc()
        {
            number++;
            await Task.Delay(10);

            number++;
            if (number == 10)
            {
                defer.Resolve();
            }
        }

        var worker = new AsyncTaskWorker(3);
        await worker.Run(AsyncFunc);

        await worker.Run(AsyncFunc);

        await worker.Run(AsyncFunc);

        await worker.Run(AsyncFunc);

        await worker.Run(AsyncFunc);

        await defer.Wait();

        Assert.AreEqual(number, 10);
    }
示例#2
0
    public async Task DeferTest()
    {
        var defer = new Defer();

        Assert.AreEqual(false, defer.IsCompleted);

        defer.Resolve();

        await defer.Wait();

        Assert.AreEqual(true, defer.IsCompleted);
    }
示例#3
0
    public async Task MaxConcurrencyTest()
    {
        var defer    = new Defer();
        var endDefer = new Defer();

        var worker = new AsyncTaskWorker(3);

        // Three blocked tasks
        await worker.Run(() => defer.Wait());

        await worker.Run(() => defer.Wait());

        await worker.Run(() => defer.Wait());

        // Task will be suspended
        var step4Task = worker.Run(
            () =>
        {
            endDefer.Resolve();
            return(Task.CompletedTask);
        });

        await Task.Delay(100);

        Assert.IsFalse(step4Task.IsCompleted);
        Assert.IsFalse(endDefer.IsCompleted);

        // End the blocked tasks
        defer.Resolve();

        // Wait for step4 end
        await step4Task;
        await endDefer.Wait();

        Assert.IsTrue(step4Task.IsCompleted);
    }
示例#4
0
    public async Task DisposeAsyncCreatorTest()
    {
        var step1defer = new Defer();
        var step2defer = new Defer();

        using var pool = new ObjectPool <TestObject>(
                  1,
                  async() =>
        {
            await Task.Delay(10);
            return(new TestObject());
        });

        var thrown = false;

        var taskA = Task.Run(
            async() =>
        {
            using (await pool.GetRefAsync())
            {
                step1defer.Resolve();
                try
                {
                    using (await pool.GetRefAsync())
                    {
                        Assert.Fail("Should not be run.");
                    }
                }
                catch (OperationCanceledException)
                {
                    thrown = true;
                    step2defer.Resolve();
                }
            }
        });

        var taskB = Task.Run(
            async() =>
        {
            await step1defer.Wait();
            pool.Dispose();
            await step2defer.Wait();
            Assert.IsTrue(thrown);
        });

        await Task.WhenAll(taskA, taskB);
    }
示例#5
0
    public async Task BlockTest()
    {
        var step1defer = new Defer();
        var step2defer = new Defer();

        using var pool = new ObjectPool <TestObject>(3, () => { return(new TestObject()); });

        var flag = false;

        var taskA = Task.Run(
            async() =>
        {
            using (await pool.GetRefAsync())
                using (await pool.GetRefAsync())
                {
                    using (await pool.GetRefAsync())
                    {
                        // the pool is empty now
                        step1defer.Resolve();
                        await Task.Delay(50);
                        Assert.IsFalse(flag);
                    } // relese once

                    await step2defer.Wait();
                    Assert.IsTrue(flag);
                }
        });

        var taskB = Task.Run(
            async() =>
        {
            await step1defer.Wait();

            // should block here
            using (await pool.GetRefAsync())
            {
                flag = true;
                step2defer.Resolve();
            }
        });

        await Task.WhenAll(taskA, taskB);
    }
示例#6
0
    public async Task CancelTest()
    {
        var flag = false;

        using var cancellationTokenSource = new CancellationTokenSource();
        var cancellationToken = cancellationTokenSource.Token;

        var worker = new AsyncTaskWorker(1);

        // blocked task
        var blockedDefer = new Defer();
        await worker.Run(async() =>
        {
            await blockedDefer.Wait();
        });

        // Task will be suspended
        var suspendedTask = worker.Run(
            () =>
        {
            flag = true;
            return(Task.CompletedTask);
        },
            cancellationToken);

        Assert.IsFalse(suspendedTask.IsCompleted);
        Assert.IsFalse(flag);

        // Cancel pending suspension
        cancellationTokenSource.Cancel();

        Assert.ThrowsAsync <OperationCanceledException>(() => suspendedTask.AsTask());

        Assert.IsTrue(suspendedTask.IsCanceled);
        Assert.IsFalse(flag);
        blockedDefer.Resolve();
    }