private void ServiceQueue_1_Writer_n_Readers(ServiceQueueRotationAlgorithm algorithm, int readers) { using (var serviceQueue = new ServiceQueue(algorithm)) using (var writer = new ServiceQueueWriter()) { var endpoint = GetEndpoint(); serviceQueue.Bind(endpoint); var counterSignal = new CounterSignal(NumberOfMessages, 0); for (var i = 0; i < readers; i++) { var reader = new ServiceQueueReader(); reader.MessageReceived += (s, m) => counterSignal.Increment(); reader.Connect(endpoint); } writer.Connect(endpoint); var messageSent = new TestMessage(); var sw = Stopwatch.StartNew(); for (var i = 0; i < NumberOfMessages; i++) { writer.SendMessage(messageSent); } Assert.IsTrue(counterSignal.Wait(TimeOut), "Timeout waiting for message"); sw.Stop(); Assert.Inconclusive("{0} elapsed reading {1} messages ({2:N0} per second)", sw.Elapsed, NumberOfMessages, NumberOfMessages / sw.Elapsed.TotalSeconds); } }
public void initial_value_signal_not_set_Wait_TimeSpan_CancellationToken_cancelled() { var counterSignal = new CounterSignal(2, 1); var cancellationToken = new CancellationTokenSource(TimeSpan.FromMilliseconds(15)).Token; Assert.Throws <OperationCanceledException>(() => counterSignal.Wait(TimeSpan.FromSeconds(1), cancellationToken)); }
public void One_Publisher_Hundred_Subscribers_Single_Broadcasts() { using (var publisher = new Publisher()) { var endpoint = GetEndpoint(); publisher.Bind(endpoint); var counterSignal = new CounterSignal(NumberOfMessagesToReceive); var subscribers = Enumerable.Range(1, 100).Select(i => { var sub = new Subscriber(); sub.MessageReceived += (s, m) => counterSignal.Increment(); sub.Connect(endpoint); return(sub); }).ToList(); Thread.Sleep(100); var messageSent = new TestMessage(); var sw = Stopwatch.StartNew(); for (var i = 0; i < NumberOfMessagesToReceive / 100; i++) { publisher.Broadcast(messageSent); } Assert.IsTrue(counterSignal.Wait(TimeOut), "Timeout waiting for message"); sw.Stop(); subscribers.ForEach(sub => sub.Dispose()); Assert.Inconclusive("{0} elapsed reading {1} messages ({2:N0} per second)", sw.Elapsed, NumberOfMessagesToReceive, NumberOfMessagesToReceive / sw.Elapsed.TotalSeconds); } }
public void initial_value_signal_not_set_Wait_TimeSpan_CancellationToken_timed_out() { var counterSignal = new CounterSignal(2, 1); var cancellationToken = new CancellationTokenSource(TimeSpan.FromSeconds(1)).Token; Assert.IsFalse(counterSignal.Wait(TimeSpan.FromMilliseconds(15), cancellationToken)); }
public void ServiceQueue_1_Writer_1_Reader(ServiceQueueRotationAlgorithm algorithm) { using (var serviceQueue = new ServiceQueue(algorithm)) using (var reader = new ServiceQueueReader()) using (var writer = new ServiceQueueWriter()) { var endpoint = GetEndpoint(); serviceQueue.Bind(endpoint); var counterSignal = new CounterSignal(NumberOfMessages, 0); reader.MessageReceived += (s, m) => counterSignal.Increment(); reader.Connect(endpoint); writer.Connect(endpoint); var messageSent = new TestMessage(); var sw = Stopwatch.StartNew(); for (var i = 0; i < NumberOfMessages; i++) { writer.SendMessage(messageSent); } Assert.IsTrue(counterSignal.Wait(TimeOut), "Timeout waiting for message"); sw.Stop(); Assert.Inconclusive("{0} elapsed reading {1} messages ({2:N0} per second)", sw.Elapsed, NumberOfMessages, NumberOfMessages / sw.Elapsed.TotalSeconds); } }
public void ServiceQueue_connect_Writer_send_multiple_message_received() { const int count = 1000; using (var serviceQueue = new ServiceQueue()) using (var writer = new ServiceQueueWriter()) { var endpoint = new RedFoxEndpoint("/path"); var messageFrames = new List <MessageFrame>(); var counterSignal = new CounterSignal(count, 0); serviceQueue.MessageFrameReceived += m => { messageFrames.Add(m); counterSignal.Increment(); }; serviceQueue.Bind(endpoint); writer.Connect(endpoint); for (var i = 0; i < count; i++) { writer.SendMessage(new TestMessage()); } Assert.IsTrue(counterSignal.Wait(TimeSpan.FromSeconds(30))); Assert.AreEqual(count, messageFrames.Count); } }
public void One_Publisher_One_Subscriber_Batch_Broadcast() { using (var publisher = new Publisher()) using (var subscriber = new Subscriber()) { var endpoint = GetEndpoint(); publisher.Bind(endpoint); subscriber.Connect(endpoint); Thread.Sleep(100); var counterSignal = new CounterSignal(NumberOfMessagesToReceive); subscriber.MessageReceived += (s, m) => counterSignal.Increment(); var messageSent = new TestMessage(); var batch = new List<TestMessage>(); for (var i = 0; i < NumberOfMessagesToReceive; i++) batch.Add(messageSent); var sw = Stopwatch.StartNew(); publisher.Broadcast(batch); Assert.IsTrue(counterSignal.Wait(TimeOut), "Timeout waiting for message"); sw.Stop(); Assert.Inconclusive("{0} elapsed reading {1} messages ({2:N0} per second)", sw.Elapsed, NumberOfMessagesToReceive, NumberOfMessagesToReceive / sw.Elapsed.TotalSeconds); } }
public void One_Publisher_Two_Subscribers_Single_Broadcasts() { using (var publisher = new Publisher()) using (var subscriber1 = new Subscriber()) using (var subscriber2 = new Subscriber()) { var endpoint = GetEndpoint(); publisher.Bind(endpoint); subscriber1.Connect(endpoint); subscriber2.Connect(endpoint); Thread.Sleep(100); var counterSignal = new CounterSignal(NumberOfMessagesToReceive); subscriber1.MessageReceived += (s, m) => counterSignal.Increment(); subscriber2.MessageReceived += (s, m) => counterSignal.Increment(); var messageSent = new TestMessage(); var sw = Stopwatch.StartNew(); for (var i = 0; i < NumberOfMessagesToReceive / 2; i++) { publisher.Broadcast(messageSent); } Assert.IsTrue(counterSignal.Wait(TimeOut), "Timeout waiting for message"); sw.Stop(); Assert.Inconclusive("{0} elapsed reading {1} messages ({2:N0} per second)", sw.Elapsed, NumberOfMessagesToReceive, NumberOfMessagesToReceive / sw.Elapsed.TotalSeconds); } }
public void counter_add_signal_IsSet_true() { var counterSignal = new CounterSignal(10, 1); counterSignal.Add(100); Assert.IsTrue(counterSignal.IsSet); Assert.IsTrue(counterSignal.Wait(TimeSpan.Zero)); }
/// <summary> /// Gets and removes the object at the beginning of the queue /// </summary> public T Dequeue(CancellationToken cancellationToken) { T item; do { _counterSignal.Wait(cancellationToken); } while (!_queue.TryDequeue(out item)); _counterSignal.Decrement(); return(item); }
private void One_Responder_N_Requesters(int n) { var echoWorker = new ResponderWorker(); var workerFactory = new ResponderWorkerFactory(request => echoWorker); using (var responder = new Responder(workerFactory)) { var endpoint = GetEndpoint(); responder.Bind(endpoint); var requesters = Enumerable.Range(0, n).Select(x => new Requester()).ToList(); foreach (var requester in requesters) { requester.Connect(endpoint); } Thread.Sleep(100); var threadsStartedSignal = new CounterSignal(n, 0); var startSignal = new ManualResetEventSlim(); var tasks = new List <Task>(); foreach (var requester in requesters) { var req = requester; var task = Task.Factory.StartNew(() => { threadsStartedSignal.Increment(); startSignal.Wait(); var message = new TestMessage(); for (var i = 0; i < NumberOfRequests / n; i++) { req.Request(message); } }, TaskCreationOptions.LongRunning); tasks.Add(task); } Assert.IsTrue(threadsStartedSignal.Wait(TimeSpan.FromSeconds(1))); var sw = Stopwatch.StartNew(); startSignal.Set(); Assert.IsTrue(Task.WhenAll(tasks).Wait(TimeSpan.FromMinutes(1))); sw.Stop(); foreach (var requester in requesters) { requester.Disconnect(); } Assert.Inconclusive("{0} elapsed sending/receiving {1} messages ({2:N0} per second)", sw.Elapsed, NumberOfRequests, NumberOfRequests / sw.Elapsed.TotalSeconds); } }
public void ServiceQueue_AddMessageFrame_connect_multiple_readers_LoadBalance() { var endpoint1 = new RedFoxEndpoint("/path1"); var endpoint2 = new RedFoxEndpoint("/path2"); using (var serviceQueue = new ServiceQueue(ServiceQueueRotationAlgorithm.LoadBalance)) using (var serviceQueueReader1 = new ServiceQueueReader()) using (var serviceQueueReader2 = new ServiceQueueReader()) { const int count = 1000; var counter = new CounterSignal(count, 0); var messagesReceived1 = new List <IMessage>(); serviceQueueReader1.MessageReceived += (s, m) => { messagesReceived1.Add(m); counter.Increment(); }; var messagesReceived2 = new List <IMessage>(); serviceQueueReader2.MessageReceived += (s, m) => { messagesReceived2.Add(m); counter.Increment(); }; var testMessage = new TestMessage(); var testMessageFrame = new MessageFrameCreator(DefaultMessageSerialization.Instance).CreateFromMessage(testMessage); serviceQueue.Bind(endpoint1); serviceQueue.Bind(endpoint2); serviceQueueReader1.Connect(endpoint1); serviceQueueReader2.Connect(endpoint2); for (var i = 0; i < count; i++) { serviceQueue.AddMessageFrame(testMessageFrame); } Assert.IsTrue(counter.Wait()); Assert.AreEqual(count, messagesReceived1.Count + messagesReceived2.Count); Assert.AreNotEqual(0, messagesReceived1.Count); Assert.AreNotEqual(0, messagesReceived2.Count); var ratio = (decimal)messagesReceived1.Count / messagesReceived2.Count; Assert.Greater(ratio, 0.9); Assert.Less(ratio, 1.1); } }
public void ServiceQueue_AddMessageFrame_connect_multiple_readers_multiple_message_received(ServiceQueueRotationAlgorithm rotationAlgorithm) { var endpoint = new RedFoxEndpoint("/path"); using (var serviceQueue = new ServiceQueue(rotationAlgorithm)) using (var serviceQueueReader1 = new ServiceQueueReader()) using (var serviceQueueReader2 = new ServiceQueueReader()) { const int count = 1000; var counter = new CounterSignal(count, 0); var messagesReceived1 = new List <IMessage>(); serviceQueueReader1.MessageReceived += (s, m) => { messagesReceived1.Add(m); counter.Increment(); }; var messagesReceived2 = new List <IMessage>(); serviceQueueReader2.MessageReceived += (s, m) => { messagesReceived2.Add(m); counter.Increment(); }; var testMessage = new TestMessage(); var testMessageFrame = new MessageFrameCreator(DefaultMessageSerialization.Instance).CreateFromMessage(testMessage); for (var i = 0; i < count; i++) { serviceQueue.AddMessageFrame(testMessageFrame); } serviceQueue.Bind(endpoint); serviceQueueReader1.Connect(endpoint); serviceQueueReader2.Connect(endpoint); Assert.IsTrue(counter.Wait(TimeSpan.FromSeconds(10))); Assert.AreEqual(count, messagesReceived1.Count + messagesReceived2.Count); } }
public void benchmark_get_value() { var counterSignal = new CounterSignal(1, 0); const int iterations = 10000000; var value = false; var sw = Stopwatch.StartNew(); for (var i = 0; i < iterations; i++) { value |= counterSignal.Wait(TimeSpan.Zero); } sw.Stop(); if (value) { Console.WriteLine(); // prevent too aggressive optimization } Assert.Inconclusive("{0} ({1:N0} ops/sec)", sw.Elapsed, (decimal)iterations / sw.ElapsedMilliseconds * 1000m); }
public void initial_value_signal_set_Wait_TimeSpan() { var counterSignal = new CounterSignal(1, 1); Assert.IsTrue(counterSignal.Wait(TimeSpan.Zero)); }
public void initial_value_signal_set_Wait_TimeSpan() { var counterSignal = new CounterSignal(1, 1); Assert.IsTrue(counterSignal.Wait(TimeSpan.Zero)); }
public void initial_value_signal_not_set_Wait_TimeSpan_CancellationToken_timed_out() { var counterSignal = new CounterSignal(2, 1); var cancellationToken = new CancellationTokenSource(TimeSpan.FromSeconds(1)).Token; Assert.IsFalse(counterSignal.Wait(TimeSpan.FromMilliseconds(15), cancellationToken)); }
public void initial_value_signal_not_set_Wait_TimeSpan_false() { var counterSignal = new CounterSignal(2, 1); Assert.IsFalse(counterSignal.Wait(TimeSpan.Zero)); }
public void initial_value_signal_not_set_Wait_TimeSpan_CancellationToken_cancelled() { var counterSignal = new CounterSignal(2, 1); var cancellationToken = new CancellationTokenSource(TimeSpan.FromMilliseconds(15)).Token; Assert.Throws<OperationCanceledException>(() => counterSignal.Wait(TimeSpan.FromSeconds(1), cancellationToken)); }
public void One_Publisher_Hundred_Subscribers_Single_Broadcasts() { using (var publisher = new Publisher()) { var endpoint = GetEndpoint(); publisher.Bind(endpoint); var counterSignal = new CounterSignal(NumberOfMessagesToReceive); var subscribers = Enumerable.Range(1, 100).Select(i => { var sub = new Subscriber(); sub.MessageReceived += (s, m) => counterSignal.Increment(); sub.Connect(endpoint); return sub; }).ToList(); Thread.Sleep(100); var messageSent = new TestMessage(); var sw = Stopwatch.StartNew(); for (var i = 0; i < NumberOfMessagesToReceive / 100; i++) { publisher.Broadcast(messageSent); } Assert.IsTrue(counterSignal.Wait(TimeOut), "Timeout waiting for message"); sw.Stop(); subscribers.ForEach(sub => sub.Dispose()); Assert.Inconclusive("{0} elapsed reading {1} messages ({2:N0} per second)", sw.Elapsed, NumberOfMessagesToReceive, NumberOfMessagesToReceive / sw.Elapsed.TotalSeconds); } }
public void initial_value_signal_not_set_Wait_TimeSpan_false() { var counterSignal = new CounterSignal(2, 1); Assert.IsFalse(counterSignal.Wait(TimeSpan.Zero)); }
public void ServiceQueue_AddMessageFrame_connect_multiple_readers_LoadBalance() { var endpoint1 = new RedFoxEndpoint("/path1"); var endpoint2 = new RedFoxEndpoint("/path2"); using (var serviceQueue = new ServiceQueue(ServiceQueueRotationAlgorithm.LoadBalance)) using (var serviceQueueReader1 = new ServiceQueueReader()) using (var serviceQueueReader2 = new ServiceQueueReader()) { const int count = 1000; var counter = new CounterSignal(count, 0); var messagesReceived1 = new List<IMessage>(); serviceQueueReader1.MessageReceived += (s, m) => { messagesReceived1.Add(m); counter.Increment(); }; var messagesReceived2 = new List<IMessage>(); serviceQueueReader2.MessageReceived += (s, m) => { messagesReceived2.Add(m); counter.Increment(); }; var testMessage = new TestMessage(); var testMessageFrame = new MessageFrameCreator(DefaultMessageSerialization.Instance).CreateFromMessage(testMessage); serviceQueue.Bind(endpoint1); serviceQueue.Bind(endpoint2); serviceQueueReader1.Connect(endpoint1); serviceQueueReader2.Connect(endpoint2); for (var i = 0; i < count; i++) serviceQueue.AddMessageFrame(testMessageFrame); Assert.IsTrue(counter.Wait()); Assert.AreEqual(count, messagesReceived1.Count + messagesReceived2.Count); Assert.AreNotEqual(0, messagesReceived1.Count); Assert.AreNotEqual(0, messagesReceived2.Count); var ratio = (decimal)messagesReceived1.Count / messagesReceived2.Count; Assert.Greater(ratio, 0.9); Assert.Less(ratio, 1.1); } }
public void ServiceQueue_AddMessageFrame_connect_multiple_readers_multiple_message_received(ServiceQueueRotationAlgorithm rotationAlgorithm) { var endpoint = new RedFoxEndpoint("/path"); using (var serviceQueue = new ServiceQueue(rotationAlgorithm)) using (var serviceQueueReader1 = new ServiceQueueReader()) using (var serviceQueueReader2 = new ServiceQueueReader()) { const int count = 1000; var counter = new CounterSignal(count, 0); var messagesReceived1 = new List<IMessage>(); serviceQueueReader1.MessageReceived += (s, m) => { messagesReceived1.Add(m); counter.Increment(); }; var messagesReceived2 = new List<IMessage>(); serviceQueueReader2.MessageReceived += (s, m) => { messagesReceived2.Add(m); counter.Increment(); }; var testMessage = new TestMessage(); var testMessageFrame = new MessageFrameCreator(DefaultMessageSerialization.Instance).CreateFromMessage(testMessage); for (var i = 0; i < count; i++) serviceQueue.AddMessageFrame(testMessageFrame); serviceQueue.Bind(endpoint); serviceQueueReader1.Connect(endpoint); serviceQueueReader2.Connect(endpoint); Assert.IsTrue(counter.Wait(TimeSpan.FromSeconds(10))); Assert.AreEqual(count, messagesReceived1.Count + messagesReceived2.Count); } }
public void benchmark_get_value() { var counterSignal = new CounterSignal(1, 0); const int iterations = 10000000; var value = false; var sw = Stopwatch.StartNew(); for (var i = 0; i < iterations; i++) { value |= counterSignal.Wait(TimeSpan.Zero); } sw.Stop(); if (value) Console.WriteLine(); // prevent too aggressive optimization Assert.Inconclusive("{0} ({1:N0} ops/sec)", sw.Elapsed, (decimal)iterations / sw.ElapsedMilliseconds * 1000m); }
public void ServiceQueue_connect_Writer_send_multiple_message_received() { const int count = 1000; using (var serviceQueue = new ServiceQueue()) using (var writer = new ServiceQueueWriter()) { var endpoint = new RedFoxEndpoint("/path"); var messageFrames = new List<MessageFrame>(); var counterSignal = new CounterSignal(count, 0); serviceQueue.MessageFrameReceived += m => { messageFrames.Add(m); counterSignal.Increment(); }; serviceQueue.Bind(endpoint); writer.Connect(endpoint); for (var i = 0; i < count; i++) writer.SendMessage(new TestMessage()); Assert.IsTrue(counterSignal.Wait(TimeSpan.FromSeconds(30))); Assert.AreEqual(count, messageFrames.Count); } }
private void One_Responder_N_Requesters(int n) { var echoWorker = new ResponderWorker(); var workerFactory = new ResponderWorkerFactory(request => echoWorker); using (var responder = new Responder(workerFactory)) { var endpoint = GetEndpoint(); responder.Bind(endpoint); var requesters = Enumerable.Range(0, n).Select(x => new Requester()).ToList(); foreach (var requester in requesters) { requester.Connect(endpoint); } Thread.Sleep(100); var threadsStartedSignal = new CounterSignal(n, 0); var startSignal = new ManualResetEventSlim(); var tasks = new List<Task>(); foreach (var requester in requesters) { var req = requester; var task = Task.Factory.StartNew(() => { threadsStartedSignal.Increment(); startSignal.Wait(); var message = new TestMessage(); for (var i = 0; i < NumberOfRequests / n; i++) { req.Request(message); } }, TaskCreationOptions.LongRunning); tasks.Add(task); } Assert.IsTrue(threadsStartedSignal.Wait(TimeSpan.FromSeconds(1))); var sw = Stopwatch.StartNew(); startSignal.Set(); Assert.IsTrue(Task.WhenAll(tasks).Wait(TimeSpan.FromMinutes(1))); sw.Stop(); foreach (var requester in requesters) { requester.Disconnect(); } Assert.Inconclusive("{0} elapsed sending/receiving {1} messages ({2:N0} per second)", sw.Elapsed, NumberOfRequests, NumberOfRequests / sw.Elapsed.TotalSeconds); } }
public void counter_add_signal_IsSet_true() { var counterSignal = new CounterSignal(10, 1); counterSignal.Add(100); Assert.IsTrue(counterSignal.IsSet); Assert.IsTrue(counterSignal.Wait(TimeSpan.Zero)); }