public async Task GetTwitterUsersAsync_Cancellation_Test()
        {
            #region Stubs
            var buffer    = new BufferBlock <SyncTwitterUser[]>();
            var canTokenS = new CancellationTokenSource();
            canTokenS.Cancel();

            var maxUsers = 1000;
            #endregion

            #region Mocks
            var maxUsersNumberProviderMock = new Mock <IMaxUsersNumberProvider>(MockBehavior.Strict);
            maxUsersNumberProviderMock
            .Setup(x => x.GetMaxUsersNumberAsync())
            .ReturnsAsync(maxUsers);

            var twitterUserDalMock = new Mock <ITwitterUserDal>(MockBehavior.Strict);

            var loggerMock = new Mock <ILogger <RetrieveTwitterUsersProcessor> >();
            #endregion

            var processor = new RetrieveTwitterUsersProcessor(twitterUserDalMock.Object, maxUsersNumberProviderMock.Object, loggerMock.Object);
            processor.WaitFactor = 1;
            await processor.GetTwitterUsersAsync(buffer, canTokenS.Token);
        }
        public async Task GetTwitterUsersAsync_Exception_Test()
        {
            #region Stubs
            var buffer = new BufferBlock <SyncTwitterUser[]>();

            var maxUsers = 1000;
            #endregion

            #region Mocks
            var maxUsersNumberProviderMock = new Mock <IMaxUsersNumberProvider>(MockBehavior.Strict);
            maxUsersNumberProviderMock
            .Setup(x => x.GetMaxUsersNumberAsync())
            .ReturnsAsync(maxUsers);

            var twitterUserDalMock = new Mock <ITwitterUserDal>(MockBehavior.Strict);
            twitterUserDalMock
            .Setup(x => x.GetAllTwitterUsersAsync(
                       It.Is <int>(y => y == maxUsers)))
            .Returns(async() => await DelayFaultedTask <SyncTwitterUser[]>(new Exception()));

            var loggerMock = new Mock <ILogger <RetrieveTwitterUsersProcessor> >();
            #endregion

            var processor = new RetrieveTwitterUsersProcessor(twitterUserDalMock.Object, maxUsersNumberProviderMock.Object, loggerMock.Object);
            processor.WaitFactor = 10;
            var t = processor.GetTwitterUsersAsync(buffer, CancellationToken.None);

            await Task.WhenAny(t, Task.Delay(50));

            #region Validations
            maxUsersNumberProviderMock.VerifyAll();
            twitterUserDalMock.VerifyAll();
            Assert.AreEqual(0, buffer.Count);
            #endregion
        }
コード例 #3
0
        public async Task GetTwitterUsersAsync_Multi2_Test()
        {
            #region Stubs
            var buffer = new BufferBlock <SyncTwitterUser[]>();
            var users  = new List <SyncTwitterUser>();

            for (var i = 0; i < 31; i++)
            {
                users.Add(new SyncTwitterUser());
            }

            var maxUsers = 1000;
            #endregion

            #region Mocks
            var maxUsersNumberProviderMock = new Mock <IMaxUsersNumberProvider>(MockBehavior.Strict);
            maxUsersNumberProviderMock
            .Setup(x => x.GetMaxUsersNumberAsync())
            .ReturnsAsync(maxUsers);

            var twitterUserDalMock = new Mock <ITwitterUserDal>(MockBehavior.Strict);
            twitterUserDalMock
            .SetupSequence(x => x.GetAllTwitterUsersAsync(
                               It.Is <int>(y => y == maxUsers)))
            .ReturnsAsync(users.ToArray())
            .ReturnsAsync(new SyncTwitterUser[0])
            .ReturnsAsync(new SyncTwitterUser[0])
            .ReturnsAsync(new SyncTwitterUser[0])
            .ReturnsAsync(new SyncTwitterUser[0]);

            var loggerMock = new Mock <ILogger <RetrieveTwitterUsersProcessor> >();
            #endregion

            var processor = new RetrieveTwitterUsersProcessor(twitterUserDalMock.Object, maxUsersNumberProviderMock.Object, loggerMock.Object);
            processor.WaitFactor = 2;
            var t  = processor.GetTwitterUsersAsync(buffer, CancellationToken.None);
            var t2 = Task.Run(async() =>
            {
                while (buffer.Count < 11)
                {
                    await Task.Delay(50);
                }
            });

            await Task.WhenAny(t, t2, Task.Delay(5000));

            #region Validations
            maxUsersNumberProviderMock.VerifyAll();
            twitterUserDalMock.VerifyAll();
            Assert.AreEqual(11, buffer.Count);
            buffer.TryReceive(out var result);
            Assert.AreEqual(3, result.Length);
            #endregion
        }
        public async Task GetTwitterUsersAsync_Test()
        {
            #region Stubs
            var buffer = new BufferBlock <SyncTwitterUser[]>();
            var users  = new[]
            {
                new SyncTwitterUser(),
                new SyncTwitterUser(),
                new SyncTwitterUser(),
            };
            var maxUsers = 1000;
            #endregion

            #region Mocks
            var maxUsersNumberProviderMock = new Mock <IMaxUsersNumberProvider>(MockBehavior.Strict);
            maxUsersNumberProviderMock
            .Setup(x => x.GetMaxUsersNumberAsync())
            .ReturnsAsync(maxUsers);

            var twitterUserDalMock = new Mock <ITwitterUserDal>(MockBehavior.Strict);
            twitterUserDalMock
            .Setup(x => x.GetAllTwitterUsersAsync(
                       It.Is <int>(y => y == maxUsers)))
            .ReturnsAsync(users);

            var loggerMock = new Mock <ILogger <RetrieveTwitterUsersProcessor> >();
            #endregion

            var processor = new RetrieveTwitterUsersProcessor(twitterUserDalMock.Object, maxUsersNumberProviderMock.Object, loggerMock.Object);
            processor.WaitFactor = 10;
            processor.GetTwitterUsersAsync(buffer, CancellationToken.None);

            await Task.Delay(50);

            #region Validations
            maxUsersNumberProviderMock.VerifyAll();
            twitterUserDalMock.VerifyAll();
            Assert.AreEqual(3, buffer.Count);
            buffer.TryReceive(out var result);
            Assert.AreEqual(1, result.Length);
            #endregion
        }