Allow registration of message id factory (#24)

* Allow registration of message id factory

* guarding againts null argument + providing a better exception when ID generator fails

* applying changes mandated by stylecop

* more meaningful name

* xml doco

* Logical grouping of tests

* bringing all BrokeredMessage tests under one roof
This commit is contained in:
Sean Feldman 2016-12-08 08:24:14 -07:00 коммит произвёл John Taubensee
Родитель a40d6e0718
Коммит ec342b5635
2 изменённых файлов: 88 добавлений и 0 удалений

Просмотреть файл

@ -15,6 +15,8 @@ namespace Microsoft.Azure.ServiceBus
/// <summary>Represents the unit of communication between ServiceBus client and Service.</summary>
public sealed class BrokeredMessage : IDisposable
{
static Func<string> messageIdGeneratorFunc = () => (string)null;
readonly object disposablesSyncObject = new object();
readonly bool ownsBodyStream;
readonly bool bodyObjectDecoded;
@ -55,6 +57,14 @@ namespace Microsoft.Azure.ServiceBus
/// <summary>Initializes a new instance of the <see cref="BrokeredMessage" /> class.</summary>
public BrokeredMessage()
{
try
{
this.messageId = messageIdGeneratorFunc();
}
catch (Exception ex)
{
throw new InvalidOperationException("BrokeredMessage ID generator function has failed.", ex);
}
}
/// <summary>Initializes a new instance of the
@ -902,6 +912,20 @@ namespace Microsoft.Azure.ServiceBus
internal MessageReceiver Receiver { get; set; }
/// <summary>Specify generator to be used to generate BrokeredMessage.MessageId value.
/// <param name="messageIdGenerator">Message ID generator.</param>
/// <remarks>Be default, no value is assigned.</remarks>
/// </summary>
/// <exception cref="ArgumentNullException">Thrown if invoked with null.</exception>
public static void SetMessageIdGenerator(Func<string> messageIdGenerator)
{
if (messageIdGenerator == null)
{
throw new ArgumentNullException(nameof(messageIdGenerator));
}
messageIdGeneratorFunc = messageIdGenerator;
}
/// <summary>Deserializes the brokered message body into an object of the specified type by using the
/// <see cref="System.Runtime.Serialization.DataContractSerializer" /> with a binary
/// <see cref="System.Xml.XmlDictionaryReader" />.</summary>

Просмотреть файл

@ -0,0 +1,64 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
namespace Microsoft.Azure.ServiceBus.UnitTests
{
using System;
using Xunit;
public class BrokeredMessageTests
{
public class When_BrokeredMessage_message_id_generator_is_not_specified
{
[Fact]
public void Message_should_have_MessageId_set()
{
var message = new BrokeredMessage();
Assert.Null(message.MessageId);
}
}
public class When_BrokeredMessage_message_is_given_a_null_id_generator
{
[Fact]
public void Should_throw_an_exception()
{
Assert.Throws<ArgumentNullException>(() => BrokeredMessage.SetMessageIdGenerator(null));
}
}
public class When_BrokeredMessage_id_generator_throws
{
[Fact]
public void Should_throw_with_original_exception_included()
{
var exceptionToThrow = new Exception("boom!");
Func<string> idGenerator = () =>
{
throw exceptionToThrow;
};
BrokeredMessage.SetMessageIdGenerator(idGenerator);
var exception = Assert.Throws<InvalidOperationException>(() => new BrokeredMessage());
Assert.Equal(exceptionToThrow, exception.InnerException);
}
}
public class When_BrokeredMessage_message_id_generator_is_specified
{
[Fact]
public void Message_should_have_MessageId_set()
{
var seed = 1;
BrokeredMessage.SetMessageIdGenerator(() => $"id-{seed++}");
var message1 = new BrokeredMessage();
var message2 = new BrokeredMessage();
Assert.Equal("id-1", message1.MessageId);
Assert.Equal("id-2", message2.MessageId);
}
}
}
}