Ejemplo n.º 1
0
        public void TestTcpChannel()
        {
            BaseChannel tcp = new TcpChannel();

            Assert.AreEqual(tcp.Open(), true);
            Assert.AreEqual(tcp.GetState(), ChannelState.Opened);

            // 00 02 00 00 00 05 06 03 02 00 00

            byte[] a       = new byte[] { 0x00, 0x02, 0x00, 0x00, 0x00, 0x06, 0x0A, 0x03, 0x00, 0x0D, 0x00, 0x01 };
            int    i       = 0;
            int    success = 0;

            while (true)
            {
                if (i == 100)
                {
                    return;
                }

                tcp.Write(a);
                byte[] buf = tcp.Read(256);
                if (buf.Length == 11)
                {
                    success++;
                }
                i++;
                Console.WriteLine(BitConverter.ToString(buf, 0).Replace("-", string.Empty).ToUpper());
            }
        }
        // [TestCase(400000, 100, 13348)]
        public void SendAndReceiveViaTcpTest(int length, int concurentLevel, int port)
        {
            TcpClient client;
            TcpClient serverClient;

            IntegrationTestsHelper.CreateTwoConnectedTcpClients(port, out client, out serverClient);

            var         channelA     = new TcpChannel(client);
            var         channelB     = new TcpChannel(serverClient);
            List <byte> recievedList = new List <byte>(length);
            int         doneThreads  = 0;

            Exception innerException = null;

            channelB.OnReceive += (_, msg) => {
                recievedList.AddRange(msg);
                if (recievedList.Count >= length)
                {
                    byte lastValue = recievedList[0];
                    for (int i = 0; i < length; i++)
                    {
                        var val = recievedList[0];
                        recievedList.RemoveAt(0);
                        try { Assert.AreEqual(lastValue, val, "Value is not as expected sience index " + i); }
                        catch (Exception e) { innerException = e; }
                    }
                    doneThreads++;
                }
            };
            channelB.AllowReceive = true;

            IntegrationTestsHelper.RunInParrallel <byte[]>(concurentLevel,
                                                           (i) =>
            {
                return(IntegrationTestsHelper.CreateArray(length, (byte)i));
            },
                                                           (i, msg) =>
            {
                channelA.Write(msg, 0, msg.Length);
            });
            IntegrationTestsHelper.WaitOrThrow(() => (doneThreads >= concurentLevel), () => innerException);
            channelA.Disconnect();
            channelB.Disconnect();
        }