Beispiel #1
0
        public void Tcp_Adapter_Writing_Reading()
        {
            ResonanceTransporter t1 = new ResonanceTransporter(new TcpAdapter(TcpAdapter.GetLocalIPAddress(), 9999));
            ResonanceTransporter t2 = new ResonanceTransporter();

            ResonanceTcpServer server = new ResonanceTcpServer(9999);

            server.Start();
            server.ConnectionRequest += (x, e) =>
            {
                t2.Adapter = e.Accept();
                t2.Connect();
            };

            t1.Connect();

            while (t2.State != ResonanceComponentState.Connected)
            {
                Thread.Sleep(10);
            }

            TestUtils.Read_Write_Test(this, t1, t2, false, 1000, 5);

            server.Dispose();
        }
Beispiel #2
0
        public void Transporters_Router_Standard_Propagates_Connection_Loss()
        {
            IResonanceTransporter receiver1 = null;
            IResonanceTransporter client1   = null;
            IResonanceTransporter receiver2 = null;
            IResonanceTransporter client2   = null;
            TransporterRouter     router    = null;

            ResonanceTcpServer server = new ResonanceTcpServer(1333);

            server.ConnectionRequest += (x, e) =>
            {
                if (receiver1 == null)
                {
                    receiver1 = ResonanceTransporter.Builder.Create()
                                .WithAdapter(e.Accept())
                                .WithJsonTranscoding()
                                .WithKeepAlive(TimeSpan.FromSeconds(1), 1)
                                .Build();

                    receiver1.Connect();
                }
                else
                {
                    receiver2 = ResonanceTransporter.Builder.Create()
                                .WithAdapter(e.Accept())
                                .WithJsonTranscoding()
                                .WithKeepAlive()
                                .Build();

                    receiver2.Connect();

                    router = new TransporterRouter(receiver1, receiver2, RoutingMode.TwoWay, WritingMode.Standard);
                    router.Bind();
                }
            };
            server.Start();


            client1 = ResonanceTransporter.Builder.Create()
                      .WithTcpAdapter()
                      .WithAddress("127.0.0.1")
                      .WithPort(1333)
                      .WithJsonTranscoding()
                      .Build();

            client1.NotifyOnDisconnect = false;
            client1.KeepAliveConfiguration.EnableAutoResponse = false;

            client1.Connect();

            client2 = ResonanceTransporter.Builder.Create()
                      .WithTcpAdapter()
                      .WithAddress("127.0.0.1")
                      .WithPort(1333)
                      .WithJsonTranscoding()
                      .WithKeepAlive()
                      .Build();

            client2.Connect();

            Thread.Sleep(15000);

            client1.Disconnect();

            Assert.IsTrue(client1.State == ResonanceComponentState.Disconnected);
            Assert.IsTrue(receiver1.State == ResonanceComponentState.Failed);
            Assert.IsTrue(receiver2.State == ResonanceComponentState.Disconnected);
            Assert.IsTrue(client2.State == ResonanceComponentState.Failed);


            client1.Dispose();
            client2.Dispose();
            receiver1.Dispose();
            receiver2.Dispose();
            server.Dispose();
            router.Dispose();
        }
Beispiel #3
0
        public void Transporters_Router_Standard_Propagates_Disconnection()
        {
            IResonanceTransporter receiver1 = null;
            IResonanceTransporter client1   = null;
            IResonanceTransporter receiver2 = null;
            IResonanceTransporter client2   = null;
            TransporterRouter     router    = null;

            ResonanceTcpServer server = new ResonanceTcpServer(1333);

            server.ConnectionRequest += (x, e) =>
            {
                if (receiver1 == null)
                {
                    receiver1 = ResonanceTransporter.Builder.Create()
                                .WithAdapter(e.Accept())
                                .WithJsonTranscoding()
                                .Build();

                    receiver1.Connect();
                }
                else
                {
                    receiver2 = ResonanceTransporter.Builder.Create()
                                .WithAdapter(e.Accept())
                                .WithJsonTranscoding()
                                .Build();

                    receiver2.Connect();

                    router = new TransporterRouter(receiver1, receiver2, RoutingMode.TwoWay, WritingMode.Standard);
                    router.Bind();
                }
            };
            server.Start();


            client1 = ResonanceTransporter.Builder.Create()
                      .WithTcpAdapter()
                      .WithAddress("127.0.0.1")
                      .WithPort(1333)
                      .WithJsonTranscoding()
                      .Build();

            client1.Connect();

            client2 = ResonanceTransporter.Builder.Create()
                      .WithTcpAdapter()
                      .WithAddress("127.0.0.1")
                      .WithPort(1333)
                      .WithJsonTranscoding()
                      .Build();

            client2.Connect();

            Thread.Sleep(1000);

            client1.Disconnect();

            Thread.Sleep(500);

            Assert.IsTrue(client1.State == ResonanceComponentState.Disconnected);
            Assert.IsTrue(receiver1.State == ResonanceComponentState.Failed);
            Assert.IsTrue(receiver2.State == ResonanceComponentState.Disconnected);
            Assert.IsTrue(client2.State == ResonanceComponentState.Failed);


            client1.Dispose();
            client2.Dispose();
            receiver1.Dispose();
            receiver2.Dispose();
            server.Dispose();
            router.Dispose();
        }
Beispiel #4
0
        public void Transporters_Router_Standard_Routes_Data()
        {
            IResonanceTransporter receiver1 = null;
            IResonanceTransporter client1   = null;
            IResonanceTransporter receiver2 = null;
            IResonanceTransporter client2   = null;
            TransporterRouter     router    = null;

            ResonanceTcpServer server = new ResonanceTcpServer(1333);

            server.ConnectionRequest += (x, e) =>
            {
                if (receiver1 == null)
                {
                    receiver1 = ResonanceTransporter.Builder.Create()
                                .WithAdapter(e.Accept())
                                .WithJsonTranscoding()
                                .Build();

                    receiver1.Connect();
                }
                else
                {
                    receiver2 = ResonanceTransporter.Builder.Create()
                                .WithAdapter(e.Accept())
                                .WithJsonTranscoding()
                                .Build();

                    receiver2.Connect();

                    router = new TransporterRouter(receiver1, receiver2, RoutingMode.TwoWay, WritingMode.Standard);
                    router.Bind();
                }
            };
            server.Start();


            client1 = ResonanceTransporter.Builder.Create()
                      .WithTcpAdapter()
                      .WithAddress("127.0.0.1")
                      .WithPort(1333)
                      .WithJsonTranscoding()
                      .Build();

            client1.Connect();

            client2 = ResonanceTransporter.Builder.Create()
                      .WithTcpAdapter()
                      .WithAddress("127.0.0.1")
                      .WithPort(1333)
                      .WithJsonTranscoding()
                      .Build();

            client2.Connect();

            client2.RegisterRequestHandler <CalculateRequest, CalculateResponse>((request) =>
            {
                return(new CalculateResponse()
                {
                    Sum = request.A + request.B
                });
            });

            client1.RegisterRequestHandler <CalculateRequest, CalculateResponse>((request) =>
            {
                return(new CalculateResponse()
                {
                    Sum = request.A + request.B
                });
            });

            Thread.Sleep(1000);

            receiver1.RequestReceived += (_, __) =>
            {
                Assert.Fail();
            };

            receiver2.RequestReceived += (_, __) =>
            {
                Assert.Fail();
            };

            var response = client1.SendRequest <CalculateRequest, CalculateResponse>(new CalculateRequest()
            {
                A = 10,
                B = 5
            });

            Assert.AreEqual(response.Sum, 15);

            response = client2.SendRequest <CalculateRequest, CalculateResponse>(new CalculateRequest()
            {
                A = 11,
                B = 5
            });

            Assert.AreEqual(response.Sum, 16);

            client1.Dispose();
            client2.Dispose();
            receiver1.Dispose();
            receiver2.Dispose();
            server.Dispose();
            router.Dispose();
        }
Beispiel #5
0
        public void Tcp_Connect_Send_And_Receive_100_Concurrent_Transporters()
        {
            if (IsRunningOnAzurePipelines)
            {
                return;
            }

            List <IResonanceTransporter> receivers = new List <IResonanceTransporter>();

            ResonanceTcpServer server = new ResonanceTcpServer(4321);

            server.ConnectionRequest += (x, e) =>
            {
                IResonanceTransporter t = ResonanceTransporter.Builder.Create()
                                          .WithAdapter(e.Accept())
                                          .WithJsonTranscoding()
                                          .Build();

                receivers.Add(t);

                t.RegisterRequestHandler <CalculateRequest, CalculateResponse>((request) =>
                {
                    return(new CalculateResponse()
                    {
                        Sum = request.A + request.B
                    });
                });

                t.Connect();
            };

            server.Start();

            List <Thread> threads = new List <Thread>();

            for (int i = 0; i < 100; i++)
            {
                var t = ThreadFactory.StartNew(() =>
                {
                    IResonanceTransporter client = ResonanceTransporter.Builder.Create()
                                                   .WithTcpAdapter()
                                                   .WithAddress("127.0.0.1")
                                                   .WithPort(4321)
                                                   .WithJsonTranscoding()
                                                   .Build();

                    client.Connect();

                    var request = new CalculateRequest()
                    {
                        A = 10, B = 15
                    };
                    var response = client.SendRequest <CalculateRequest, CalculateResponse>(request, new ResonanceRequestConfig()
                    {
                        Timeout = TimeSpan.FromSeconds(5)
                    });

                    Assert.AreEqual(response.Sum, request.A + request.B);

                    client.Dispose();
                });

                threads.Add(t);
            }

            TestHelper.WaitWhile(() => threads.Count < 100 || receivers.Count < 100, TimeSpan.FromSeconds(30));

            threads.ForEach(x => x.Join());
            receivers.ForEach(x => x.Dispose());
            server.Dispose();
        }
Beispiel #6
0
        public void Udp_Discovery_And_Tcp_Transporter_Connection()
        {
            ResonanceUdpDiscoveryService <DiscoveryInfoTransporter, JsonEncoder> service =
                new ResonanceUdpDiscoveryService <DiscoveryInfoTransporter, JsonEncoder>(new DiscoveryInfoTransporter()
            {
                TcpServerPort = 9999
            }, 1984);

            service.Start();

            ResonanceUdpDiscoveryClient <DiscoveryInfoTransporter, JsonDecoder> client = new ResonanceUdpDiscoveryClient <DiscoveryInfoTransporter, JsonDecoder>(1984);

            var services = client.Discover(TimeSpan.FromSeconds(10), 1);

            Assert.IsTrue(services.Count == 1);
            Assert.IsTrue(services[0].DiscoveryInfo.TcpServerPort == service.DiscoveryInfo.TcpServerPort);

            service.Dispose();
            client.Dispose();

            var discoveredService = services.First();

            ResonanceTransporter t1 = new ResonanceTransporter(new TcpAdapter(discoveredService.Address, discoveredService.DiscoveryInfo.TcpServerPort));
            ResonanceTransporter t2 = new ResonanceTransporter();

            ResonanceTcpServer server = new ResonanceTcpServer(9999);

            server.Start();
            server.ConnectionRequest += (x, e) =>
            {
                t2.Adapter = e.Accept();
                t2.Connect();
            };

            t1.Connect();

            while (t2.State != ResonanceComponentState.Connected)
            {
                Thread.Sleep(10);
            }

            t2.RequestReceived += (s, e) =>
            {
                CalculateRequest receivedRequest = e.Message.Object as CalculateRequest;
                t2.SendResponse(new CalculateResponse()
                {
                    Sum = receivedRequest.A + receivedRequest.B
                }, e.Message.Token);
            };

            var request = new CalculateRequest()
            {
                A = 10, B = 15
            };
            var response = t1.SendRequest <CalculateRequest, CalculateResponse>(request);

            Assert.AreEqual(response.Sum, request.A + request.B);

            t1.Dispose(true);
            t2.Dispose(true);
            server.Dispose();
        }