public void ReturnsNoMessagesWhenLastMessageIdIsEqualToLastMessage()
            {
                // id = 27
                // 24, 25, 27
                //         ^

                var trace = new TraceManager();
                var bus = new InProcessMessageBus(trace, false);
                bus.Send("testclient", "foo", "1").Wait();
                bus.Send("testclient", "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);
            }
Esempio n. 3
0
            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("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 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);
            }
Esempio n. 6
0
        private static void StartSendLoop(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("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);
                    }
                }
            }
        }
Esempio n. 7
0
 private static void StartClientLoop(InProcessMessageBus bus, string[] eventKeys)
 {
     Interlocked.Increment(ref _clientsRunning);
     ReceiveLoop(bus, eventKeys, null);
 }
Esempio n. 8
0
        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);
            }
        }
Esempio n. 9
0
        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(bus, payload), TaskCreationOptions.LongRunning);
            }

            MeasureStats();

            Console.ReadLine();
        }