Esempio n. 1
0
        public void Connection_No_Response_Throws_Exception()
        {
            if (IsRunningOnAzurePipelines)
            {
                return;
            }

            IResonanceTransporter signal2 = ResonanceTransporter.Builder
                                            .Create()
                                            .WithInMemoryAdapter()
                                            .WithAddress("TST")
                                            .WithJsonTranscoding()
                                            .Build();

            signal2.Connect();

            Thread.Sleep(100);

            WebRTCAdapter adapter2 = new WebRTCAdapter(signal2, WebRTCAdapterRole.Connect);

            Assert.ThrowsException <ResonanceWebRTCConnectionFailedException>(() =>
            {
                adapter2.Connect();
            });

            adapter2.Dispose();
            signal2.Dispose();
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            Console.Title = "Resonance Shared Memory Demo";

            Console.WriteLine("Resonance Shared Memory Demo.");
            Console.WriteLine();

            Console.Write("Adapter Address: ");
            String address = Console.ReadLine();

            Console.WriteLine("Connecting...");

            IResonanceTransporter transporter = ResonanceTransporter.Builder
                                                .Create()
                                                .WithSharedMemoryAdapter()
                                                .WithAddress(address)
                                                .WithJsonTranscoding()
                                                .Build();

            transporter.StateChanged += (_, e) =>
            {
                if (e.NewState == ResonanceComponentState.Failed)
                {
                    Console.WriteLine();
                    Console.WriteLine($"Transporter Disconnected.\n{transporter.FailedStateException.Message}");
                }
            };

            transporter.RegisterMessageHandler <SharedMemoryMessage>((t, msg) =>
            {
                Console.WriteLine();
                Console.WriteLine($"Message Received: {msg.Object.Text}");
                Console.Write("Send Message: ");
            });

            transporter.Connect();

            Console.WriteLine("Connected.");

            while (true)
            {
                Console.Write("Send Message: ");
                String text = Console.ReadLine();

                if (text.ToLower() == "exit")
                {
                    Console.WriteLine("Disconnecting...");
                    transporter.Disconnect();
                    Console.ReadLine();
                    return;
                }

                transporter.Send(new SharedMemoryMessage()
                {
                    Text = text
                });
            }
        }
Esempio n. 3
0
        public static void Read_Write_Test(ResonanceTest test, IResonanceTransporter t1, IResonanceTransporter t2, bool enableCryptography, int count, int maxOutliersPercentage)
        {
            t1.CryptographyConfiguration.Enabled = enableCryptography;
            t2.CryptographyConfiguration.Enabled = enableCryptography;

            t2.Connect();
            t1.Connect();

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

            Stopwatch watch = new Stopwatch();

            List <double> measurements = new List <double>();

            for (int i = 0; i < count; i++)
            {
                watch.Restart();

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

                measurements.Add(watch.ElapsedMilliseconds);

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

            watch.Stop();

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

            if (count > 1 && maxOutliersPercentage > 0)
            {
                var outliers = TestHelper.GetOutliers(measurements);

                double percentageOfOutliers = outliers.Count / (double)measurements.Count * 100d;

                if (!test.IsRunningOnAzurePipelines)
                {
                    Assert.IsTrue(percentageOfOutliers < maxOutliersPercentage, $"Request/Response duration measurements contains {percentageOfOutliers}% outliers and is considered a performance issue.");
                }
            }
        }
Esempio n. 4
0
        public void Auto_Decoding__Needs_A_Second_Run()
        {
            return;

            //This test needs a second run. not sure why.


            if (IsRunningOnAzurePipelines)
            {
                return;                            //Hangs when running in a sequence of tests for some reason.
            }
            IResonanceTransporter t1 = ResonanceTransporter.Builder
                                       .Create()
                                       .WithInMemoryAdapter()
                                       .WithAddress("TST")
                                       .WithTranscoding <BsonEncoder, AutoDecoder>()
                                       .Build();

            IResonanceTransporter t2 = ResonanceTransporter.Builder
                                       .Create()
                                       .WithInMemoryAdapter()
                                       .WithAddress("TST")
                                       .WithTranscoding <JsonEncoder, AutoDecoder>()
                                       .Build();


            t1.Connect();
            t2.Connect();

            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, new ResonanceRequestConfig()
            {
                Timeout = TimeSpan.FromSeconds(20)
            });

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

            Assert.AreEqual(response.Sum, request.A + request.B);
        }
Esempio n. 5
0
        public void Bson_Transcoding_With_DateTime_Kind()
        {
            if (IsRunningOnAzurePipelines)
            {
                return;                            //Hangs when running in a sequence of tests for some reason.
            }
            IResonanceTransporter t1 = ResonanceTransporter.Builder
                                       .Create()
                                       .WithInMemoryAdapter()
                                       .WithAddress("TST")
                                       .WithBsonTranscoding()
                                       .Build();

            IResonanceTransporter t2 = ResonanceTransporter.Builder
                                       .Create()
                                       .WithInMemoryAdapter()
                                       .WithAddress("TST")
                                       .WithBsonTranscoding()
                                       .Build();

            t1.Connect();
            t2.Connect();

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


            for (int i = 0; i < 1000; i++)
            {
                var request = new CalculateRequestWithDate()
                {
                    A = 10, B = 15, Date = DateTime.UtcNow
                };
                var response = t1.SendRequest <CalculateRequestWithDate, CalculateResponseWithDate>(request);

                Assert.AreEqual(response.Sum, request.A + request.B);
                Assert.AreEqual(request.Date.Kind, response.Date.Kind);
                Assert.AreEqual(request.Date.ToString(), response.Date.ToString());
            }

            t1.Dispose(true);
            t2.Dispose(true);
        }
Esempio n. 6
0
        static void Main(string[] args)
        {
            Console.WriteLine("Starting Resonance UDP Broadcast Demo...");

            Console.WriteLine("Connecting transporter with UDP adapter on all addresses...");

            //Initialize a transporter with UDP adapter. Listening and sending to all interfaces.
            IResonanceTransporter transporter = ResonanceTransporter.Builder
                                                .Create()
                                                .WithUdpAdapter()
                                                .Broadcast(1234)
                                                .PreventLoopback()
                                                .WithJsonTranscoding()
                                                .Build();

            //Connect the transporter.
            transporter.Connect();

            //Register the service to handle incoming messages from other clients.
            transporter.RegisterService <IRPCBroadcastService, RPCBroadcastService>(RpcServiceCreationType.Singleton);

            //Create a service client proxy to send messages to other clients.
            IRPCBroadcastService client = transporter.CreateClientProxy <IRPCBroadcastService>();

            Console.WriteLine("Now connected. Make sure at least one more client is also connected before sending messages.");
            Console.WriteLine();

            String input = String.Empty;

            while (input.ToLower() != "exit")
            {
                Console.Write("Enter a message to broadcast: ");
                input = Console.ReadLine();

                try
                {
                    client.BroadcastMessage(input);
                }
                catch (Exception ex)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine(ex.Message);
                    Console.ForegroundColor = ConsoleColor.Gray;
                }

                Console.WriteLine();
            }
        }
Esempio n. 7
0
        public void Protobuf_Transcoding_Type_Resolver()
        {
            if (IsRunningOnAzurePipelines)
            {
                return;
            }

            IResonanceTransporter t1 = ResonanceTransporter.Builder
                                       .Create()
                                       .WithInMemoryAdapter()
                                       .WithAddress("TST")
                                       .WithProtobufTranscoding()
                                       .WithMessageTypeHeaderMethod(MessageTypeHeaderMethod.Name)
                                       .WithTypeResolver <ProtobufTypeResolver>()
                                       .Build();

            IResonanceTransporter t2 = ResonanceTransporter.Builder
                                       .Create()
                                       .WithInMemoryAdapter()
                                       .WithAddress("TST")
                                       .WithProtobufTranscoding()
                                       .WithMessageTypeHeaderMethod(MessageTypeHeaderMethod.Name)
                                       .WithTypeResolver <ProtobufTypeResolver>()
                                       .Build();

            t1.Connect();
            t2.Connect();

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

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

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

            Assert.AreEqual(response.Sum, request.A + request.B);
        }
Esempio n. 8
0
        public void Basic_Test()
        {
            if (IsRunningOnAzurePipelines)
            {
                return;
            }

            IResonanceTransporter signal1 = ResonanceTransporter.Builder
                                            .Create()
                                            .WithInMemoryAdapter()
                                            .WithAddress("TST")
                                            .WithJsonTranscoding()
                                            .Build();

            IResonanceTransporter signal2 = ResonanceTransporter.Builder
                                            .Create()
                                            .WithInMemoryAdapter()
                                            .WithAddress("TST")
                                            .WithJsonTranscoding()
                                            .Build();

            signal1.Connect();
            signal2.Connect();

            WebRTCAdapter adapter1 = new WebRTCAdapter(signal1, WebRTCAdapterRole.Accept);

            Task.Factory.StartNew(() =>
            {
                adapter1.Connect();
            });

            Thread.Sleep(100);

            WebRTCAdapter adapter2 = new WebRTCAdapter(signal2, WebRTCAdapterRole.Connect);

            adapter2.Connect();

            IResonanceTransporter t1 = ResonanceTransporter.Builder
                                       .Create()
                                       .WithAdapter(adapter1)
                                       .WithJsonTranscoding()
                                       .Build();

            IResonanceTransporter t2 = ResonanceTransporter.Builder
                                       .Create()
                                       .WithAdapter(adapter2)
                                       .WithJsonTranscoding()
                                       .Build();

            t2.RequestReceived += (x, e) =>
            {
                t2.SendResponseAsync(new CalculateResponse()
                {
                    Sum = 15
                }, e.Message.Token);
            };

            t1.Connect();
            t2.Connect();

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

            Assert.IsTrue(response.Sum == 15);

            t1.Dispose();
            t2.Dispose();
            signal1.Dispose();
            signal2.Dispose();
        }
Esempio n. 9
0
        public void Stress_Test_With_Small_Data_Volume()
        {
            if (IsRunningOnAzurePipelines)
            {
                return;
            }

            IResonanceTransporter signal1 = ResonanceTransporter.Builder
                                            .Create()
                                            .WithInMemoryAdapter()
                                            .WithAddress("TST")
                                            .WithJsonTranscoding()
                                            .Build();

            IResonanceTransporter signal2 = ResonanceTransporter.Builder
                                            .Create()
                                            .WithInMemoryAdapter()
                                            .WithAddress("TST")
                                            .WithJsonTranscoding()
                                            .Build();

            signal1.Connect();
            signal2.Connect();

            WebRTCAdapter adapter1 = null;

            signal1.RegisterRequestHandler <WebRTCOfferRequest>(async(_, request) =>
            {
                adapter1 = new WebRTCAdapter(signal1, request.Object, request.Token);
                await adapter1.ConnectAsync();
            });

            WebRTCAdapter adapter2 = new WebRTCAdapter(signal2, WebRTCAdapterRole.Connect);

            adapter2.Connect();

            IResonanceTransporter t1 = ResonanceTransporter.Builder
                                       .Create()
                                       .WithAdapter(adapter1)
                                       .WithJsonTranscoding()
                                       .Build();

            IResonanceTransporter t2 = ResonanceTransporter.Builder
                                       .Create()
                                       .WithAdapter(adapter2)
                                       .WithJsonTranscoding()
                                       .Build();

            t2.RequestReceived += (x, e) =>
            {
                t2.SendResponseAsync(new LargeMessageResponse()
                {
                    Data = (e.Message.Object as LargeMessageRequest).Data
                }, e.Message.Token);
            };

            t1.Connect();
            t2.Connect();

            t1.DefaultRequestTimeout = TimeSpan.FromSeconds(60);

            for (int i = 0; i < 100; i++)
            {
                byte[] data     = TestHelper.GetRandomByteArray(2);
                var    response = t1.SendRequest <LargeMessageRequest, LargeMessageResponse>(new LargeMessageRequest()
                {
                    Data = data
                });
                Assert.IsTrue(data.SequenceEqual(response.Data));
            }

            t1.Dispose();
            t2.Dispose();
            signal1.Dispose();
            signal2.Dispose();
        }
Esempio n. 10
0
        public void Message_Larger_Than_16_KB_Splits()
        {
            if (IsRunningOnAzurePipelines)
            {
                return;
            }

            IResonanceTransporter signal1 = ResonanceTransporter.Builder
                                            .Create()
                                            .WithInMemoryAdapter()
                                            .WithAddress("TST")
                                            .WithJsonTranscoding()
                                            .Build();

            IResonanceTransporter signal2 = ResonanceTransporter.Builder
                                            .Create()
                                            .WithInMemoryAdapter()
                                            .WithAddress("TST")
                                            .WithJsonTranscoding()
                                            .Build();

            signal1.Connect();
            signal2.Connect();

            WebRTCAdapter adapter1 = null;

            signal1.RegisterRequestHandler <WebRTCOfferRequest>(async(_, request) =>
            {
                adapter1 = new WebRTCAdapter(signal1, request.Object, request.Token);
                await adapter1.ConnectAsync();
            });

            WebRTCAdapter adapter2 = new WebRTCAdapter(signal2, WebRTCAdapterRole.Connect);

            adapter2.Connect();

            IResonanceTransporter t1 = ResonanceTransporter.Builder
                                       .Create()
                                       .WithAdapter(adapter1)
                                       .WithJsonTranscoding()
                                       .Build();

            IResonanceTransporter t2 = ResonanceTransporter.Builder
                                       .Create()
                                       .WithAdapter(adapter2)
                                       .WithJsonTranscoding()
                                       .Build();

            t2.RequestReceived += (x, e) =>
            {
                t2.SendResponseAsync(new LargeMessageResponse()
                {
                    Data = (e.Message.Object as LargeMessageRequest).Data
                }, e.Message.Token);
            };

            t1.Connect();
            t2.Connect();

            byte[] data = TestHelper.GetRandomByteArray(200);

            var response = t1.SendRequest <LargeMessageRequest, LargeMessageResponse>(new LargeMessageRequest()
            {
                Data = data
            });

            Assert.IsTrue(data.SequenceEqual(response.Data));

            t1.Dispose();
            t2.Dispose();
            signal1.Dispose();
            signal2.Dispose();
        }
Esempio n. 11
0
        public void Create_Adapter_From_Offer()
        {
            if (IsRunningOnAzurePipelines)
            {
                return;
            }

            IResonanceTransporter signal1 = ResonanceTransporter.Builder
                                            .Create()
                                            .WithInMemoryAdapter()
                                            .WithAddress("TST")
                                            .WithJsonTranscoding()
                                            .Build();

            IResonanceTransporter signal2 = ResonanceTransporter.Builder
                                            .Create()
                                            .WithInMemoryAdapter()
                                            .WithAddress("TST")
                                            .WithJsonTranscoding()
                                            .Build();

            signal1.Connect();
            signal2.Connect();

            WebRTCAdapter adapter1 = null;

            signal1.RegisterRequestHandler <WebRTCOfferRequest>(async(_, request) =>
            {
                adapter1 = new WebRTCAdapter(signal1, request.Object, request.Token);
                await adapter1.ConnectAsync();
            });

            WebRTCAdapter adapter2 = new WebRTCAdapter(signal2, WebRTCAdapterRole.Connect);

            adapter2.Connect();

            IResonanceTransporter t1 = ResonanceTransporter.Builder
                                       .Create()
                                       .WithAdapter(adapter1)
                                       .WithJsonTranscoding()
                                       .Build();

            IResonanceTransporter t2 = ResonanceTransporter.Builder
                                       .Create()
                                       .WithAdapter(adapter2)
                                       .WithJsonTranscoding()
                                       .Build();

            t2.RequestReceived += (x, e) =>
            {
                t2.SendResponseAsync(new CalculateResponse()
                {
                    Sum = 15
                }, e.Message.Token);
            };

            t1.Connect();
            t2.Connect();

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

            Assert.IsTrue(response.Sum == 15);

            t1.Dispose();
            t2.Dispose();
            signal1.Dispose();
            signal2.Dispose();
        }
Esempio n. 12
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();
        }
Esempio n. 13
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();
        }
Esempio n. 14
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();
        }
Esempio n. 15
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();
        }