-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure Singleton Closes Any Opened Object During Concurrent Open/Close (
#174) * Check if disposed after calling OnCreateAsync() * Verify singleton handles concurrent close/open
- Loading branch information
1 parent
c96c7cf
commit 7871912
Showing
2 changed files
with
69 additions
and
1 deletion.
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,63 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Microsoft.Azure.Amqp; | ||
using Xunit; | ||
|
||
namespace Test.Microsoft.Azure.Amqp | ||
{ | ||
public class SingletonTests | ||
{ | ||
[Fact] | ||
public async Task SingletonConcurrentCloseOpenTests() | ||
{ | ||
var createTcs = new TaskCompletionSource<object>(); | ||
var closeTcs = new TaskCompletionSource<object>(); | ||
|
||
var singleton = new SingletonTester(createTcs.Task, closeTcs.Task); | ||
|
||
var creating = singleton.GetOrCreateAsync(TimeSpan.FromSeconds(1)); | ||
var closing = singleton.CloseAsync(); | ||
|
||
closeTcs.SetResult(new object()); | ||
await closing; | ||
|
||
createTcs.SetResult(new object()); | ||
await creating; | ||
|
||
await Assert.ThrowsAsync<ObjectDisposedException>(() => singleton.GetOrCreateAsync(TimeSpan.FromSeconds(1))); | ||
|
||
var createdObj = GetInternalProperty<object>(singleton, "Value"); | ||
Assert.Null(createdObj); | ||
} | ||
|
||
private class SingletonTester : Singleton<object> | ||
{ | ||
private readonly Task<object> _onCreateComplete; | ||
private readonly Task<object> _onSafeCloseComplete; | ||
|
||
public SingletonTester(Task<object> onCreateComplete, Task<object> onSafeCloseComplete) | ||
{ | ||
_onCreateComplete = onCreateComplete; | ||
_onSafeCloseComplete = onSafeCloseComplete; | ||
} | ||
|
||
protected override async Task<object> OnCreateAsync(TimeSpan timeout) | ||
{ | ||
await _onCreateComplete; | ||
return new object(); | ||
} | ||
|
||
protected override void OnSafeClose(object value) | ||
{ | ||
_onSafeCloseComplete.GetAwaiter().GetResult(); | ||
} | ||
} | ||
|
||
private T GetInternalProperty<T>(object from, string propertyName) | ||
where T : class | ||
{ | ||
var prop = from.GetType().GetProperty(propertyName, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); | ||
return prop.GetValue(from) as T; | ||
} | ||
} | ||
} |