Пример #1
0
        public async Task PublishMessageAfterRegister_RegisterMultipleTimes_ReceiverCountAsMuchAsRegisterTimes(int regTimes)
        {
            // Arrange
            var router      = new UnicastMessageRouter <int>();
            var key         = string.Empty;
            var message     = 15;
            var randomValue = new Random(regTimes);

            for (int i = 0; i < regTimes; i++)
            {
                var val = randomValue.Next();
                await router.RegisterCallbackAsync(
                    new Registration <int>(string.Empty, args =>
                {
                    val++;
                    return(Task.CompletedTask);
                }),
                    CancellationToken.None);
            }

            // Act
            var result = await router.PublishMessageAsync(key, message);

            // Assert
            Assert.AreEqual(regTimes, result.ReceiverCount);
        }
Пример #2
0
        public async Task PublishMessage_AllQueueReceived(int queueCount, int messageCount)
        {
            var queueTask = new Task[queueCount];
            var router    = new UnicastMessageRouter <int>();

            for (int i = 0; i < queueCount; i++)
            {
                var mq      = new MessageQueue <int>();
                var binding = await mq.BindAsync($"queue[{i}]", router);

                queueTask[i] = Task.Run(async() =>
                {
                    for (int j = 0; j < messageCount; j++)
                    {
                        var msg = await mq.TakeMessageAsync();
                        Assert.AreEqual(j, msg.Payload);
                    }

                    Assert.IsFalse(mq.TryTakeMessage(out _));
                    await binding.DisposeAsync();
                });
            }

            for (int i = 0; i < messageCount; i++)
            {
                for (int j = 0; j < queueCount; j++)
                {
                    await router.PublishMessageAsync($"queue[{j}]", i);
                }
            }

            await Task.WhenAll(queueTask);
        }
Пример #3
0
        public async Task PublishAfterDispose_NoCallback()
        {
            // Arrange
            var router = new UnicastMessageRouter <int>();
            var cb     = Mock.Create <AsyncMessageHandler <int> >();

            Mock.Arrange(() => cb(Arg.IsAny <MessageArgs <int> >()))
            .Returns(Task.CompletedTask)
            .Occurs(1);

            await using (await router.RegisterCallbackAsync(new Registration <int>(string.Empty, cb), CancellationToken.None))
            {
                // Act
                await router.PublishMessageAsync(string.Empty, 1);
            }

            // Act
            await router.PublishMessageAsync(string.Empty, 1);

            // Assert
            Mock.Assert(cb);
        }
Пример #4
0
        public async Task PublishMessageAfterRegister_HandlerThrowException_ResultContainException()
        {
            // Arrange
            var router  = new UnicastMessageRouter <int>();
            var key     = string.Empty;
            var message = 15;
            await router.RegisterCallbackAsync(
                new Registration <int>(string.Empty, args => Task.FromException(new Exception())),
                CancellationToken.None);

            // Act
            var result = await router.PublishMessageAsync(key, message);

            // Assert
            Assert.IsNotNull(result.Exception);
        }
Пример #5
0
        public async Task PublishMessageAfterRegister_NoException()
        {
            // Arrange
            var router  = new UnicastMessageRouter <int>();
            var key     = string.Empty;
            var message = 15;
            await router.RegisterCallbackAsync(
                new Registration <int>(string.Empty, args => Task.CompletedTask),
                CancellationToken.None);

            // Act
            var result = await router.PublishMessageAsync(key, message);

            // Assert
            Assert.IsNull(result.Exception);
        }
Пример #6
0
        public async Task PublishMessageAfterRegister_CallbackSucceed(string bindingKey, string publishKey, bool shouldBeReceived)
        {
            // Arrange
            var router  = new UnicastMessageRouter <int>();
            var cb      = Mock.Create <AsyncMessageHandler <int> >();
            var key     = bindingKey;
            var message = 15;

            Mock.Arrange(() => cb(Arg.IsAny <MessageArgs <int> >()))
            .Returns(Task.CompletedTask)
            .Occurs(shouldBeReceived ? 1:0);

            await router.RegisterCallbackAsync(new Registration <int>(publishKey, cb), CancellationToken.None);

            // Act
            await router.PublishMessageAsync(key, message);

            // Assert
            Mock.Assert(cb);
        }
Пример #7
0
        public async Task PublishMessage_FirstHandlerThrow_SecondHandlerInvoked()
        {
            // Arrange
            var router  = new UnicastMessageRouter <int>();
            var cb      = Mock.Create <AsyncMessageHandler <int> >();
            var key     = string.Empty;
            var message = 15;

            Mock.Arrange(() => cb(Arg.IsAny <MessageArgs <int> >()))
            .Returns(Task.CompletedTask)
            .Occurs(1);

            await router.RegisterCallbackAsync(new Registration <int>(key, args => Task.FromException(new Exception())),
                                               CancellationToken.None);

            await router.RegisterCallbackAsync(new Registration <int>(key, cb), CancellationToken.None); // register twice

            // Act
            await router.PublishMessageAsync(key, message);

            // Assert
            Mock.Assert(cb);
        }
Пример #8
0
        public async Task PublishMessage_BindInMultipleThreads_TotalCallback(int times)
        {
            // Arrange
            var router = new UnicastMessageRouter <int>();
            var cb     = Mock.Create <AsyncMessageHandler <int> >();

            Mock.Arrange(() => cb(Arg.IsAny <MessageArgs <int> >()))
            .Returns(Task.CompletedTask)
            .Occurs(times);

            await router.RegisterCallbackAsync(new Registration <int>(string.Empty, cb));

            var cancellationTokenSrc = new CancellationTokenSource();

            var bindTask = Task.Run(async() =>
            {
                var reg = new Registration <int>(string.Empty, args => Task.CompletedTask);

                while (!cancellationTokenSrc.IsCancellationRequested)
                {
                    await using (await router.RegisterCallbackAsync(reg, CancellationToken.None))
                    {
                        await Task.Delay(1, CancellationToken.None);
                    }
                }
            }, CancellationToken.None);

            for (int i = 0; i < times; i++)
            {
                await router.PublishMessageAsync(string.Empty, 1);
            }

            cancellationTokenSrc.Cancel();
            await bindTask;

            Mock.Assert(cb);
        }