-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add
TotalCount
to Statistics (#604)
* Add TotalCount to Statistics * Move FileSystemRegistration to "Helpers" namespace
- Loading branch information
Showing
28 changed files
with
537 additions
and
502 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
83 changes: 83 additions & 0 deletions
83
Source/Testably.Abstractions.Testing/Helpers/FileSystemRegistration.cs
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,83 @@ | ||
using System; | ||
using System.Threading; | ||
using Testably.Abstractions.Testing.Statistics; | ||
|
||
namespace Testably.Abstractions.Testing.Helpers; | ||
|
||
internal sealed class FileSystemRegistration : IStatisticsGate | ||
{ | ||
private static readonly AsyncLocal<bool> IsDisabled = new(); | ||
private static readonly AsyncLocal<bool> IsInit = new(); | ||
|
||
/// <summary> | ||
/// The total count of registered statistic calls. | ||
/// </summary> | ||
public int TotalCount => _counter; | ||
|
||
private int _counter; | ||
|
||
#region IStatisticsGate Members | ||
|
||
/// <inheritdoc cref="IStatisticsGate.GetCounter()" /> | ||
public int GetCounter() | ||
{ | ||
return Interlocked.Increment(ref _counter); | ||
} | ||
|
||
/// <inheritdoc cref="IStatisticsGate.TryGetLock(out IDisposable)" /> | ||
public bool TryGetLock(out IDisposable release) | ||
{ | ||
if (IsDisabled.Value) | ||
{ | ||
release = TemporaryDisable.None; | ||
return false; | ||
} | ||
|
||
IsDisabled.Value = true; | ||
release = new TemporaryDisable(() => IsDisabled.Value = false); | ||
return true; | ||
} | ||
|
||
#endregion | ||
|
||
/// <summary> | ||
/// Ignores all registrations until the return value is disposed. | ||
/// </summary> | ||
internal IDisposable Ignore() | ||
{ | ||
if (IsDisabled.Value) | ||
{ | ||
return TemporaryDisable.None; | ||
} | ||
|
||
IsDisabled.Value = true; | ||
IsInit.Value = true; | ||
return new TemporaryDisable(() => | ||
{ | ||
IsDisabled.Value = false; | ||
IsInit.Value = false; | ||
}); | ||
} | ||
|
||
internal bool IsInitializing() | ||
=> IsInit.Value; | ||
|
||
private sealed class TemporaryDisable : IDisposable | ||
{ | ||
public static IDisposable None { get; } = new NoOpDisposable(); | ||
|
||
private readonly Action _onDispose; | ||
|
||
public TemporaryDisable(Action onDispose) | ||
{ | ||
_onDispose = onDispose; | ||
} | ||
|
||
#region IDisposable Members | ||
|
||
/// <inheritdoc cref="IDisposable.Dispose()" /> | ||
public void Dispose() => _onDispose(); | ||
|
||
#endregion | ||
} | ||
} |
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
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
Oops, something went wrong.