Esempio n. 1
0
        public async Task RaceReturnsOnceAndForFirstTask()
        {
            const int delay1 = 500, delay2 = 100, delay3 = 5000;
            var       task1Finished     = false;
            var       task2Finished     = false;
            var       task3Finished     = false;
            var       expectedException = new PromiseTestException("Expected to happen");

            var task1 = Task.Run(async() =>
            {
                await Task.Delay(delay1);
                task1Finished = true;
            });

            var task2 = Task.Run(async() =>
            {
                await Task.Delay(delay2);
                task2Finished = true;
            });

            var task3 = Task.Run(async() =>
            {
                await Task.Delay(delay3);
                task3Finished = true;
                throw expectedException;
            });

            var item = SharpPromise.Promise.Race(task1, task2, task3);
            await item.Then(() =>
            {
                task2Finished.ShouldBeTrue("Task 2 did not finish in time.");
            },
                            ex =>
            {
                0.ShouldBe(1, "Test 1 resulted in an exception, which should not happen");
            });


            var task4Finished = false;
            var task5Finished = false;
            var task6Finished = false;

            var task4 = Task.Run(async() =>
            {
                await Task.Delay(delay1);
                task4Finished = true;
            });

            var task5 = Task.Run(async() =>
            {
                await Task.Delay(delay3);
                task5Finished = true;
            });

            var task6 = Task.Run(async() =>
            {
                await Task.Delay(delay2);
                task6Finished = true;
                throw expectedException;
            });

            var item2 = SharpPromise.Promise.Race(task4, task5, task6);
            await item2.Then(() =>
            {
                0.ShouldBe(1, "Test 2 resulted in a success, which should not have happened.");
            },
                             ex =>
            {
                task6Finished.ShouldBe(true);
            });
        }
Esempio n. 2
0
        public async Task RaceReturnsOnceAndOnFirstResolve()
        {
            const int delay1 = 300, delay2 = 100, delay3 = 5000;
            var       task1Finished = false;
            var       task2Finished = false;
            var       task3Finished = false;

            var expectedException = new PromiseTestException("Perfectly acceptable exception that should exist for testing.");

            var promise1 = SharpPromise.Promise.Resolve()
                           .Then(async() =>
            {
                await Task.Delay(delay1);
                task1Finished = true;
            });

            var promise2 = SharpPromise.Promise.Resolve()
                           .Then(async() =>
            {
                await Task.Delay(delay2);
                task2Finished = true;
            });

            var promise3 = SharpPromise.Promise.Resolve()
                           .Then(async() =>
            {
                await Task.Delay(delay3);
                task3Finished = true;
                throw expectedException;
            });

            var item = SharpPromise.Promise.Race(promise1, promise2, promise3);
            await item.Then(() =>
            {
                task2Finished.ShouldBeTrue("Task 2 did not finish in time.");
                task1Finished.ShouldBeFalse("Task 1 finished before 2");
            },
                            ex =>
            {
                0.ShouldBe(1, "Test 1 resulted in the thrown exception, which should never be reached.");
            });

            var task4Finished = false;
            var task5Finished = false;
            var task6Finished = false;

            var promise4 = SharpPromise.Promise.Resolve()
                           .Then(async() =>
            {
                await Task.Delay(delay1);
                task4Finished = true;
            });

            var promise5 = SharpPromise.Promise.Resolve()
                           .Then(async() =>
            {
                await Task.Delay(delay3);
                task5Finished = true;
            });

            var promise6 = SharpPromise.Promise.Resolve()
                           .Then(async() =>
            {
                await Task.Delay(delay2);
                task6Finished = true;
                throw expectedException;
            });

            var item2 = SharpPromise.Promise.Race(promise4, promise5, promise6);
            await item2.Then(() =>
            {
                0.ShouldBe(1, "Test 2 resulted in an accepted state, which should never be reached.");
            },
                             ex =>
            {
                task6Finished.ShouldBeTrue("Task 6 did not finish in time.");
            });
        }