Exemple #1
0
        public async Task HandledByIsAcknowledgeAfterAcknowledgeIsCalled()
        {
            var message = new FakeReceiverMessage("Hello, world!");

            await message.AcknowledgeAsync();

            message.HandledBy.Should().Be(nameof(message.AcknowledgeAsync));
        }
Exemple #2
0
        public async Task HandledByIsRejectAfterRejectIsCalled()
        {
            var message = new FakeReceiverMessage("Hello, world!");

            await message.RejectAsync();

            message.HandledBy.Should().Be(nameof(message.RejectAsync));
        }
Exemple #3
0
        public void StringPayloadIsSetFromBase64EncodedBinaryPayload()
        {
            var payload = new byte[] { 1, 2, 3, 4, 5 };

            var message = new FakeReceiverMessage(payload);

            message.StringPayload.Should().Be(Convert.ToBase64String(payload));
        }
Exemple #4
0
        public void BinaryPayloadIsSetFromBinaryPayload()
        {
            var payload = new byte[] { 1, 2, 3, 4, 5 };

            var message = new FakeReceiverMessage(payload);

            message.BinaryPayload.Should().BeSameAs(payload);
        }
        public async Task HandledIsTrueAfterRejectIsCalled()
        {
            using var message = new FakeReceiverMessage("Hello, world!");

            await message.RejectAsync().ConfigureAwait(false);

            message.Handled.Should().BeTrue();
        }
        public async Task HandledByIsRollbackAfterRollbackIsCalled()
        {
            using var message = new FakeReceiverMessage("Hello, world!");

            await message.RollbackAsync().ConfigureAwait(false);

            message.HandledBy.Should().Be(nameof(message.RollbackAsync));
        }
Exemple #7
0
        public async Task HandledIsTrueAfterRollbackIsCalled()
        {
            var message = new FakeReceiverMessage("Hello, world!");

            await message.RollbackAsync();

            message.Handled.Should().BeTrue();
        }
Exemple #8
0
        public async Task HandledIsTrueAfterAcknowledgeIsCalled()
        {
            var message = new FakeReceiverMessage("Hello, world!");

            await message.AcknowledgeAsync();

            message.Handled.Should().BeTrue();
        }
        public async Task RollbackThrowsIfHandledIsTrue()
        {
            using var message = new FakeReceiverMessage("Hello, world!");

            await message.AcknowledgeAsync().ConfigureAwait(false);

            Func <Task> act = () => message.RollbackAsync();

            await act.Should().ThrowAsync <InvalidOperationException>()
            .WithMessage($"Cannot {nameof(message.RollbackAsync)} message: the message has already been handled by {nameof(message.AcknowledgeAsync)}.").ConfigureAwait(false);
        }
Exemple #10
0
        public async Task RejectThrowsIfHandledIsTrue()
        {
            var message = new FakeReceiverMessage("Hello, world!");

            await message.AcknowledgeAsync();

            Func <Task> act = () => message.RejectAsync();

            act.Should().Throw <InvalidOperationException>()
            .WithMessage($"Cannot {nameof(message.RejectAsync)} message: the message has already been handled by {nameof(message.AcknowledgeAsync)}.");
        }
        public async Task RollbackCallsInnerMessageRollbackIfRollbackForwarderIsNull()
        {
            var receiver           = new FakeReceiver();
            var forwardingReceiver = new ForwardingReceiver("foo", receiver, rollbackForwarder: null);
            var message            = new FakeReceiverMessage("Hello, world!");

            var forwardingMessage = new ForwardingReceiverMessage(forwardingReceiver, message);

            await forwardingMessage.RollbackAsync();

            message.HandledBy.Should().Be(nameof(IReceiverMessage.RollbackAsync));
        }
Exemple #12
0
        public async Task AcknowledgeCallsInnerMessageAcknowledgeIfAcknowledgeForwarderIsNull()
        {
            using var receiver           = new FakeReceiver();
            using var forwardingReceiver = new ForwardingReceiver("foo", receiver, acknowledgeForwarder: null);
            using var message            = new FakeReceiverMessage("Hello, world!");

            var forwardingMessage = new ForwardingReceiverMessage(forwardingReceiver, message);

            await forwardingMessage.AcknowledgeAsync().ConfigureAwait(false);

            message.HandledBy.Should().Be(nameof(IReceiverMessage.AcknowledgeAsync));
        }
        public async Task AcknowledgeHandlesInnerMessageAccordingToAcknowledgeOutcomeWhenAcknowledgeForwarderIsNotNull(ForwardingOutcome outcome)
        {
            var forwarder = new FakeSender();

            var receiver           = new FakeReceiver();
            var forwardingReceiver = new ForwardingReceiver("foo", receiver, acknowledgeForwarder: forwarder, acknowledgeOutcome: outcome);
            var message            = new FakeReceiverMessage("Hello, world!");

            var forwardingMessage = new ForwardingReceiverMessage(forwardingReceiver, message);

            await forwardingMessage.AcknowledgeAsync();

            message.HandledBy.Should().Be($"{outcome}Async");
        }
Exemple #14
0
        public async Task RollbackHandlesInnerMessageAccordingToRollbackOutcomeWhenRollbackForwarderIsNotNull(ForwardingOutcome outcome)
        {
            using var forwarder = new FakeSender();

            using var receiver           = new FakeReceiver();
            using var forwardingReceiver = new ForwardingReceiver("foo", receiver, rollbackForwarder: forwarder, rollbackOutcome: outcome);
            using var message            = new FakeReceiverMessage("Hello, world!");

            var forwardingMessage = new ForwardingReceiverMessage(forwardingReceiver, message);

            await forwardingMessage.RollbackAsync().ConfigureAwait(false);

            message.HandledBy.Should().Be($"{outcome}Async");
        }
        public async Task AcknowledgeSendsMessageToAcknowledgeForwarderWhenAcknowledgeForwarderIsNotNull()
        {
            var forwarder = new FakeSender();

            var receiver           = new FakeReceiver();
            var forwardingReceiver = new ForwardingReceiver("foo", receiver, acknowledgeForwarder: forwarder);
            var message            = new FakeReceiverMessage("Hello, world!");

            var forwardingMessage = new ForwardingReceiverMessage(forwardingReceiver, message);

            await forwardingMessage.AcknowledgeAsync();

            forwarder.SentMessages.Should().ContainSingle();
            forwarder.SentMessages[0].StringPayload.Should().Be("Hello, world!");
        }
Exemple #16
0
        public async Task RollbackSendsMessageToRollbackForwarderWhenRollbackForwarderIsNotNull()
        {
            using var forwarder = new FakeSender();

            using var receiver           = new FakeReceiver();
            using var forwardingReceiver = new ForwardingReceiver("foo", receiver, rollbackForwarder: forwarder);
            using var message            = new FakeReceiverMessage("Hello, world!");

            var forwardingMessage = new ForwardingReceiverMessage(forwardingReceiver, message);

            await forwardingMessage.RollbackAsync().ConfigureAwait(false);

            forwarder.SentMessages.Should().ContainSingle();
            forwarder.SentMessages[0].StringPayload.Should().Be("Hello, world!");
        }
Exemple #17
0
        public void PublicHeaderPropertyHasSameContentsAsExplicitInterfaceHeaderProperty()
        {
            var message = new FakeReceiverMessage("Hello, world!");

            message.Headers.Add("foo", "abc");
            message.Headers.Add("bar", 123);

            HeaderDictionary interfaceHeaders = ((IReceiverMessage)message).Headers;

            interfaceHeaders.ContainsKey("foo");
            interfaceHeaders["foo"].Should().Be("abc");

            interfaceHeaders.ContainsKey("bar");
            interfaceHeaders["bar"].Should().Be(123);
        }
Exemple #18
0
        public async Task TheCallbackPassedToStart1IsInvokedWhenAMessageIsReceived()
        {
            using var receiver = new FakeReceiver();

            var received = false;

            receiver.Start(async m =>
            {
                received = true;
                await m.AcknowledgeAsync().ConfigureAwait(false);
            });

            using var message = new FakeReceiverMessage("Hello, world!");
            await receiver.MessageHandler.OnMessageReceivedAsync(receiver, message).ConfigureAwait(false);

            Assert.True(received);
        }
        public async Task OnMessageReceivedCallsInnerHandlerOnMessageReceivedWithForwardingReceiverMessage()
        {
            var receiver           = new FakeReceiver();
            var forwardingReceiver = new ForwardingReceiver("foo", receiver);
            var messageHandler     = new FakeMessageHandler();

            var handler = new ForwardingMessageHandler(forwardingReceiver, messageHandler);

            var message = new FakeReceiverMessage("Hello, world!");

            await handler.OnMessageReceivedAsync(receiver, message);

            messageHandler.ReceivedMessages.Should().ContainSingle();
            messageHandler.ReceivedMessages[0].Receiver.Should().BeSameAs(forwardingReceiver);
            messageHandler.ReceivedMessages[0].Message.Should().BeOfType <ForwardingReceiverMessage>();
            ((ForwardingReceiverMessage)messageHandler.ReceivedMessages[0].Message).Message.Should().BeSameAs(message);
            ((ForwardingReceiverMessage)messageHandler.ReceivedMessages[0].Message).ForwardingReceiver.Should().BeSameAs(forwardingReceiver);
        }
        public void ReceiverMessageConstructorUncompressedStringPayload()
        {
            var payload         = "Hello, world!";
            var receiverMessage = new FakeReceiverMessage(payload, false);

            var message = new SenderMessage(receiverMessage);

            message.StringPayload.Should().Be(payload);
            message.BinaryPayload.Should().BeEquivalentTo(Encoding.UTF8.GetBytes(payload));

            message.Headers.Should().ContainKey(HeaderNames.MessageId);
            message.MessageId.Should().NotBeNull();

            message.Headers.Should().NotContainKey(HeaderNames.IsBinaryPayload);
            message.IsBinary.Should().BeFalse();

            message.Headers.Should().NotContainKey(HeaderNames.IsCompressedPayload);
            message.IsCompressed.Should().BeFalse();
        }
Exemple #21
0
        public async Task TheCallbackPassedToStart4IsInvokedWhenAMessageIsReceived()
        {
            using var receiver = new FakeReceiver();

            var received = false;

#pragma warning disable CS0618 // Type or member is obsolete
            receiver.Start((r, m) =>
            {
                received = true;
                m.Acknowledge();
            });
#pragma warning restore CS0618 // Type or member is obsolete

            using var message = new FakeReceiverMessage("Hello, world!");
            await receiver.MessageHandler.OnMessageReceivedAsync(receiver, message).ConfigureAwait(false);

            Assert.True(received);
        }
        public void ReceiverMessageConstructorCompressedStringPayload()
        {
            var payload         = GetCompressablePayload("Hello, world!");
            var receiverMessage = new FakeReceiverMessage(payload, true);

            var message = new SenderMessage(receiverMessage);

            message.StringPayload.Should().Be(Convert.ToBase64String(_gzip.Compress(Encoding.UTF8.GetBytes(payload))));
            message.BinaryPayload.Should().BeEquivalentTo(_gzip.Compress(Encoding.UTF8.GetBytes(payload)));

            message.Headers.Should().ContainKey(HeaderNames.MessageId);
            message.MessageId.Should().NotBeNull();

            message.Headers.Should().NotContainKey(HeaderNames.IsBinaryPayload);
            message.IsBinary.Should().BeFalse();

            message.Headers[HeaderNames.IsCompressedPayload].Should().Be("true");
            message.IsCompressed.Should().BeTrue();
        }
        public void ReceiverMessageConstructorCompressedBinaryPayload()
        {
            var payload         = GetCompressablePayload(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
            var receiverMessage = new FakeReceiverMessage(payload, true);

            var message = new SenderMessage(receiverMessage);

            message.StringPayload.Should().Be(Convert.ToBase64String(_gzip.Compress(payload)));
            message.BinaryPayload.Should().BeEquivalentTo(_gzip.Compress(payload));

            message.Headers.Should().ContainKey(HeaderNames.MessageId);
            message.MessageId.Should().NotBeNull();

            message.Headers[HeaderNames.IsBinaryPayload].Should().Be("true");
            message.IsBinary.Should().BeTrue();

            message.Headers[HeaderNames.IsCompressedPayload].Should().Be("true");
            message.IsCompressed.Should().BeTrue();
        }
Exemple #24
0
        public void StringPayloadIsSetFromStringPayload()
        {
            var message = new FakeReceiverMessage("Hello, world!");

            message.StringPayload.Should().BeSameAs("Hello, world!");
        }
Exemple #25
0
        public void BinaryPayloadIsSetFromUTF8EncodedStringPayload()
        {
            var message = new FakeReceiverMessage("Hello, world!");

            message.BinaryPayload.Should().BeEquivalentTo(Encoding.UTF8.GetBytes("Hello, world!"), x => x.WithStrictOrdering());
        }
Exemple #26
0
        public void PublicHeaderPropertyIsNotTheSameAsExplicitInterfaceHeaderProperty()
        {
            var message = new FakeReceiverMessage("Hello, world!");

            ((object)message.Headers).Should().NotBeSameAs(((IReceiverMessage)message).Headers);
        }
Exemple #27
0
        public void HandledByIsNullBeforeMessageIsHandled()
        {
            var message = new FakeReceiverMessage("Hello, world!");

            message.HandledBy.Should().BeNull();
        }
Exemple #28
0
        public void HandledIsFalseBeforeMessageIsHandled()
        {
            var message = new FakeReceiverMessage("Hello, world!");

            message.Handled.Should().BeFalse();
        }