-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement tests to prevent syncronizecontext deadlocks (#18)
Co-authored-by: andrew-cat <[email protected]>
- Loading branch information
Showing
5 changed files
with
243 additions
and
18 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
114 changes: 114 additions & 0 deletions
114
src/ConfigCat.Client.Tests/SynchronizationContextDeadlockTests.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,114 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using System; | ||
using System.Threading; | ||
using Moq; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Threading.Tasks; | ||
|
||
namespace ConfigCat.Client.Tests | ||
{ | ||
[TestClass] | ||
public class SynchronizationContextDeadlockTests | ||
{ | ||
private const string SDKKEY = "PKDVCLf-Hq-h-kCzMp-L7Q/psuH7BGHoUmdONrzzUOY7A"; | ||
|
||
private readonly Mock<SynchronizationContext> syncContextMock; | ||
|
||
private static SynchronizationContext synchronizationContextBackup; | ||
|
||
public SynchronizationContextDeadlockTests() | ||
{ | ||
synchronizationContextBackup = SynchronizationContext.Current; | ||
|
||
syncContextMock = new Mock<SynchronizationContext> | ||
{ | ||
CallBase = true | ||
}; | ||
|
||
SynchronizationContext.SetSynchronizationContext(syncContextMock.Object); | ||
} | ||
|
||
[ClassCleanup] | ||
public static void ClassCleanup() | ||
{ | ||
SynchronizationContext.SetSynchronizationContext(synchronizationContextBackup); | ||
} | ||
|
||
[TestInitialize] | ||
public void TestInitialize() | ||
{ | ||
syncContextMock.Reset(); | ||
} | ||
|
||
[TestMethod] | ||
public void AutoPollDeadLockCheck() | ||
{ | ||
var client = new ConfigCatClient(new AutoPollConfiguration | ||
{ | ||
SdkKey = SDKKEY, | ||
Logger = new ConsoleLogger(LogLevel.Off) | ||
}); | ||
|
||
ClientDeadlockCheck(client); | ||
} | ||
|
||
[TestMethod] | ||
public void ManualPollDeadLockCheck() | ||
{ | ||
var client = new ConfigCatClient(new ManualPollConfiguration | ||
{ | ||
SdkKey = SDKKEY, | ||
Logger = new ConsoleLogger(LogLevel.Off) | ||
}); | ||
|
||
ClientDeadlockCheck(client); | ||
} | ||
|
||
[TestMethod] | ||
public void LazyLoadDeadLockCheck() | ||
{ | ||
var client = new ConfigCatClient(new LazyLoadConfiguration | ||
{ | ||
SdkKey = SDKKEY, | ||
Logger = new ConsoleLogger(LogLevel.Off) | ||
}); | ||
|
||
ClientDeadlockCheck(client); | ||
} | ||
|
||
private void ClientDeadlockCheck(IConfigCatClient client) | ||
{ | ||
var methods = typeof(IConfigCatClient).GetMethods().Where(x => !x.IsSpecialName).OrderBy(o => o.Name); | ||
|
||
foreach (var m in methods) | ||
{ | ||
var parameters = Enumerable.Repeat<object>(null, m.GetParameters().Length).ToArray(); | ||
|
||
MethodInfo mi = m; | ||
|
||
if (m.IsGenericMethod) | ||
{ | ||
mi = m.MakeGenericMethod(typeof(string)); | ||
} | ||
|
||
Console.WriteLine($"Invoke '{mi.Name}' method"); | ||
|
||
if (mi.ReturnType.IsSubclassOf(typeof(Task))) | ||
{ | ||
var task = (Task)mi.Invoke(client, parameters); | ||
|
||
task.ConfigureAwait(false); | ||
task.Wait(); | ||
} | ||
else | ||
{ | ||
mi.Invoke(client, parameters); | ||
} | ||
|
||
syncContextMock.Verify(x => x.Post(It.IsAny<SendOrPostCallback>(), It.IsAny<object>()), Times.Never, $"Method: {mi.Name}"); | ||
syncContextMock.Verify(x => x.Send(It.IsAny<SendOrPostCallback>(), It.IsAny<object>()), Times.Never, $"Method: {mi.Name}"); | ||
} | ||
} | ||
} | ||
} |
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