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); } } } }
static void Main(string[] args) { //Note: 0MQ tcp sockets only accept IPv4 addresses, wild card or interface-name for the Bind operation. var clientInboundAddr = "tcp://*:8001"; var clientOutboundAddr = "tcp://*:8002"; var serverInboundAddr = "tcp://*:8003"; var serverOutboundAddr = "tcp://*:8004"; ZmqBroker broker = null; try { broker = new ZmqBroker(new ZMQ.Context(), clientInboundAddr, clientOutboundAddr, serverInboundAddr, serverOutboundAddr); broker.Listen(); Console.WriteLine("Press enter to quit"); Console.ReadLine(); } finally { if (broker != null) { broker.Dispose(); } } }
//[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(); } } }