static void Main(string[] args) { var n = new NanoBusClient(); n.ConnectAsync(new Uri("ws://localhost:8091/NanoBus"), 1).Wait(); List <IDisposable> subscriptionList = new List <IDisposable>(3); var x = n.GetNanoEventBus <SampleDocument>(); subscriptionList.Add(x.Subscribe <SampleDocument.Messages.Created>(async m => { Console.WriteLine(m); })); subscriptionList.Add(x.Subscribe <SampleDocument.Messages.Updated>(async m => { Console.WriteLine(m); })); subscriptionList.Add(x.Subscribe <SampleDocument.Messages.Deleted>(async m => { Console.WriteLine(m); })); while (true) { var doc = new SampleDocument() { DocumentId = Guid.NewGuid() }; x.PublishAsync(new SampleDocument.Messages.Created(doc)).Wait(); x.PublishAsync(new SampleDocument.Messages.Updated(doc)).Wait(); x.PublishAsync(new SampleDocument.Messages.Deleted(doc)).Wait(); } }
private async Task TestRun(int numberOfServers, int numberOfClients, int numberOfSubscriptions, int numberOfPublishedMessages, int basePort, int numberOfClientPoolSize) { long receivedMessagesCount = 0; int numberOfHandlers = numberOfServers * numberOfClients * numberOfSubscriptions; int numberOfMessages = numberOfPublishedMessages * numberOfHandlers; Console.WriteLine("************************"); Console.WriteLine("numberOfServers: {0}", numberOfServers); Console.WriteLine("numberOfClients: {0}", numberOfClients); Console.WriteLine("numberOfClientPoolSize: {0}", numberOfClientPoolSize); Console.WriteLine("numberOfSubscriptions: {0}", numberOfSubscriptions); Console.WriteLine("numberOfPublishedMessages: {0}", numberOfPublishedMessages); Console.WriteLine("numberOfHandlers: {0}", numberOfHandlers); Console.WriteLine("numberOfMessages: {0}", numberOfMessages); ManualResetEvent ev = new ManualResetEvent(false); CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); ConcurrentBag <NanoBusServiceManager> serviceManagers = new ConcurrentBag <NanoBusServiceManager>(); ConcurrentBag <NanoBusClient> clients = new ConcurrentBag <NanoBusClient>(); //ConcurrentQueue<string> receivedMessages = new ConcurrentQueue<string>(); try { var sw = Stopwatch.StartNew(); Parallel.For(0, numberOfServers, x => { var service = new NanoBusService(); var serviceManager = new NanoBusServiceManager(service); serviceManager.StartAsync(string.Format("http://localhost:{0}/NanoBus/", basePort + x)); serviceManagers.Add(serviceManager); Parallel.For(0, numberOfClients, async y => { var client = new NanoBusClient(); await client.ConnectAsync(new Uri(string.Format("ws://localhost:{0}/NanoBus/", basePort + x)), numberOfClientPoolSize).ConfigureAwait(false); clients.Add(client); Parallel.For(0, numberOfSubscriptions, z => { client.GetNanoEventBus <Session>().Subscribe <SessionStartDomainMessage>(msg => { Interlocked.Increment(ref receivedMessagesCount); //Console.WriteLine("receive msg client({0}).pool({1}).iteration({2}) [{3}/{4}]", y, z, msg.Iteration, receivedMessagesCount, numberOfMessages); if (Interlocked.Read(ref receivedMessagesCount) == numberOfMessages) { ev.Set(); } return(Task.FromResult(0)); }); }); }); }); var started = DateTime.Now; //wait for all subscription while (serviceManagers.Sum(a => a.NanoBusService.As <NanoBusService>().Sessions.Sum(n => n.Value.Handlers.Count)) != numberOfClients) { Thread.Sleep(TimeSpan.FromMilliseconds(100)); if (DateTime.Now.Subtract(started) > TimeSpan.FromSeconds(30)) { Assert.Fail("Failed to connect in 30 seconds"); } } sw.Stop(); Console.WriteLine("Initialized in {0} seconds", sw.Elapsed.TotalSeconds); Measure(() => { for (int i = 0; i < numberOfPublishedMessages; i++) { var msg = new SessionStartDomainMessage(new Session() { Iteration = i, CreatedAt = DateTime.Now, SessionId = Guid.NewGuid() }); //Console.WriteLine("send msg iteration({0})", msg.Iteration); clients.TakeRandom().GetNanoEventBus <Session>().PublishAsync(msg).ConfigureAwait(false).GetAwaiter().GetResult(); } ev.WaitOne(TimeSpan.FromSeconds(15)); }, ref receivedMessagesCount); //Assert.AreEqual(numberOfPublishedMessages, clients.Sum(a => a.NanoClientConnections.Sum(c => c.TxCount)), "Not all messages sent by clients"); //Assert.AreEqual(numberOfPublishedMessages, servers.Sum(a => a.Sessions.Sum(c => c.Value.RxCount)), "Not all messages received by servers"); //Assert.AreEqual(numberOfPublishedMessages * numberOfClients, servers.Sum(a => a.Sessions.Sum(c => c.Value.TxCount)), "Not all messages sent by servers"); Assert.AreEqual(numberOfMessages, Interlocked.Read(ref receivedMessagesCount)); } finally { var sw = Stopwatch.StartNew(); serviceManagers.ToList().ForEach(c => c.Stop()); clients.ToList().ForEach(c => c.Dispose()); sw.Stop(); Console.WriteLine("Cleanup in {0} seconds", sw.Elapsed.TotalSeconds); } }