Skip to content

Commit 17220a4

Browse files
committed
various cleanup
1 parent 4c68852 commit 17220a4

File tree

4 files changed

+20
-19
lines changed

4 files changed

+20
-19
lines changed

src/Abstract/IAsyncSingleton{T}.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public interface IAsyncSingleton<T> : IDisposable, IAsyncDisposable
3232
ValueTask<T> Get(CancellationToken cancellationToken, params object[] objects);
3333

3434
/// <summary>
35-
/// <see cref="Get"/> should be used instead of this if possible. This method can block the calling thread! It's lazy; it's initialized only when retrieving. <para/>
35+
/// Get should be used instead of this if possible. This method can block the calling thread! It's lazy; it's initialized only when retrieving. <para/>
3636
/// This can still be used with an async initialization func, but it will block on the func.
3737
/// </summary>
3838
/// <remarks>The initialization func needs to be set before calling this, either in the ctor or via the other methods</remarks>

src/AsyncSingleton.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
namespace Soenneker.Utils.AsyncSingleton;
1010

1111
///<inheritdoc cref="IAsyncSingleton"/>
12-
public class AsyncSingleton : IAsyncSingleton
12+
public sealed class AsyncSingleton : IAsyncSingleton
1313
{
1414
private object? _instance;
1515
private readonly AsyncLock _lock = new();
@@ -263,8 +263,6 @@ public void Dispose()
263263
// Clear the instance explicitly to allow for garbage collection.
264264
_instance = null;
265265
}
266-
267-
GC.SuppressFinalize(this);
268266
}
269267

270268
public async ValueTask DisposeAsync()
@@ -278,7 +276,6 @@ public async ValueTask DisposeAsync()
278276

279277
if (localInstance is null)
280278
{
281-
GC.SuppressFinalize(this);
282279
return;
283280
}
284281

@@ -295,7 +292,5 @@ public async ValueTask DisposeAsync()
295292

296293
// Clear the instance reference explicitly to assist GC
297294
_instance = null;
298-
299-
GC.SuppressFinalize(this);
300295
}
301296
}

test/Soenneker.Utils.AsyncSingleton.Tests/AsyncSingletonTests.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ public async Task Get_in_parallel_should_return_both_instances()
3535

3636
await httpClientSingleton.Init();
3737

38-
Task t1 = Task.Run(async () => await httpClientSingleton.Init());
39-
Task t2 = Task.Run(async () => await httpClientSingleton.Init());
38+
Task t1 = Task.Run(async () => await httpClientSingleton.Init(), TestContext.Current.CancellationToken);
39+
Task t2 = Task.Run(async () => await httpClientSingleton.Init(), TestContext.Current.CancellationToken);
4040

4141
await Task.WhenAll(t1, t2);
4242
}
@@ -104,13 +104,17 @@ public async Task DisposeAsync_with_cancellationToken_with_nondisposable_should_
104104
[Fact]
105105
public async Task Async_with_object_and_cancellationToken_should_not_throw()
106106
{
107-
var httpClientSingleton = new AsyncSingleton(async (token, _) => new object());
107+
var httpClientSingleton = new AsyncSingleton(async (token, _) =>
108+
{
109+
await Task.Delay(100, token);
110+
return new object();
111+
});
108112

109113
await httpClientSingleton.Init(CancellationToken.None, 3);
110114
}
111115

112116
[Fact]
113-
public async Task Sync_with_object_and_cancellationToken_should_not_throw()
117+
public void Sync_with_object_and_cancellationToken_should_not_throw()
114118
{
115119
var httpClientSingleton = new AsyncSingleton((token, obj) => new object());
116120

@@ -120,10 +124,11 @@ public async Task Sync_with_object_and_cancellationToken_should_not_throw()
120124
[Fact]
121125
public async Task Async_Get_should_only_initialize_once()
122126
{
123-
int x = 0;
127+
var x = 0;
124128

125129
var httpClientSingleton = new AsyncSingleton(async () =>
126130
{
131+
await Task.Delay(100);
127132
x++;
128133
return new HttpClient();
129134
});
@@ -136,7 +141,7 @@ public async Task Async_Get_should_only_initialize_once()
136141
[Fact]
137142
public async Task Sync_Get_Async_should_only_initialize_once()
138143
{
139-
int x = 0;
144+
var x = 0;
140145

141146
var httpClientSingleton = new AsyncSingleton(() =>
142147
{
@@ -153,7 +158,7 @@ public async Task Sync_Get_Async_should_only_initialize_once()
153158
[Fact]
154159
public void Sync_Get_Sync_should_only_initialize_once()
155160
{
156-
int x = 0;
161+
var x = 0;
157162

158163
var httpClientSingleton = new AsyncSingleton(() =>
159164
{

test/Soenneker.Utils.AsyncSingleton.Tests/AsyncSingleton{T}Tests.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,13 @@ public async Task DisposeAsync_with_cancellationToken_with_nondisposable_should_
113113
[Fact]
114114
public async Task Async_with_object_and_cancellationToken_should_not_throw()
115115
{
116-
var httpClientSingleton = new AsyncSingleton<object>(async (token, obj) => new object());
116+
var httpClientSingleton = new AsyncSingleton<object>(async (token, obj) => { await Task.Delay(100, token); return new object(); });
117117

118118
_ = await httpClientSingleton.Get(CancellationToken.None, 3);
119119
}
120120

121121
[Fact]
122-
public async Task Sync_with_object_and_cancellationToken_should_not_throw()
122+
public void Sync_with_object_and_cancellationToken_should_not_throw()
123123
{
124124
var httpClientSingleton = new AsyncSingleton<object>((token, obj) => new object());
125125

@@ -129,10 +129,11 @@ public async Task Sync_with_object_and_cancellationToken_should_not_throw()
129129
[Fact]
130130
public async Task Async_Get_should_only_initialize_once()
131131
{
132-
int x = 0;
132+
var x = 0;
133133

134134
var httpClientSingleton = new AsyncSingleton<HttpClient>(async () =>
135135
{
136+
await Task.Delay(100);
136137
x++;
137138
return new HttpClient();
138139
});
@@ -146,7 +147,7 @@ public async Task Async_Get_should_only_initialize_once()
146147
[Fact]
147148
public async Task Sync_Get_Async_should_only_initialize_once()
148149
{
149-
int x = 0;
150+
var x = 0;
150151

151152
var httpClientSingleton = new AsyncSingleton<HttpClient>(() =>
152153
{
@@ -163,7 +164,7 @@ public async Task Sync_Get_Async_should_only_initialize_once()
163164
[Fact]
164165
public void Sync_Get_Sync_should_only_initialize_once()
165166
{
166-
int x = 0;
167+
var x = 0;
167168

168169
var httpClientSingleton = new AsyncSingleton<HttpClient>(() =>
169170
{

0 commit comments

Comments
 (0)