Skip to content

Commit 3c98459

Browse files
committed
Fix: Some renames
1 parent 42db66a commit 3c98459

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

src/CodeOfChaos.Types/AsynLazy.cs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,38 @@ namespace System;
99
public class AsyncLazy<T>(Func<CancellationToken, Task<T>> valueFactory) : IAsyncDisposable {
1010
private readonly SemaphoreSlim _semaphore = new(1, 1);
1111
private Task<T>? _value;
12+
private bool _disposed;
1213

1314
// -----------------------------------------------------------------------------------------------------------------
1415
// Methods
1516
// -----------------------------------------------------------------------------------------------------------------
16-
public async Task<T> GetValueAsync(CancellationToken ct = default) {
17+
public async ValueTask<T> GetValueAsync(CancellationToken ct = default) {
1718
ct.ThrowIfCancellationRequested();
1819

19-
Task<T>? value = _value;
20-
if (value != null) return await value.ConfigureAwait(false);
20+
21+
if (_value is {} value ) return await value.ConfigureAwait(false);
2122

2223
await _semaphore.WaitAsync(ct).ConfigureAwait(false);
24+
2325
try {
24-
value = _value;
25-
if (value == null) {
26-
_value = value = valueFactory(ct);
27-
}
26+
// Check once more within the semaphore lock
27+
_value ??= valueFactory(ct);
28+
}
29+
catch (Exception ex) {
30+
_value = Task.FromException<T>(ex);
31+
throw;
2832
}
2933
finally {
3034
_semaphore.Release();
3135
}
3236

33-
return await value.ConfigureAwait(false);
37+
return await _value.ConfigureAwait(false);
3438
}
3539

3640
public async ValueTask DisposeAsync() {
41+
if (_disposed) return;
42+
_disposed = true;
43+
3744
// First do all the regular stuff
3845
_semaphore.Dispose();
3946
GC.SuppressFinalize(this);

src/CodeOfChaos.Types/SemanticVersion.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ namespace CodeOfChaos.Types;
2121
/// The Addendum is allowed to contain any information.
2222
/// </remarks>
2323
[StructLayout(LayoutKind.Sequential)]
24-
public readonly struct SemanticVersion(int major, int minor, int patch, string? addendum = null) : IComparable<SemanticVersion>, IEquatable<SemanticVersion> {
24+
public readonly struct SemanticVersion(int major, int minor, int patch, string? addendum = null) :
25+
IComparable<SemanticVersion>,
26+
IEquatable<SemanticVersion>
27+
{
2528
[XmlIgnore] public int Major { get; } = major;
2629
[XmlIgnore] public int Minor { get; } = minor;
2730
[XmlIgnore] public int Patch { get; } = patch;

tests/Tests.CodeOfChaos.Types/AsyncLazyTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace Tests.CodeOfChaos.Types;
1313
[TestSubject(typeof(AsyncLazy<>))]
1414
public class AsyncLazyTests {
1515
[Test]
16-
public async Task GetValueAsync_ShouldReturnValue_FromTestoryMethod() {
16+
public async Task GetValueAsync_ShouldReturnValue_FromFactoryMethod() {
1717
// Arrange
1818
int expectedValue = 42;
1919
var lazy = new AsyncLazy<int>(_ => Task.FromResult(expectedValue));
@@ -53,7 +53,7 @@ public async Task GetValueAsync_ShouldHandleCancellationToken() {
5353

5454
// Act & Assert
5555
await cts.CancelAsync();
56-
await Assert.ThrowsAsync<OperationCanceledException>(() => lazy.GetValueAsync(cts.Token));
56+
await Assert.ThrowsAsync<OperationCanceledException>(async () => await lazy.GetValueAsync(cts.Token));
5757
}
5858

5959
[Test]
@@ -118,7 +118,7 @@ public async Task GetValueAsync_ShouldEnsureAtomicity() {
118118

119119
// Act
120120
for (int i = 0; i < count; i++) {
121-
tasks.Add(lazy.GetValueAsync());// Initiate concurrent access to the value
121+
tasks.Add(lazy.GetValueAsync().AsTask());// Initiate concurrent access to the value
122122
}
123123

124124
int[] results = await Task.WhenAll(tasks);

0 commit comments

Comments
 (0)