public void RegisteredCallbacks_AreInvokedExactlyOnce() { // Arrange var firstCancellationTokenSource = new CancellationTokenSource(); var secondCancellationTokenSource = new CancellationTokenSource(); var thirdCancellationTokenSource = new CancellationTokenSource(); var firstCancellationToken = firstCancellationTokenSource.Token; var secondCancellationToken = secondCancellationTokenSource.Token; var thirdCancellationToken = thirdCancellationTokenSource.Token; var firstCancellationChangeToken = new CancellationChangeToken(firstCancellationToken); var secondCancellationChangeToken = new CancellationChangeToken(secondCancellationToken); var thirdCancellationChangeToken = new CancellationChangeToken(thirdCancellationToken); var compositeChangeToken = new CompositeChangeToken(new List <IChangeToken> { firstCancellationChangeToken, secondCancellationChangeToken, thirdCancellationChangeToken }); var count1 = 0; var count2 = 0; compositeChangeToken.RegisterChangeCallback(_ => count1++, null); compositeChangeToken.RegisterChangeCallback(_ => count2++, null); // Act firstCancellationTokenSource.Cancel(); secondCancellationTokenSource.Cancel(); // Assert Assert.Equal(1, count1); Assert.Equal(1, count2); }
public async Task RegisteredCallbackGetsInvokedExactlyOnce_WhenMultipleConcurrentChangeEventsOccur() { // Arrange var event1 = new ManualResetEvent(false); var event2 = new ManualResetEvent(false); var event3 = new ManualResetEvent(false); var cancellationTokenSource = new CancellationTokenSource(); var cancellationToken = cancellationTokenSource.Token; var cancellationChangeToken = new CancellationChangeToken(cancellationToken); var count = 0; Action <object> callback = _ => { count++; event3.Set(); event1.WaitOne(5000); }; var compositeChangeToken = new CompositeChangeToken(new List <IChangeToken> { cancellationChangeToken }); compositeChangeToken.RegisterChangeCallback(callback, null); // Act var firstChange = Task.Run(() => { event2.WaitOne(5000); cancellationTokenSource.Cancel(); }); var secondChange = Task.Run(() => { event3.WaitOne(5000); cancellationTokenSource.Cancel(); event1.Set(); }); event2.Set(); await Task.WhenAll(firstChange, secondChange); // Assert Assert.Equal(1, count); }
public void ShouldNotCollectDisposablesIfChangedTokenEncountered() { // Arrange var firstCancellationTokenSource = new CancellationTokenSource(); var secondCancellationTokenSource = new CancellationTokenSource(); var thirdCancellationTokenSource = new CancellationTokenSource(); var count = 0; var compositeChangeToken = new CompositeChangeToken(new List <IChangeToken> { new ProxyCancellationChangeToken(firstCancellationTokenSource.Token, disposing: () => count++), new ProxyCancellationChangeToken(secondCancellationTokenSource.Token, disposing: () => count++), new ProxyCancellationChangeToken(thirdCancellationTokenSource.Token, disposing: () => count++) }); // Act firstCancellationTokenSource.Cancel(); compositeChangeToken.RegisterChangeCallback(_ => { }, null); secondCancellationTokenSource.Cancel(); // Assert Assert.Equal(1, count); }