-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
All RPC message handling methods have access to RemotingSession.Curre…
…nt, close 98.
- Loading branch information
Showing
5 changed files
with
119 additions
and
18 deletions.
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
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,57 @@ | ||
using CoreRemoting.Toolbox; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace CoreRemoting.Tests | ||
{ | ||
public class DisposableTests | ||
{ | ||
[Fact] | ||
public void Disposable_executes_action_on_Dispose() | ||
{ | ||
var disposed = false; | ||
|
||
void Dispose() => | ||
disposed = true; | ||
|
||
using (Disposable.Create(Dispose)) | ||
Assert.False(disposed); | ||
|
||
Assert.True(disposed); | ||
} | ||
|
||
[Fact] | ||
public async Task AsyncDisposable_executes_action_on_DisposeAsync() | ||
{ | ||
var disposed = false; | ||
|
||
async Task DisposeTask() | ||
{ | ||
await Task.Yield(); | ||
disposed = true; | ||
} | ||
|
||
await using (Disposable.Create(DisposeTask)) | ||
Assert.False(disposed); | ||
|
||
Assert.True(disposed); | ||
} | ||
|
||
[Fact] | ||
public async Task AsyncTaskDisposable_executes_action_on_DisposeAsync() | ||
{ | ||
var disposed = false; | ||
|
||
async ValueTask DisposeAsync() | ||
{ | ||
await Task.Yield(); | ||
disposed = true; | ||
} | ||
|
||
await using (Disposable.Create(DisposeAsync)) | ||
Assert.False(disposed); | ||
|
||
Assert.True(disposed); | ||
} | ||
} | ||
} |
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
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
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,52 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace CoreRemoting.Toolbox | ||
{ | ||
/// <summary> | ||
/// Helper class to create disposable primitives. | ||
/// </summary> | ||
public static class Disposable | ||
{ | ||
private class SyncDisposable(Action disposeAction) : IDisposable | ||
{ | ||
void IDisposable.Dispose() => | ||
disposeAction?.Invoke(); | ||
} | ||
|
||
/// <summary> | ||
/// Creates a disposable object. | ||
/// </summary> | ||
/// <param name="disposeAction">An action to invoke on disposal.</param> | ||
public static IDisposable Create(Action disposeAction) => | ||
new SyncDisposable(disposeAction); | ||
|
||
private class AsyncDisposable( | ||
Func<ValueTask> disposeAsync, | ||
Func<Task> disposeTaskAsync) : IAsyncDisposable | ||
{ | ||
async ValueTask IAsyncDisposable.DisposeAsync() | ||
{ | ||
if (disposeAsync != null) | ||
await disposeAsync(); | ||
|
||
if (disposeTaskAsync != null) | ||
await disposeTaskAsync(); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Creates an asynchronous disposable object. | ||
/// </summary> | ||
/// <param name="disposeAsync">An action to invoke on disposal.</param> | ||
public static IAsyncDisposable Create(Func<ValueTask> disposeAsync) => | ||
new AsyncDisposable(disposeAsync, null); | ||
|
||
/// <summary> | ||
/// Creates an asynchronous disposable object. | ||
/// </summary> | ||
/// <param name="disposeAsync">An action to invoke on disposal.</param> | ||
public static IAsyncDisposable Create(Func<Task> disposeAsync) => | ||
new AsyncDisposable(null, disposeAsync); | ||
} | ||
} |