public async void TestSendAndReceive()
        {
            var resolver = new DependencyResolver();

            using (var broker = new ZmqBroker(this.zmqContext, ClientInboundAddress, ClientOutboundAddress, ServerInboundAddress, ServerOutboundAddress))
            {
                broker.Listen();

                using (var server = new ZmqServer(this.zmqContext, ServerInboundAddress, ServerOutboundAddress, new ServiceFactory(resolver)))
                {
                    server.Listen();

                    using (var client = new ZmqClient(this.zmqContext, ClientInboundAddress, ClientOutboundAddress))
                    {
                        var clientFactory = new ServiceClientFactory(client);

                        var serviceClient = clientFactory.CreateServiceClient <ITestService2>();

                        Assert.That(serviceClient.GetPerson(1), Is.Not.Null);

                        var persons = await serviceClient.ListPersonsAsync(5);

                        Assert.That(persons, Is.Not.Null);
                        Assert.AreEqual(5, persons.Count());

                        var nullCollection = await serviceClient.ListPersonsAsync(-1);

                        Assert.IsNull(nullCollection);

                        var nullObject = serviceClient.GetPerson(-1);
                        Assert.IsNull(nullObject);
                    }
                }
            }
        }
        public async void TestSendAndReceive()
        {
            var resolver = new DependencyResolver();

            using (var broker = new ZmqBroker(this.zmqContext, ClientInboundAddress, ClientOutboundAddress, ServerInboundAddress, ServerOutboundAddress))
            {
                broker.Listen();

                using (var server = new ZmqServer(this.zmqContext, ServerInboundAddress, ServerOutboundAddress, new ServiceFactory(resolver)))
                {
                    server.Listen();

                    using (var client = new ZmqClient(this.zmqContext, ClientInboundAddress, ClientOutboundAddress))
                    {
                        var clientFactory = new ServiceClientFactory(client);

                        var serviceClient = clientFactory.CreateServiceClient<ITestService2>();

                        Assert.That(serviceClient.GetPerson(1), Is.Not.Null);

                        var persons = await serviceClient.ListPersonsAsync(5);
                        Assert.That(persons, Is.Not.Null);
                        Assert.AreEqual(5, persons.Count());

                        var nullCollection = await serviceClient.ListPersonsAsync(-1);
                        Assert.IsNull(nullCollection);

                        var nullObject = serviceClient.GetPerson(-1);
                        Assert.IsNull(nullObject);
                    }
                }
            }
        }
Example #3
0
        private void TestConnection()
        {
            var client = new ZmqClient(6789);
            var call   = new PyUniCall("testfun", 69420, new List <object>(), new Dictionary <string, object>());

            Debug.Log(client.SendData(call));
            call.args   = new List <object>(new object[] { 5, 7 });
            call.kwargs = new Dictionary <string, object>();
            call.kwargs.Add("array", new PyUniValNdArray(new float[] { 5.5f, 6e-3f, 7.00007f, 80.08f, 9 }));
            var res = client.SendData(call);

            Debug.Log(res);
            Debug.Log((res as PyUniResult).result[0]);
        }
        //[TestCase(5, 1000000)]
        public async void TestLoadBalancing(int nServers, int nMsgs)
        {
            var resolver = new DependencyResolver();

            using (var broker = new ZmqBroker(this.zmqContext, ClientInboundAddress, ClientOutboundAddress, ServerInboundAddress, ServerOutboundAddress))
            {
                broker.Listen();

                var servers = Enumerable.Range(0, nServers)
                              .Select(i => new ZmqServer(this.zmqContext, ServerInboundAddress, ServerOutboundAddress, new ServiceFactory(resolver)))
                              .ToArray();

                try
                {
                    foreach (var server in servers)
                    {
                        server.Listen();
                    }

                    using (var client = new ZmqClient(this.zmqContext, ClientInboundAddress, ClientOutboundAddress))
                    {
                        var clientFactory = new ServiceClientFactory(client);

                        var serviceClient = clientFactory.CreateServiceClient <ITestService2>();

                        var tasks = Enumerable.Range(0, nMsgs)
                                    //.Select(i => serviceClient.SumAsync(5, 15))
                                    .Select(i => serviceClient.ListPersonsAsync(7))
                                    .ToArray();

                        await Task.WhenAll(tasks);

                        Assert.True(tasks.All(t => t.Result.Count() == 7));
                    }
                }
                finally
                {
                    foreach (var server in servers)
                    {
                        server.Dispose();
                    }
                }
            }
        }
        public async void TestSendAndReceiveExceptions()
        {
            var resolver = new DependencyResolver();

            using (var broker = new ZmqBroker(this.zmqContext, ClientInboundAddress, ClientOutboundAddress, ServerInboundAddress, ServerOutboundAddress))
            {
                broker.Listen();

                using (var server = new ZmqServer(this.zmqContext, ServerInboundAddress, ServerOutboundAddress, new ServiceFactory(resolver)))
                {
                    server.Listen();

                    using (var client = new ZmqClient(this.zmqContext, ClientInboundAddress, ClientOutboundAddress))
                    {
                        var clientFactory = new ServiceClientFactory(client);

                        var serviceClient = clientFactory.CreateServiceClient<ITestService>();

                        //Synchronous
                        var err = Assert.Catch(async () => await serviceClient.FailAsync());
                        Assert.IsNotNull(err);
                        Assert.IsNotInstanceOf<AggregateException>(err);

                        //Asynchronous task based
                        err = Assert.Catch(() => serviceClient.Fail());
                        Assert.IsNotNull(err);
                        Assert.IsNotInstanceOf<AggregateException>(err);

                        //Asynchronous IAsyncResult based , awaiting with Task
                        err = Assert.Catch(async () => await Task.Factory.FromAsync(serviceClient.BeginFail, serviceClient.EndFail, null));
                        Assert.IsNotNull(err);
                        Assert.IsNotInstanceOf<AggregateException>(err);

                        //Timeout exceptions
                        var factoryWithTimeout = new ServiceClientFactory(client);
                        var serviceClientWithTimeout = factoryWithTimeout.CreateServiceClient<ITestService>(50); //50ms

                        Assert.Throws<TimeoutException>(async () => await serviceClientWithTimeout.ReplyAfter(1000));
                    }
                }
            }
        }
        public async void TestSendAndReceiveExceptions()
        {
            var resolver = new DependencyResolver();

            using (var broker = new ZmqBroker(this.zmqContext, ClientInboundAddress, ClientOutboundAddress, ServerInboundAddress, ServerOutboundAddress))
            {
                broker.Listen();

                using (var server = new ZmqServer(this.zmqContext, ServerInboundAddress, ServerOutboundAddress, new ServiceFactory(resolver)))
                {
                    server.Listen();

                    using (var client = new ZmqClient(this.zmqContext, ClientInboundAddress, ClientOutboundAddress))
                    {
                        var clientFactory = new ServiceClientFactory(client);

                        var serviceClient = clientFactory.CreateServiceClient <ITestService>();

                        //Synchronous
                        var err = Assert.Catch(async() => await serviceClient.FailAsync());
                        Assert.IsNotNull(err);
                        Assert.IsNotInstanceOf <AggregateException>(err);

                        //Asynchronous task based
                        err = Assert.Catch(() => serviceClient.Fail());
                        Assert.IsNotNull(err);
                        Assert.IsNotInstanceOf <AggregateException>(err);

                        //Asynchronous IAsyncResult based , awaiting with Task
                        err = Assert.Catch(async() => await Task.Factory.FromAsync(serviceClient.BeginFail, serviceClient.EndFail, null));
                        Assert.IsNotNull(err);
                        Assert.IsNotInstanceOf <AggregateException>(err);

                        //Timeout exceptions
                        var factoryWithTimeout       = new ServiceClientFactory(client);
                        var serviceClientWithTimeout = factoryWithTimeout.CreateServiceClient <ITestService>(50); //50ms

                        Assert.Throws <TimeoutException>(async() => await serviceClientWithTimeout.ReplyAfter(1000));
                    }
                }
            }
        }
        //[TestCase(5, 1000000)]
        public async void TestLoadBalancing(int nServers, int nMsgs)
        {
            var resolver = new DependencyResolver();

            using (var broker = new ZmqBroker(this.zmqContext, ClientInboundAddress, ClientOutboundAddress, ServerInboundAddress, ServerOutboundAddress))
            {
                broker.Listen();

                var servers = Enumerable.Range(0, nServers)
                                        .Select(i => new ZmqServer(this.zmqContext, ServerInboundAddress, ServerOutboundAddress, new ServiceFactory(resolver)))
                                        .ToArray();

                try
                {
                    foreach (var server in servers) server.Listen();

                    using (var client = new ZmqClient(this.zmqContext, ClientInboundAddress, ClientOutboundAddress))
                    {
                        var clientFactory = new ServiceClientFactory(client);

                        var serviceClient = clientFactory.CreateServiceClient<ITestService2>();

                        var tasks = Enumerable.Range(0, nMsgs)
                            //.Select(i => serviceClient.SumAsync(5, 15))
                                              .Select(i => serviceClient.ListPersonsAsync(7))
                                              .ToArray();

                        await Task.WhenAll(tasks);

                        Assert.True(tasks.All(t => t.Result.Count() == 7));
                    }
                }
                finally
                {
                    foreach (var server in servers) server.Dispose();
                }

            }
        }