/// <summary> /// Fills binary data to the queue /// </summary> public PushResult FillData(IEnumerable <byte[]> items, bool createAsSaved, bool highPriority) { try { if (_queue.Status == QueueStatus.Stopped) { return(PushResult.StatusNotSupported); } int max = _queue.PriorityMessagesList.Count + _queue.MessagesList.Count + items.Count(); if (_queue.Options.MessageLimit > 0 && max > _queue.Options.MessageLimit) { return(PushResult.LimitExceeded); } foreach (byte[] item in items) { HorseMessage message = new HorseMessage(MessageType.QueueMessage, _queue.Name); message.HighPriority = highPriority; message.WaitResponse = _queue.Options.Acknowledge != QueueAckDecision.None; if (_queue.Options.UseMessageId) { message.SetMessageId(_queue.Server.MessageIdGenerator.Create()); } message.Content = new MemoryStream(item); message.Content.Position = 0; message.CalculateLengths(); QueueMessage qm = new QueueMessage(message, createAsSaved); if (highPriority) { lock (_queue.PriorityMessagesList) _queue.PriorityMessagesList.AddLast(qm); } else { lock (_queue.MessagesList) _queue.MessagesList.AddLast(qm); } } _queue.Info.UpdateHighPriorityMessageCount(_queue.PriorityMessagesList.Count); _queue.Info.UpdateRegularMessageCount(_queue.MessagesList.Count); _ = _queue.Trigger(); return(PushResult.Success); } catch (Exception e) { _queue.Server.SendError("FILL_DATA", e, $"QueueName:{_queue.Name}"); return(PushResult.Error); } }
public async Task DisconnectDueToPingTimeout() { TestHorseMq server = new TestHorseMq(); await server.Initialize(); int port = server.Start(); TcpClient client = new TcpClient(); await client.ConnectAsync("127.0.0.1", port); NetworkStream stream = client.GetStream(); stream.Write(PredefinedMessages.PROTOCOL_BYTES_V2); HorseMessage msg = new HorseMessage(); msg.Type = MessageType.Server; msg.ContentType = KnownContentTypes.Hello; msg.SetStringContent("GET /\r\nName: Test-" + port); msg.CalculateLengths(); HmqWriter.Write(msg, stream); await Task.Delay(1000); Assert.Equal(1, server.ClientConnected); ThreadPool.UnsafeQueueUserWorkItem(async s => { byte[] buffer = new byte[128]; while (client.Connected) { int r = await s.ReadAsync(buffer); if (r == 0) { client.Dispose(); break; } } }, stream, false); await Task.Delay(15000); Assert.False(client.Connected); Assert.Equal(1, server.ClientDisconnected); }