Esempio n. 1
0
        public void AsyncEnumerable()
        {
            using var server = new ServerSocket();
            int port = server.BindRandomPort("tcp://*");

            using var client = new ClientSocket();
            client.Connect($"tcp://127.0.0.1:{port}");

            int totalCount = 0;

            var t1 = Task.Run(async() =>
            {
                int count = 0;

                await foreach (var(_, msg) in server.ReceiveStringAsyncEnumerable())
                {
                    count++;
                    Interlocked.Increment(ref totalCount);

                    if (msg == "1")
                    {
                        m_testOutputHelper.WriteLine($"T1 read {count} messages");
                        return;
                    }
                }
            });

            var t2 = Task.Run(async() => {
                int count = 0;

                await foreach (var(_, msg) in server.ReceiveStringAsyncEnumerable())
                {
                    count++;
                    Interlocked.Increment(ref totalCount);

                    if (msg == "1")
                    {
                        m_testOutputHelper.WriteLine($"T2 read {count} messages");
                        return;
                    }
                }
            });

            for (int i = 0; i < 15000; i++)
            {
                client.Send("0");
            }

            // Send the end message to both of the threads
            client.Send("1");
            client.Send("1");

            t1.Wait();
            t2.Wait();

            Assert.Equal(15002, totalCount);
        }
Esempio n. 2
0
        public void Tcp()
        {
            using var server = new ServerSocket();
            using var client = new ClientSocket();
            int port = server.BindRandomPort("tcp://*");

            client.Connect($"tcp://localhost:{port}");

            client.Send("Hello");
            var(routingId, clientMsg) = server.ReceiveString();
            Assert.NotEqual <uint>(0, routingId);
            Assert.Equal("Hello", clientMsg);

            server.Send(routingId, "World");
            var serverMsg = client.ReceiveString();

            Assert.Equal("World", serverMsg);
        }