Exemplo n.º 1
0
        public static PendingScheduledCommand FromScheduledEnvelope <T>(
            T processManager,
            ScheduledEnvelope scheduledEnvelope,
            IMessageSerializer serializer)
            where T : ProcessManager
        {
            if (processManager == null)
            {
                throw new ArgumentNullException(nameof(processManager));
            }

            if (scheduledEnvelope == null)
            {
                throw new ArgumentNullException(nameof(scheduledEnvelope));
            }

            if (serializer == null)
            {
                throw new ArgumentNullException(nameof(serializer));
            }

            return(new PendingScheduledCommand
            {
                ProcessManagerType = typeof(T).FullName,
                ProcessManagerId = processManager.Id,
                MessageId = scheduledEnvelope.Envelope.MessageId,
                OperationId = scheduledEnvelope.Envelope.OperationId,
                CorrelationId = scheduledEnvelope.Envelope.CorrelationId,
                Contributor = scheduledEnvelope.Envelope.Contributor,
                CommandJson = serializer.Serialize(scheduledEnvelope.Envelope.Message),
                ScheduledTimeUtc = scheduledEnvelope.ScheduledTimeUtc,
            });
        }
Exemplo n.º 2
0
        public void FromScheduledEnvelope_generates_PendingScheduledCommand_correctly()
        {
            // Arrange
            var random         = new Random();
            var processManager = new FakeProcessManager();
            var messageId      = Guid.NewGuid();
            var correlationId  = Guid.NewGuid();
            var message        = new FakeCommand
            {
                Int32Value  = random.Next(),
                StringValue = Guid.NewGuid().ToString(),
            };
            var scheduledEnvelope =
                new ScheduledEnvelope(
                    new Envelope(messageId, message, correlationId: correlationId),
                    DateTime.Now.AddTicks(random.Next()));
            var serializer = new JsonMessageSerializer();

            // Act
            var actual = PendingScheduledCommand.FromScheduledEnvelope(processManager, scheduledEnvelope, serializer);

            // Assert
            actual.ProcessManagerType.Should().Be(typeof(FakeProcessManager).FullName);
            actual.ProcessManagerId.Should().Be(processManager.Id);
            actual.MessageId.Should().Be(messageId);
            actual.CorrelationId.Should().Be(correlationId);
            actual.CommandJson.Should().Be(serializer.Serialize(message));
            actual.ScheduledTimeUtc.Should().Be(scheduledEnvelope.ScheduledTimeUtc);
        }
        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);
        }
 private async Task SendScheduledCommands(
     IEnumerable <PendingScheduledCommand> scheduledCommands,
     CancellationToken cancellationToken)
 {
     foreach (PendingScheduledCommand scheduledCommand in scheduledCommands)
     {
         ScheduledEnvelope scheduledEnvelope = RestoreScheduledEnvelope(scheduledCommand);
         await _scheduledMessageBus.Send(scheduledEnvelope, cancellationToken).ConfigureAwait(false);
     }
 }
        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>();
        }
Exemplo n.º 7
0
        public void FromScheduledEnvelope_sets_CorrelationId_correctly()
        {
            var correlationId     = Guid.NewGuid();
            var scheduledEnvelope = new ScheduledEnvelope(
                new Envelope(correlationId, new object()),
                DateTimeOffset.Now);

            var actual = PendingScheduledCommand.FromScheduledEnvelope(
                new FooProcessManager(),
                scheduledEnvelope,
                new JsonMessageSerializer());

            actual.CorrelationId.Should().Be(scheduledEnvelope.Envelope.CorrelationId);
        }
Exemplo n.º 8
0
        public void FromScheduledEnvelope_sets_CommandJson_correctly()
        {
            FooCommand command           = new Fixture().Create <FooCommand>();
            var        scheduledEnvelope = new ScheduledEnvelope(
                new Envelope(command),
                DateTimeOffset.Now);
            var serializer = new JsonMessageSerializer();

            var actual = PendingScheduledCommand.FromScheduledEnvelope(
                new FooProcessManager(),
                scheduledEnvelope,
                serializer);

            serializer.Deserialize(actual.CommandJson).ShouldBeEquivalentTo(
                command,
                opts => opts.RespectingRuntimeTypes());
        }
        /// <summary>
        /// Sends an enveloped message to the Azure Service Bus entity at the scheduled time.
        /// </summary>
        /// <param name="envelope">A scheduled envelope message to be sent.</param>
        /// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe while waiting for the task to complete.</param>
        /// <returns>A task representing the asynchronous operation.</returns>
        public Task Send(ScheduledEnvelope envelope, CancellationToken cancellationToken)
        {
            if (envelope == null)
            {
                throw new ArgumentNullException(nameof(envelope));
            }

            return(_sender.SendAsync(new Message
            {
                Body = Encoding.UTF8.GetBytes(_serializer.Serialize(envelope.Envelope.Message)),
                ScheduledEnqueueTimeUtc = envelope.ScheduledTimeUtc,
                MessageId = envelope.Envelope.MessageId.ToString("n"),
                CorrelationId = envelope.Envelope.CorrelationId?.ToString("n"),
                UserProperties =
                {
                    ["Khala.Messaging.Envelope.OperationId"] = envelope.Envelope.OperationId,
                    ["Khala.Messaging.Envelope.Contributor"] = envelope.Envelope.Contributor,
                },
            }));
        }
        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"));
        }
 public Task Send(ScheduledEnvelope envelope, CancellationToken cancellationToken) => _completionSource.Task;
 public Task Send(ScheduledEnvelope envelope, CancellationToken cancellationToken)
 {
     _sent.Enqueue(envelope);
     return(Task.CompletedTask);
 }