public PipelineGlobalInboxTests()
        {
            IAmAnInboxSync inbox = new InMemoryInbox();

            var registry = new SubscriberRegistry();

            registry.Register <MyCommand, MyCommandHandler>();

            var container = new ServiceCollection();

            container.AddTransient <MyCommandHandler>();
            container.AddSingleton <IAmAnInboxSync>(inbox);
            container.AddTransient <UseInboxHandler <MyCommand> >();
            container.AddSingleton <IBrighterOptions>(new BrighterOptions()
            {
                HandlerLifetime = ServiceLifetime.Transient
            });

            var handlerFactory = new ServiceProviderHandlerFactory(container.BuildServiceProvider());

            _requestContext = new RequestContext();

            InboxConfiguration inboxConfiguration = new InboxConfiguration();

            _chainBuilder = new PipelineBuilder <MyCommand>(registry, (IAmAHandlerFactorySync)handlerFactory, inboxConfiguration);
            PipelineBuilder <MyCommand> .ClearPipelineCache();
        }
        public void When_expiring_some_but_not_all()
        {
            //Arrange
            const string contextKey = "Inbox_Cache_Expiry_Tests";

            var inbox = new InMemoryInbox()
            {
                //set some aggressive outbox reclamation times for the test
                EntryTimeToLive        = TimeSpan.FromMilliseconds(500),
                ExpirationScanInterval = TimeSpan.FromMilliseconds(100)
            };

            var earlyCommands = new SimpleCommand[] { new SimpleCommand(), new SimpleCommand(), new SimpleCommand() };

            //Act
            foreach (var command in earlyCommands)
            {
                inbox.Add(command, contextKey);
            }

            Task.Delay(2000).Wait();  //expire these items

            var lateCommands = new SimpleCommand[] { new SimpleCommand(), new SimpleCommand(), new SimpleCommand() };

            foreach (var command in lateCommands) //This will trigger cleanup
            {
                inbox.Add(command, contextKey);
            }

            //Assert
            inbox.EntryCount.Should().Be(3);
        }
예제 #3
0
        public void When_testing_for_a_missing_command()
        {
            //Arrange
            var          inbox      = new InMemoryInbox();
            const string contextKey = "Developer_Test";

            var command = new SimpleCommand();

            //Act
            var exists = inbox.Exists <SimpleCommand>(command.Id, contextKey);

            //Assert
            exists.Should().BeFalse();
        }
예제 #4
0
    public async Task Try()
    {
        var persister  = new InMemorySagaPersister();
        var inbox      = new InMemoryInbox();
        var dispatcher = new FakeDispatcher();
        var manager    = new SagaManager(persister, inbox, dispatcher);

        await manager.Process <SagaData>("messageId", "correlationId", new ContextBag(),
                                         HandlerCallback);

        var dataContainer = await persister.LoadByCorrelationId("correlationId");

        var sagaData = (SagaData)dataContainer.SagaData;

        Assert.AreEqual(1, sagaData.Counter);
    }
예제 #5
0
        public void When_storing_a_seen_message_in_the_inbox()
        {
            //Arrange
            var          inbox      = new InMemoryInbox();
            const string contextKey = "Developer_Test";

            var command = new SimpleCommand();

            //Act
            inbox.Add(command, contextKey);

            var storedCommand = inbox.Get <SimpleCommand>(command.Id, contextKey);

            //Assert
            storedCommand.Should().NotBeNull();
            storedCommand.Id.Should().Be(command.Id);
        }
예제 #6
0
        public void When_storing_many_but_not_requested_exists_should_not_find()
        {
            //Arrange
            var          inbox      = new InMemoryInbox();
            const string contextKey = "Developer_Test";

            var commands = new SimpleCommand[] { new SimpleCommand(), new SimpleCommand(), new SimpleCommand(), new SimpleCommand(), new SimpleCommand() };

            foreach (var command in commands)
            {
                inbox.Add(command, contextKey);
            }

            //Act
            var firstCommandExists = inbox.Exists <SimpleCommand>(Guid.NewGuid(), contextKey);

            //Assert
            firstCommandExists.Should().BeFalse();
        }
예제 #7
0
        public void When_storing_multiple_entries_exists_should_find()
        {
            //Arrange
            var          inbox      = new InMemoryInbox();
            const string contextKey = "Developer_Test";

            var commands = new SimpleCommand[] { new SimpleCommand(), new SimpleCommand(), new SimpleCommand(), new SimpleCommand(), new SimpleCommand() };

            foreach (var command in commands)
            {
                inbox.Add(command, contextKey);
            }

            //Act
            var firstCommandExists = inbox.Exists <SimpleCommand>(commands[0].Id, contextKey);
            var lastCommandExists  = inbox.Exists <SimpleCommand>(commands[4].Id, contextKey);

            //Assert
            firstCommandExists.Should().BeTrue();
            lastCommandExists.Should().BeTrue();
        }
        public void When_expiring_a_cache_entry_no_longer_there()
        {
            //Arrange
            const string contextKey = "Inbox_Cache_Expiry_Tests";

            var inbox = new InMemoryInbox()
            {
                //set some aggressive outbox reclamation times for the test
                EntryTimeToLive        = TimeSpan.FromMilliseconds(50),
                ExpirationScanInterval = TimeSpan.FromMilliseconds(100)
            };

            var command = new SimpleCommand();

            //Act
            inbox.Add(command, contextKey);

            Task.Delay(500).Wait(); //give the entry to time to expire

            //Trigger a cache clean
            SimpleCommand foundCommand = null;

            try
            {
                foundCommand = inbox.Get <SimpleCommand>(command.Id, contextKey);
            }
            catch (Exception e) when(e is RequestNotFoundException <SimpleCommand> || e is TypeLoadException)
            {
                //early sweeper run means it doesn't exist already
            }

            Task.Delay(500).Wait(); //Give the sweep time to run

            var afterExpiryExists = inbox.Exists <SimpleCommand>(command.Id, contextKey);

            //Assert
            foundCommand.Should().NotBeNull();
            afterExpiryExists.Should().BeFalse();
        }
예제 #9
0
        public void When_storing_multiple_entries_retrieve_the_right_one()
        {
            //Arrange
            var          inbox      = new InMemoryInbox();
            const string contextKey = "Developer_Test";

            var commands = new SimpleCommand[] { new SimpleCommand(), new SimpleCommand(), new SimpleCommand(), new SimpleCommand(), new SimpleCommand() };

            foreach (var command in commands)
            {
                inbox.Add(command, contextKey);
            }

            //Act
            var firstCommand = inbox.Get <SimpleCommand>(commands[0].Id, contextKey);
            var lastCommand  = inbox.Get <SimpleCommand>(commands[4].Id, contextKey);

            //Assert
            firstCommand.Should().NotBeNull();
            lastCommand.Should().NotBeNull();

            firstCommand.Id.Should().Be(commands[0].Id, contextKey);
            lastCommand.Id.Should().Be(commands[4].Id, contextKey);
        }
예제 #10
0
    public async Task PerformScenario(string scenario)
    {
        var controller = new TestController(scenario);
        var persister  = new InMemorySagaPersister();
        var inbox      = new InMemoryInbox();
        var dispatcher = new FakeDispatcher();
        var persisterA = new TestingSagaDataPersister(persister, inbox, 'A', controller.GetBarrier);
        var persisterB = new TestingSagaDataPersister(persister, inbox, 'B', controller.GetBarrier);

        var managerA = new SagaManager(persisterA, persisterA, dispatcher);
        var managerB = new SagaManager(persisterB, persisterB, dispatcher);

        var processA = Task.Run(() => ProcessMessage(managerA, controller));
        var processB = Task.Run(() => ProcessMessage(managerB, controller));

        var done = Task.WhenAll(processA, processB);
        await done.ConfigureAwait(false);

        var dataContainer = await persister.LoadByCorrelationId("correlationId");

        var sagaData = (SagaData)dataContainer.SagaData;

        Assert.AreEqual(1, sagaData.Counter);
    }