public async Task WithAcknowledge() { TestMqServer server = new TestMqServer(); server.Initialize(42602); server.Start(); TmqClient client1 = new TmqClient(); TmqClient client2 = new TmqClient(); client1.ClientId = "client-1"; client2.ClientId = "client-2"; client2.AutoAcknowledge = true; await client1.ConnectAsync("tmq://localhost:42602"); await client2.ConnectAsync("tmq://localhost:42602"); Assert.True(client1.IsConnected); Assert.True(client2.IsConnected); bool received = false; client2.MessageReceived += (c, m) => received = m.Source == "client-1"; TmqMessage message = new TmqMessage(MessageType.Client, "client-2"); message.SetStringContent("Hello, World!"); bool sent = await client1.SendWithAcknowledge(message); Assert.True(sent); Assert.True(received); }
public async Task CreateWithProperties() { TestMqServer server = new TestMqServer(); server.Initialize(41206); server.Start(); TmqClient client = new TmqClient(); await client.ConnectAsync("tmq://localhost:41206"); Assert.True(client.IsConnected); TmqResponseCode created = await client.CreateQueue("ch-test", MessageA.ContentType, true, o => { o.SendOnlyFirstAcquirer = true; o.AcknowledgeTimeout = TimeSpan.FromSeconds(33); o.Status = MessagingQueueStatus.Pull; }); Assert.Equal(TmqResponseCode.Ok, created); Channel channel = server.Server.FindChannel("ch-test"); Assert.NotNull(channel); ChannelQueue queue = channel.FindQueue(MessageA.ContentType); Assert.NotNull(queue); Assert.True(queue.Options.SendOnlyFirstAcquirer); Assert.Equal(TimeSpan.FromSeconds(33), queue.Options.AcknowledgeTimeout); Assert.Equal(QueueStatus.Pull, queue.Status); }
public async Task FromClientToChannelTimeout() { TestMqServer server = new TestMqServer(); server.Initialize(42306); server.Start(); TmqClient client = new TmqClient(); client.AutoAcknowledge = false; client.AcknowledgeTimeout = TimeSpan.FromSeconds(3); await client.ConnectAsync("tmq://localhost:42306"); Assert.True(client.IsConnected); Channel channel = server.Server.Channels.FirstOrDefault(); Assert.NotNull(channel); ChannelQueue queue = channel.Queues.FirstOrDefault(); Assert.NotNull(queue); queue.Options.RequestAcknowledge = true; //subscribe await client.Join(channel.Name, true); //push a message to the queue MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes("Hello, World!")); bool sent = await client.Push(channel.Name, queue.Id, ms, true); Assert.False(sent); }
public async Task FromClientToChannelManuel() { TestMqServer server = new TestMqServer(); server.Initialize(42305); server.Start(); TmqClient client = new TmqClient(); client.AutoAcknowledge = false; client.MessageReceived += async(c, m) => { await client.SendAsync(m.CreateAcknowledge()); }; await client.ConnectAsync("tmq://localhost:42305"); Assert.True(client.IsConnected); Channel channel = server.Server.Channels.FirstOrDefault(); Assert.NotNull(channel); ChannelQueue queue = channel.Queues.FirstOrDefault(); Assert.NotNull(queue); queue.Options.RequestAcknowledge = true; //subscribe await client.Join(channel.Name, true); //push a message to the queue MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes("Hello, World!")); bool sent = await client.Push(channel.Name, queue.Id, ms, true); Assert.True(sent); }
public void Start() { _connector.AddHost("tmq://localhost:48050"); _connector.Connected += Connected; _connector.MessageReceived += MessageReceived; _connector.Run(); _timer = new Timer(o => { if (_connector.IsConnected) { TmqClient client = _connector.GetClient(); ProducerEvent e = new ProducerEvent(); e.No = _eventCount; e.Guid = Guid.NewGuid().ToString(); e.Name = "Producer Event"; Console.WriteLine($"Sending package #{e.No}"); _eventCount++; client.PushJson("BasicChannel", ModelTypes.ProducerEvent, e, false); client.PushJson("AckChannel", ModelTypes.ProducerEvent, e, false); } }, null, 1000, 1000); }
public async Task ClientReadsMessageFromQueue() { TestMqServer server = new TestMqServer(); server.Initialize(42801); server.Start(); bool received = false; MessageReader reader = MessageReader.JsonReader(); reader.On <MessageA>("ch-1", MessageA.ContentType, a => { received = true; }); TmqClient client = new TmqClient(); await client.ConnectAsync("tmq://localhost:42801"); Assert.True(client.IsConnected); reader.Attach(client); TmqResponseCode joined = await client.Join("ch-1", true); Assert.Equal(TmqResponseCode.Ok, joined); await Task.Delay(1000); MessageA m = new MessageA("Msg-A"); MemoryStream ms = new MemoryStream(); await System.Text.Json.JsonSerializer.SerializeAsync(ms, m); bool sent = await client.Push("ch-1", MessageA.ContentType, ms, false); Assert.True(sent); await Task.Delay(500); Assert.True(received); }
public async Task LeaveChannel() { TestMqServer server = new TestMqServer(); server.Initialize(41203); server.Start(); TmqClient client = new TmqClient(); client.Connect("tmq://localhost:41203"); TmqResponseCode joined = await client.Join("ch-1", true); Assert.Equal(TmqResponseCode.Ok, joined); TmqResponseCode left = await client.Leave("ch-1", false); Assert.Equal(TmqResponseCode.Ok, left); await Task.Delay(1000); Channel channel = server.Server.Channels.FirstOrDefault(); Assert.NotNull(channel); List <ChannelClient> clients = channel.ClientsClone; Assert.Empty(clients); }
public async Task SendToOnlineConsumers(int onlineConsumerCount) { int port = 47100 + onlineConsumerCount; TestMqServer server = new TestMqServer(); server.Initialize(port); server.Start(300, 300); TmqClient producer = new TmqClient(); await producer.ConnectAsync("tmq://localhost:" + port); Assert.True(producer.IsConnected); int msgReceived = 0; for (int i = 0; i < onlineConsumerCount; i++) { TmqClient consumer = new TmqClient(); consumer.ClientId = "consumer-" + i; await consumer.ConnectAsync("tmq://localhost:" + port); Assert.True(consumer.IsConnected); consumer.MessageReceived += (c, m) => Interlocked.Increment(ref msgReceived); TmqResponseCode joined = await consumer.Join("ch-route", true); Assert.Equal(TmqResponseCode.Ok, joined); } await producer.Push("ch-route", MessageA.ContentType, "Hello, World!", false); await Task.Delay(1500); Assert.Equal(onlineConsumerCount, msgReceived); }
public async Task Create(bool verifyResponse) { int port = verifyResponse ? 35905 : 35904; TestMqServer server = new TestMqServer(); server.Initialize(port); server.Start(); TmqClient client = new TmqClient(); client.Connect("tmq://localhost:" + port); TmqResponseCode created = await client.CreateChannel("new-channel", verifyResponse); if (verifyResponse) { Assert.Equal(TmqResponseCode.Ok, created); } else { await Task.Delay(1000); Assert.Equal(TmqResponseCode.Ok, created); } }
public async Task CreateWithProperties() { TestMqServer server = new TestMqServer(); server.Initialize(41206); server.Start(); TmqClient client = new TmqClient(); await client.ConnectAsync("tmq://localhost:41206"); Assert.True(client.IsConnected); TmqResponseCode created = await client.CreateChannel("new-channel", o => { o.AllowMultipleQueues = false; o.SendOnlyFirstAcquirer = true; o.AcknowledgeTimeout = TimeSpan.FromSeconds(33); o.Status = MessagingQueueStatus.Pull; }); Assert.Equal(TmqResponseCode.Ok, created); Channel found = server.Server.FindChannel("new-channel"); Assert.NotNull(found); Assert.False(found.Options.AllowMultipleQueues); Assert.True(found.Options.SendOnlyFirstAcquirer); }
public TmqClient CreateInstance() { TmqClient client = new TmqClient(); if (!string.IsNullOrEmpty(_id)) { client.ClientId = _id; } if (!string.IsNullOrEmpty(_name)) { client.SetClientName(_name); } if (!string.IsNullOrEmpty(_type)) { client.SetClientType(_type); } if (!string.IsNullOrEmpty(_token)) { client.SetClientToken(_token); } if (_enhance != null) { _enhance(client); } return(client); }
public async Task SendToOfflineConsumers() { int port = 47117; TestMqServer server = new TestMqServer(); server.Initialize(port); server.Start(300, 300); TmqClient producer = new TmqClient(); await producer.ConnectAsync("tmq://localhost:" + port); Assert.True(producer.IsConnected); await producer.Push("ch-route", MessageA.ContentType, "Hello, World!", false); await Task.Delay(700); bool msgReceived = false; TmqClient consumer = new TmqClient(); consumer.ClientId = "consumer"; await consumer.ConnectAsync("tmq://localhost:" + port); Assert.True(consumer.IsConnected); consumer.MessageReceived += (c, m) => msgReceived = true; TmqResponseCode joined = await consumer.Join("ch-route", true); Assert.Equal(TmqResponseCode.Ok, joined); await Task.Delay(800); Assert.False(msgReceived); }
public async Task RequestAcknowledge(bool queueAckIsActive) { int port = 47118 + Convert.ToInt32(queueAckIsActive); TestMqServer server = new TestMqServer(); server.Initialize(port); server.Start(300, 300); Channel ch = server.Server.FindChannel("ch-route"); ChannelQueue queue = ch.Queues.FirstOrDefault(); Assert.NotNull(queue); queue.Options.AcknowledgeTimeout = TimeSpan.FromSeconds(3); queue.Options.RequestAcknowledge = queueAckIsActive; TmqClient producer = new TmqClient(); await producer.ConnectAsync("tmq://localhost:" + port); Assert.True(producer.IsConnected); TmqClient consumer = new TmqClient(); consumer.AutoAcknowledge = true; consumer.AcknowledgeTimeout = TimeSpan.FromSeconds(4); consumer.ClientId = "consumer"; await consumer.ConnectAsync("tmq://localhost:" + port); Assert.True(consumer.IsConnected); TmqResponseCode joined = await consumer.Join("ch-route", true); Assert.Equal(TmqResponseCode.Ok, joined); bool ack = await producer.Push("ch-route", MessageA.ContentType, "Hello, World!", true); Assert.Equal(queueAckIsActive, ack); }
public async Task Create(bool verifyResponse) { int port = verifyResponse ? 40905 : 40904; TestMqServer server = new TestMqServer(); server.Initialize(port); server.Start(); TmqClient client = new TmqClient(); client.Connect("tmq://localhost:" + port); TmqResponseCode created = await client.CreateQueue("ch-2", MessageA.ContentType, verifyResponse); Assert.Equal(TmqResponseCode.Ok, created); await Task.Delay(1000); Channel channel = server.Server.Channels.FirstOrDefault(x => x.Name == "ch-2"); Assert.NotNull(channel); ChannelQueue queue = channel.Queues.FirstOrDefault(); Assert.NotNull(queue); Assert.Equal(MessageA.ContentType, queue.Id); }
public void Start() { _connector.AddHost("tmq://localhost:48050"); _connector.Connected += Connected; _connector.MessageReceived += MessageReceived; _connector.Run(); _timer = new Timer(async o => { if (_connector.IsConnected) { TmqClient client = _connector.GetClient(); TmqMessage message = new TmqMessage(MessageType.Client, "producer-id"); message.ContentType = ModelTypes.ConsumerRequest; ConsumerRequest request = new ConsumerRequest(); request.Guid = Guid.NewGuid().ToString(); await message.SetJsonContent(request); TmqMessage response = await client.Request(message); ProducerResponse rmodel = await response.GetJsonContent <ProducerResponse>(); Console.WriteLine($"> response received for: {rmodel.RequestGuid}"); } }, null, 6000, 6000); }
/// <summary> /// Client creation action for server instances /// </summary> private static TmqClient CreateInstanceClient(InstanceOptions options) { TmqClient client = new TmqClient(); client.SetClientName(options.Name); client.SetClientToken(options.Token); client.SetClientType("server"); return(client); }
public async Task ConnectDisconnectStress(int concurrentClients, int connectionCount, int minAliveMs, int maxAliveMs) { Random rnd = new Random(); int connected = 0; int disconnected = 0; int port = 42110 + rnd.Next(0, 89); TestMqServer server = new TestMqServer(); server.Initialize(port); server.Start(); for (int i = 0; i < concurrentClients; i++) { Thread thread = new Thread(async() => { for (int j = 0; j < connectionCount; j++) { try { TmqClient client = new TmqClient(); client.Connect("tmq://localhost:" + port); Assert.True(client.IsConnected); Interlocked.Increment(ref connected); await Task.Delay(rnd.Next(minAliveMs, maxAliveMs)); client.Disconnect(); Interlocked.Increment(ref disconnected); await Task.Delay(50); Assert.True(client.IsConnected); } catch { } } }); thread.Start(); } TimeSpan total = TimeSpan.FromMilliseconds(maxAliveMs * connectionCount); TimeSpan elapsed = TimeSpan.Zero; while (elapsed < total) { elapsed += TimeSpan.FromMilliseconds(100); await Task.Delay(100); } await Task.Delay(maxAliveMs); await Task.Delay(3000); Assert.Equal(connected, concurrentClients * connectionCount); Assert.Equal(disconnected, concurrentClients * connectionCount); }
private void Connected(SocketBase client) { Console.WriteLine("consumer connection established"); TmqClient tc = (TmqClient)client; tc.AutoAcknowledge = true; tc.Join("AckChannel", false); tc.Join("BasicChannel", false); }
public Producer() { _connector = new TmqAbsoluteConnector(TimeSpan.FromSeconds(5), () => { TmqClient client = new TmqClient(); client.ClientId = "producer-id"; client.SetClientType("producer"); client.SetClientToken("S3cr37_pr0duc3r_t0k3n"); return(client); }); }
private void Connected(SocketBase client) { Console.WriteLine("producer connection established"); TmqClient tmq = (TmqClient)client; if (_firstConnection) { _firstConnection = false; tmq.CreateQueue("BasicChannel", ModelTypes.ProducerEvent, false).Wait(); } }
public Consumer() { _connector = new TmqStickyConnector(TimeSpan.FromSeconds(5), () => { TmqClient client = new TmqClient(); client.ClientId = "consumer-id"; client.SetClientType("consumer"); client.SetClientToken("anonymous"); client.AutoAcknowledge = true; return(client); }); }
public async Task RequestAcknowledge() { int port = 47412; TestMqServer server = new TestMqServer(); server.Initialize(port); server.Start(300, 300); Channel channel = server.Server.FindChannel("ch-pull"); ChannelQueue queue = channel.FindQueue(MessageA.ContentType); Assert.NotNull(channel); Assert.NotNull(queue); queue.Options.RequestAcknowledge = true; queue.Options.AcknowledgeTimeout = TimeSpan.FromSeconds(15); TmqClient consumer = new TmqClient(); consumer.AutoAcknowledge = true; consumer.ClientId = "consumer"; await consumer.ConnectAsync("tmq://localhost:" + port); Assert.True(consumer.IsConnected); bool msgReceived = false; consumer.MessageReceived += (c, m) => msgReceived = true; TmqResponseCode joined = await consumer.Join("ch-pull", true); Assert.Equal(TmqResponseCode.Ok, joined); TmqClient producer = new TmqClient(); producer.AcknowledgeTimeout = TimeSpan.FromSeconds(15); await producer.ConnectAsync("tmq://localhost:" + port); Assert.True(producer.IsConnected); Task <bool> taskAck = producer.Push("ch-pull", MessageA.ContentType, "Hello, World!", true); await Task.Delay(500); Assert.False(taskAck.IsCompleted); Assert.False(msgReceived); Assert.Single(queue.RegularMessages); TmqMessage pull = await consumer.Pull("ch-pull", MessageA.ContentType); Assert.NotNull(pull); }
public async Task PushToLateClients() { TestMqServer server = new TestMqServer(); server.Initialize(42506); server.Start(); TmqClient client = new TmqClient(); await client.ConnectAsync("tmq://localhost:42506"); Assert.True(client.IsConnected); Channel channel = server.Server.Channels.FirstOrDefault(); Assert.NotNull(channel); ChannelQueue queue = channel.Queues.FirstOrDefault(); Assert.NotNull(queue); await queue.SetStatus(QueueStatus.Push); bool received = false; client.MessageReceived += (c, m) => { if (m.Type == MessageType.Channel) { received = true; } }; MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes("Hello, World!")); bool sent = await client.Push(channel.Name, queue.Id, ms, false); Assert.True(sent); await Task.Delay(1500); Assert.NotEmpty(queue.RegularMessages); Assert.False(received); TmqResponseCode joined = await client.Join(channel.Name, true); Assert.Equal(TmqResponseCode.Ok, joined); await Task.Delay(1500); Assert.Empty(queue.RegularMessages); Assert.True(received); }
public async Task QueueWaitAcknowledgeOneClient() { TestMqServer server = new TestMqServer(); server.Initialize(40582); server.Start(); TmqClient client = new TmqClient(); await client.ConnectAsync("tmq://localhost:40582"); client.AutoAcknowledge = true; Assert.True(client.IsConnected); Channel channel = server.Server.Channels.FirstOrDefault(); Assert.NotNull(channel); ChannelQueue queue = channel.Queues.FirstOrDefault(); Assert.NotNull(queue); queue.Options.Status = QueueStatus.Push; queue.Options.RequestAcknowledge = true; queue.Options.AcknowledgeTimeout = TimeSpan.FromSeconds(6); TmqResponseCode joined = await client.Join(channel.Name, true); Assert.Equal(TmqResponseCode.Ok, joined); await Task.Delay(250); bool received = false; client.MessageReceived += (c, m) => { if (m.Type == MessageType.Channel) { received = true; } }; MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes("Hello, World!")); bool sent = await client.Push(channel.Name, queue.Id, ms, true); Assert.True(sent); Assert.Empty(queue.HighPriorityMessages); Assert.Empty(queue.RegularMessages); Assert.True(received); }
public async Task UseUniqueMessageId(bool enabled) { int port = enabled ? 42701 : 42702; TestMqServer server = new TestMqServer(); server.Initialize(port); server.Server.Options.UseMessageId = enabled; server.Server.FindChannel("ch-1").FindQueue(MessageA.ContentType).Options.UseMessageId = false; server.Start(); TmqClient client = new TmqClient(); client.UseUniqueMessageId = false; await client.ConnectAsync("tmq://localhost:" + port); Assert.True(client.IsConnected); TmqResponseCode joined = await client.Join("ch-1", true); Assert.Equal(TmqResponseCode.Ok, joined); await Task.Delay(250); TmqMessage received = null; client.MessageReceived += (c, m) => received = m; MessageA a = new MessageA("A"); string serialized = Newtonsoft.Json.JsonConvert.SerializeObject(a); MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(serialized)); bool sent = await client.Push("ch-1", MessageA.ContentType, ms, false); Assert.True(sent); await Task.Delay(1000); Assert.NotNull(received); if (enabled) { Assert.NotNull(received.MessageId); } else { Assert.Null(received.MessageId); } }
public void ConnectWithInfo() { TestMqServer server = new TestMqServer(); server.Initialize(42101); server.Start(); TmqClient client = new TmqClient(); client.Data.Properties.Add("Name", "Test-42101"); client.Connect("tmq://localhost:42101/path"); Thread.Sleep(50); Assert.True(client.IsConnected); Assert.Equal(1, server.ClientConnected); }
public void KeepAliveWithPingPong() { TestMqServer server = new TestMqServer(); server.Initialize(42104); server.Start(); TmqClient client = new TmqClient(); client.Data.Properties.Add("Name", "Test-42104"); client.Connect("tmq://localhost:42104/path"); Thread.Sleep(25000); Assert.True(client.IsConnected); Assert.Equal(1, server.ClientConnected); }
public async Task SendAcknowledgeFromServerToProducer() { TestMqServer server = new TestMqServer(); server.Initialize(42599); server.Start(); server.SendAcknowledgeFromMQ = true; TmqClient client = new TmqClient(); await client.ConnectAsync("tmq://localhost:42599"); Assert.True(client.IsConnected); bool ack = await client.Push("ch-route", MessageA.ContentType, "Hello", true); Assert.True(ack); }
private void MessageReceived(ClientSocketBase <TmqMessage> client, TmqMessage message) { if (message.Type == MessageType.Client && message.ContentType == ModelTypes.ConsumerRequest) { ConsumerRequest request = message.GetJsonContent <ConsumerRequest>().Result; Console.WriteLine($"Consumer request received: {request.Guid}"); ProducerResponse model = new ProducerResponse(); model.RequestGuid = request.Guid; model.ResponseGuid = Guid.NewGuid().ToString(); TmqMessage response = message.CreateResponse(); response.ContentType = ModelTypes.ProducerResponse; response.SetJsonContent(model); TmqClient tmq = (TmqClient)client; tmq.Send(response); } }
public async Task ClientReadsMessagesFromMultipleChannels() { TestMqServer server = new TestMqServer(); server.Initialize(42802); server.Start(); TmqClient client = new TmqClient(); await client.ConnectAsync("tmq://localhost:42802"); Assert.True(client.IsConnected); TmqResponseCode joined = await client.Join("ch-1", true); Assert.Equal(TmqResponseCode.Ok, joined); joined = await client.Join("ch-0", true); Assert.Equal(TmqResponseCode.Ok, joined); await Task.Delay(250); bool ch0 = false; bool ch1 = false; MessageReader reader = MessageReader.JsonReader(); reader.On <MessageA>("ch-0", MessageA.ContentType, a => ch0 = true); reader.On <MessageA>("ch-1", MessageA.ContentType, a => ch1 = true); reader.Attach(client); MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(System.Text.Json.JsonSerializer.Serialize(new MessageA("Ax")))); bool sent = await client.Push("ch-1", MessageA.ContentType, ms, false); Assert.True(sent); sent = await client.Push("ch-0", MessageA.ContentType, ms, false); Assert.True(sent); await Task.Delay(1000); Assert.True(ch0); Assert.True(ch1); }