public async Task MethodCall_ReqRsp_WithStatefulLayers()
        {
            var layerProvider      = new TestLayerProvider_StatefulAppend("Layer");
            var layerStackProvider = new LayerStackProvider <Dummy>(layerProvider);

            transportBuilder.SetLayerStackProvider(layerStackProvider);
            await DefaultSetup(new CalculatorService(), 1);

            layerProvider.Layers.Clear();

            var calculatorProxy = new CalculatorProxy <SimpleInMemConnection>(connections[0]);

            var request = new Message <PairedInput>(new PairedInput {
                First = 1, Second = 2
            });
            IMessage <Output> response = await calculatorProxy.AddAsync(request, CancellationToken.None);

            Assert.IsFalse(response.IsError);

            Assert.AreEqual(2, layerProvider.Layers.Count);
            Assert.AreEqual("Layer0SendLayer0Receive", layerProvider.Layers[0].State);
            Assert.AreEqual("Layer1ReceiveLayer1Send", layerProvider.Layers[1].State);

            request = new Message <PairedInput>(new PairedInput {
                First = 1, Second = 2
            });
            response = await calculatorProxy.AddAsync(request, CancellationToken.None);

            Assert.IsFalse(response.IsError);

            Assert.AreEqual(4, layerProvider.Layers.Count);
            Assert.AreEqual("Layer2SendLayer2Receive", layerProvider.Layers[2].State);
            Assert.AreEqual("Layer3ReceiveLayer3Send", layerProvider.Layers[3].State);
        }
Exemplo n.º 2
0
        public static void Main()
        {
            // Bond has an abstraction for network protocols called a Transport. Epoxy is a custom protocol that is
            // lightweight and built into Bond.Comm. If it doesn't meet your needs, you can write your own Transport.
            var transport  = new EpoxyTransportBuilder().Construct();
            var connection = transport.ConnectToAsync(new IPEndPoint(IPAddress.Loopback, EpoxyTransport.DefaultPort)).Result;

            // For each service, Bond will generate a proxy with methods corresponding to the service methods you defined.
            var proxy = new CalculatorProxy <EpoxyConnection>(connection);

            var addArgs = new BinaryOpArgs
            {
                left  = 2,
                right = 2
            };
            // The result of a Bond call is a IMessage, which is either a payload or an error.
            // Display() shows how to handle both cases.
            var addResult = proxy.AddAsync(addArgs).Result;

            Display("Add", addArgs, addResult);

            var divArgs = new BinaryOpArgs
            {
                left  = 1,
                right = 0
            };
            var divResult = proxy.DivideAsync(divArgs).Result;

            Display("Divide", divArgs, divResult);

            Console.ReadLine();
        }
        public async Task MethodCall()
        {
            await DefaultSetup(new CalculatorService(), 1);

            const int first     = 91;
            const int second    = 23;
            int       addResult = first + second;
            int       subResult = first - second;

            var calculatorProxy = new CalculatorProxy <SimpleInMemConnection>(connections[0]);

            var input = new PairedInput
            {
                First  = first,
                Second = second
            };
            var request = new Message <PairedInput>(input);
            IMessage <Output> addResponse = await calculatorProxy.AddAsync(request, System.Threading.CancellationToken.None);

            IMessage <Output> subResponse = await calculatorProxy.SubtractAsync(request, System.Threading.CancellationToken.None);

            Output addOutput = addResponse.Payload.Deserialize();
            Output subOutput = subResponse.Payload.Deserialize();

            Assert.AreEqual(addResult, addOutput.Result);
            Assert.AreEqual(subResult, subOutput.Result);
        }
        public async void TestWithServerAndClientConnections()
        {
            await DefaultSetup(new CalculatorService(), 1);

            IEnumerator <Guid> pairIds = listener.GetPairIds().GetEnumerator();

            pairIds.MoveNext();
            Guid firstPair = pairIds.Current;
            SimpleInMemConnection serverConnection = listener.GetConnection(firstPair, ConnectionType.Server);
            SimpleInMemConnection clientConnection = listener.GetConnection(firstPair, ConnectionType.Client);

            const int first     = 91;
            const int second    = 23;
            int       addResult = first + second;
            int       subResult = first - second;

            var serverProxy = new CalculatorProxy <SimpleInMemConnection>(serverConnection);
            var clientProxy = new CalculatorProxy <SimpleInMemConnection>(clientConnection);


            var input = new PairedInput
            {
                First  = first,
                Second = second
            };
            var request = new Message <PairedInput>(input);
            IMessage <Output> addResponse = await clientProxy.AddAsync(request, System.Threading.CancellationToken.None);

            IMessage <Output> subResponse = await clientProxy.SubtractAsync(request, System.Threading.CancellationToken.None);

            Assert.IsFalse(addResponse.IsError);
            Assert.IsFalse(subResponse.IsError);
            Output addOutput = addResponse.Payload.Deserialize();
            Output subOutput = subResponse.Payload.Deserialize();

            Assert.AreEqual(addResult, addOutput.Result);
            Assert.AreEqual(subResult, subOutput.Result);

            addResponse = await serverProxy.AddAsync(request, System.Threading.CancellationToken.None);

            subResponse = await serverProxy.SubtractAsync(request, System.Threading.CancellationToken.None);

            Assert.IsTrue(addResponse.IsError);
            Assert.IsTrue(subResponse.IsError);
            Error addError = addResponse.Error.Deserialize();
            Error subError = subResponse.Error.Deserialize();

            Assert.AreEqual((int)ErrorCode.METHOD_NOT_FOUND, (int)addError.error_code);
            Assert.AreEqual("Got request for unknown method [unittest.simpleinmem.Calculator.Add].", addError.message);
            Assert.AreEqual((int)ErrorCode.METHOD_NOT_FOUND, (int)subError.error_code);
            Assert.AreEqual("Got request for unknown method [unittest.simpleinmem.Calculator.Subtract].", subError.message);
        }
Exemplo n.º 5
0
        public async void TestWithServerAndClientConnections()
        {
            await DefaultSetup(new CalculatorService(), 1);
            IEnumerator<Guid> pairIds = listener.GetPairIds().GetEnumerator();
            pairIds.MoveNext();
            Guid firstPair = pairIds.Current;
            SimpleInMemConnection serverConnection = listener.GetConnection(firstPair, ConnectionType.Server);
            SimpleInMemConnection clientConnection = listener.GetConnection(firstPair, ConnectionType.Client);

            const int first = 91;
            const int second = 23;
            int addResult = first + second;
            int subResult = first - second;

            var serverProxy = new CalculatorProxy<SimpleInMemConnection>(serverConnection);
            var clientProxy = new CalculatorProxy<SimpleInMemConnection>(clientConnection);


            var input = new PairedInput
            {
                First = first,
                Second = second
            };
            var request = new Message<PairedInput>(input);
            IMessage<Output> addResponse = await clientProxy.AddAsync(request, System.Threading.CancellationToken.None);
            IMessage<Output> subResponse = await clientProxy.SubtractAsync(request, System.Threading.CancellationToken.None);
            Assert.IsFalse(addResponse.IsError);
            Assert.IsFalse(subResponse.IsError);
            Output addOutput = addResponse.Payload.Deserialize();
            Output subOutput = subResponse.Payload.Deserialize();
            Assert.AreEqual(addResult, addOutput.Result);
            Assert.AreEqual(subResult, subOutput.Result);

            addResponse = await serverProxy.AddAsync(request, System.Threading.CancellationToken.None);
            subResponse = await serverProxy.SubtractAsync(request, System.Threading.CancellationToken.None);
            Assert.IsTrue(addResponse.IsError);
            Assert.IsTrue(subResponse.IsError);
            Error addError = addResponse.Error.Deserialize();
            Error subError = subResponse.Error.Deserialize();
            Assert.AreEqual((int)ErrorCode.MethodNotFound, (int)addError.error_code);
            Assert.AreEqual("Got request for unknown method [unittest.simpleinmem.Calculator.Add].", addError.message);
            Assert.AreEqual((int)ErrorCode.MethodNotFound, (int)subError.error_code);
            Assert.AreEqual("Got request for unknown method [unittest.simpleinmem.Calculator.Subtract].", subError.message);
        }
        public async Task MethodCall_WithLayerStack()
        {
            var testList = new List <string>();
            var layer1   = new TestLayer_Append("foo", testList);
            var layer2   = new TestLayer_Append("bar", testList);

            transportBuilder.SetLayerStackProvider(new LayerStackProvider <Dummy>(layer1, layer2));
            await DefaultSetup(new CalculatorService(), 1);

            const int first     = 91;
            const int second    = 23;
            int       addResult = first + second;

            var calculatorProxy = new CalculatorProxy <SimpleInMemConnection>(connections[0]);

            var input = new PairedInput
            {
                First  = first,
                Second = second
            };
            var request = new Message <PairedInput>(input);
            IMessage <Output> addResponse = await calculatorProxy.AddAsync(request, System.Threading.CancellationToken.None);

            Output addOutput = addResponse.Payload.Deserialize();

            Assert.AreEqual(addResult, addOutput.Result);

            Assert.AreEqual(8, testList.Count);
            Assert.AreEqual(layer1.value, testList[0]);
            Assert.AreEqual(testList[0] + layer2.value, testList[1]);
            Assert.AreEqual(testList[1] + layer2.value, testList[2]);
            Assert.AreEqual(testList[2] + layer1.value, testList[3]);
            Assert.AreEqual(layer1.value, testList[4]);
            Assert.AreEqual(testList[4] + layer2.value, testList[5]);
            Assert.AreEqual(testList[5] + layer2.value, testList[6]);
            Assert.AreEqual(testList[6] + layer1.value, testList[7]);
        }
        public async Task MultipleClientConnectionsMethodCalls()
        {
            Stopwatch sw = Stopwatch.StartNew();

            await DefaultSetup(new CalculatorService(), 10);

            Task[] connectionTasks = new Task[connections.Length];

            for (int connectionIndex = 0; connectionIndex < connections.Length; connectionIndex++)
            {
                SimpleInMemConnection conn = connections[connectionIndex];
                connectionTasks[connectionIndex] = Task.Run(() =>
                {
                    int taskCount = 25;
                    Task[] tasks  = new Task[taskCount];

                    for (int taskIndex = 0; taskIndex < taskCount; taskIndex++)
                    {
                        tasks[taskIndex] = Task.Run(async() =>
                        {
                            Random rand           = new Random(DateTime.UtcNow.Millisecond);
                            int first             = rand.Next(1, 100);
                            int second            = rand.Next(1, 50);
                            int expectedAddResult = first + second;
                            int expectedSubResult = first - second;
                            var addTraceId        = Guid.NewGuid().ToString();
                            var subTraceId        = Guid.NewGuid().ToString();
                            var calculatorProxy   = new CalculatorProxy <SimpleInMemConnection>(conn);

                            var addInput = new PairedInput
                            {
                                First   = first,
                                Second  = second,
                                TraceId = addTraceId
                            };

                            var subInput = new PairedInput
                            {
                                First   = first,
                                Second  = second,
                                TraceId = subTraceId
                            };

                            Message <PairedInput> addRequest = new Message <PairedInput>(addInput);
                            Message <PairedInput> subRequest = new Message <PairedInput>(subInput);
                            IMessage <Output> addResponse    = await calculatorProxy.AddAsync(addRequest, System.Threading.CancellationToken.None);
                            IMessage <Output> subResponse    = await calculatorProxy.SubtractAsync(subRequest, System.Threading.CancellationToken.None);
                            Output addOutput = addResponse.Payload.Deserialize();
                            Output subOutput = subResponse.Payload.Deserialize();

                            Assert.AreEqual(expectedAddResult, addOutput.Result);
                            Assert.AreEqual(addInput.TraceId, addOutput.TraceId);
                            Assert.AreEqual(expectedSubResult, subOutput.Result);
                            Assert.AreEqual(subInput.TraceId, subOutput.TraceId);
                        });
                    }

                    Task.WaitAll(tasks);
                });
            }

            Task.WaitAll(connectionTasks);
            sw.Stop();
            Console.WriteLine($"{nameof(MultipleClientConnectionsMethodCalls)} - test time: {sw.Elapsed.TotalSeconds}");
        }
Exemplo n.º 8
0
        public async Task MethodCall_ReqRsp_WithStatefulLayers()
        {
            var layerProvider = new TestLayerProvider_StatefulAppend("Layer");
            var layerStackProvider = new LayerStackProvider<Dummy>(LoggerTests.BlackHole, layerProvider);
            transportBuilder.SetLayerStackProvider(layerStackProvider);
            await DefaultSetup(new CalculatorService(), 1);

            layerProvider.Layers.Clear();

            var calculatorProxy = new CalculatorProxy<SimpleInMemConnection>(connections[0]);

            var request = new Message<PairedInput>(new PairedInput { First = 1, Second = 2 });
            IMessage<Output> response = await calculatorProxy.AddAsync(request, CancellationToken.None);
            Assert.IsFalse(response.IsError);

            Assert.AreEqual(2, layerProvider.Layers.Count);
            Assert.AreEqual("Layer0SendLayer0Receive", layerProvider.Layers[0].State);
            Assert.AreEqual("Layer1ReceiveLayer1Send", layerProvider.Layers[1].State);

            request = new Message<PairedInput>(new PairedInput { First = 1, Second = 2 });
            response = await calculatorProxy.AddAsync(request, CancellationToken.None);
            Assert.IsFalse(response.IsError);

            Assert.AreEqual(4, layerProvider.Layers.Count);
            Assert.AreEqual("Layer2SendLayer2Receive", layerProvider.Layers[2].State);
            Assert.AreEqual("Layer3ReceiveLayer3Send", layerProvider.Layers[3].State);
        }
Exemplo n.º 9
0
        public async Task MethodCall_WithLayerStack()
        {
            var testList = new List<string>();
            var layer1 = new TestLayer_Append("foo", testList);
            var layer2 = new TestLayer_Append("bar", testList);

            transportBuilder.SetLayerStackProvider(new LayerStackProvider<Dummy>(LoggerTests.BlackHole, layer1, layer2));
            await DefaultSetup(new CalculatorService(), 1);

            const int first = 91;
            const int second = 23;
            int addResult = first + second;

            var calculatorProxy = new CalculatorProxy<SimpleInMemConnection>(connections[0]);

            var input = new PairedInput
            {
                First = first,
                Second = second
            };
            var request = new Message<PairedInput>(input);
            IMessage<Output> addResponse = await calculatorProxy.AddAsync(request, System.Threading.CancellationToken.None);
            Output addOutput = addResponse.Payload.Deserialize();
            Assert.AreEqual(addResult, addOutput.Result);

            Assert.AreEqual(8, testList.Count);
            Assert.AreEqual(layer1.value, testList[0]);
            Assert.AreEqual(testList[0] + layer2.value, testList[1]);
            Assert.AreEqual(testList[1] + layer2.value, testList[2]);
            Assert.AreEqual(testList[2] + layer1.value, testList[3]);
            Assert.AreEqual(layer1.value, testList[4]);
            Assert.AreEqual(testList[4] + layer2.value, testList[5]);
            Assert.AreEqual(testList[5] + layer2.value, testList[6]);
            Assert.AreEqual(testList[6] + layer1.value, testList[7]);
        }
Exemplo n.º 10
0
        public async Task MultipleClientConnectionsMethodCalls()
        {
            Stopwatch sw = Stopwatch.StartNew();

            await DefaultSetup(new CalculatorService(), 10);

            Task[] connectionTasks = new Task[connections.Length];

            for (int connectionIndex = 0; connectionIndex < connections.Length; connectionIndex++)
            {
                SimpleInMemConnection conn = connections[connectionIndex];
                connectionTasks[connectionIndex] = Task.Run(() =>
                {
                    int taskCount = 25;
                    Task[] tasks = new Task[taskCount];

                    for (int taskIndex = 0; taskIndex < taskCount; taskIndex++)
                    {
                        tasks[taskIndex] = Task.Run(async () =>
                        {
                            Random rand = new Random(DateTime.UtcNow.Millisecond);
                            int first = rand.Next(1, 100);
                            int second = rand.Next(1, 50);
                            int expectedAddResult = first + second;
                            int expectedSubResult = first - second;
                            var addTraceId = Guid.NewGuid().ToString();
                            var subTraceId = Guid.NewGuid().ToString();
                            var calculatorProxy = new CalculatorProxy<SimpleInMemConnection>(conn);

                            var addInput = new PairedInput
                            {
                                First = first,
                                Second = second,
                                TraceId = addTraceId
                            };

                            var subInput = new PairedInput
                            {
                                First = first,
                                Second = second,
                                TraceId = subTraceId
                            };

                            Message<PairedInput> addRequest = new Message<PairedInput>(addInput);
                            Message<PairedInput> subRequest = new Message<PairedInput>(subInput);
                            IMessage<Output> addResponse = await calculatorProxy.AddAsync(addRequest, System.Threading.CancellationToken.None);
                            IMessage<Output> subResponse = await calculatorProxy.SubtractAsync(subRequest, System.Threading.CancellationToken.None);
                            Output addOutput = addResponse.Payload.Deserialize();
                            Output subOutput = subResponse.Payload.Deserialize();

                            Assert.AreEqual(expectedAddResult, addOutput.Result);
                            Assert.AreEqual(addInput.TraceId, addOutput.TraceId);
                            Assert.AreEqual(expectedSubResult, subOutput.Result);
                            Assert.AreEqual(subInput.TraceId, subOutput.TraceId);
                        });
                    }

                    Task.WaitAll(tasks);
                });
            }

            Task.WaitAll(connectionTasks);
            sw.Stop();
            Console.WriteLine($"{nameof(MultipleClientConnectionsMethodCalls)} - test time: {sw.Elapsed.TotalSeconds}");
        }
Exemplo n.º 11
0
        public async Task MethodCall()
        {
            await DefaultSetup(new CalculatorService(), 1);

            const int first = 91;
            const int second = 23;
            int addResult = first + second;
            int subResult = first - second;

            var calculatorProxy = new CalculatorProxy<SimpleInMemConnection>(connections[0]);

            var input = new PairedInput
            {
                First = first,
                Second = second
            };
            var request = new Message<PairedInput>(input);
            IMessage<Output> addResponse = await calculatorProxy.AddAsync(request, System.Threading.CancellationToken.None);
            IMessage<Output> subResponse = await calculatorProxy.SubtractAsync(request, System.Threading.CancellationToken.None);
            Output addOutput = addResponse.Payload.Deserialize();
            Output subOutput = subResponse.Payload.Deserialize();
            Assert.AreEqual(addResult, addOutput.Result);
            Assert.AreEqual(subResult, subOutput.Result);
        }