示例#1
0
        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);
        }
示例#2
0
        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);
        }
示例#3
0
        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);
            }
        }
示例#4
0
        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);
        }
示例#5
0
        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);
        }
示例#6
0
        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);
        }
示例#7
0
文件: Program.cs 项目: Akrotiri/twino
        static void Main(string[] args)
        {
            TwinoServer server = new TwinoServer(ServerOptions.CreateDefault());

            server.UseTmq(async(socket, msg) =>
            {
                Console.WriteLine(msg);
                await Task.CompletedTask;
            });
            server.Start(82);

            TmqClient client = new TmqClient();

            client.Data.Properties.Add("Host", "localhost");
            client.ClientId = "123";

            client.Connect("tmq://localhost:82/sample");

            Console.ReadLine();
        }
示例#8
0
        public async Task JoinChannelWithResponse()
        {
            TestMqServer server = new TestMqServer();

            server.Initialize(41202);
            server.Start();

            TmqClient client = new TmqClient();

            client.Connect("tmq://localhost:41202");

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

            Assert.Equal(TmqResponseCode.Ok, joined);

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

            Assert.NotNull(channel);

            List <ChannelClient> clients = channel.ClientsClone;

            Assert.Single(clients);
        }