Skip to content

Commit

Permalink
Support faulted awaitables in IAwaitableFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
stakx committed Jan 1, 2021
1 parent 047bf95 commit 5802db8
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/Moq/Async/AwaitableFactory`1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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<Exception> exceptions);

object IAwaitableFactory.CreateFaulted(IEnumerable<Exception> 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);
Expand Down
21 changes: 21 additions & 0 deletions src/Moq/Async/AwaitableFactory`2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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<Exception> exceptions);

object IAwaitableFactory.CreateFaulted(IEnumerable<Exception> 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)
Expand Down
5 changes: 5 additions & 0 deletions src/Moq/Async/IAwaitableFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -11,6 +12,10 @@ internal interface IAwaitableFactory

object CreateCompleted(object result = null);

object CreateFaulted(Exception exception);

object CreateFaulted(IEnumerable<Exception> exceptions);

bool TryGetResult(object awaitable, out object result);
}
}
17 changes: 17 additions & 0 deletions src/Moq/Async/TaskFactory.cs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -17,5 +20,19 @@ public override Task CreateCompleted()
{
return Task.FromResult<object>(default);
}

public override Task CreateFaulted(Exception exception)
{
var tcs = new TaskCompletionSource<object>();
tcs.SetException(exception);
return tcs.Task;
}

public override Task CreateFaulted(IEnumerable<Exception> exceptions)
{
var tcs = new TaskCompletionSource<object>();
tcs.SetException(exceptions);
return tcs.Task;
}
}
}
16 changes: 16 additions & 0 deletions src/Moq/Async/TaskFactory`1.cs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -12,6 +14,20 @@ public override Task<TResult> CreateCompleted(TResult result)
return Task.FromResult(result);
}

public override Task<TResult> CreateFaulted(Exception exception)
{
var tcs = new TaskCompletionSource<TResult>();
tcs.SetException(exception);
return tcs.Task;
}

public override Task<TResult> CreateFaulted(IEnumerable<Exception> exceptions)
{
var tcs = new TaskCompletionSource<TResult>();
tcs.SetException(exceptions);
return tcs.Task;
}

public override bool TryGetResult(Task<TResult> task, out TResult result)
{
if (task.Status == TaskStatus.RanToCompletion)
Expand Down
16 changes: 16 additions & 0 deletions src/Moq/Async/ValueTaskFactory.cs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -17,5 +19,19 @@ public override ValueTask CreateCompleted()
{
return default;
}

public override ValueTask CreateFaulted(Exception exception)
{
var tcs = new TaskCompletionSource<object>();
tcs.SetException(exception);
return new ValueTask(tcs.Task);
}

public override ValueTask CreateFaulted(IEnumerable<Exception> exceptions)
{
var tcs = new TaskCompletionSource<object>();
tcs.SetException(exceptions);
return new ValueTask(tcs.Task);
}
}
}
16 changes: 16 additions & 0 deletions src/Moq/Async/ValueTaskFactory`1.cs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -12,6 +14,20 @@ public override ValueTask<TResult> CreateCompleted(TResult result)
return new ValueTask<TResult>(result);
}

public override ValueTask<TResult> CreateFaulted(Exception exception)
{
var tcs = new TaskCompletionSource<TResult>();
tcs.SetException(exception);
return new ValueTask<TResult>(tcs.Task);
}

public override ValueTask<TResult> CreateFaulted(IEnumerable<Exception> exceptions)
{
var tcs = new TaskCompletionSource<TResult>();
tcs.SetException(exceptions);
return new ValueTask<TResult>(tcs.Task);
}

public override bool TryGetResult(ValueTask<TResult> valueTask, out TResult result)
{
if (valueTask.IsCompletedSuccessfully)
Expand Down

0 comments on commit 5802db8

Please sign in to comment.