public void SendAfterQueueDeleteShouldRecreateQueue() { var name = $"IntegrationTest_{UniqueKeyUtility.Generate()}"; try { _messageQueueManager.Exists(name, true).Should().BeFalse(); using (var queue = CreateLocalQueue(name, true, LocaleQueueMode.DurableCreate, AccessMode.SendAndReceive)) { queue.Send("ABC"); _messageQueueManager.Exists(name, true).Should().BeTrue(); _messageQueueManager.Delete(name, true); _messageQueueManager.Exists(name, true).Should().BeFalse(); queue.Disconnect(); queue.Send("ABC"); _messageQueueManager.Exists(name, true).Should().BeTrue(); queue.Reconnect(); _messageQueueManager.Exists(name, true).Should().BeTrue(); queue.Disconnect(); queue.Connect(); _messageQueueManager.Exists(name, true).Should().BeTrue(); } } finally { _messageQueueManager.Delete(name, true); } }
public void CancelReceiveAsyncShouldStopBeforeTimeout() { var name = $"IntegrationTest_{UniqueKeyUtility.Generate()}"; try { using (var queue = new LocaleQueue(NullLogger.Instance, _messageQueueManager, _messageQueueTransactionFactory, name, true, LocaleQueueMode.DurableCreate, true, AccessMode.Receive)) { var cancellationTokenSource = new CancellationTokenSource(); var stopwatch = new Stopwatch(); stopwatch.Start(); var task = queue.ReceiveAsync(1000, cancellationTokenSource.Token); cancellationTokenSource.CancelAfter(500); try { task.Result?.Message.Should().BeNull(); } catch (Exception) { // ignored } stopwatch.Stop(); stopwatch.ElapsedMilliseconds.Should().BeInRange(400, 711); } } finally { _messageQueueManager.Delete(name, true); } }
public void SendMessageToReceiveWithCancellationShouldReceiveMessage() { var name = $"IntegrationTest_{UniqueKeyUtility.Generate()}"; try { using (var queue = CreateLocalQueue(name, true, LocaleQueueMode.DurableCreate, AccessMode.Send)) { queue.Send("Message"); } using (var queue = CreateLocalQueue(name, true, LocaleQueueMode.DurableCreate) ) { var message = queue.Receive(1000, _cancellationToken); message.Should().NotBeNull(); var dto = message.Message; dto.GetType().Should().Be(typeof(string)); dto.Should().Be("Message"); } } finally { _messageQueueManager.Delete(name, true); } }
public void SendAndReceiveAsyncDtoMessageShouldWork() { var name = $"IntegrationTest_{UniqueKeyUtility.Generate()}"; try { using (var queue = CreateLocalQueue(name, true, LocaleQueueMode.DurableCreate, AccessMode.Send)) { queue.Send(new MyDto { S = "ABC", I = 2 }); } using (var queue = CreateLocalQueue(name, true, LocaleQueueMode.DurableCreate)) { var dto = (MyDto)queue.ReceiveAsync(100, _cancellationToken).Result.Message; dto.S.Should().Be("ABC"); dto.I.Should().Be(2); } } finally { _messageQueueManager.Delete(name, true); } }
public void UseNonTransactionalQueue() { var name = $"IntegrationTest_{UniqueKeyUtility.Generate()}"; try { var stopwatch = new Stopwatch(); using (var queue = CreateLocalQueue(name, true, LocaleQueueMode.DurableCreate, AccessMode.SendAndReceive, false)) { stopwatch.Start(); queue.Send("Message"); var message = queue.Receive(1000, new CancellationToken()); stopwatch.Stop(); message.Message.Should().Be("Message"); stopwatch.ElapsedMilliseconds.Should().BeLessThan(1000); } } finally { _messageQueueManager.Delete(name, true); } }
public void CancelReceiveWithCancellationShouldReturnNull() { var name = $"IntegrationTest_{UniqueKeyUtility.Generate()}"; try { using (var queue = CreateLocalQueue(name, true, LocaleQueueMode.DurableCreate)) { var stopwatch = new Stopwatch(); var cancellationTokenSource = new CancellationTokenSource(); stopwatch.Start(); cancellationTokenSource.CancelAfter(500); var message = queue.Receive(1000, cancellationTokenSource.Token); var dto = message.Message; stopwatch.Stop(); stopwatch.ElapsedMilliseconds.Should().BeLessThan(900); dto.Should().BeNull(); } } finally { _messageQueueManager.Delete(name, true); } }
public void PeekAsyncMessageFromQueueShouldWork() { var name = $"IntegrationTest_{UniqueKeyUtility.Generate()}"; try { var queue = _messageQueueManager.Create(name, true, true); var asyncResult = _messageQueueManager.BeginPeek(queue, TimeSpan.FromMilliseconds(100)); var message = _messageQueueManager.EndPeek(queue, asyncResult); message.Should().BeNull("Because queue is empty"); var id = SendMessage(queue, "Message"); asyncResult = _messageQueueManager.BeginPeek(queue, TimeSpan.FromMilliseconds(100)); message = _messageQueueManager.EndPeek(queue, asyncResult); message.Should().NotBeNull(); message.Id.Should().Be(id); } finally { _messageQueueManager.Delete(name, true); } }
/// <summary> /// Request a responses from your service, this will invoke the request handler registered on your service as if it was called from another service.<br/> /// </summary> /// <param name="config">Request/Response Configuration</param> /// <param name="request">Request message</param> /// <typeparam name="TRequest">Type of Request Message</typeparam> /// <typeparam name="TResponse">Type of Response Message</typeparam> /// <returns>Response Message</returns> public TResponse Request <TRequest, TResponse>(RequestResponseConfig config, TRequest request) { var queue = _requestQueues.FirstOrDefault(q => q.Key == config.Name); var replyQueue = UniqueKeyUtility.Generate(); if (queue.Value != null) { MockMessage(queue.Value, new RequestMessage { MessageBody = SerializeToJson(request), RequestType = typeof(TRequest).FullName, ResponseType = typeof(TResponse).FullName, Name = config.Name, ReplyQueue = replyQueue }, true); // ReSharper disable once ImplicitlyCapturedClosure TimerUtility.WaitForIt(() => _responses.Any(r => r.ReplyQueue == replyQueue), Debugger.IsAttached ? 360000 : config.MillisecondsTimeout); } Thread.Sleep(100); var response = _responses.FirstOrDefault(r => r.ReplyQueue == replyQueue); return(JsonConvert.DeserializeObject <TResponse>(response?.MessageBody)); }
public void SendAndReceiveLargeMessageShouldWork() { var name = $"IntegrationTest_{UniqueKeyUtility.Generate()}"; try { using (var queue = CreateLocalQueue(name, true, LocaleQueueMode.DurableCreate, AccessMode.SendAndReceive)) { queue.Send(new string('A', 5000000)); queue.Count().Should().Be(2); queue.Transactional.Should().BeTrue(); queue.AccessMode.Should().Be(AccessMode.SendAndReceive); } using (var queue = CreateLocalQueue(name, true, LocaleQueueMode.Durable)) { var message = (string)queue.Receive(100, _cancellationToken).Message; message.Length.Should().Be(5000000); } } finally { _messageQueueManager.Delete(name, true); } }
public MessageStateRepositoryTests() { _messageId = UniqueKeyUtility.Generate(); _subscriberName = UniqueKeyUtility.Generate(); _entityConnectionConfig = new EntityConnectionConfig(new DatabaseConnectionConfig(@"(localdb)\MSSQLLocalDB", "Grumpy.RipplesMQ.Database_Model")); _repositoryContextFactory = new RepositoryContextFactory(NullLogger.Instance, _entityConnectionConfig); }
public void MessageQueueFactoryCanCreateLocaleInstance() { var name = $"IntegrationTest_{UniqueKeyUtility.Generate()}"; using (var queue = new QueueFactory(NullLogger.Instance).CreateLocale(name, true, LocaleQueueMode.TemporaryMaster, true, AccessMode.Receive)) { queue.Should().NotBeNull(); queue.GetType().Should().Be(typeof(LocaleQueue)); } }
private static PublishMessage CreatePublishMessage <T>(PublishSubscribeConfig publishSubscribeConfig, T message) { return(new PublishMessage { MessageBody = SerializeToJson(message), MessageType = typeof(T).FullName, MessageId = UniqueKeyUtility.Generate(), Persistent = publishSubscribeConfig.Persistent, ReplyQueue = null, Topic = publishSubscribeConfig.Topic }); }
private static string Generate(string queueName, bool durable) { var name = durable ? queueName : "#" + queueName; if (!durable) { var uniqueKey = UniqueKeyUtility.Generate(); name = name.Left(MaxQueueLength - uniqueKey.Length - 1) + "." + uniqueKey; } return(name); }
/// <inheritdoc /> public PublishReplyMessage SendPublishMessage <T>(string topic, T message, bool persistent, CancellationToken cancellationToken) { var id = UniqueKeyUtility.Generate(); var publishMessage = new PublishMessage { ServerName = _processInformation.MachineName, ServiceName = _messageBusConfig.ServiceName, MessageId = id, Topic = topic, Persistent = persistent, MessageBody = SerializeToJson(message), MessageType = typeof(T).FullName, ReplyQueue = persistent ? _queueNameUtility.ReplyQueue <PublishMessage>(id) : null, PublishDateTime = DateTimeOffset.Now, ErrorCount = 0 }; PublishReplyMessage replyMessage; _logger.Debug("Message Broker Client sending Publish Message {@Message}", publishMessage); if (persistent) { using (var replyQueue = _queueFactory.CreateLocale(publishMessage.ReplyQueue, true, LocaleQueueMode.TemporaryMaster, false, AccessMode.Receive)) { SendToMessageBroker(publishMessage); replyMessage = replyQueue.Receive <PublishReplyMessage>(3000, cancellationToken); if (replyMessage == null) { throw new PublishReplyTimeoutException(publishMessage); } } } else { SendToMessageBroker(publishMessage); replyMessage = new PublishReplyMessage { PublishDateTime = publishMessage.PublishDateTime, MessageId = publishMessage.MessageId, Topic = publishMessage.Topic }; } replyMessage.CompletedDateTime = DateTimeOffset.Now; return(replyMessage); }
/// <inheritdoc /> public async Task <TResponse> RequestResponseAsync <TRequest, TResponse>(string name, TRequest request, int millisecondsTimeout, CancellationToken cancellationToken) { var requestMessage = new RequestMessage { RequesterServerName = _processInformation.MachineName, RequesterServiceName = _messageBusConfig.ServiceName, RequestId = UniqueKeyUtility.Generate(), Name = name, MessageBody = SerializeToJson(request), RequestType = typeof(TRequest).FullName, ResponseType = typeof(TResponse).FullName, ReplyQueue = _queueNameUtility.ReplyQueue <RequestMessage>(name), RequestDateTime = DateTimeOffset.Now }; ITransactionalMessage message; using (var queue = _queueFactory.CreateLocale(requestMessage.ReplyQueue, true, LocaleQueueMode.TemporaryMaster, true, AccessMode.Receive)) { SendToMessageBroker(requestMessage); message = await queue.ReceiveAsync(millisecondsTimeout, cancellationToken); } switch (message?.Message) { case null: throw new RequestResponseTimeoutException(requestMessage, millisecondsTimeout); case ResponseMessage responseMessage: responseMessage.CompletedDateTime = DateTimeOffset.Now; if (responseMessage.MessageType == null && responseMessage.MessageBody == "null") { return(default(TResponse)); } if (responseMessage.MessageType == typeof(TResponse).FullName) { return(JsonConvert.DeserializeObject <TResponse>(responseMessage.MessageBody)); } throw new InvalidMessageTypeException(requestMessage, responseMessage, typeof(TResponse), responseMessage.GetType()); case ResponseErrorMessage responseErrorMessage: responseErrorMessage.CompletedDateTime = DateTimeOffset.Now; throw new RequestHandlerException(requestMessage, responseErrorMessage); } throw new InvalidMessageTypeException(requestMessage, typeof(ResponseMessage), message.GetType()); }
public void CreatePrivateQueueShouldCreateFirstTime() { var name = $"IntegrationTest_{UniqueKeyUtility.Generate()}"; try { _messageQueueManager.Create(name, true, true).Should().NotBeNull(); Assert.Throws <QueueCreateException>(() => _messageQueueManager.Create(name, true, true).Should().BeNull()); } finally { _messageQueueManager.Delete(name, true); } }
public void ListQueueNamesShouldReturnList() { var name = $"IntegrationTest_{UniqueKeyUtility.Generate()}"; try { _messageQueueManager.Create(name, true, true); _messageQueueManager.List(".", true).Should().Contain(n => n == name.ToLower()); } finally { _messageQueueManager.Delete(name, true); } }
public void CreatePublicQueueShouldWork() { var name = $"IntegrationTest_{UniqueKeyUtility.Generate()}"; try { _messageQueueManager.Create(name, false, true).Should().NotBeNull(); _messageQueueManager.Exists(name, false).Should().BeFalse(); } finally { _messageQueueManager.Delete(name, false); } }
public void ListQueue() { var name = $"IntegrationTest_{UniqueKeyUtility.Generate()}"; try { _messageQueueManager.Create(name, true, true); _messageQueueManager.List(".", true).Count().Should().BeGreaterOrEqualTo(1); } finally { _messageQueueManager.Delete(name, true); } }
public void GetShouldBeNullIfNotExists() { var name = $"IntegrationTest_{UniqueKeyUtility.Generate()}"; try { GetQueue(name).Should().BeNull(); _messageQueueManager.Create(name, true, true).Should().NotBeNull(); GetQueue(name).Should().NotBeNull(); } finally { _messageQueueManager.Delete(name, true); } }
public void ReceiveFromNoneExistingQueueShouldThrow() { var name = $"IntegrationTest_{UniqueKeyUtility.Generate()}"; try { using (var queue = CreateLocalQueue(name, true, LocaleQueueMode.Durable)) { Assert.Throws <QueueMissingException>(() => queue.Receive(1, _cancellationToken)); } } finally { _messageQueueManager.Delete(name, true); } }
public void OnNoneExistingQueueExistShouldBeCreate() { var name = $"IntegrationTest_{UniqueKeyUtility.Generate()}"; try { using (var queue = CreateLocalQueue(name, true, LocaleQueueMode.DurableCreate)) { queue.Exists().Should().BeTrue(); } } finally { _messageQueueManager.Delete(name, true); } }
public void DeleteLocaleQueueShouldDeleteQueue() { var name = $"IntegrationTest_{UniqueKeyUtility.Generate()}"; try { _messageQueueManager.Create(name, true, true); _messageQueueManager.Delete(name, true); TimerUtility.WaitForIt(() => !_messageQueueManager.Exists(name, true), 10000); _messageQueueManager.Exists(name, true).Should().BeFalse(); } finally { _messageQueueManager.Delete(name, true); } }
public void SendToNoneExistingQueueShouldThrow() { var name = $"IntegrationTest_{UniqueKeyUtility.Generate()}"; try { using (var queue = CreateLocalQueue(name, true, LocaleQueueMode.Durable, AccessMode.Send)) { Assert.Throws <QueueMissingException>(() => queue.Send("Message")); } } finally { _messageQueueManager.Delete(name, true); } }
public void DurableQueueShouldExistAfterUse() { var name = $"IntegrationTest_{UniqueKeyUtility.Generate()}"; try { using (var queue = CreateLocalQueue(name, true, LocaleQueueMode.DurableCreate)) { queue.Receive(1, _cancellationToken); } _messageQueueManager.Exists(name, true).Should().BeTrue(); } finally { _messageQueueManager.Delete(name, true); } }
public void SendAndReceiveMessageToFromQueueShouldWork() { var name = $"IntegrationTest_{UniqueKeyUtility.Generate()}"; try { var queue = _messageQueueManager.Create(name, true, true); var message = ReceiveMessage(queue); message.Should().BeNull(); SendMessage(queue, ""); message = ReceiveMessage(queue); message.Should().NotBeNull(); } finally { _messageQueueManager.Delete(name, true); } }
public void CanDelete() { var name = $"IntegrationTest_{UniqueKeyUtility.Generate()}"; try { _messageQueueManager.Create(name, true, false); using (var queue = CreateLocalQueue(name, true, LocaleQueueMode.DurableCreate)) { queue.Delete(); } _messageQueueManager.Exists(name, true).Should().BeFalse(); } finally { _messageQueueManager.Delete(name, true); } }
public void SendAndReceiveStringMessageShouldWork() { var name = $"IntegrationTest_{UniqueKeyUtility.Generate()}"; try { using (var queue = CreateLocalQueue(name, true, LocaleQueueMode.DurableCreate, AccessMode.Send)) { queue.Send("ABC"); } using (var queue = CreateLocalQueue(name, true, LocaleQueueMode.DurableCreate)) { ((string)queue.Receive(100, _cancellationToken).Message).Should().Be("ABC"); } } finally { _messageQueueManager.Delete(name, true); } }
public void ReceiveAsyncFromEmptyQueueShouldTimeout() { var name = $"IntegrationTest_{UniqueKeyUtility.Generate()}"; try { using (var queue = new LocaleQueue(NullLogger.Instance, _messageQueueManager, _messageQueueTransactionFactory, name, true, LocaleQueueMode.DurableCreate, true, AccessMode.Receive)) { var stopwatch = new Stopwatch(); stopwatch.Start(); var task = queue.ReceiveAsync(1000, _cancellationToken); stopwatch.ElapsedMilliseconds.Should().BeLessThan(900); task.Result?.Message.Should().BeNull(); stopwatch.ElapsedMilliseconds.Should().BeGreaterThan(900); } } finally { _messageQueueManager.Delete(name, true); } }
public void CanSendAndReceiveFromPrivateQueue() { var name = $"IntegrationTest_{UniqueKeyUtility.Generate()}"; try { using (var queue = CreateLocalQueue(name, true, LocaleQueueMode.DurableCreate, AccessMode.Send)) { queue.Send("Hallo"); } using (var queue = new LocaleQueue(NullLogger.Instance, _messageQueueManager, _messageQueueTransactionFactory, name, true, LocaleQueueMode.DurableCreate, true, AccessMode.Receive)) { var message = (string)queue.Receive(100, _cancellationToken).Message; message.Should().Be("Hallo"); } } finally { _messageQueueManager.Delete(name, true); } }