public async Task MultipleWaitTest() { const int key = 1337; using var dictionary = new WaitingDictionary <int, Mock>(); var waitTask1 = dictionary.WaitAsync(key); var waitTask2 = dictionary.WaitAsync(key); Assert.IsTrue(!waitTask1.IsCompletedSuccessfully); Assert.IsTrue(!waitTask2.IsCompletedSuccessfully); await dictionary.SetAsync(key, new Mock()); await Task.Delay(200); Assert.IsTrue(waitTask1.IsCompletedSuccessfully); Assert.IsTrue(waitTask2.IsFaulted); }
public async Task DuplicateTest() { const int key = 1337; using var dictionary = new WaitingDictionary <int, Mock>( new MiddlewareBuilder <Mock>() .RegisterDuplicateActionInSet((old, @new) => new Mock(old, @new)) ); var mock = new Mock(); await dictionary.SetAsync(key, mock); await dictionary.SetAsync(key, mock); var waitMock = await dictionary.WaitAsync(key); Assert.IsNotEmpty(waitMock.Nodes); }
public async Task NormalTest() { const int key = 1337; using var dictionary = new WaitingDictionary <int, Mock>( new MiddlewareBuilder <Mock>() .RegisterCompletionActionInSet(() => TestContext.WriteLine("Set completed")) .RegisterCompletionActionInWait(() => TestContext.WriteLine("Wait completed")) ); var waitTask = dictionary.WaitAsync(key); Assert.IsTrue(!waitTask.IsCompletedSuccessfully); var mock = new Mock(); await dictionary.SetAsync(key, mock); Assert.IsTrue(waitTask.IsCompletedSuccessfully); }
public async Task CancelTest(Type type) { const int key = 1337; using var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(100)); var dictionary = new WaitingDictionary <int, Mock>( new MiddlewareBuilder <Mock>() .RegisterCancellationActionInWait((tcs, hasOwnToken) => tcs.SetException((Exception)Activator.CreateInstance(type))) ); var waitTask = dictionary.WaitAsync(key, cts.Token); await Task.Delay(200, CancellationToken.None); try { await waitTask; } catch (Exception e) { Assert.IsTrue(e.GetType() == type); } }