public void CorrelationSequenceIsIncremented() { var correlationSequenceNumbers = new List<int>(); var counter = new SharedCounter(1); _activator1.Handle<string>(async (bus, ctx, str) => { correlationSequenceNumbers.Add(int.Parse(ctx.Headers[Headers.CorrelationSequence])); await bus.Advanced.Routing.Send("bus2", "hej!"); }); _activator2.Handle<string>(async (bus, ctx, str) => { correlationSequenceNumbers.Add(int.Parse(ctx.Headers[Headers.CorrelationSequence])); await bus.Advanced.Routing.Send("bus3", "hej!"); }); _activator3.Handle<string>(async (bus, ctx, str) => { correlationSequenceNumbers.Add(int.Parse(ctx.Headers[Headers.CorrelationSequence])); counter.Decrement(); }); _activator1.Bus.SendLocal("heeeej!").Wait(); counter.WaitForResetEvent(); Assert.That(correlationSequenceNumbers, Is.EqualTo(new[] { 0, 1, 2 })); }
public void SentMessageCarriesTheTypeHeader() { var activator = Using(new BuiltinHandlerActivator()); var receivedHeaders = new Dictionary<string, string>(); var counter = new SharedCounter(1); activator.Handle<SomeMessage>(async (bus, context, message) => { foreach (var kvp in context.Headers) { receivedHeaders.Add(kvp.Key, kvp.Value); } counter.Decrement(); }); Configure.With(activator) .Transport(t => t.UseInMemoryTransport(new InMemNetwork(), "type-header-test")) .Options(o => o.LogPipeline()) .Start(); activator.Bus.SendLocal(new SomeMessage()).Wait(); counter.WaitForResetEvent(); Assert.That(receivedHeaders.ContainsKey(Headers.Type), Is.True); Assert.That(receivedHeaders[Headers.Type], Is.EqualTo("Rebus.Tests.Integration.SomeMessage, Rebus.Tests")); }
public void CorrelationIdFlows() { var correlationIds = new List<string>(); var counter = new SharedCounter(1); _activator1.Handle<string>(async (bus, ctx, str) => { correlationIds.Add(ctx.Headers[Headers.CorrelationId]); await bus.Advanced.Routing.Send("bus2", "hej!"); }); _activator2.Handle<string>(async (bus, ctx, str) => { correlationIds.Add(ctx.Headers[Headers.CorrelationId]); await bus.Advanced.Routing.Send("bus3", "hej!"); }); _activator3.Handle<string>(async (bus, ctx, str) => { correlationIds.Add(ctx.Headers[Headers.CorrelationId]); counter.Decrement(); }); _activator1.Bus.SendLocal("heeeej!").Wait(); counter.WaitForResetEvent(); Assert.That(correlationIds.GroupBy(c => c).Count(), Is.EqualTo(1)); }
public void CanDeferTransportMessage() { var counter = new SharedCounter(1); Using(counter); var customHeaders = new Dictionary<string, string> { {"testheader", "customizzle valuizzle"} }; var didDeferTheMessage = false; _forwarderActivator.Handle<string>(async (bus, str) => { if (!didDeferTheMessage) { Console.WriteLine("Got the message for the first time - deferring it!"); await bus.Advanced.TransportMessage.Defer(TimeSpan.FromSeconds(2), customHeaders); didDeferTheMessage = true; return; } Console.WriteLine("Got the message after it was deferred... nice!"); counter.Decrement(); }); _forwarderActivator.Bus.SendLocal("hej med dig min ven!!!").Wait(); counter.WaitForResetEvent(); }
public async Task ItWorks() { var counter = new SharedCounter(1); Using(counter); _activator.Handle<string>(async str => { throw new ApplicationException("1st level!!"); }); _activator.Handle<IFailed<string>>(async failed => { if (failed.Message != "hej med dig!") { counter.Fail("Did not receive the expected message!"); return; } counter.Decrement(); }); await _bus.SendLocal("hej med dig!"); counter.WaitForResetEvent(); }
public void CanForwardMessageToErrorQueue() { var sharedCounter = new SharedCounter(1) { Delay = TimeSpan.FromSeconds(0.1) }; Using(sharedCounter); _forwarderActivator.Handle<string>(async (bus, str) => { await bus.Advanced.TransportMessage.Forward(ForwardedMessagesQueue, new Dictionary<string, string> {{"testheader", "OK"}}); }); _receiverActivator.Handle<string>(async (bus, context, str) => { var headers = context.TransportMessage.Headers; if (!headers.ContainsKey("testheader")) { sharedCounter.Fail("Could not find 'testheader' header!"); return; } var headerValue = headers["testheader"]; if (headerValue != "OK") { sharedCounter.Fail("'testheader' header had value {0}", headerValue); return; } sharedCounter.Decrement(); }); _forwarderActivator.Bus.SendLocal("hej med dig min ven!!!").Wait(); sharedCounter.WaitForResetEvent(); }
public async Task ItWorksWithCovarianceToo() { var counter = new SharedCounter(1); Using(counter); _activator.Handle<BaseMessage>(async baseMessage => { throw new ApplicationException("1st level!!"); }); _activator.Handle<IFailed<BaseMessage>>(async failed => { if (failed.Message is ConcreteMessage) { counter.Decrement(); return; } counter.Fail("Did not receive the expected message!"); }); await _bus.SendLocal(new ConcreteMessage()); counter.WaitForResetEvent(); }
public async Task DoesNotBackOffAtAllWhenBusy(int messageCount) { var counter = new SharedCounter(messageCount); _activator.Handle<string>(async str => { await Task.Delay(1); counter.Decrement(); }); var startTime = DateTime.UtcNow; await Task.Delay(TimeSpan.FromSeconds(5)); Printt("Sending 100k msgs"); await Task.WhenAll(Enumerable.Range(0, messageCount) .Select(i => _activator.Bus.SendLocal($"THIS IS MESSAGE {i}"))); Printt("Receiving them..."); _activator.Bus.Advanced.Workers.SetNumberOfWorkers(1); counter.WaitForResetEvent(120); Printt("Done... waiting a little extra"); await Task.Delay(TimeSpan.FromSeconds(5)); var stopTime = DateTime.UtcNow; var waitsPerSecond = _snitch.WaitTimes .GroupBy(t => t.RoundTo(TimeSpan.FromSeconds(1))) .ToDictionary(g => g.Key, g => g.Count()); var waitNoMessagesPerSecond = _snitch.WaitNoMessageTimes .GroupBy(t => t.RoundTo(TimeSpan.FromSeconds(1))) .ToDictionary(g => g.Key, g => g.Count()); var seconds = startTime.RoundTo(TimeSpan.FromSeconds(1)).To(stopTime.RoundTo(TimeSpan.FromSeconds(1))) .GetIntervals(TimeSpan.FromSeconds(1)); Console.WriteLine(string.Join(Environment.NewLine, seconds.Select(time => $"{time}: {new string('.', waitsPerSecond.GetValueOrDefault(time))}{new string('*', waitNoMessagesPerSecond.GetValueOrDefault(time))}"))); }
public void DoesNotLogWarningsUnderNormalUse() { var counter = new SharedCounter(3); _activator.Handle<string>(async str => counter.Decrement()); _activator.Bus.SendLocal("hej").Wait(); _activator.Bus.SendLocal("med").Wait(); _activator.Bus.SendLocal("dig").Wait(); counter.WaitForResetEvent(); CleanUpDisposables(); var logLinesWarnLevelOrAbove = _logs.Where(l => l.Level >= LogLevel.Warn).ToList(); Assert.That(logLinesWarnLevelOrAbove.Any(), Is.False, $@"Got the following log lines >= WARN: {string.Join(Environment.NewLine, logLinesWarnLevelOrAbove)}"); }
public async Task CanInitiateSagaAndOverrideItsId() { var counter = new SharedCounter(5); _activator.Register(() => new DefaultSaga(counter)); var id1 = Guid.NewGuid(); var id2 = Guid.NewGuid(); // five messages using 2 ids await _activator.Bus.SendLocal(new DefaultSagaMessage(id1)); await _activator.Bus.SendLocal(new DefaultSagaMessage(id2)); await _activator.Bus.SendLocal(new DefaultSagaMessage(id1)); await _activator.Bus.SendLocal(new DefaultSagaMessage(id2)); await _activator.Bus.SendLocal(new DefaultSagaMessage(id1)); counter.WaitForResetEvent(); var sagaInstances = _sagas.Instances.ToList(); Assert.That(sagaInstances.Count, Is.EqualTo(2)); }
public void CorrelationIdIsFirstMessageId() { var messageIds = new List<string>(); var correlationIds = new List<string>(); var counter = new SharedCounter(1); _activator1.Handle<string>(async (bus, ctx, str) => { messageIds.Add(ctx.Headers[Headers.MessageId]); correlationIds.Add(ctx.Headers[Headers.CorrelationId]); await bus.Advanced.Routing.Send("bus2", "hej!"); }); _activator2.Handle<string>(async (bus, ctx, str) => { messageIds.Add(ctx.Headers[Headers.MessageId]); correlationIds.Add(ctx.Headers[Headers.CorrelationId]); counter.Decrement(); }); _activator1.Bus.SendLocal("heeeej!").Wait(); counter.WaitForResetEvent(); var firstMessageId = messageIds.First(); Assert.That(correlationIds.All(c => c == firstMessageId), Is.True); }
public void MessageIdsAreDifferent() { var messageIds = new List<string>(); var counter = new SharedCounter(1); _activator1.Handle<string>(async (bus, ctx, str) => { messageIds.Add(ctx.Headers[Headers.MessageId]); await bus.Advanced.Routing.Send("bus2", "hej!"); }); _activator2.Handle<string>(async (bus, ctx, str) => { messageIds.Add(ctx.Headers[Headers.MessageId]); counter.Decrement(); }); _activator1.Bus.SendLocal("heeeej!").Wait(); counter.WaitForResetEvent(); Assert.That(messageIds.GroupBy(i => i).Count(), Is.EqualTo(2)); }
async Task RunDeferTest() { var replyCounter = new SharedCounter(1); _client.Handle<string>(async reply => { Console.WriteLine($"CLIENT got reply '{reply}' (elapsed: {_stopwatch.Elapsed.TotalSeconds:0.# s})"); replyCounter.Decrement(); }); await _client.Bus.Send("request"); replyCounter.WaitForResetEvent(); }
public async Task FailedMessageAllowsForAccessingHeaders() { var counter = new SharedCounter(1); Using(counter); _activator.Handle<string>(async str => { throw new ApplicationException("1st level!!"); }); var headersOfFailedMessage = new Dictionary<string, string>(); _activator.Handle<IFailed<string>>(async failed => { if (failed.Message != "hej med dig!") { counter.Fail("Did not receive the expected message!"); return; } foreach (var kvp in failed.Headers) { headersOfFailedMessage.Add(kvp.Key, kvp.Value); } Console.WriteLine(); Console.WriteLine("-------------------------------------------------------------------------------------------------------------------------------------------------------------"); Console.WriteLine("-------------------------------------------------------------------------------------------------------------------------------------------------------------"); Console.WriteLine("-------------------------------------------------------------------------------------------------------------------------------------------------------------"); Console.WriteLine("ERROR DESCRIPTION:"); Console.WriteLine(); Console.WriteLine(failed.ErrorDescription); Console.WriteLine("-------------------------------------------------------------------------------------------------------------------------------------------------------------"); Console.WriteLine("-------------------------------------------------------------------------------------------------------------------------------------------------------------"); Console.WriteLine("-------------------------------------------------------------------------------------------------------------------------------------------------------------"); Console.WriteLine("CAUGHT EXCEPTIONS:"); Console.WriteLine(); Console.WriteLine(string.Join(Environment.NewLine + Environment.NewLine, failed.Exceptions)); Console.WriteLine("-------------------------------------------------------------------------------------------------------------------------------------------------------------"); Console.WriteLine("-------------------------------------------------------------------------------------------------------------------------------------------------------------"); Console.WriteLine("-------------------------------------------------------------------------------------------------------------------------------------------------------------"); Console.WriteLine(); counter.Decrement(); }); var headers = new Dictionary<string, string> { {"custom-header", "with-a-custom-value" } }; await _bus.SendLocal("hej med dig!", headers); counter.WaitForResetEvent(); Console.WriteLine(string.Join(Environment.NewLine, headersOfFailedMessage.Select(kvp => $" {kvp.Key}: {kvp.Value}"))); Assert.That(headersOfFailedMessage["custom-header"], Is.EqualTo("with-a-custom-value")); }
public async Task IncludesFullErrorDetailsWhenSecondLevelRetriesFailToo() { var counter = new SharedCounter(1); Using(counter); _activator.Handle<string>(async str => { throw new ApplicationException("1st level!!"); }); _activator.Handle<IFailed<string>>(async failed => { throw new ApplicationException("2nd level!!"); }); await _bus.SendLocal("hej med dig!"); var transportMessage = await _network.WaitForNextMessageFrom("error"); var errorDetails = transportMessage.Headers[Headers.ErrorDetails]; Console.WriteLine(errorDetails); Assert.That(errorDetails, Does.Contain("1st level!!")); Assert.That(errorDetails, Does.Contain("2nd level!!")); }