Пример #1
0
        public void TestCheckSyncValueTaskIntDynamicWorked(VTaskOptions options)
        {
            //SETUP
            dynamic valueTask = ValueTaskIntMethod(options);
            int     result    = 0;

            //ATTEMPT
            try
            {
                result = ValueTaskSyncCheckers.CheckSyncValueTaskWorkedDynamicAndReturnResult <int>(valueTask);
            }
            catch (Exception e)
            {
                options.ShouldNotEqual(VTaskOptions.Sync);
                if (options.HasFlag(VTaskOptions.Async))
                {
                    e.Message.ShouldEqual("Expected a sync task, but got an async task");
                }
                if (options.HasFlag(VTaskOptions.ThrowException))
                {
                    e.Message.ShouldEqual("Exception thrown");
                }

                return;
            }

            //VERIFY
            options.ShouldEqual(VTaskOptions.Sync);
            result.ShouldEqual(1);
        }
Пример #2
0
        public void TestCheckSyncValueTaskIntWorked(VTaskOptions options)
        {
            //SETUP
            var valueTaskInt = ValueTaskIntMethod(options);

            //ATTEMPT
            try
            {
                valueTaskInt.CheckSyncValueTaskWorkedAndReturnResult();
            }
            catch (Exception e)
            {
                options.ShouldNotEqual(VTaskOptions.Sync);
                if (options.HasFlag(VTaskOptions.Async))
                {
                    e.Message.ShouldEqual("Expected a sync task, but got an async task");
                }
                if (options.HasFlag(VTaskOptions.ThrowException))
                {
                    e.Message.ShouldEqual("Exception thrown");
                }

                return;
            }

            //VERIFY
            options.ShouldEqual(VTaskOptions.Sync);
        }
Пример #3
0
        private async ValueTask <int> ValueTaskIntMethod(VTaskOptions options)
        {
            if (options.HasFlag(VTaskOptions.Async))
            {
                var cancelToken = options.HasFlag(VTaskOptions.CancelAsync)
                    ? new CancellationToken(true)
                    : CancellationToken.None;
                await Task.Delay(1000, cancelToken);
            }

            if (options.HasFlag(VTaskOptions.ThrowException))
            {
                throw new InvalidOperationException("Exception thrown");
            }

            return(1);
        }
Пример #4
0
        public async Task TestValueTaskMethodAsyncOk(VTaskOptions options)
        {
            //SETUP

            //ATTEMPT
            try
            {
                await ValueTaskMethod(options);
            }
            catch
            {
                (options.HasFlag(VTaskOptions.ThrowException) || options.HasFlag(VTaskOptions.CancelAsync)).ShouldBeTrue();
                return;
            }

            //VERIFY
            options.HasFlag(VTaskOptions.CancelAsync).ShouldBeFalse();
            options.HasFlag(VTaskOptions.ThrowException).ShouldBeFalse();
        }
Пример #5
0
        public void TestValueTaskIntMethodSyncOk(VTaskOptions options)
        {
            //SETUP

            //ATTEMPT
            var valueTaskInt = ValueTaskIntMethod(options);

            //VERIFY
            valueTaskInt.IsFaulted.ShouldEqual(options.HasFlag(VTaskOptions.ThrowException));
        }
Пример #6
0
        private async ValueTask ValueTaskMethod(VTaskOptions options, int depth = 0)
        {
            if (options.HasFlag(VTaskOptions.Async))
            {
                var cancelToken = options.HasFlag(VTaskOptions.CancelAsync)
                    ? new CancellationToken(true)
                    : CancellationToken.None;
                await Task.Delay(1, cancelToken);
            }

            if (depth > 0 && !options.HasFlag(VTaskOptions.Async))
            {
                var valueTask = ValueTaskMethod(options, depth - 1);
                valueTask.CheckSyncValueTaskWorked();
                return;
            }

            if (options.HasFlag(VTaskOptions.ThrowException))
            {
                throw new Exception("Exception thrown");
            }
        }
Пример #7
0
        private void CheckSyncValueTask(ValueTask valueTask, VTaskOptions options)
        {
            if (options.HasFlag(VTaskOptions.ThrowException))
            {
                valueTask.IsFaulted.ShouldBeTrue();
                valueTask.IsCompleted.ShouldBeTrue();
                valueTask.IsCompletedSuccessfully.ShouldBeFalse();

                var task = valueTask.AsTask();
                task.Exception.ShouldNotBeNull();
            }
            else
            {
                valueTask.IsFaulted.ShouldBeFalse();
                valueTask.IsCompleted.ShouldBeTrue();
                valueTask.IsCompletedSuccessfully.ShouldBeTrue();

                var task = valueTask.AsTask();
                task.Exception.ShouldBeNull();
            }

            valueTask.IsCanceled.ShouldEqual(options.HasFlag(VTaskOptions.CancelAsync));
        }