Example #1
0
    public async Task YesWeCan()
    {
        var activator = Using(new BuiltinHandlerActivator());

        activator.Handle <string>(async _ => throw new ArgumentException("1st"));
        activator.Handle <IFailed <string> >(async _ => throw new ArgumentException("2nd"));

        var loggerFactory = new ListLoggerFactory(outputToConsole: true);

        var bus = Configure.With(activator)
                  .Logging(l => l.Use(loggerFactory))
                  .Transport(t => t.UseInMemoryTransport(new InMemNetwork(), "who-cares"))
                  .Options(o =>
        {
            o.SimpleRetryStrategy(secondLevelRetriesEnabled: true);
            o.FailFastOn <ArgumentException>();
        })
                  .Start();

        await bus.SendLocal("HEJ MED DIG!");

        // wait until an error is logged
        await loggerFactory.LogLines.WaitUntil(lines => lines.Any(l => l.Level == LogLevel.Error));

        // provide extra time for additional stuff to happen
        await Task.Delay(TimeSpan.FromMilliseconds(100));

        Assert.That(loggerFactory.Count(l => l.Level == LogLevel.Warn), Is.EqualTo(2),
                    "Expected exactly two WARNings, because each one was due to a fail-fast exception");

        Assert.That(loggerFactory.Count(l => l.Level == LogLevel.Error), Is.EqualTo(1),
                    "Expected exactly one ERROR, because the message is only dead-lettered once");
    }
        public async Task OnlyDeliversMessageOnceWhenThereIsNoHandlerForIt()
        {
            _activator.Bus.Advanced.SyncBus.SendLocal("hej med dig din gamle hængerøv");

            await Task.Delay(TimeSpan.FromSeconds(2));

            var numberOfWarnings = _loggerFactory.Count(l => l.Level == LogLevel.Warn);
            var numberOfErrors   = _loggerFactory.Count(l => l.Level == LogLevel.Error);

            Assert.That(numberOfWarnings, Is.EqualTo(1), "Expected onle one single WARNING, because the delivery should not be retried");
            Assert.That(numberOfErrors, Is.EqualTo(1), "Expected an error message saying that the message is moved to the error queue");
        }
        public async Task TestMySqlSagaLocks()
        {
            var loggerFactory = new ListLoggerFactory(outputToConsole: true);
            var network       = new InMemNetwork();

            var handlerActivator = Using(new BuiltinHandlerActivator());

            handlerActivator.Handle <ProcessThisThingRequest>((bus, request) => bus.Reply(new ProcessThisThingReply(request.Thing, request.SagaId)));

            Configure.With(handlerActivator)
            .Logging(l => l.None())
            .Transport(t => t.UseInMemoryTransport(network, "processor"))
            .Start();

            var sagaActivator = Using(new BuiltinHandlerActivator());

            sagaActivator.Register((bus, context) => new TypicalContendedSagaExample(bus));

            Configure.With(sagaActivator)
            .Logging(l => l.Use(loggerFactory))
            .Transport(t => t.UseInMemoryTransport(network, "lock-test"))
            .Sagas(s =>
            {
                s.StoreInMySql(MySqlTestHelper.ConnectionString, _dataTableName, _indexTableName);
                s.EnforceExclusiveAccess(c =>
                {
                    var options            = new MySqlExclusiveAccessLockOptions(MySqlTestHelper.ConnectionString);
                    var rebusLoggerFactory = c.Get <IRebusLoggerFactory>();
                    var connectionProvider = options.ConnectionProviderFactory(c);
                    var asyncTaskFactory   = c.Get <IAsyncTaskFactory>();
                    var rebusTime          = c.Get <IRebusTime>();
                    var locker             = new MySqlExclusiveAccessLock(connectionProvider, _lockTableName, rebusLoggerFactory, asyncTaskFactory, rebusTime, options);
                    if (options.EnsureTablesAreCreated)
                    {
                        locker.EnsureTableIsCreated();
                    }
                    return(locker);
                });
            })
            .Routing(t => t.TypeBased().Map <ProcessThisThingRequest>("processor"))
            .Options(o =>
            {
                o.SetMaxParallelism(100);
                o.SetNumberOfWorkers(10);
            })
            .Start();

            await sagaActivator.Bus.SendLocal(new ProcessTheseThings(Enumerable.Range(0, 10).Select(no => $"THING-{no}")));

            await Task.Delay(TimeSpan.FromSeconds(System.Diagnostics.Debugger.IsAttached ? 30 : 3));

            Assert.That(loggerFactory.Count(l => l.Level >= LogLevel.Warn), Is.EqualTo(0), "Didn't expect any logging with level WARNING or above");
        }
Example #4
0
    public async Task ItWorks()
    {
        const string queueName        = "testappqueue";
        const int    numberOfMessages = 10;

        var network           = new InMemNetwork();
        var listLoggerFactory = new ListLoggerFactory(detailed: true);
        var sharedCounter     = new SharedCounter(numberOfMessages);

        Using(sharedCounter);

        // deliver a message for our endpoint
        network.CreateQueue(queueName);

        var client = GetOneWayClient(network, listLoggerFactory);

        numberOfMessages.Times(() => client.Advanced.Routing.Send(queueName, "HEJ MED DIG MIN VEN").Wait());

        // prepare our endpoint
        var builder = new ContainerBuilder();

        builder.RegisterRebus(
            config => config
            .Logging(l => l.Use(listLoggerFactory))
            .Transport(t => t.UseInMemoryTransport(network, queueName))
            .Options(o =>
        {
            o.SetNumberOfWorkers(1);
            o.SetMaxParallelism(30);
        })
            );
        // obtain bus instance for subscriptions...
        builder.RegisterBuildCallback(c => {
            var bus = c.Resolve <IBus>();
        });

        builder.RegisterHandler <MyMessageHandler>();

        builder.RegisterInstance(sharedCounter);

        // start it
        using (builder.Build())
        {
            sharedCounter.WaitForResetEvent(timeoutSeconds: 5);
        }

        // ensure no exceptions occurred

        Assert.That(listLoggerFactory.Count(log => log.Level >= LogLevel.Warn), Is.EqualTo(0),
                    "Expected exactly ZERO warnings");
    }
        public async Task VerifyThatInitializationOnlyOccursOnce()
        {
            var database      = MongoTestHelper.GetMongoDatabase();
            var loggerFactory = new ListLoggerFactory(outputToConsole: true);
            var storage       = new MongoDbSagaStorage(database, loggerFactory);

            storage.Initialize();

            await storage.Insert(new SomeSagaData { Id = Guid.NewGuid() }, Enumerable.Empty <ISagaCorrelationProperty>());

            await storage.Insert(new SomeSagaData { Id = Guid.NewGuid() }, Enumerable.Empty <ISagaCorrelationProperty>());

            await storage.Insert(new SomeSagaData { Id = Guid.NewGuid() }, Enumerable.Empty <ISagaCorrelationProperty>());

            var numberOfInitializations = loggerFactory
                                          .Count(line => line.Text.Contains("Initializing index for saga data"));

            Assert.That(numberOfInitializations, Is.EqualTo(1),
                        "Only expected the collection's indexes to be initialized once!");
        }
Example #6
0
        public async Task ThisIsHowItIsDone()
        {
            var loggerFactory = new ListLoggerFactory(outputToConsole: true);
            var network       = new InMemNetwork();

            var handlerActivator = Using(new BuiltinHandlerActivator());

            handlerActivator.Handle <ProcessThisThingRequest>((bus, request) => bus.Reply(new ProcessThisThingReply(request.Thing, request.SagaId)));

            Configure.With(handlerActivator)
            .Logging(l => l.None())
            .Transport(t => t.UseInMemoryTransport(network, "processor"))
            .Start();

            var sagaActivator = Using(new BuiltinHandlerActivator());

            sagaActivator.Register((bus, context) => new TypicalContendedSagaExample(bus));

            Configure.With(sagaActivator)
            .Logging(l => l.Use(loggerFactory))
            .Transport(t => t.UseInMemoryTransport(network, "lock-test"))
            .Sagas(s =>
            {
                s.StoreInMemory();
                s.EnforceExclusiveAccess(new CustomLocker());
            })
            .Routing(t => t.TypeBased().Map <ProcessThisThingRequest>("processor"))
            .Options(o =>
            {
                o.SetMaxParallelism(100);
                o.SetNumberOfWorkers(10);
            })
            .Start();

            await sagaActivator.Bus.SendLocal(new ProcessTheseThings(Enumerable.Range(0, 10).Select(no => $"THING-{no}")));

            await Task.Delay(TimeSpan.FromSeconds(3));

            Assert.That(loggerFactory.Count(l => l.Level >= LogLevel.Warn), Is.EqualTo(0), "Didn't expect any logging with level WARNING or above");
        }