BusTests.DeserializationErrors.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using Abc.Zebus.Lotus;
  5. using Abc.Zebus.Testing;
  6. using Abc.Zebus.Testing.Extensions;
  7. using Abc.Zebus.Testing.Transport;
  8. using Abc.Zebus.Tests.Messages;
  9. using Abc.Zebus.Transport;
  10. using Abc.Zebus.Util;
  11. using NUnit.Framework;
  12. namespace Abc.Zebus.Tests.Core
  13. {
  14. public partial class BusTests
  15. {
  16. public class DeserializationErrors : BusTests
  17. {
  18. public override void Setup()
  19. {
  20. base.Setup();
  21. _bus.DeserializationFailureDumpDirectoryPath = PathUtil.InBaseDirectory(Guid.NewGuid().ToString().Substring(0, 4));
  22. _configuration.IsErrorPublicationEnabled = true;
  23. }
  24. [Test]
  25. public void should_send_MessageProcessingFailed_if_unable_to_deserialize_message()
  26. {
  27. SetupPeersHandlingMessage<MessageProcessingFailed>(_peerUp);
  28. _bus.Start();
  29. var command = new FakeCommand(123);
  30. _messageSerializer.AddSerializationExceptionFor(command.TypeId(), "Serialization error");
  31. using (SystemDateTime.PauseTime())
  32. using (MessageId.PauseIdGeneration())
  33. {
  34. var transportMessage = command.ToTransportMessage();
  35. var messageProcessingFailedBytes = new MessageProcessingFailed(null, null, null, DateTime.UtcNow, null).ToTransportMessage().Content;
  36. _messageSerializer.AddSerializationFuncFor<MessageProcessingFailed>(x =>
  37. {
  38. x.FailingMessage.ShouldEqual(transportMessage);
  39. x.ExceptionUtcTime.ShouldEqual(SystemDateTime.UtcNow);
  40. x.ExceptionMessage.ShouldContain("Unable to deserialize message");
  41. return messageProcessingFailedBytes;
  42. });
  43. _transport.RaiseMessageReceived(transportMessage);
  44. var processingFailedTransportMessage = new TransportMessage(MessageUtil.TypeId<MessageProcessingFailed>(), messageProcessingFailedBytes, _self);
  45. _transport.ExpectExactly(new TransportMessageSent(processingFailedTransportMessage, _peerUp));
  46. }
  47. }
  48. [Test]
  49. public void should_send_MessageProcessingFailed_when_error_publication_os_not_enabled()
  50. {
  51. _configuration.IsErrorPublicationEnabled = false;
  52. SetupPeersHandlingMessage<MessageProcessingFailed>(_peerUp);
  53. _bus.Start();
  54. var exception = new Exception("Expected exception");
  55. _messageSerializer.AddSerializationExceptionFor<FakeCommand>(exception);
  56. _transport.RaiseMessageReceived(new FakeCommand(123).ToTransportMessage());
  57. _transport.ExpectNothing();
  58. }
  59. [Test]
  60. public void should_include_exception_in_MessageProcessingFailed()
  61. {
  62. SetupPeersHandlingMessage<MessageProcessingFailed>(_peerUp);
  63. _bus.Start();
  64. var exception = new Exception("Expected exception");
  65. _messageSerializer.AddSerializationExceptionFor<FakeCommand>(exception);
  66. _transport.RaiseMessageReceived(new FakeCommand(123).ToTransportMessage());
  67. var message = _transport.MessagesSent.OfType<MessageProcessingFailed>().ExpectedSingle();
  68. message.ExceptionMessage.ShouldContain(exception.ToString());
  69. }
  70. [Test]
  71. public void should_include_dump_path_in_MessageProcessingFailed()
  72. {
  73. SetupPeersHandlingMessage<MessageProcessingFailed>(_peerUp);
  74. _bus.Start();
  75. var exception = new Exception("Expected exception");
  76. _messageSerializer.AddSerializationExceptionFor<FakeCommand>(exception);
  77. _transport.RaiseMessageReceived(new FakeCommand(123).ToTransportMessage());
  78. var message = _transport.MessagesSent.OfType<MessageProcessingFailed>().ExpectedSingle();
  79. message.ExceptionMessage.ShouldContain(_bus.DeserializationFailureDumpDirectoryPath);
  80. }
  81. [Test]
  82. public void should_ack_transport_when_handling_undeserializable_message()
  83. {
  84. var command = new FakeCommand(123);
  85. _messageSerializer.AddSerializationExceptionFor(command.TypeId());
  86. var transportMessage = command.ToTransportMessage();
  87. _transport.RaiseMessageReceived(transportMessage);
  88. _transport.AckedMessages.ShouldContain(transportMessage);
  89. }
  90. [Test]
  91. public void should_dump_incoming_message_if_unable_to_deserialize_it()
  92. {
  93. var command = new FakeCommand(123);
  94. _messageSerializer.AddSerializationExceptionFor(command.TypeId());
  95. var transportMessage = command.ToTransportMessage();
  96. _transport.RaiseMessageReceived(transportMessage);
  97. var dumpFileName = System.IO.Directory.GetFiles(_bus.DeserializationFailureDumpDirectoryPath).ExpectedSingle();
  98. dumpFileName.ShouldContain("Abc.Zebus.Tests.Messages.FakeCommand");
  99. File.ReadAllBytes(dumpFileName).Length.ShouldEqual(2);
  100. }
  101. }
  102. }
  103. }