public ProcessClientOutboxMessageCommandV2HandlerTestsFixture()
        {
            Now          = DateTime.UtcNow;
            EndpointName = "SFA.DAS.NServiceBus";

            Events = new List <object>
            {
                new FooEvent(Now.AddDays(-1)),
                new BarEvent(Now)
            };

            TransportOperations        = Events.Select(e => new TransportOperation(Guid.NewGuid(), e)).ToList();
            ClientOutboxMessage        = new ClientOutboxMessageV2(GuidComb.NewGuidComb(), EndpointName, TransportOperations);
            SynchronizedStorageSession = new Mock <SynchronizedStorageSession>();

            Context = new TestableMessageHandlerContext
            {
                MessageId = ClientOutboxMessage.MessageId.ToString(),
                SynchronizedStorageSession = SynchronizedStorageSession.Object
            };

            Command             = new ProcessClientOutboxMessageCommandV2();
            ClientOutboxStorage = new Mock <IClientOutboxStorageV2>();

            ClientOutboxStorage.Setup(o => o.GetAsync(ClientOutboxMessage.MessageId, SynchronizedStorageSession.Object)).ReturnsAsync(ClientOutboxMessage);

            Handler = new ProcessClientOutboxMessageCommandV2Handler(ClientOutboxStorage.Object);
        }
        public ProcessOutboxMessageCommandHandlerTestsFixture()
        {
            Now = DateTime.UtcNow;

            EndpointName = "SFA.DAS.NServiceBus";

            Events = new List <Event>
            {
                new FooEvent {
                    Created = Now.AddDays(-1)
                },
                new BarEvent {
                    Created = Now
                }
            };

            OutboxMessage = new OutboxMessage(GuidComb.NewGuidComb(), EndpointName, Events);

            Context = new TestableMessageHandlerContext
            {
                MessageId = OutboxMessage.MessageId.ToString()
            };

            Command = new ProcessOutboxMessageCommand();
            Outbox  = new Mock <IOutbox>();

            Outbox.Setup(o => o.GetAsync(OutboxMessage.MessageId)).ReturnsAsync(OutboxMessage);

            Handler = new ProcessOutboxMessageCommandHandler(Outbox.Object);
        }
Example #3
0
        public ProcessOutboxMessagesJobTestsFixture SetOutboxMessagesAwaitingDispatch()
        {
            OutboxMessages.Add(new OutboxMessage(GuidComb.NewGuidComb(), "SFA.DAS.NServiceBus.Foo"));
            OutboxMessages.Add(new OutboxMessage(GuidComb.NewGuidComb(), "SFA.DAS.NServiceBus.Bar"));

            return(this);
        }
        public ClientOutboxCleanerTestsFixture SetClientOutboxMessagesAwaitingDispatch()
        {
            ClientOutboxMessages.Add(new ClientOutboxMessage(GuidComb.NewGuidComb(), "SFA.DAS.NServiceBus.Foo"));
            ClientOutboxMessages.Add(new ClientOutboxMessage(GuidComb.NewGuidComb(), "SFA.DAS.NServiceBus.Bar"));
            ClientOutboxMessageV2s.Add(new ClientOutboxMessageV2(GuidComb.NewGuidComb(), "SFA.DAS.NServiceBus.FooV2"));
            ClientOutboxMessageV2s.Add(new ClientOutboxMessageV2(GuidComb.NewGuidComb(), "SFA.DAS.NServiceBus.BarV2"));

            return(this);
        }
Example #5
0
        public ClientOutboxPersisterV2TestsFixture()
        {
            Now             = DateTime.UtcNow;
            DateTimeService = new Mock <IDateTimeService>();
            Settings        = new Mock <ReadOnlySettings>();
            Connection      = new Mock <DbConnection>();
            Transaction     = new Mock <DbTransaction> {
                CallBase = true
            };
            Command    = new Mock <DbCommand>();
            Parameters = new Mock <DbParameterCollection>();
            ClientOutboxTransaction = new SqlClientOutboxTransaction(Connection.Object, Transaction.Object);
            EndpointName            = "SFA.DAS.NServiceBus";

            Events = new List <object>
            {
                new FooEvent(Now.AddDays(-1)),
                new BarEvent(Now)
            };

            TransportOperations     = Events.Select(e => new TransportOperation(Guid.NewGuid(), e)).ToList();
            TransportOperationsData = JsonConvert.SerializeObject(TransportOperations, new JsonSerializerSettings {
                TypeNameHandling = TypeNameHandling.Auto
            });
            ClientOutboxMessage        = new ClientOutboxMessageV2(GuidComb.NewGuidComb(), EndpointName, TransportOperations);
            SynchronizedStorageSession = new Mock <SynchronizedStorageSession>();
            SqlSession              = SynchronizedStorageSession.As <ISqlStorageSession>();
            OutboxMessages          = new List <IClientOutboxMessageAwaitingDispatch>();
            CancellationTokenSource = new CancellationTokenSource();
            CancellationToken       = CancellationTokenSource.Token;

            DateTimeService.Setup(d => d.UtcNow).Returns(Now);
            Parameters.Setup(p => p.Add(It.IsAny <DbParameter>()));
            Command.SetupSet(c => c.CommandText = It.IsAny <string>());
            Command.SetupSet(c => c.Transaction = It.IsAny <DbTransaction>());

            Command.Protected().Setup <DbParameter>("CreateDbParameter").Returns(() =>
            {
                var parameter = new Mock <DbParameter> {
                    CallBase = true
                };

                parameter.SetupProperty(p => p.ParameterName);
                parameter.SetupProperty(p => p.Value);

                return(parameter.Object);
            });

            Command.Protected().Setup <DbParameterCollection>("DbParameterCollection").Returns(Parameters.Object);
            Connection.Protected().Setup <DbTransaction>("BeginDbTransaction", IsolationLevel.Unspecified).Returns(Transaction.Object);
            Connection.Protected().Setup <DbCommand>("CreateDbCommand").Returns(Command.Object);
            Settings.Setup(s => s.Get <Func <DbConnection> >("SqlPersistence.ConnectionBuilder")).Returns(() => Connection.Object);
            SqlSession.Setup(s => s.Connection).Returns(Connection.Object);
            SqlSession.Setup(s => s.Transaction).Returns(Transaction.Object);

            ClientOutboxStorage = new ClientOutboxPersisterV2(DateTimeService.Object, Settings.Object);
        }
Example #6
0
        public OutboxTestsFixture()
        {
            Now               = DateTime.UtcNow;
            Connection        = new Mock <DbConnection>();
            UnitOfWorkContext = new Mock <IUnitOfWorkContext>();
            Settings          = new Mock <ReadOnlySettings>();
            Transaction       = new Mock <DbTransaction> {
                CallBase = true
            };
            Command           = new Mock <DbCommand>();
            Parameters        = new Mock <DbParameterCollection>();
            OutboxTransaction = new OutboxTransaction(Transaction.Object);
            EndpointName      = "SFA.DAS.NServiceBus";

            Events = new List <Event>
            {
                new FooEvent {
                    Created = Now.AddDays(-1)
                },
                new BarEvent {
                    Created = Now
                }
            };

            EventsData = JsonConvert.SerializeObject(Events, new JsonSerializerSettings {
                TypeNameHandling = TypeNameHandling.Auto
            });
            OutboxMessage  = new OutboxMessage(GuidComb.NewGuidComb(), EndpointName, Events);
            OutboxMessages = new List <IOutboxMessageAwaitingDispatch>();

            Parameters.Setup(p => p.Add(It.IsAny <DbParameter>()));
            Command.SetupSet(c => c.CommandText = It.IsAny <string>());
            Command.SetupSet(c => c.Transaction = It.IsAny <DbTransaction>());

            Command.Protected().Setup <DbParameter>("CreateDbParameter").Returns(() =>
            {
                var parameter = new Mock <DbParameter> {
                    CallBase = true
                };

                parameter.SetupProperty(p => p.ParameterName);
                parameter.SetupProperty(p => p.Value);

                return(parameter.Object);
            });

            Command.Protected().Setup <DbParameterCollection>("DbParameterCollection").Returns(Parameters.Object);
            Connection.Protected().Setup <DbTransaction>("BeginDbTransaction", IsolationLevel.Unspecified).Returns(Transaction.Object);
            Connection.Protected().Setup <DbCommand>("CreateDbCommand").Returns(Command.Object);
            UnitOfWorkContext.Setup(c => c.Get <DbConnection>()).Returns(Connection.Object);
            UnitOfWorkContext.Setup(c => c.Get <DbTransaction>()).Returns(Transaction.Object);
            Settings.Setup(s => s.Get <string>("NServiceBus.Routing.EndpointName")).Returns(EndpointName);

            Outbox = new Outbox(Connection.Object, UnitOfWorkContext.Object);
        }
Example #7
0
        public ClientOutboxPersisterV2TestsFixture SetupGetAwaitingDispatchReader()
        {
            OutboxMessages = new List <IClientOutboxMessageAwaitingDispatch>
            {
                new ClientOutboxMessage(GuidComb.NewGuidComb(), "SFA.DAS.NServiceBus.Foo"),
                new ClientOutboxMessage(GuidComb.NewGuidComb(), "SFA.DAS.NServiceBus.Foo")
            };

            DataReader = new Mock <DbDataReader>();

            DataReader.SetupSequence(r => r.ReadAsync(CancellationToken.None)).ReturnsAsync(true).ReturnsAsync(true).ReturnsAsync(false);
            DataReader.SetupSequence(r => r.GetGuid(0)).Returns(OutboxMessages[0].MessageId).Returns(OutboxMessages[1].MessageId);
            DataReader.SetupSequence(r => r.GetString(1)).Returns(OutboxMessages[0].EndpointName).Returns(OutboxMessages[1].EndpointName);
            Command.Protected().Setup <Task <DbDataReader> >("ExecuteDbDataReaderAsync", CommandBehavior.Default, CancellationToken.None).ReturnsAsync(DataReader.Object);

            return(this);
        }
Example #8
0
        public CommandContext Get()
        {
            var command     = ExtensionMethods.CurrentMessageBeingHandled;
            var commandType = command.GetType();

            lock (_cacheLock)
            {
                Func <object, Guid> property;

                if (!_cache.TryGetValue(commandType, out property))
                {
                    var propertyInfo = commandType.GetProperty("Id");

                    if (propertyInfo != null)
                    {
                        Logger.Debug("Message Id property found for use as CommitId");

                        var commandParameter = Expression.Parameter(typeof(object), "command");
                        var propertyCall     = Expression.Property(Expression.Convert(commandParameter, commandType), propertyInfo);

                        property = Expression.Lambda <Func <object, Guid> >(propertyCall, commandParameter).Compile();
                    }
                    else
                    {
                        Logger.Debug("Message Id property not found a CommitId will be automatically generated");

                        property = c => GuidComb.NewGuidComb();
                    }

                    _cache[commandType] = property;
                }

                return(new CommandContext
                {
                    Id = property(command),
                    Headers = _bus.CurrentMessageContext.Headers
                              .Where(h =>
                                     !h.Key.Equals("CorrId", StringComparison.InvariantCultureIgnoreCase) &&
                                     !h.Key.Equals("WinIdName", StringComparison.InvariantCultureIgnoreCase) &&
                                     !h.Key.StartsWith("NServiceBus", StringComparison.InvariantCultureIgnoreCase))
                              .ToDictionary(h => h.Key, h => (object)h.Value)
                });
            }
        }
        public async Task CommitAsync(Func <Task> next)
        {
            var clientOutboxTransaction = _unitOfWorkContext.Get <IClientOutboxTransaction>();
            var transportOperations     = _unitOfWorkContext.GetEvents().Select(e => new TransportOperation(GuidComb.NewGuidComb(), e)).ToList();
            var clientOutboxMessage     = transportOperations.Any() ? new ClientOutboxMessageV2(GuidComb.NewGuidComb(), _settings.EndpointName(), transportOperations) : null;

            if (clientOutboxMessage != null)
            {
                await _clientOutboxStorage.StoreAsync(clientOutboxMessage, clientOutboxTransaction).ConfigureAwait(false);
            }

            await next().ConfigureAwait(false);

            if (clientOutboxMessage != null)
            {
                var tasks = clientOutboxMessage.TransportOperations.Select(o =>
                {
                    if (o.Message.IsCommand())
                    {
                        return(SendMessage(o));
                    }
                    else
                    {
                        return(PublishMessage(o));
                    }
                });

                await Task.WhenAll(tasks).ConfigureAwait(false);

                await _clientOutboxStorage.SetAsDispatchedAsync(clientOutboxMessage.MessageId, clientOutboxTransaction).ConfigureAwait(false);
            }
        }
Example #10
0
 public CreateUserCommand()
 {
     UserId = GuidComb.NewGuidComb();
 }
Example #11
0
 public SendMessageCommand()
 {
     MessageId = GuidComb.NewGuidComb();
 }