static void Main(string[] args) { var resolver = new DefaultDependencyResolver(); var bus = new InProcessMessageBus(resolver); var eventKeys = new[] { "a", "b", "c" }; string payload = GetPayload(); TaskScheduler.UnobservedTaskException += OnUnobservedTaskException; for (int i = 0; i < _clients; i++) { Task.Factory.StartNew(() => StartClientLoop(bus, eventKeys), TaskCreationOptions.LongRunning); //ThreadPool.QueueUserWorkItem(_ => StartClientLoop(bus, eventKeys)); //(new Thread(_ => StartClientLoop(bus, eventKeys))).Start(); } for (var i = 1; i <= _senders; i++) { //ThreadPool.QueueUserWorkItem(_ => StartSendLoop(bus, payload)); Task.Factory.StartNew(() => StartSendLoop(i, bus, payload), TaskCreationOptions.LongRunning); } MeasureStats(); Console.ReadLine(); }
private static void StartSendLoop(int clientId, InProcessMessageBus bus, string payload) { while (_exception == null) { long old = _rate; var interval = TimeSpan.FromMilliseconds((1000.0 / _rate) * _senders); //var interval = TimeSpan.FromMilliseconds(1000.0 / _rate); while (Interlocked.Read(ref _rate) == old && _exception == null) { try { var sw = Stopwatch.StartNew(); bus.Send(clientId.ToString(), "a", payload).ContinueWith(task => { Interlocked.Exchange(ref _exception, task.Exception); }, TaskContinuationOptions.OnlyOnFaulted); sw.Stop(); Interlocked.Exchange(ref _lastSendTimeTicks, sw.ElapsedTicks); Interlocked.Increment(ref _sent); Interlocked.Increment(ref _avgLastSendsCount); Thread.Sleep(interval); } catch (Exception ex) { Interlocked.Exchange(ref _exception, ex); } } } }
private static void ReceiveLoop(InProcessMessageBus bus, string[] eventKeys, string id) { try { bus.GetMessages(eventKeys, id, CancellationToken.None).ContinueWith(task => { if (task.IsFaulted) { Interlocked.Exchange(ref _exception, task.Exception); } else { var result = task.Result; Interlocked.Increment(ref _received); Interlocked.Increment(ref _avgLastReceivedCount); ReceiveLoop(bus, eventKeys, result.LastMessageId); } }); } catch (Exception ex) { Interlocked.Exchange(ref _exception, ex); } }
public RabbitMqMessageBus(IDependencyResolver resolver, string rabbitMqExchangeName, IModel rabbitMqChannel) { _bus = new InProcessMessageBus <long>(resolver, this); _rabbitmqchannel = rabbitMqChannel; _rabbitmqExchangeName = rabbitMqExchangeName; EnsureConnection(); }
public RedisMessageBus(string server, int port, string password, int db, string eventKey, IDependencyResolver resolver) { _server = server; _port = port; _password = password; _db = db; _eventKey = eventKey; _bus = new InProcessMessageBus <long>(resolver, this); EnsureConnection(); }
public void ReturnsNoMessagesWhenLastMessageIdIsEqualToLastMessage() { // id = 27 // 24, 25, 27 // ^ var trace = new TraceManager(); var bus = new InProcessMessageBus(trace, false); bus.Send("foo", "1").Wait(); bus.Send("foo", "2").Wait(); // REVIEW: Will block //var result = bus.GetMessagesSince(new[] { "foo" }, 2).Result.ToList(); //Assert.Equal(0, result.Count); }
public void ReturnsAllMessagesWhenLastMessageIdIsLessThanAllMessages() { // id = 27 // _, 28, 29, 32 // ^ var trace = new TraceManager(); var bus = new InProcessMessageBus(trace, false); bus.Send("testclient", "bar", "1").Wait(); bus.Send("testclient", "bar", "2").Wait(); bus.Send("testclient", "foo", "3").Wait(); bus.Send("testclient", "foo", "4").Wait(); var result = bus.GetMessages(new[] { "foo" }, "1", CancellationToken.None).Result; Assert.Equal(2, result.Messages.Count); }
public void ReturnsAllMessagesWhenLastMessageIdIsLessThanAllMessages() { // id = 27 // _, 28, 29, 32 // ^ var trace = new TraceManager(); var bus = new InProcessMessageBus(trace, false); bus.Send("bar", "1").Wait(); bus.Send("bar", "2").Wait(); bus.Send("foo", "3").Wait(); bus.Send("foo", "4").Wait(); var result = bus.GetMessagesSince(new[] { "foo" }, 1).Result.ToList(); Assert.Equal(2, result.Count); }
public void GetAllSinceReturnsAllMessagesIfIdGreaterThanMaxId() { var trace = new TraceManager(); var bus = new InProcessMessageBus(trace, false); for (int i = 0; i < 10; i++) { bus.Send("testclient", "a", i).Wait(); } var result = bus.GetMessages(new[] { "a" }, "100", CancellationToken.None).Result; Assert.Equal(10, result.Messages.Count); for (int i = 0; i < 10; i++) { Assert.Equal(i, result.Messages[i].Value); } }
public void ReturnsNoMessagesWhenLastMessageIdIsGreaterThanAllMessages() { // id = 27 // 14, 18, 25, 26 // ^ var trace = new TraceManager(); var bus = new InProcessMessageBus(trace, false); bus.Send("foo", "1").Wait(); bus.Send("foo", "2").Wait(); bus.Send("bar", "3").Wait(); bus.Send("bar", "4").Wait(); // REVIEW: Will block // var result = bus.GetMessagesSince(new[] { "foo" }, 3).Result.ToList(); // Assert.Equal(0, result.Count); }
public void ReturnsMessagesGreaterThanLastMessageIdWhenLastMessageIdNotInStore() { // id = 27 // 24, 25, 28, 30, 45 // ^ var trace = new TraceManager(); var bus = new InProcessMessageBus(trace, false); bus.Send("bar", "1").Wait(); bus.Send("foo", "2").Wait(); bus.Send("bar", "3").Wait(); bus.Send("foo", "4").Wait(); bus.Send("bar", "5").Wait(); bus.Send("foo", "6").Wait(); var result = bus.GetMessagesSince(new[] { "foo" }, 3).Result.ToList(); Assert.Equal(2, result.Count); }
public void ReturnsMessagesGreaterThanLastMessageIdWhenLastMessageIdNotInStore() { // id = 27 // 24, 25, 28, 30, 45 // ^ var trace = new TraceManager(); var bus = new InProcessMessageBus(trace, false); bus.Send("testclient", "bar", "1").Wait(); bus.Send("testclient", "foo", "2").Wait(); bus.Send("testclient", "bar", "3").Wait(); bus.Send("testclient", "foo", "4").Wait(); bus.Send("testclient", "bar", "5").Wait(); bus.Send("testclient", "foo", "6").Wait(); var result = bus.GetMessages(new[] { "foo" }, "3", CancellationToken.None).Result; Assert.Equal(2, result.Messages.Count); }
public void FarmDisconnectOnlyRaisesEventOnce() { // Each node shares the same bus but are indepenent servers var bus = new InProcessMessageBus(new TraceManager(), garbageCollectMessages: false); var nodeCount = 3; var nodes = new List <ServerNode>(); for (int i = 0; i < nodeCount; i++) { nodes.Add(new ServerNode(bus)); } var timeout = TimeSpan.FromSeconds(5); foreach (var node in nodes) { node.Server.Configuration.HeartBeatInterval = timeout; node.Server.Configuration.DisconnectTimeout = TimeSpan.Zero; node.Server.MapConnection <FarmConnection>("/echo"); } var loadBalancer = new LoadBalancer(nodes.Select(f => f.Server).ToArray()); var transport = new Client.Transports.LongPollingTransport(loadBalancer); var connection = new Client.Connection("http://goo/echo"); connection.Start(transport).Wait(); for (int i = 0; i < nodes.Count; i++) { nodes[i].Broadcast(String.Format("From Node {0}: {1}", i, i + 1)); Thread.Sleep(TimeSpan.FromSeconds(1)); } connection.Stop(); Thread.Sleep(TimeSpan.FromTicks(timeout.Ticks * nodes.Count)); Assert.Equal(1, nodes.Sum(n => n.Connection.DisconnectCount)); }
public void FarmDisconnectOnlyRaisesEventOnce() { // Each node shares the same bus but are indepenent servers var bus = new InProcessMessageBus(new TraceManager(), garbageCollectMessages: false); var nodeCount = 3; var nodes = new List<ServerNode>(); for (int i = 0; i < nodeCount; i++) { nodes.Add(new ServerNode(bus)); } var timeout = TimeSpan.FromSeconds(5); foreach (var node in nodes) { node.Server.Configuration.HeartBeatInterval = timeout; node.Server.Configuration.DisconnectTimeout = TimeSpan.Zero; node.Server.MapConnection<FarmConnection>("/echo"); } var loadBalancer = new LoadBalancer(nodes.Select(f => f.Server).ToArray()); var transport = new Client.Transports.LongPollingTransport(loadBalancer); var connection = new Client.Connection("http://goo/echo"); connection.Start(transport).Wait(); for (int i = 0; i < nodes.Count; i++) { nodes[i].Broadcast(String.Format("From Node {0}: {1}", i, i + 1)); Thread.Sleep(TimeSpan.FromSeconds(1)); } connection.Stop(); Thread.Sleep(TimeSpan.FromTicks(timeout.Ticks * nodes.Count)); Assert.Equal(1, nodes.Sum(n => n.Connection.DisconnectCount)); }
public void ReturnsNoMessagesWhenThereAreNoMessages() { var trace = new TraceManager(); var bus = new InProcessMessageBus(trace, false); // REVIEW: Will block // var result = bus.GetMessagesSince(new[] { "foo" }, 1).Result.ToList(); // Assert.Equal(0, result.Count); }
private static void StartClientLoop(InProcessMessageBus bus, string[] eventKeys) { Interlocked.Increment(ref _clientsRunning); ReceiveLoop(bus, eventKeys, null); }
private static void ReceiveLoop(InProcessMessageBus bus, string[] eventKeys, ulong? id) { try { bus.GetMessagesSince(eventKeys, id).ContinueWith(task => { if (task.IsFaulted) { Interlocked.Exchange(ref _exception, task.Exception); } else { var list = task.Result; id = list[list.Count - 1].Id; Interlocked.Increment(ref _received); Interlocked.Increment(ref _avgLastReceivedCount); ReceiveLoop(bus, eventKeys, id); } }); } catch (Exception ex) { Interlocked.Exchange(ref _exception, ex); } }
static void Main(string[] args) { var resolver = new DefaultDependencyResolver(new JsonConvertAdapter()); var bus = new InProcessMessageBus(resolver); var eventKeys = new[] { "a", "b", "c" }; string payload = GetPayload(); TaskScheduler.UnobservedTaskException += OnUnobservedTaskException; for (int i = 0; i < _clients; i++) { Task.Factory.StartNew(() => StartClientLoop(bus, eventKeys), TaskCreationOptions.LongRunning); //ThreadPool.QueueUserWorkItem(_ => StartClientLoop(bus, eventKeys)); //(new Thread(_ => StartClientLoop(bus, eventKeys))).Start(); } for (var i = 1; i <= _senders; i++) { //ThreadPool.QueueUserWorkItem(_ => StartSendLoop(bus, payload)); Task.Factory.StartNew(() => StartSendLoop(i, bus, payload), TaskCreationOptions.LongRunning); } MeasureStats(); Console.ReadLine(); }