-
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.
Minor changes related to ConfigureAwait(false).
- Loading branch information
Showing
9 changed files
with
136 additions
and
59 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
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 CoreRemoting.Toolbox; | ||
using System; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace CoreRemoting.Tests | ||
{ | ||
public partial 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 void Disposable_ignores_nulls() | ||
{ | ||
Action dispose = null; | ||
|
||
using (Disposable.Create(dispose)) | ||
{ | ||
// doesn't throw | ||
} | ||
} | ||
|
||
[Fact] | ||
public void Disposable_combines_disposables() | ||
{ | ||
var count = 0; | ||
void Dispose() => | ||
count++; | ||
|
||
var d1 = Disposable.Create(Dispose); | ||
var d2 = Disposable.Create(Dispose); | ||
var d3 = Disposable.Create(Dispose); | ||
|
||
using (Disposable.Create(d1, d2, d3)) | ||
Assert.Equal(0, count); | ||
|
||
Assert.Equal(3, count); | ||
} | ||
} | ||
} |
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
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,39 @@ | ||
using System; | ||
|
||
namespace CoreRemoting.Toolbox | ||
{ | ||
/// <summary> | ||
/// Helper class to create disposable primitives. | ||
/// </summary> | ||
public static partial 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 ParamsDisposable(params IDisposable[] disposables) : IDisposable | ||
{ | ||
void IDisposable.Dispose() | ||
{ | ||
foreach (var disposable in disposables ?? []) | ||
disposable?.Dispose(); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Creates a disposable object. | ||
/// </summary> | ||
/// <param name="disposables">Disposable items to dispose on disposal.</param> | ||
public static IDisposable Create(params IDisposable[] disposables) => | ||
new ParamsDisposable(disposables); | ||
} | ||
} |