-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix Akka.Util.Result
edge case
#7433
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
// ----------------------------------------------------------------------- | ||
// <copyright file="ResultSpec.cs" company="Akka.NET Project"> | ||
// Copyright (C) 2009-2024 Lightbend Inc. <http://www.lightbend.com> | ||
// Copyright (C) 2013-2024 .NET Foundation <https://github.com/akkadotnet/akka.net> | ||
// </copyright> | ||
// ----------------------------------------------------------------------- | ||
|
||
using System; | ||
using System.Threading.Tasks; | ||
using Akka.Util; | ||
using FluentAssertions; | ||
using Xunit; | ||
using static FluentAssertions.FluentActions; | ||
|
||
namespace Akka.Tests.Util; | ||
|
||
public class ResultSpec | ||
{ | ||
[Fact(DisplayName = "Result constructor with value should return success")] | ||
public void SuccessfulResult() | ||
{ | ||
var result = new Result<int>(1); | ||
|
||
result.IsSuccess.Should().BeTrue(); | ||
result.Value.Should().Be(1); | ||
result.Exception.Should().BeNull(); | ||
} | ||
|
||
[Fact(DisplayName = "Result constructor with exception should return failed")] | ||
public void ExceptionResult() | ||
{ | ||
var result = new Result<int>(new TestException("BOOM")); | ||
|
||
result.IsSuccess.Should().BeFalse(); | ||
result.Exception.Should().NotBeNull(); | ||
result.Exception.Should().BeOfType<TestException>(); | ||
} | ||
|
||
[Fact(DisplayName = "Result.Success with value should return success")] | ||
public void SuccessfulStaticSuccess() | ||
{ | ||
var result = Result.Success(1); | ||
|
||
result.IsSuccess.Should().BeTrue(); | ||
result.Value.Should().Be(1); | ||
result.Exception.Should().BeNull(); | ||
} | ||
|
||
[Fact(DisplayName = "Result.Failure with exception should return failed")] | ||
public void ExceptionStaticFailure() | ||
{ | ||
var result = Result.Failure<int>(new TestException("BOOM")); | ||
|
||
result.IsSuccess.Should().BeFalse(); | ||
result.Exception.Should().NotBeNull(); | ||
result.Exception.Should().BeOfType<TestException>(); | ||
} | ||
|
||
[Fact(DisplayName = "Result.From with successful Func should return success")] | ||
public void SuccessfulFuncResult() | ||
{ | ||
var result = Result.From(() => 1); | ||
|
||
result.IsSuccess.Should().BeTrue(); | ||
result.Value.Should().Be(1); | ||
result.Exception.Should().BeNull(); | ||
} | ||
|
||
[Fact(DisplayName = "Result.From with throwing Func should return failed")] | ||
public void ThrowFuncResult() | ||
{ | ||
var result = Result.From<int>(() => throw new TestException("BOOM")); | ||
|
||
result.IsSuccess.Should().BeFalse(); | ||
result.Exception.Should().NotBeNull(); | ||
result.Exception.Should().BeOfType<TestException>(); | ||
} | ||
|
||
[Fact(DisplayName = "Result.FromTask with successful task should return success")] | ||
public void SuccessfulTaskResult() | ||
{ | ||
var task = CompletedTask(1); | ||
var result = Result.FromTask(task); | ||
|
||
result.IsSuccess.Should().BeTrue(); | ||
result.Value.Should().Be(1); | ||
result.Exception.Should().BeNull(); | ||
} | ||
|
||
[Fact(DisplayName = "Result.FromTask with faulted task should return failed")] | ||
public void FaultedTaskResult() | ||
{ | ||
var task = FaultedTask(1); | ||
var result = Result.FromTask(task); | ||
|
||
result.IsSuccess.Should().BeFalse(); | ||
result.Exception.Should().NotBeNull(); | ||
result.Exception.Should().BeOfType<AggregateException>() | ||
.Which.InnerException.Should().BeOfType<TestException>(); | ||
} | ||
|
||
[Fact(DisplayName = "Result.FromTask with cancelled task should return failed")] | ||
public void CancelledTaskResult() | ||
{ | ||
var task = CancelledTask(1); | ||
var result = Result.FromTask(task); | ||
|
||
result.IsSuccess.Should().BeFalse(); | ||
result.Exception.Should().NotBeNull(); | ||
result.Exception.Should().BeOfType<TaskCanceledException>(); | ||
} | ||
|
||
[Fact(DisplayName = "Result.FromTask with incomplete task should throw")] | ||
public void IncompleteTaskResult() | ||
{ | ||
var tcs = new TaskCompletionSource<int>(); | ||
Invoking(() => Result.FromTask(tcs.Task)) | ||
.Should().Throw<ArgumentException>().WithMessage("Task is not completed.*"); | ||
} | ||
|
||
private static Task<int> CompletedTask(int n) | ||
{ | ||
var tcs = new TaskCompletionSource<int>(); | ||
Task.Run(async () => | ||
{ | ||
await Task.Yield(); | ||
tcs.TrySetResult(n); | ||
}); | ||
tcs.Task.Wait(); | ||
return tcs.Task; | ||
} | ||
|
||
private static Task<int> CancelledTask(int n) | ||
{ | ||
var tcs = new TaskCompletionSource<int>(); | ||
Task.Run(async () => | ||
{ | ||
await Task.Yield(); | ||
tcs.TrySetCanceled(); | ||
}); | ||
|
||
try | ||
{ | ||
tcs.Task.Wait(); | ||
} | ||
catch | ||
{ | ||
// no-op | ||
} | ||
|
||
return tcs.Task; | ||
} | ||
|
||
private static Task<int> FaultedTask(int n) | ||
{ | ||
var tcs = new TaskCompletionSource<int>(); | ||
Task.Run(async () => | ||
{ | ||
await Task.Yield(); | ||
try | ||
{ | ||
throw new TestException("BOOM"); | ||
} | ||
catch (Exception ex) | ||
{ | ||
tcs.TrySetException(ex); | ||
} | ||
}); | ||
|
||
try | ||
{ | ||
tcs.Task.Wait(); | ||
} | ||
catch | ||
{ | ||
// no-op | ||
} | ||
|
||
return tcs.Task; | ||
} | ||
|
||
private class TestException: Exception | ||
{ | ||
public TestException(string message) : base(message) | ||
{ | ||
} | ||
|
||
public TestException(string message, Exception innerException) : base(message, innerException) | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -137,7 +137,30 @@ public static Result<T> Failure<T>(Exception exception) | |
/// <returns>TBD</returns> | ||
public static Result<T> FromTask<T>(Task<T> task) | ||
{ | ||
return task.IsCanceled || task.IsFaulted ? new Result<T>(task.Exception) : new Result<T>(task.Result); | ||
if(!task.IsCompleted) | ||
throw new ArgumentException("Task is not completed. Result.FromTask only accepts completed tasks.", nameof(task)); | ||
|
||
if(task.Exception is not null) | ||
return new Result<T>(task.Exception); | ||
|
||
if (task.IsCanceled && task.Exception is null) | ||
{ | ||
try | ||
{ | ||
_ = task.GetAwaiter().GetResult(); | ||
} | ||
catch(Exception e) | ||
{ | ||
return new Result<T>(e); | ||
} | ||
|
||
throw new InvalidOperationException("Should never reach this line!"); | ||
} | ||
Comment on lines
+146
to
+158
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix, detect edge case where a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. LGTM |
||
|
||
if(task.IsFaulted && task.Exception is null) | ||
throw new InvalidOperationException("Should never happen! something is wrong with .NET Task code!"); | ||
|
||
return new Result<T>(task.Result); | ||
} | ||
|
||
/// <summary> | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New behavior,
.FromTask()
did not check if theTask
argument is completed or not, it should only accept completed tasks