Example #1
0
        public async Task DfTaskCanBeUsedAsReturnTypeOfAsyncMethodToEnableComposition()
        {
            await CreateAndUseNewBlock(1, 1, async runner1 =>
            {
                await CreateAndUseNewBlock(1, 1, async runner2 =>
                {
                    long[] numberOfTimesFirstOperationWasRunWhenSecondOperationRuns = new long[10];

                    long numberOfTimesFirstOperationWasRun = 0;

                    long numberOfTimesSecondOperationWasRun = 0;

                    async DfTask Part1()
                    {
                        await runner1.Run(() =>
                        {
                            SimulateWork(TimeSpan.Zero, ref numberOfTimesFirstOperationWasRun);
                        });
                    }

                    async DfTask Part2()
                    {
                        await runner2.Run(() =>
                        {
                            SimulateWork(
                                TimeSpan.FromMilliseconds(100),
                                ref numberOfTimesSecondOperationWasRun,
                                ref numberOfTimesFirstOperationWasRun,
                                numberOfTimesFirstOperationWasRunWhenSecondOperationRuns);
                        });
                    }

                    async Task Method1()
                    {
                        await Part1();

                        await Part2();
                    }

                    var tasks = Enumerable.Range(0, 10).Select(_ => Method1()).ToArray();

                    await Task.WhenAll(tasks);

                    PossibleValuesComparer
                    .AreEqual(
                        numberOfTimesFirstOperationWasRunWhenSecondOperationRuns,
                        new PossibleValues <long>[] { 3, 4, 5, 6, 7, 8, 9, 10, 10, 10 })
                    .Should().BeTrue();
                });
            });
        }
Example #2
0
        public async Task FastProducerThatThrowsAnExceptionWillBeSlowedBySlowConsumerThatCatchesTheException()
        {
            await CreateAndUseNewBlock(1, 1, async runner1 =>
            {
                await CreateAndUseNewBlock(1, 1, async runner2 =>
                {
                    long[] numberOfTimesFirstOperationWasRunWhenSecondOperationRuns = new long[10];

                    long numberOfTimesFirstOperationWasRun = 0;

                    long numberOfTimesSecondOperationWasRun = 0;

                    async Task Method1()
                    {
                        try
                        {
                            await runner1.Run(() =>
                            {
                                SimulateWork(TimeSpan.Zero, ref numberOfTimesFirstOperationWasRun);

                                throw new Exception("Error message");
                            });
                        }
                        catch
                        {
                            await runner2.Run(() =>
                            {
                                SimulateWork(
                                    TimeSpan.FromMilliseconds(100),
                                    ref numberOfTimesSecondOperationWasRun,
                                    ref numberOfTimesFirstOperationWasRun,
                                    numberOfTimesFirstOperationWasRunWhenSecondOperationRuns);
                            });
                        }
                    }

                    var tasks = Enumerable.Range(0, 10).Select(_ => Method1()).ToArray();

                    await Task.WhenAll(tasks);

                    PossibleValuesComparer
                    .AreEqual(
                        numberOfTimesFirstOperationWasRunWhenSecondOperationRuns,
                        new PossibleValues <long>[] { 3, 4, 5, 6, 7, 8, 9, 10, 10, 10 })
                    .Should().BeTrue();
                });
            });
        }
Example #3
0
        private async Task RunBasicTwoOperationsTest(int numberOfThreadsForFirstOperation, int queue1Size,
                                                     int numberOfThreadsForSecondOperation, int queye2Size, int numberOfTimesToProcess,
                                                     PossibleValues <long>[] expectedNumberOfFirstOperationCompleteForEachTimeOperationTwoIsInvoked)
        {
            await CreateAndUseNewBlock(numberOfThreadsForFirstOperation, queue1Size, async runner1 =>
            {
                await CreateAndUseNewBlock(numberOfThreadsForSecondOperation, queye2Size, async runner2 =>
                {
                    long[] numberOfTimesFirstOperationWasRunWhenSecondOperationRuns = new long[numberOfTimesToProcess];

                    long numberOfTimesFirstOperationWasRun = 0;

                    long numberOfTimesSecondOperationWasRun = 0;

                    async Task Method1()
                    {
                        await runner1.Run(() => { SimulateWork(TimeSpan.Zero, ref numberOfTimesFirstOperationWasRun); });

                        await runner2.Run(() =>
                        {
                            SimulateWork(
                                TimeSpan.FromMilliseconds(100),
                                ref numberOfTimesSecondOperationWasRun,
                                ref numberOfTimesFirstOperationWasRun,
                                numberOfTimesFirstOperationWasRunWhenSecondOperationRuns);
                        });
                    }


                    var tasks = Enumerable.Range(0, numberOfTimesToProcess).Select(_ => Method1()).ToArray();

                    await Task.WhenAll(tasks);


                    PossibleValuesComparer
                    .AreEqual(
                        numberOfTimesFirstOperationWasRunWhenSecondOperationRuns,
                        expectedNumberOfFirstOperationCompleteForEachTimeOperationTwoIsInvoked)
                    .Should().BeTrue();
                });
            });
        }
Example #4
0
        public async Task FastProducerWillBeSlowedBySlowConsumerForAsyncVersion()
        {
            await CreateAndUseNewAsyncBlock(1, 1, async runner1 =>
            {
                await CreateAndUseNewAsyncBlock(1, 1, async runner2 =>
                {
                    long[] numberOfTimesFirstOperationWasRunWhenSecondOperationRuns = new long[10];

                    long numberOfTimesFirstOperationWasRun = 0;

                    long numberOfTimesSecondOperationWasRun = 0;

                    async Task Method1()
                    {
                        await runner1.Run(async() =>
                        {
                            Interlocked.Increment(ref numberOfTimesFirstOperationWasRun);
                        });

                        await runner2.Run(async() =>
                        {
                            await Task.Delay(TimeSpan.FromMilliseconds(100));

                            var numberOfTimes = Interlocked.Increment(ref numberOfTimesSecondOperationWasRun);

                            numberOfTimesFirstOperationWasRunWhenSecondOperationRuns[numberOfTimes - 1] = Interlocked.Read(ref numberOfTimesFirstOperationWasRun);
                        });
                    }

                    var tasks = Enumerable.Range(0, 10).Select(_ => Method1()).ToArray();

                    await Task.WhenAll(tasks);

                    PossibleValuesComparer
                    .AreEqual(
                        numberOfTimesFirstOperationWasRunWhenSecondOperationRuns,
                        new PossibleValues <long>[] { 3, 4, 5, 6, 7, 8, 9, 10, 10, 10 })
                    .Should().BeTrue();
                });
            });
        }
Example #5
0
        public async Task DfTaskOfTResultWhenAllThatTakesDfTasksOfDifferentTypesTest()
        {
            await CreateAndUseNewBlock(1, 1, async runner1 =>
            {
                await CreateAndUseNewBlock(1, 1, async runner2 =>
                {
                    await CreateAndUseNewBlock(1, 1, async runner3 =>
                    {
                        long[] numberOfTimesFirstOperationWasRunWhenSecondOperationRuns = new long[10];

                        long[] numberOfTimesSecondOperationWasRunWhenThirdOperationRuns = new long[10];

                        long[] numberOfTimesFirstOperationWasRunWhenThirdOperationRuns = new long[10];

                        long numberOfTimesFirstOperationWasRun = 0;

                        long numberOfTimesSecondOperationWasRun = 0;

                        long numberOfTimesThirdOperationWasRun = 0;

                        long numberOfTimesThirdOperationWasRun2 = 0;

                        async Task Method1()
                        {
                            var dfTask1 = runner1.Run(() =>
                            {
                                SimulateWork(TimeSpan.FromMilliseconds(0), ref numberOfTimesFirstOperationWasRun);

                                return(1);
                            });

                            var dfTask2 = runner2.Run(() =>
                            {
                                SimulateWork(
                                    TimeSpan.FromMilliseconds(0),
                                    ref numberOfTimesSecondOperationWasRun,
                                    ref numberOfTimesFirstOperationWasRun,
                                    numberOfTimesFirstOperationWasRunWhenSecondOperationRuns);

                                return("yes");
                            });

                            var results = await DfTask.WhenAll(dfTask1, dfTask2, (x, y) => x + y);

                            results.Should().Be("1yes");

                            await runner3.Run(() =>
                            {
                                SimulateWork(
                                    TimeSpan.FromMilliseconds(100),
                                    ref numberOfTimesThirdOperationWasRun,
                                    ref numberOfTimesSecondOperationWasRun,
                                    numberOfTimesSecondOperationWasRunWhenThirdOperationRuns);

                                SimulateWork(
                                    TimeSpan.FromMilliseconds(0),
                                    ref numberOfTimesThirdOperationWasRun2,
                                    ref numberOfTimesFirstOperationWasRun,
                                    numberOfTimesFirstOperationWasRunWhenThirdOperationRuns);
                            });
                        }
                        var tasks = Enumerable.Range(0, 10).Select(_ => Method1()).ToArray();

                        await Task.WhenAll(tasks);

                        PossibleValuesComparer
                        .AreEqual(
                            numberOfTimesSecondOperationWasRunWhenThirdOperationRuns,
                            new PossibleValues <long>[] { 3, 4, 5, 6, 7, 8, 9, 10, 10, 10 })
                        .Should().BeTrue();

                        PossibleValuesComparer
                        .AreEqual(
                            numberOfTimesFirstOperationWasRunWhenThirdOperationRuns,
                            new PossibleValues <long>[] { 3, 4, 5, 6, 7, 8, 9, 10, 10, 10 })
                        .Should().BeTrue();
                    });
                });
            });
        }