Пример #1
0
        public void RaiseChangeEvents_CancelsCancellationTokenSourceForExpiredTokens()
        {
            // Arrange
            var cts1 = new CancellationTokenSource();
            var cts2 = new CancellationTokenSource();
            var cts3 = new CancellationTokenSource();

            var token1 = new TestPollingChangeToken {
                Id = 1, CancellationTokenSource = cts1
            };
            var token2 = new TestPollingChangeToken {
                Id = 2, HasChanged = true, CancellationTokenSource = cts2
            };
            var token3 = new TestPollingChangeToken {
                Id = 3, CancellationTokenSource = cts3
            };

            var tokens = new ConcurrentDictionary <IPollingChangeToken, IPollingChangeToken>
            {
                [token1] = token1,
                [token2] = token2,
                [token3] = token3,
            };

            // Act
            PhysicalFilesWatcher.RaiseChangeEvents(tokens);

            // Assert
            Assert.False(cts1.IsCancellationRequested);
            Assert.False(cts3.IsCancellationRequested);
            Assert.True(cts2.IsCancellationRequested);

            // Ensure token2 is removed from the collection.
            Assert.Equal(new[] { token1, token3, }, tokens.Keys.OfType <TestPollingChangeToken>().OrderBy(t => t.Id));
        }
Пример #2
0
        public void RaiseChangeEvents_CancelsAndRemovesMultipleChangedTokens()
        {
            // Arrange
            var cts1 = new CancellationTokenSource();
            var cts2 = new CancellationTokenSource();
            var cts3 = new CancellationTokenSource();
            var cts4 = new CancellationTokenSource();
            var cts5 = new CancellationTokenSource();

            var token1 = new TestPollingChangeToken {
                Id = 1, HasChanged = true, CancellationTokenSource = cts1
            };
            var token2 = new TestPollingChangeToken {
                Id = 2, CancellationTokenSource = cts2
            };
            var token3 = new TestPollingChangeToken {
                Id = 3, CancellationTokenSource = cts3
            };
            var token4 = new TestPollingChangeToken {
                Id = 4, HasChanged = true, CancellationTokenSource = cts4
            };
            var token5 = new TestPollingChangeToken {
                Id = 5, HasChanged = true, CancellationTokenSource = cts5
            };

            var tokens = new ConcurrentDictionary <IPollingChangeToken, IPollingChangeToken>
            {
                [token1] = token1,
                [token2] = token2,
                [token3] = token3,
                [token4] = token4,
                [token5] = token5,
            };

            // Act
            PhysicalFilesWatcher.RaiseChangeEvents(tokens);

            // Assert
            Assert.False(cts2.IsCancellationRequested);
            Assert.False(cts3.IsCancellationRequested);

            Assert.True(cts1.IsCancellationRequested);
            Assert.True(cts4.IsCancellationRequested);
            Assert.True(cts5.IsCancellationRequested);

            // Ensure changed tokens are removed
            Assert.Equal(new[] { token2, token3, }, tokens.Keys.OfType <TestPollingChangeToken>().OrderBy(t => t.Id));
        }