Example #1
0
        private static async Task SendMessage(Envelope envelope, JsonMessageSerializer serializer)
        {
            var messageBus = new ServiceBusMessageBus(_connectionStringBuilder, serializer);
            await messageBus.Send(new ScheduledEnvelope(envelope, DateTime.UtcNow.AddMinutes(-1.0)), default);

            await messageBus.Close();
        }
        public async Task Send_sets_Contributor_user_property_correctly()
        {
            // Arrange
            await ReceiveAndForgetAll();

            IMessageSerializer serializer = new JsonMessageSerializer();
            var sut = new ServiceBusMessageBus(_connectionStringBuilder, serializer);

            var    messageId   = Guid.NewGuid();
            string contributor = Guid.NewGuid().ToString();
            var    scheduled   = new ScheduledEnvelope(
                new Envelope(
                    messageId,
                    message: new Fixture().Create <SomeMessage>(),
                    contributor: contributor),
                DateTime.UtcNow);

            // Act
            await sut.Send(scheduled, CancellationToken.None);

            // Assert
            (Message received, DateTime receivedAt) = await ReceiveSingle();

            received.UserProperties.Should().Contain("Khala.Messaging.Envelope.Contributor", contributor);
        }
        public async Task Send_works_for_already_passed_scheduled_time()
        {
            // Arrange
            await ReceiveAndForgetAll();

            IMessageSerializer serializer = new JsonMessageSerializer();
            var sut = new ServiceBusMessageBus(_connectionStringBuilder, serializer);

            var scheduled = new ScheduledEnvelope(
                new Envelope(new Fixture().Create <SomeMessage>()),
                DateTime.UtcNow.AddTicks(-TimeSpan.FromDays(1).Ticks));

            // Act
            await sut.Send(scheduled, CancellationToken.None);

            // Assert
            (Message received, DateTime receivedAt) = await ReceiveSingle();

            var precision = TimeSpan.FromMilliseconds(1000);

            receivedAt.Should().BeOnOrAfter(scheduled.ScheduledTimeUtc.AddTicks(-precision.Ticks));

            object message = serializer.Deserialize(Encoding.UTF8.GetString(received.Body));

            message.Should().BeEquivalentTo(scheduled.Envelope.Message);
        }
        public async Task Close_releases_message_sender_resources()
        {
            var sut = new ServiceBusMessageBus(_connectionStringBuilder, new JsonMessageSerializer());

            await sut.Close();

            var         scheduled = new ScheduledEnvelope(new Envelope(new object()), DateTime.UtcNow);
            Func <Task> action    = () => sut.Send(scheduled, CancellationToken.None);

            action.Should().Throw <Exception>();
        }
        public async Task Send_sets_CorrelationId_correctly_if_exists()
        {
            // Arrange
            await ReceiveAndForgetAll();

            IMessageSerializer serializer = new JsonMessageSerializer();
            var sut = new ServiceBusMessageBus(_connectionStringBuilder, serializer);

            var correlationId = Guid.NewGuid();
            var scheduled     = new ScheduledEnvelope(
                new Envelope(
                    messageId: Guid.NewGuid(),
                    message: new Fixture().Create <SomeMessage>(),
                    correlationId: correlationId),
                DateTime.UtcNow);

            // Act
            await sut.Send(scheduled, CancellationToken.None);

            // Assert
            (Message received, DateTime receivedAt) = await ReceiveSingle();

            received.CorrelationId.Should().Be(correlationId.ToString("n"));
        }