Skip to content

Commit

Permalink
Throw from NServiceBus.MsmqUtilities.Convert will result in message loss
Browse files Browse the repository at this point in the history
fixes #2583
also add allow exceptions to acceptance tests
  • Loading branch information
SimonCropp committed Nov 27, 2014
1 parent cb8ec41 commit f3a7821
Show file tree
Hide file tree
Showing 27 changed files with 466 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ public EndpointConfigurationBuilder CustomEndpointName(string customEndpointName
return this;
}

public EndpointConfigurationBuilder AllowExceptions()
{
configuration.AllowExceptions = true;

return this;
}

public EndpointConfigurationBuilder AddMapping<T>(Type endpoint)
{
configuration.EndpointMappings.Add(typeof(T),endpoint);
Expand Down
26 changes: 26 additions & 0 deletions src/NServiceBus.AcceptanceTesting/ScenarioContext.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
namespace NServiceBus.AcceptanceTesting
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Activation;
using System.Runtime.Remoting.Contexts;
using System.Runtime.Remoting.Messaging;
Expand Down Expand Up @@ -60,6 +62,30 @@ public IMessage SyncProcessMessage(IMessage msg)
}

public bool EndpointsStarted { get; set; }
public string Exceptions { get; set; }
public void RecordLog(string endpointName, string level, string message)
{
endpointLogs.Add(new EndpointLogItem
{
Endpoint = endpointName,
Level = level.ToLower(),
Message = message
});
}

public List<EndpointLogItem> GetAllLogs()
{
return endpointLogs.ToList();
}

List<EndpointLogItem> endpointLogs = new List<EndpointLogItem>();

public class EndpointLogItem
{
public string Endpoint { get; set; }
public string Message { get; set; }
public string Level { get; set; }
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public string EndpointName

public Type AuditEndpoint { get; set; }

public bool AllowExceptions { get; set; }

string endpointName;
}
}
10 changes: 9 additions & 1 deletion src/NServiceBus.AcceptanceTesting/Support/ScenarioRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,15 @@ static RunResult PerformTestRun(IList<EndpointBehaviour> behaviorDescriptors, IL
{
runResult.ActiveEndpoints = runners.Select(r => r.EndpointName).ToList();

PerformScenarios(runDescriptor,runners, () => done(runDescriptor.ScenarioContext));
PerformScenarios(runDescriptor,runners, () =>
{
if (!string.IsNullOrEmpty(runDescriptor.ScenarioContext.Exceptions))
{
Console.Out.WriteLine(runDescriptor.ScenarioContext.Exceptions);
throw new Exception("Failures in endpoints");
}
return done(runDescriptor.ScenarioContext);
});
}
finally
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace NServiceBus.AcceptanceTests.BasicMessaging
{
using System;
using NUnit.Framework;

public class When_starting_a_send_only : NServiceBusAcceptanceTest
{
[Test]
public void Should_not_need_audit_or_fault_forwarding_config_to_start()
{
using ((IDisposable)Configure.With(new Type[]
{
})
.DefaultBuilder()
.UnicastBus()
.SendOnly())
{
}

}
}
}
49 changes: 40 additions & 9 deletions src/NServiceBus.AcceptanceTests/EndpointTemplates/DefaultServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@
using AcceptanceTesting.Support;
using Config.ConfigurationSource;
using Hosting.Helpers;
using log4net.Appender;
using log4net.Core;
using NServiceBus;
using NServiceBus.AcceptanceTesting;
using Settings;

public class DefaultServer : IEndpointSetupTemplate
{
public Configure GetConfiguration(RunDescriptor runDescriptor, EndpointConfiguration endpointConfiguration, IConfigurationSource configSource)
{
var settings = runDescriptor.Settings;
SetupLogging(endpointConfiguration);

SetupLogging(endpointConfiguration, runDescriptor.ScenarioContext);

var types = GetTypesToUse(endpointConfiguration);

Expand Down Expand Up @@ -77,7 +80,7 @@ static IEnumerable<Type> GetNestedTypeRecursive(Type rootType,Type builderType)
}
}

static void SetupLogging(EndpointConfiguration endpointConfiguration)
static void SetupLogging(EndpointConfiguration endpointConfiguration, ScenarioContext scenarioContext)
{
var logDir = ".\\logfiles\\";

Expand All @@ -89,14 +92,42 @@ static void SetupLogging(EndpointConfiguration endpointConfiguration)
if (File.Exists(logFile))
File.Delete(logFile);

var logLevel = "WARN";
var logLevelOverride = Environment.GetEnvironmentVariable("tests_loglevel");
try
{
SetLoggingLibrary.Log4Net(null, new ContextAppender(scenarioContext, endpointConfiguration));
}
catch (Exception ex)
{
Console.Out.WriteLine(ex);

if (!string.IsNullOrEmpty(logLevelOverride))
logLevel = logLevelOverride;
}
}
}
public class ContextAppender : AppenderSkeleton
{
public ContextAppender(ScenarioContext context, EndpointConfiguration endpointConfiguration)
{
this.context = context;
this.endpointConfiguration = endpointConfiguration;
}

protected override void Append(LoggingEvent loggingEvent)
{
if (!endpointConfiguration.AllowExceptions && loggingEvent.ExceptionObject != null)
{
lock (context)
{
context.Exceptions += loggingEvent.ExceptionObject + "/n/r";
}
}
if (loggingEvent.Level >= Level.Warn)
{
context.RecordLog(endpointConfiguration.EndpointName, loggingEvent.Level.ToString(), loggingEvent.RenderedMessage);
}

SetLoggingLibrary.Log4Net(null,
Logging.Loggers.Log4NetAdapter.Log4NetAppenderFactory.CreateRollingFileAppender(logLevel, logFile));
}

ScenarioContext context;
EndpointConfiguration endpointConfiguration;
}
}
17 changes: 17 additions & 0 deletions src/NServiceBus.AcceptanceTests/Exceptions/SerializerCorrupter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace NServiceBus.AcceptanceTests.Exceptions
{
using System;
using System.Reflection;

static class SerializerCorrupter
{

public static void Corrupt()
{
var msmqUtilitiesType = Type.GetType("NServiceBus.Transports.Msmq.MsmqUtilities, NServiceBus.Core");
var headerSerializerField = msmqUtilitiesType.GetField("headerSerializer", BindingFlags.Static | BindingFlags.NonPublic);
headerSerializerField.SetValue(null, null);
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
namespace NServiceBus.AcceptanceTests.Exceptions
{
using System;
using System.Linq;
using NServiceBus.AcceptanceTesting;
using NServiceBus.AcceptanceTests.EndpointTemplates;
using NUnit.Framework;

public class When_cant_convert_to_TransportMessage : NServiceBusAcceptanceTest
{
[Test]
public void Should_send_message_to_error_queue()
{
Scenario.Define<Context>()
.WithEndpoint<Sender>(b => b.Given(bus => bus.Send(new Message())))
.WithEndpoint<Receiver>()
.Done(c => c.GetAllLogs().Any(l => l.Level == "error"))
.Repeat(r => r.For(ScenarioDescriptors.Transports.Msmq))
.Should(c =>
{
var logs = c.GetAllLogs();
Assert.True(logs.Any(l => l.Message.Contains("is corrupt and will be moved to")));
})
.Run();
}

public class Context : ScenarioContext
{
}

public class Sender : EndpointConfigurationBuilder
{
public Sender()
{
EndpointSetup<DefaultServer>()
.AddMapping<Message>(typeof(Receiver));
}
}

public class Receiver : EndpointConfigurationBuilder
{
public Receiver()
{
SerializerCorrupter.Corrupt();
EndpointSetup<DefaultServer>()
.AllowExceptions();
}

}

[Serializable]
public class Message : IMessage
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
namespace NServiceBus.AcceptanceTests.Exceptions
{
using System;
using System.Linq;
using NServiceBus.AcceptanceTesting;
using NServiceBus.AcceptanceTests.EndpointTemplates;
using NUnit.Framework;
using IMessage = NServiceBus.IMessage;

public class When_cant_convert_to_TransportMessage_NoTransactions : NServiceBusAcceptanceTest
{
[Test]
public void Should_send_message_to_error_queue()
{
Scenario.Define<Context>()
.WithEndpoint<Sender>(b => b.Given(bus => bus.Send(new Message())))
.WithEndpoint<Receiver>()
.Done(c => c.GetAllLogs().Any(l => l.Level == "error"))
.Repeat(r => r.For(ScenarioDescriptors.Transports.Msmq))
.Should(c =>
{
var logs = c.GetAllLogs();
Assert.True(logs.Any(l => l.Message.Contains("is corrupt and will be moved to")));
})
.Run();
}

public class Context : ScenarioContext
{
}

public class Sender : EndpointConfigurationBuilder
{
public Sender()
{
Configure.Transactions.Disable();
EndpointSetup<DefaultServer>()
.AddMapping<Message>(typeof(Receiver));
}
}

public class Receiver : EndpointConfigurationBuilder
{
public Receiver()
{
SerializerCorrupter.Corrupt();
Configure.Transactions.Disable();
EndpointSetup<DefaultServer>()
.AllowExceptions();
}
}

[Serializable]
public class Message : IMessage
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
namespace NServiceBus.AcceptanceTests.Exceptions
{
using System;
using System.Linq;
using NServiceBus.AcceptanceTesting;
using NServiceBus.AcceptanceTests.EndpointTemplates;
using NUnit.Framework;

public class When_cant_convert_to_TransportMessage_SuppressedDTC : NServiceBusAcceptanceTest
{
[Test]
public void Should_send_message_to_error_queue()
{
Scenario.Define<Context>()
.WithEndpoint<Sender>(b => b.Given(bus => bus.Send(new Message())))
.WithEndpoint<Receiver>()
.Done(c => c.GetAllLogs().Any(l => l.Level == "error"))
.Repeat(r => r.For(ScenarioDescriptors.Transports.Msmq))
.Should(c =>
{
var logs = c.GetAllLogs();
Assert.True(logs.Any(l => l.Message.Contains("is corrupt and will be moved to")));
})
.Run();
}

public class Context : ScenarioContext
{
}

public class Sender : EndpointConfigurationBuilder
{
public Sender()
{
Configure.Transactions.Advanced(settings => settings.DisableDistributedTransactions());
EndpointSetup<DefaultServer>()
.AddMapping<Message>(typeof(Receiver));
}
}

public class Receiver : EndpointConfigurationBuilder
{
public Receiver()
{
SerializerCorrupter.Corrupt();
Configure.Transactions.Advanced(settings => settings.DisableDistributedTransactions());
EndpointSetup<DefaultServer>()
.AllowExceptions();
}
}

[Serializable]
public class Message : IMessage
{
}
}
}

0 comments on commit f3a7821

Please sign in to comment.