Esempio n. 1
0
        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);
        }
Esempio n. 2
0
        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);
        }
Esempio n. 3
0
        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);
        }
Esempio n. 4
0
        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);
        }
Esempio n. 5
0
        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);
        }
Esempio n. 6
0
        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);
        }
Esempio n. 7
0
        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);
        }
Esempio n. 8
0
        public async Task ClientReadsMessagesFromMultipleQueues()
        {
            TestMqServer server = new TestMqServer();

            server.Initialize(42803);
            server.Start();

            TmqClient client = new TmqClient();
            await client.ConnectAsync("tmq://localhost:42803");

            Assert.True(client.IsConnected);

            TmqResponseCode joined = await client.Join("ch-1", true);

            Assert.Equal(TmqResponseCode.Ok, joined);

            await Task.Delay(250);

            bool          ma     = false;
            bool          mc     = false;
            MessageReader reader = MessageReader.JsonReader();

            reader.On <MessageA>("ch-1", MessageA.ContentType, a => ma = true);
            reader.On <MessageA>("ch-1", MessageC.ContentType, c => mc = true);
            reader.Attach(client);

            MemoryStream astream = new MemoryStream(Encoding.UTF8.GetBytes(System.Text.Json.JsonSerializer.Serialize(new MessageA("Ax"))));
            MemoryStream cstream = new MemoryStream(Encoding.UTF8.GetBytes(System.Text.Json.JsonSerializer.Serialize(new MessageC("Cx", "x"))));

            bool sent = await client.Push("ch-1", MessageA.ContentType, astream, false);

            Assert.True(sent);
            sent = await client.Push("ch-1", MessageC.ContentType, cstream, false);

            Assert.True(sent);

            await Task.Delay(1000);

            Assert.True(ma);
            Assert.True(mc);
        }
Esempio n. 9
0
        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);
        }
Esempio n. 10
0
        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);
        }
Esempio n. 11
0
        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);
        }
Esempio n. 12
0
        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);
            }
        }
Esempio n. 13
0
        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);
        }
Esempio n. 14
0
        public async Task SendAndPull()
        {
            int          port   = 47411;
            TestMqServer server = new TestMqServer();

            server.Initialize(port);
            server.Start(300, 300);

            TmqClient consumer = new TmqClient();

            consumer.ClientId = "consumer";
            await consumer.ConnectAsync("tmq://localhost:" + port);

            Assert.True(consumer.IsConnected);
            TmqResponseCode joined = await consumer.Join("ch-pull", true);

            Assert.Equal(TmqResponseCode.Ok, joined);

            TmqClient producer = new TmqClient();
            await producer.ConnectAsync("tmq://localhost:" + port);

            Assert.True(producer.IsConnected);

            await producer.Push("ch-pull", MessageA.ContentType, "Hello, World!", false);

            await Task.Delay(700);

            Channel      channel = server.Server.FindChannel("ch-pull");
            ChannelQueue queue   = channel.FindQueue(MessageA.ContentType);

            Assert.NotNull(channel);
            Assert.NotNull(queue);
            Assert.Single(queue.RegularMessages);

            TmqMessage pull1 = await consumer.Pull("ch-pull", MessageA.ContentType);

            Assert.NotNull(pull1);

            TmqMessage pull2 = await consumer.Pull("ch-pull", MessageA.ContentType);

            Assert.Null(pull2);
        }
Esempio n. 15
0
        public async Task SendToOfflineConsumers()
        {
            int          port   = 47217;
            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-push", MessageA.ContentType, "Hello, World!", false);

            await Task.Delay(700);

            Channel      channel = server.Server.FindChannel("ch-push");
            ChannelQueue queue   = channel.FindQueue(MessageA.ContentType);

            Assert.NotNull(channel);
            Assert.NotNull(queue);
            Assert.Single(queue.RegularMessages);

            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-push", true);

            Assert.Equal(TmqResponseCode.Ok, joined);

            await Task.Delay(800);

            Assert.True(msgReceived);
        }
Esempio n. 16
0
        public async Task ExceptionOnBindMethod()
        {
            TestMqServer server = new TestMqServer();

            server.Initialize(42805);
            server.Start();

            bool          thrown = false;
            MessageReader reader = MessageReader.JsonReader();

            reader.OnException += (tm, e) => thrown = true;
            reader.On <MessageA>("ch-1", MessageA.ContentType, a => throw new InvalidOperationException());

            TmqClient client = new TmqClient();
            await client.ConnectAsync("tmq://localhost:42805");

            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(1500);

            Assert.True(client.IsConnected);
            Assert.True(thrown);
        }
Esempio n. 17
0
        public async Task SendOnlyFirstLateClients()
        {
            TestMqServer server = new TestMqServer();

            server.Initialize(42509);
            server.Start();

            Channel channel = server.Server.Channels.FirstOrDefault();

            Assert.NotNull(channel);

            ChannelQueue queue = channel.Queues.FirstOrDefault();

            Assert.NotNull(queue);

            queue.Options.SendOnlyFirstAcquirer = true;
            await queue.SetStatus(QueueStatus.Push);

            bool[] received = new bool[3];

            async Task <TmqClient> join(int no)
            {
                TmqClient client = new TmqClient();

                client.ClientId = "client-" + no;
                await client.ConnectAsync("tmq://localhost:42509");

                Assert.True(client.IsConnected);

                client.MessageReceived += (cx, m) =>
                {
                    if (m.Type == MessageType.Channel)
                    {
                        received[no - 1] = true;
                    }
                };

                return(client);
            }

            TmqClient client1 = await join(1);

            TmqClient client2 = await join(2);

            TmqClient client3 = await join(3);

            MemoryStream ms   = new MemoryStream(Encoding.UTF8.GetBytes("Hello, World!"));
            bool         sent = await client1.Push(channel.Name, queue.Id, ms, false);

            Assert.True(sent);

            await Task.Delay(1500);

            TmqResponseCode joined = await client1.Join(channel.Name, true);

            Assert.Equal(TmqResponseCode.Ok, joined);
            joined = await client2.Join(channel.Name, true);

            Assert.Equal(TmqResponseCode.Ok, joined);
            joined = await client3.Join(channel.Name, true);

            Assert.Equal(TmqResponseCode.Ok, joined);
            await Task.Delay(250);

            Assert.Empty(queue.HighPriorityMessages);
            Assert.Empty(queue.RegularMessages);
            int c = received.Count(x => x);

            Assert.Equal(1, c);
        }
Esempio n. 18
0
        public async Task HideNamesInChannel(bool enabled)
        {
            int          port   = enabled ? 42531 : 42532;
            TestMqServer server = new TestMqServer();

            server.Initialize(port);
            server.Start(300);

            Channel channel = server.Server.FindChannel("ch-1");

            Assert.NotNull(channel);
            ChannelQueue queue = channel.FindQueue(MessageA.ContentType);

            Assert.NotNull(queue);

            queue.Options.HideClientNames    = enabled;
            queue.Options.RequestAcknowledge = true;
            queue.Options.AcknowledgeTimeout = TimeSpan.FromSeconds(15);

            TmqClient client = new TmqClient();
            await client.ConnectAsync("tmq://localhost:" + port);

            client.AutoAcknowledge          = true;
            client.CatchAcknowledgeMessages = true;
            Assert.True(client.IsConnected);

            TmqResponseCode joined = await client.Join("ch-1", true);

            Assert.Equal(TmqResponseCode.Ok, joined);

            TmqMessage received = null;
            TmqMessage ack      = null;

            client.MessageReceived += (c, m) =>
            {
                switch (m.Type)
                {
                case MessageType.Channel:
                    received = m;
                    break;

                case MessageType.Acknowledge:
                    ack = m;
                    break;
                }
            };

            await Task.Delay(500);

            MemoryStream ms   = new MemoryStream(Encoding.UTF8.GetBytes("Hello, World!"));
            bool         sent = await client.Push("ch-1", MessageA.ContentType, ms, true);

            Assert.True(sent);

            await Task.Delay(1000);

            Assert.NotNull(received);
            Assert.NotNull(ack);

            Assert.Equal("ch-1", received.Target);
            Assert.Equal("ch-1", ack.Target);

            if (enabled)
            {
                Assert.Null(received.Source);
                Assert.Null(ack.Source);
            }
            else
            {
                Assert.Equal(client.ClientId, received.Source);
                Assert.Equal(client.ClientId, ack.Source);
            }
        }