From 5802db8aa7ca35cabd148e96dcd7249d836a793a Mon Sep 17 00:00:00 2001 From: stakx Date: Fri, 1 Jan 2021 10:49:23 +0100 Subject: [PATCH] Support faulted awaitables in `IAwaitableFactory` --- src/Moq/Async/AwaitableFactory`1.cs | 21 +++++++++++++++++++++ src/Moq/Async/AwaitableFactory`2.cs | 21 +++++++++++++++++++++ src/Moq/Async/IAwaitableFactory.cs | 5 +++++ src/Moq/Async/TaskFactory.cs | 17 +++++++++++++++++ src/Moq/Async/TaskFactory`1.cs | 16 ++++++++++++++++ src/Moq/Async/ValueTaskFactory.cs | 16 ++++++++++++++++ src/Moq/Async/ValueTaskFactory`1.cs | 16 ++++++++++++++++ 7 files changed, 112 insertions(+) diff --git a/src/Moq/Async/AwaitableFactory`1.cs b/src/Moq/Async/AwaitableFactory`1.cs index d71541d68..eb45d228a 100644 --- a/src/Moq/Async/AwaitableFactory`1.cs +++ b/src/Moq/Async/AwaitableFactory`1.cs @@ -2,7 +2,9 @@ // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. using System; +using System.Collections.Generic; using System.Diagnostics; +using System.Linq; namespace Moq.Async { @@ -23,6 +25,25 @@ object IAwaitableFactory.CreateCompleted(object result) return this.CreateCompleted(); } + public abstract TAwaitable CreateFaulted(Exception exception); + + object IAwaitableFactory.CreateFaulted(Exception exception) + { + Debug.Assert(exception != null); + + return this.CreateFaulted(exception); + } + + public abstract TAwaitable CreateFaulted(IEnumerable exceptions); + + object IAwaitableFactory.CreateFaulted(IEnumerable exceptions) + { + Debug.Assert(exceptions != null); + Debug.Assert(exceptions.Any()); + + return this.CreateFaulted(exceptions); + } + bool IAwaitableFactory.TryGetResult(object awaitable, out object result) { Debug.Assert(awaitable is TAwaitable); diff --git a/src/Moq/Async/AwaitableFactory`2.cs b/src/Moq/Async/AwaitableFactory`2.cs index f1042b534..4cac4c456 100644 --- a/src/Moq/Async/AwaitableFactory`2.cs +++ b/src/Moq/Async/AwaitableFactory`2.cs @@ -2,7 +2,9 @@ // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. using System; +using System.Collections.Generic; using System.Diagnostics; +using System.Linq; namespace Moq.Async { @@ -23,6 +25,25 @@ object IAwaitableFactory.CreateCompleted(object result) return this.CreateCompleted((TResult)result); } + public abstract TAwaitable CreateFaulted(Exception exception); + + object IAwaitableFactory.CreateFaulted(Exception exception) + { + Debug.Assert(exception != null); + + return this.CreateFaulted(exception); + } + + public abstract TAwaitable CreateFaulted(IEnumerable exceptions); + + object IAwaitableFactory.CreateFaulted(IEnumerable exceptions) + { + Debug.Assert(exceptions != null); + Debug.Assert(exceptions.Any()); + + return this.CreateFaulted(exceptions); + } + public abstract bool TryGetResult(TAwaitable awaitable, out TResult result); bool IAwaitableFactory.TryGetResult(object awaitable, out object result) diff --git a/src/Moq/Async/IAwaitableFactory.cs b/src/Moq/Async/IAwaitableFactory.cs index f31c8656b..1c38ca940 100644 --- a/src/Moq/Async/IAwaitableFactory.cs +++ b/src/Moq/Async/IAwaitableFactory.cs @@ -2,6 +2,7 @@ // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. using System; +using System.Collections.Generic; namespace Moq.Async { @@ -11,6 +12,10 @@ internal interface IAwaitableFactory object CreateCompleted(object result = null); + object CreateFaulted(Exception exception); + + object CreateFaulted(IEnumerable exceptions); + bool TryGetResult(object awaitable, out object result); } } diff --git a/src/Moq/Async/TaskFactory.cs b/src/Moq/Async/TaskFactory.cs index 1ada0969e..874751c6d 100644 --- a/src/Moq/Async/TaskFactory.cs +++ b/src/Moq/Async/TaskFactory.cs @@ -1,6 +1,9 @@ // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. +using System; +using System.Collections.Generic; +using System.Reflection; using System.Threading.Tasks; namespace Moq.Async @@ -17,5 +20,19 @@ public override Task CreateCompleted() { return Task.FromResult(default); } + + public override Task CreateFaulted(Exception exception) + { + var tcs = new TaskCompletionSource(); + tcs.SetException(exception); + return tcs.Task; + } + + public override Task CreateFaulted(IEnumerable exceptions) + { + var tcs = new TaskCompletionSource(); + tcs.SetException(exceptions); + return tcs.Task; + } } } diff --git a/src/Moq/Async/TaskFactory`1.cs b/src/Moq/Async/TaskFactory`1.cs index ccb4316b8..41d5dde6a 100644 --- a/src/Moq/Async/TaskFactory`1.cs +++ b/src/Moq/Async/TaskFactory`1.cs @@ -1,6 +1,8 @@ // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. +using System; +using System.Collections.Generic; using System.Threading.Tasks; namespace Moq.Async @@ -12,6 +14,20 @@ public override Task CreateCompleted(TResult result) return Task.FromResult(result); } + public override Task CreateFaulted(Exception exception) + { + var tcs = new TaskCompletionSource(); + tcs.SetException(exception); + return tcs.Task; + } + + public override Task CreateFaulted(IEnumerable exceptions) + { + var tcs = new TaskCompletionSource(); + tcs.SetException(exceptions); + return tcs.Task; + } + public override bool TryGetResult(Task task, out TResult result) { if (task.Status == TaskStatus.RanToCompletion) diff --git a/src/Moq/Async/ValueTaskFactory.cs b/src/Moq/Async/ValueTaskFactory.cs index fbd3fb1f2..d8775ff14 100644 --- a/src/Moq/Async/ValueTaskFactory.cs +++ b/src/Moq/Async/ValueTaskFactory.cs @@ -1,6 +1,8 @@ // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. +using System; +using System.Collections.Generic; using System.Threading.Tasks; namespace Moq.Async @@ -17,5 +19,19 @@ public override ValueTask CreateCompleted() { return default; } + + public override ValueTask CreateFaulted(Exception exception) + { + var tcs = new TaskCompletionSource(); + tcs.SetException(exception); + return new ValueTask(tcs.Task); + } + + public override ValueTask CreateFaulted(IEnumerable exceptions) + { + var tcs = new TaskCompletionSource(); + tcs.SetException(exceptions); + return new ValueTask(tcs.Task); + } } } diff --git a/src/Moq/Async/ValueTaskFactory`1.cs b/src/Moq/Async/ValueTaskFactory`1.cs index 265ef4813..213921bf7 100644 --- a/src/Moq/Async/ValueTaskFactory`1.cs +++ b/src/Moq/Async/ValueTaskFactory`1.cs @@ -1,6 +1,8 @@ // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. +using System; +using System.Collections.Generic; using System.Threading.Tasks; namespace Moq.Async @@ -12,6 +14,20 @@ public override ValueTask CreateCompleted(TResult result) return new ValueTask(result); } + public override ValueTask CreateFaulted(Exception exception) + { + var tcs = new TaskCompletionSource(); + tcs.SetException(exception); + return new ValueTask(tcs.Task); + } + + public override ValueTask CreateFaulted(IEnumerable exceptions) + { + var tcs = new TaskCompletionSource(); + tcs.SetException(exceptions); + return new ValueTask(tcs.Task); + } + public override bool TryGetResult(ValueTask valueTask, out TResult result) { if (valueTask.IsCompletedSuccessfully)