public async Task VerifySubscriberIsInitializedWhenRegistered() { // Setup the message bus. var cs = Any.String(); var subscriber = new MockSubscriber(); var mbf = new MockMessageBusFactory { Subscriber = subscriber }; var mbd = new MessageBusDescription { ConnectionString = cs, Factory = mbf }; var bus = new MessageBus(mbd); var entity = "MessageSendSuccess;MessageSendFailure"; var name = "MessageSendSuccess"; Assert.IsFalse( subscriber.IsInitialized, "The subscriber should not be initialized before it is registered."); var called = false; // Register the subscriber. await bus.RegisterHandlerAsync(entity, name, message => Task.Run(() => { called = true; })); Assert.IsTrue(subscriber.IsInitialized, "The subscriber should be initialized after it is registered."); Assert.AreEqual(subscriber.Description.ConnectionString, cs); Assert.AreEqual(subscriber.Description.Entity, entity); Assert.AreEqual(subscriber.Description.Name, name); await subscriber.Handler.Invoke(null); Assert.IsTrue(called, "The handler must be callable."); // Close the bus. await bus.CloseAsync(); Assert.IsTrue(subscriber.IsClosed, "The subscriber should be closed after the bus is closed."); }
/// <summary> /// Handles the specified subscription command line options. /// </summary> /// <param name="options"> /// The options. /// </param> public static void Handle(SubscriptionOptions options) { if (options.IsVerbose) { Debug.Listeners.Add(new ColoredConsoleTraceListener()); } OutputWriter = !string.IsNullOrWhiteSpace(options.OutputFileName) ? new StreamWriter(options.OutputFileName) : Console.Out; var description = new MessageBusDescription { ConnectionString = options.ConnectionString, Factory = DependencyResolver.Resolve<IMessageBusFactory>( options.Factory), StorageConnectionString = options.StorageConnectionString }; var bus = new MessageBus(description); bus.RegisterHandlerAsync(options.Entity, options.Name, OnMessageArrived).Wait(); Debug.Print("This is a debug print."); Console.WriteLine("Press any key to exit..."); Console.ReadKey(); Console.WriteLine("Closing message bus..."); bus.CloseAsync().ContinueWith(t => Console.WriteLine("Message bus is closed.")).Wait(); }
/// <summary> /// Handles the specified subscription command line options. /// </summary> /// <param name="options"> /// The options. /// </param> public static void Handle(SubscriptionOptions options) { if (options.IsVerbose) { Debug.Listeners.Add(new ColoredConsoleTraceListener()); } OutputWriter = !string.IsNullOrWhiteSpace(options.OutputFileName) ? new StreamWriter(options.OutputFileName) : Console.Out; var description = new MessageBusDescription { ConnectionString = options.ConnectionString, Factory = DependencyResolver.Resolve <IMessageBusFactory>( options.Factory), StorageConnectionString = options.StorageConnectionString }; var bus = new MessageBus(description); bus.RegisterHandlerAsync(options.Entity, options.Name, OnMessageArrived).Wait(); Debug.Print("This is a debug print."); Console.WriteLine("Press any key to exit..."); Console.ReadKey(); Console.WriteLine("Closing message bus..."); bus.CloseAsync().ContinueWith(t => Console.WriteLine("Message bus is closed.")).Wait(); }
/// <summary> /// Creates the message bus. /// </summary> /// <param name="cs"> /// The connection string. /// </param> /// <param name="factory"> /// The factory. /// </param> /// <param name="storage"> /// The storage connection string. /// </param> /// <returns> /// The <see cref="IMessageBus"/> instance. /// </returns> private static IMessageBus CreateMessageBus(string cs, string factory, string storage) { var description = new MessageBusDescription { ConnectionString = cs, Factory = DependencyResolver.Resolve <IMessageBusFactory>(factory), StorageConnectionString = storage }; return(new MessageBus(description)); }
public async Task VerifyMessagesArePublished() { // Setup the message bus. var cs = Any.String(); var publisher = new MockPublisher(); var mbf = new MockMessageBusFactory { Publisher = publisher }; var mbd = new MessageBusDescription { ConnectionString = cs, Factory = mbf }; var bus = new MessageBus(mbd); var entity = Any.String(); Assert.IsFalse( publisher.IsInitialized, "The publisher should not be initialized before it is called the first time."); // Send a message on the bus. var message = new MockMessage { CorrelationKey = Any.String(), Message = Any.String(), MessageKey = Any.String(), PartitionKey = Any.String(), Properties = new Dictionary <string, object> { { Any.String(), Any.String() } } }; await bus.SendAsync(entity, message); Assert.IsTrue( publisher.IsInitialized, "The publisher should be initialized after it is called the first time."); Assert.AreEqual(publisher.Description.ConnectionString, cs); Assert.AreEqual(publisher.Description.Entity, entity); await bus.CloseAsync(); Assert.IsTrue(publisher.IsClosed, "The publisher should be closed."); // Verify the message sent. Assert.AreEqual(publisher.IsInitialized, true); Assert.AreEqual(publisher.Message.CorrelationKey, message.CorrelationKey); Assert.AreEqual(publisher.Message.Message, message.Message); Assert.AreEqual(publisher.Message.MessageKey, message.MessageKey); Assert.AreEqual(publisher.Message.PartitionKey, message.PartitionKey); }
public async Task VerifyMessagesArePublished() { // Setup the message bus. var cs = Any.String(); var publisher = new MockPublisher(); var mbf = new MockMessageBusFactory { Publisher = publisher }; var mbd = new MessageBusDescription { ConnectionString = cs, Factory = mbf }; var bus = new MessageBus(mbd); var entity = Any.String(); Assert.IsFalse( publisher.IsInitialized, "The publisher should not be initialized before it is called the first time."); // Send a message on the bus. var message = new MockMessage { CorrelationKey = Any.String(), Message = Any.String(), MessageKey = Any.String(), PartitionKey = Any.String(), Properties = new Dictionary<string, object> { { Any.String(), Any.String() } } }; await bus.SendAsync(entity, message); Assert.IsTrue( publisher.IsInitialized, "The publisher should be initialized after it is called the first time."); Assert.AreEqual(publisher.Description.ConnectionString, cs); Assert.AreEqual(publisher.Description.Entity, entity); await bus.CloseAsync(); Assert.IsTrue(publisher.IsClosed, "The publisher should be closed."); // Verify the message sent. Assert.AreEqual(publisher.IsInitialized, true); Assert.AreEqual(publisher.Message.CorrelationKey, message.CorrelationKey); Assert.AreEqual(publisher.Message.Message, message.Message); Assert.AreEqual(publisher.Message.MessageKey, message.MessageKey); Assert.AreEqual(publisher.Message.PartitionKey, message.PartitionKey); }
/// <summary> /// Handles the specified publish verb options. /// </summary> /// <param name="options"> /// The options. /// </param> public static void Handle(PublicationOptions options) { if (options.IsVerbose) { Debug.Listeners.Add(new ConsoleTraceListener()); } var description = new MessageBusDescription { ConnectionString = options.ConnectionString, Factory = DependencyResolver.Resolve<IMessageBusFactory>( options.Factory) }; var bus = new MessageBus(description); var entity = options.Entity; var message = options.Message; if (!string.IsNullOrWhiteSpace(message)) { SendMessage(entity, message, bus); } var fileName = options.InputFileName; if (!string.IsNullOrWhiteSpace(fileName) && File.Exists(fileName)) { foreach (var line in File.ReadAllLines(fileName)) { SendMessage(entity, line, bus); } } }
/// <summary> /// Handles the specified publish verb options. /// </summary> /// <param name="options"> /// The options. /// </param> public static void Handle(PublicationOptions options) { if (options.IsVerbose) { Debug.Listeners.Add(new ConsoleTraceListener()); } var description = new MessageBusDescription { ConnectionString = options.ConnectionString, Factory = DependencyResolver.Resolve <IMessageBusFactory>( options.Factory) }; var bus = new MessageBus(description); var entity = options.Entity; var message = options.Message; if (!string.IsNullOrWhiteSpace(message)) { SendMessage(entity, message, bus); } var fileName = options.InputFileName; if (!string.IsNullOrWhiteSpace(fileName) && File.Exists(fileName)) { foreach (var line in File.ReadAllLines(fileName)) { SendMessage(entity, line, bus); } } }
/// <summary> /// Creates the message bus. /// </summary> /// <param name="cs"> /// The connection string. /// </param> /// <param name="factory"> /// The factory. /// </param> /// <param name="storage"> /// The storage connection string. /// </param> /// <returns> /// The <see cref="IMessageBus"/> instance. /// </returns> private static IMessageBus CreateMessageBus(string cs, string factory, string storage) { var description = new MessageBusDescription { ConnectionString = cs, Factory = DependencyResolver.Resolve<IMessageBusFactory>(factory), StorageConnectionString = storage }; return new MessageBus(description); }