Skip to content

Commit 5802db8

Browse files
committed
Support faulted awaitables in IAwaitableFactory
1 parent 047bf95 commit 5802db8

File tree

7 files changed

+112
-0
lines changed

7 files changed

+112
-0
lines changed

src/Moq/Async/AwaitableFactory`1.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
// All rights reserved. Licensed under the BSD 3-Clause License; see License.txt.
33

44
using System;
5+
using System.Collections.Generic;
56
using System.Diagnostics;
7+
using System.Linq;
68

79
namespace Moq.Async
810
{
@@ -23,6 +25,25 @@ object IAwaitableFactory.CreateCompleted(object result)
2325
return this.CreateCompleted();
2426
}
2527

28+
public abstract TAwaitable CreateFaulted(Exception exception);
29+
30+
object IAwaitableFactory.CreateFaulted(Exception exception)
31+
{
32+
Debug.Assert(exception != null);
33+
34+
return this.CreateFaulted(exception);
35+
}
36+
37+
public abstract TAwaitable CreateFaulted(IEnumerable<Exception> exceptions);
38+
39+
object IAwaitableFactory.CreateFaulted(IEnumerable<Exception> exceptions)
40+
{
41+
Debug.Assert(exceptions != null);
42+
Debug.Assert(exceptions.Any());
43+
44+
return this.CreateFaulted(exceptions);
45+
}
46+
2647
bool IAwaitableFactory.TryGetResult(object awaitable, out object result)
2748
{
2849
Debug.Assert(awaitable is TAwaitable);

src/Moq/Async/AwaitableFactory`2.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
// All rights reserved. Licensed under the BSD 3-Clause License; see License.txt.
33

44
using System;
5+
using System.Collections.Generic;
56
using System.Diagnostics;
7+
using System.Linq;
68

79
namespace Moq.Async
810
{
@@ -23,6 +25,25 @@ object IAwaitableFactory.CreateCompleted(object result)
2325
return this.CreateCompleted((TResult)result);
2426
}
2527

28+
public abstract TAwaitable CreateFaulted(Exception exception);
29+
30+
object IAwaitableFactory.CreateFaulted(Exception exception)
31+
{
32+
Debug.Assert(exception != null);
33+
34+
return this.CreateFaulted(exception);
35+
}
36+
37+
public abstract TAwaitable CreateFaulted(IEnumerable<Exception> exceptions);
38+
39+
object IAwaitableFactory.CreateFaulted(IEnumerable<Exception> exceptions)
40+
{
41+
Debug.Assert(exceptions != null);
42+
Debug.Assert(exceptions.Any());
43+
44+
return this.CreateFaulted(exceptions);
45+
}
46+
2647
public abstract bool TryGetResult(TAwaitable awaitable, out TResult result);
2748

2849
bool IAwaitableFactory.TryGetResult(object awaitable, out object result)

src/Moq/Async/IAwaitableFactory.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// All rights reserved. Licensed under the BSD 3-Clause License; see License.txt.
33

44
using System;
5+
using System.Collections.Generic;
56

67
namespace Moq.Async
78
{
@@ -11,6 +12,10 @@ internal interface IAwaitableFactory
1112

1213
object CreateCompleted(object result = null);
1314

15+
object CreateFaulted(Exception exception);
16+
17+
object CreateFaulted(IEnumerable<Exception> exceptions);
18+
1419
bool TryGetResult(object awaitable, out object result);
1520
}
1621
}

src/Moq/Async/TaskFactory.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors.
22
// All rights reserved. Licensed under the BSD 3-Clause License; see License.txt.
33

4+
using System;
5+
using System.Collections.Generic;
6+
using System.Reflection;
47
using System.Threading.Tasks;
58

69
namespace Moq.Async
@@ -17,5 +20,19 @@ public override Task CreateCompleted()
1720
{
1821
return Task.FromResult<object>(default);
1922
}
23+
24+
public override Task CreateFaulted(Exception exception)
25+
{
26+
var tcs = new TaskCompletionSource<object>();
27+
tcs.SetException(exception);
28+
return tcs.Task;
29+
}
30+
31+
public override Task CreateFaulted(IEnumerable<Exception> exceptions)
32+
{
33+
var tcs = new TaskCompletionSource<object>();
34+
tcs.SetException(exceptions);
35+
return tcs.Task;
36+
}
2037
}
2138
}

src/Moq/Async/TaskFactory`1.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors.
22
// All rights reserved. Licensed under the BSD 3-Clause License; see License.txt.
33

4+
using System;
5+
using System.Collections.Generic;
46
using System.Threading.Tasks;
57

68
namespace Moq.Async
@@ -12,6 +14,20 @@ public override Task<TResult> CreateCompleted(TResult result)
1214
return Task.FromResult(result);
1315
}
1416

17+
public override Task<TResult> CreateFaulted(Exception exception)
18+
{
19+
var tcs = new TaskCompletionSource<TResult>();
20+
tcs.SetException(exception);
21+
return tcs.Task;
22+
}
23+
24+
public override Task<TResult> CreateFaulted(IEnumerable<Exception> exceptions)
25+
{
26+
var tcs = new TaskCompletionSource<TResult>();
27+
tcs.SetException(exceptions);
28+
return tcs.Task;
29+
}
30+
1531
public override bool TryGetResult(Task<TResult> task, out TResult result)
1632
{
1733
if (task.Status == TaskStatus.RanToCompletion)

src/Moq/Async/ValueTaskFactory.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors.
22
// All rights reserved. Licensed under the BSD 3-Clause License; see License.txt.
33

4+
using System;
5+
using System.Collections.Generic;
46
using System.Threading.Tasks;
57

68
namespace Moq.Async
@@ -17,5 +19,19 @@ public override ValueTask CreateCompleted()
1719
{
1820
return default;
1921
}
22+
23+
public override ValueTask CreateFaulted(Exception exception)
24+
{
25+
var tcs = new TaskCompletionSource<object>();
26+
tcs.SetException(exception);
27+
return new ValueTask(tcs.Task);
28+
}
29+
30+
public override ValueTask CreateFaulted(IEnumerable<Exception> exceptions)
31+
{
32+
var tcs = new TaskCompletionSource<object>();
33+
tcs.SetException(exceptions);
34+
return new ValueTask(tcs.Task);
35+
}
2036
}
2137
}

src/Moq/Async/ValueTaskFactory`1.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors.
22
// All rights reserved. Licensed under the BSD 3-Clause License; see License.txt.
33

4+
using System;
5+
using System.Collections.Generic;
46
using System.Threading.Tasks;
57

68
namespace Moq.Async
@@ -12,6 +14,20 @@ public override ValueTask<TResult> CreateCompleted(TResult result)
1214
return new ValueTask<TResult>(result);
1315
}
1416

17+
public override ValueTask<TResult> CreateFaulted(Exception exception)
18+
{
19+
var tcs = new TaskCompletionSource<TResult>();
20+
tcs.SetException(exception);
21+
return new ValueTask<TResult>(tcs.Task);
22+
}
23+
24+
public override ValueTask<TResult> CreateFaulted(IEnumerable<Exception> exceptions)
25+
{
26+
var tcs = new TaskCompletionSource<TResult>();
27+
tcs.SetException(exceptions);
28+
return new ValueTask<TResult>(tcs.Task);
29+
}
30+
1531
public override bool TryGetResult(ValueTask<TResult> valueTask, out TResult result)
1632
{
1733
if (valueTask.IsCompletedSuccessfully)

0 commit comments

Comments
 (0)