public void TestMultipleSend()
        {
            MockClientCallback client = new MockClientCallback();
            MockApplication    app    = new MockApplication(client);
            EventBroker        broker = app.EventBroker;

            Assert.IsFalse(broker.Suspended);
            Thread.Sleep(WAIT_DELAY);

            client.Reset();

            int MSG_COUNT = 5;

            MockEvent[] events = new MockEvent[MSG_COUNT];

            for (int i = 0; i < events.Length; i++)
            {
                events[i] = new MockEvent();
            }

            foreach (Event e in events)
            {
                broker.Send(e);
            }

            int MAX_MSG_DELAY = 200; //200ms per msg

            Thread.Sleep(MAX_MSG_DELAY * MSG_COUNT);
            Assert.IsTrue(client.EventsSent.Count == MSG_COUNT, "All messages should be sent within the required period");
            for (int i = 0; i < client.EventsSent.Count; i++)
            {
                Assert.IsTrue(ReferenceEquals(client.EventsSent[i], events[i]), "Order of events is incorrect");
            }
        }
        public void TestSuspendResume()
        {
            MockClientCallback client = new MockClientCallback();
            MockApplication    app    = new MockApplication(client);
            EventBroker        broker = app.EventBroker;

            Thread.Sleep(WAIT_DELAY);

            broker.Suspended = true;
            Assert.IsTrue(broker.Suspended);

            client.Reset();

            MockEvent @event1 = new MockEvent();
            MockEvent @event2 = new MockEvent();

            broker.Send(@event1);
            broker.Send(@event2);

            Thread.Sleep(2 * WAIT_DELAY);
            Assert.IsTrue(broker.Suspended, "Should remain suspsended after Send is called");
            Assert.IsTrue(client.EventCount == 0, "Should NOT send any message while it's being suspended");

            broker.Suspended = false;
            Assert.IsFalse(broker.Suspended);
            Thread.Sleep(2 * WAIT_DELAY);
            Assert.IsTrue(client.EventCount == 2, "Should resume sending all messages after it's no longer suspended");
            Assert.IsTrue(ReferenceEquals(client.EventsSent[0], @event1), "Event order is incorrect");
            Assert.IsTrue(ReferenceEquals(client.EventsSent[1], @event2), "Event order is incorrect");
        }
示例#3
0
 public void Setup()
 {
     MockClientCallback client = new MockClientCallback();
     MockApplication app = new MockApplication(client);
     EventBroker broker = app.EventBroker;
     BatchSendingSupported = CanBrokerSendMessagesInBatch(client, broker);
 }
        public void Setup()
        {
            MockClientCallback client = new MockClientCallback();
            MockApplication    app    = new MockApplication(client);
            EventBroker        broker = app.EventBroker;

            BatchSendingSupported = CanBrokerSendMessagesInBatch(client, broker);
        }
        public void TestBatchSendingWithErrors()
        {
            if (!BatchSendingSupported)
            {
                Assert.Ignore("Broker does not appear to support batch sending");
            }

            // Based on the current implementation (implicit requirement), the Application.Stop() will be called if
            // an exception is thrown when the broker tries to send an event.

            MockClientCallback client = new MockClientCallback();
            MockApplication    app    = new MockApplication(client);
            EventBroker        broker = app.EventBroker;

            Thread.Sleep(WAIT_DELAY);

            broker.Suspended = true;
            Assert.IsTrue(broker.Suspended);
            client.Reset();

            // Prepare a batch of 50 msgs which will cause an exception on the 25th.
            MockEvent[] events = new MockEvent[50];
            for (int i = 0; i < events.Length; i++)
            {
                events[i] = new MockEvent();
                if (i == 24)
                {
                    events[i].ExceptionToThrow = new Exception("Simulated");
                }
            }

            for (int i = 0; i < events.Length; i++)
            {
                broker.Send(events[i]);
            }

            Thread.Sleep(WAIT_DELAY);
            Assert.IsTrue(broker.Suspended, "Should remain suspsended after Send is called");
            Assert.IsTrue(client.EventCount == 0, "Should NOT send any message while it's being suspended");

            broker.Suspended = false;
            Assert.IsFalse(broker.Suspended);
            Thread.Sleep(WAIT_DELAY * 5);

            Assert.IsFalse(app.IsRunning, "Application should be stopped");

            // TODO: Ideally broker should only send 26 messages (24 good msgs + bad msg + stop event)
            // However, it's not implemented to stop sending messages if the application is already stopped.
            // Decide to skip these checks since it's late in the game and didn't seem to cause any problem.
            Assert.Ignore("Message count is not verified.");
            //Assert.IsTrue(client.EventCount == 26 , "26 messages should be sent");
            //Assert.IsTrue(client.EventsSent[25] is ApplicationStoppedEvent, "Last event should be ApplicationStoppedEvent");
        }
        public void TestSendWithNullArgument()
        {
            MockClientCallback client = new MockClientCallback();
            MockApplication    app    = new MockApplication(client);
            EventBroker        broker = app.EventBroker;

            Assert.IsFalse(broker.Suspended);

            client.Reset();
            MockEvent @event = null;

            broker.Send(@event);
        }
        public void TestDisposeWhileSuspendOn()
        {
            // What happens if Dispose is called while the broker is being suspended?
            // Based on the current implementation, existing messages in the queue
            // will be sent out when Dispose() is called, regardless of the value of the Suspended property.

            MockClientCallback client = new MockClientCallback();
            MockApplication    app    = new MockApplication(client);
            EventBroker        broker = app.EventBroker;

            Assert.IsTrue(app.IsRunning);
            Thread.Sleep(WAIT_DELAY);

            broker.Suspended = true;
            Assert.IsTrue(broker.Suspended);

            client.Reset();

            MockEvent @event1 = new MockEvent();

            broker.Send(@event1);

            MockEvent @event2 = new MockEvent();

            broker.Send(@event2);

            Assert.IsTrue(broker.Suspended, "EventBroker Suspended should be true");

            broker.Dispose();

            // send more events after calling Dispose()
            MockEvent @event3 = new MockEvent();

            broker.Send(@event3);


            Thread.Sleep(WAIT_DELAY);

            Assert.IsTrue(app.IsRunning, "Application should not be stopped");
            Assert.IsTrue(client.EventCount == 2, "EventBroker should delivery all messages up to the point when Dispose() is called");
            Assert.IsTrue(ReferenceEquals(client.EventsSent[0], @event1), "Event order is incorrect");
            Assert.IsTrue(ReferenceEquals(client.EventsSent[1], @event2), "Event order is incorrect");

            // TODO: should Suspended be true or false if Dispose() is called?
            // On one hand, it should be false because messages have been sent out.
            // On another hand, this is an invalid use case because one should never try to use the broker once it is disposed of (bad coding).
            Assert.Ignore("EventBroker.Suspended property is not verified");
        }
        public void TestAllowBatchSending()
        {
            // TODO: Do we need another test to check the behaviour when the broker does not support batch sending?
            if (!BatchSendingSupported)
            {
                Assert.Ignore("Broker does not appear to support batch sending");
            }

            MockClientCallback client = new MockClientCallback();
            MockApplication    app    = new MockApplication(client);
            EventBroker        broker = app.EventBroker;

            Assert.IsFalse(broker.Suspended);
            Thread.Sleep(WAIT_DELAY);


            client.Reset();

            broker.Suspended = true;

            int MSG_COUNT = 50;

            MockEvent[] events = new MockEvent[MSG_COUNT];

            for (int i = 0; i < events.Length; i++)
            {
                events[i] = new MockEvent(true);
            }

            foreach (Event e in events)
            {
                broker.Send(e);
            }


            broker.Suspended = false;

            Thread.Sleep(2000);

            // When all messages are AllowSendInBatch, there's nothing to check really except that all messages
            // will be sent in the right order.

            Assert.IsTrue(client.EventsSent.Count == MSG_COUNT, "Not all messages were accounted for");
            for (int i = 0; i < client.EventsSent.Count; i++)
            {
                Assert.IsTrue(ReferenceEquals(client.EventsSent[i], events[i]), "Order of events was incorrect");
            }
        }
        public void TestDisposeWhileSuspendOff()
        {
            // What happens if Dispose is called while the broker is being suspended?
            // Based on the current implementation, existing messages in the queue
            // will be sent out when Dispose() is called, regardless of the value of the Suspended property.

            MockClientCallback client = new MockClientCallback();
            MockApplication    app    = new MockApplication(client);
            EventBroker        broker = app.EventBroker;

            Assert.IsTrue(app.IsRunning);
            Thread.Sleep(WAIT_DELAY);

            broker.Suspended = false;
            Assert.IsFalse(broker.Suspended);

            client.Reset();

            MockEvent @event1 = new MockEvent();

            broker.Send(@event1);

            MockEvent @event2 = new MockEvent();

            broker.Send(@event2);

            Assert.IsFalse(broker.Suspended, "EventBroker Suspended should be false");

            broker.Dispose();

            // send more events after calling Dispose()
            MockEvent @event3 = new MockEvent();

            broker.Send(@event3);


            Thread.Sleep(WAIT_DELAY);

            Assert.IsFalse(broker.Suspended, "EventBroker Suspended should be false after Dispose() is called");
            Assert.IsTrue(client.EventCount == 2, "EventBroker should delivery all messages up to the point when Dispose() is called");
            Assert.IsTrue(ReferenceEquals(client.EventsSent[0], @event1), "Event order is incorrect");
            Assert.IsTrue(ReferenceEquals(client.EventsSent[1], @event2), "Event order is incorrect");

            Assert.IsTrue(app.IsRunning, "Application should not be stopped");
        }
        public void TestSuspendResumeWithErrors()
        {
            // Based on the current implementation (implicit requirement), the Application.Stop() will be called if
            // an exception is thrown when the broker tries to send an event.

            MockClientCallback client = new MockClientCallback();
            MockApplication    app    = new MockApplication(client);
            EventBroker        broker = app.EventBroker;

            Assert.IsTrue(app.IsRunning);
            Thread.Sleep(WAIT_DELAY);

            broker.Suspended = true;
            Assert.IsTrue(broker.Suspended);

            client.Reset();

            MockEvent @event1 = new MockEvent();

            broker.Send(@event1);

            MockEvent @event2 = new MockEvent()
            {
                ExceptionToThrow = new Exception("Simulated")
            };

            broker.Send(@event2);

            Thread.Sleep(WAIT_DELAY);
            Assert.IsTrue(broker.Suspended, "Should remain suspsended");
            Assert.IsTrue(client.EventCount == 0, "Should NOT send any message while it's being suspended");
            Assert.IsTrue(app.IsRunning, "Application should still be running because the broker is being suspended");

            broker.Suspended = false;
            Assert.IsFalse(broker.Suspended);
            Thread.Sleep(WAIT_DELAY);

            Assert.IsTrue(client.EventCount == 3 /* including the app stop event */, "EventBroker should send all messages + AppStopEvent when it's no longer suspended");
            Assert.IsTrue(ReferenceEquals(client.EventsSent[0], @event1), "Event order is incorrect");
            Assert.IsTrue(ReferenceEquals(client.EventsSent[1], @event2), "Event order is incorrect");
            Assert.IsTrue(client.EventsSent[2] is ApplicationStoppedEvent, "Last event must be ApplicationStoppedEvent");

            Assert.IsFalse(app.IsRunning, "Application should be stopped because of the exception");
        }
        public void TestAllowBatchSending2()
        {
            if (!BatchSendingSupported)
            {
                Assert.Ignore("Broker does not appear to support batch sending");
            }

            int MSG_COUNT = 50;

            MockClientCallback client = new MockClientCallback();
            MockApplication    app    = new MockApplication(client);
            EventBroker        broker = app.EventBroker;

            Assert.IsFalse(broker.Suspended);
            Thread.Sleep(WAIT_DELAY);

            client.Reset();

            broker.Suspended = true;
            MockEvent[] events = new MockEvent[MSG_COUNT];
            for (int i = 0; i < events.Length; i++)
            {
                events[i] = new MockEvent(false);
            }
            foreach (Event e in events)
            {
                broker.Send(e);
            }


            broker.Suspended = false;

            Thread.Sleep(2000);
            Assert.AreEqual(MSG_COUNT, client.EventsSent.Count, "Not all messages were accounted for");

            Assert.AreEqual(MSG_COUNT, client.EventsSetSent.Count, "Each message should be sent individually");

            for (int i = 0; i < client.EventsSent.Count; i++)
            {
                Assert.IsTrue(ReferenceEquals(client.EventsSent[i], events[i]), "Order of all events was incorrect");
            }
        }
        public void TestMinSpeed()
        {
            // Make sure there's no significant delay caused by the broker

            MockClientCallback client = new MockClientCallback();
            MockApplication    app    = new MockApplication(client);
            EventBroker        broker = app.EventBroker;

            Thread.Sleep(WAIT_DELAY);

            client.Reset();

            // use a thread to continously send messages
            Thread producer = new Thread((arg) =>
            {
                ManualResetEvent stop = arg as ManualResetEvent;
                while (!stop.WaitOne(1))
                {
                    broker.Send(new MockEvent());
                }
            });

            ManualResetEvent stopEvent = new ManualResetEvent(false);

            producer.Start(stopEvent);

            // In theory, without any other delay caused by external code,
            // the broker should send msgs out at min rate of 20 msg per sec.
            //
            int      MIN_SPEED_MSG_PER_SEC = 20;
            TimeSpan duration = TimeSpan.FromSeconds(5);

            Thread.Sleep(duration);
            stopEvent.Set();
            producer.Join();

            int    msgCount    = client.EventsSent.Count;
            double minMsgCount = MIN_SPEED_MSG_PER_SEC * duration.TotalSeconds;

            Assert.Greater(msgCount, minMsgCount, "Messages must be sent at min rate of 20 mps");
            Trace.WriteLine(String.Format("Speed: {0} msg per sec", msgCount / duration.TotalSeconds));
        }
        public void TestSingleSend()
        {
            MockClientCallback client = new MockClientCallback();
            MockApplication    app    = new MockApplication(client);
            EventBroker        broker = app.EventBroker;

            Assert.IsFalse(broker.Suspended);

            Thread.Sleep(WAIT_DELAY);
            client.Reset();

            MockEvent @event = new MockEvent();

            broker.Send(@event);
            Thread.Sleep(WAIT_DELAY);
            Assert.IsTrue(client.EventCount == 1, "Should send a single message");

            Assert.IsTrue(client.LastEventSet.Events.Length == 1);
            Assert.IsTrue(ReferenceEquals(client.LastEventSet.Events[0], @event));
        }
        public void TestBatchSending()
        {
            if (!BatchSendingSupported)
            {
                Assert.Ignore("Broker does not appear to support batch sending");
            }

            MockClientCallback client = new MockClientCallback();
            MockApplication    app    = new MockApplication(client);
            EventBroker        broker = app.EventBroker;

            Thread.Sleep(WAIT_DELAY);

            broker.Suspended = true;
            Assert.IsTrue(broker.Suspended);
            client.Reset();

            Event[] events = new Event[50];
            for (int i = 0; i < events.Length; i++)
            {
                events[i] = new MockEvent();
            }

            for (int i = 0; i < events.Length; i++)
            {
                broker.Send(events[i]);
            }

            Thread.Sleep(WAIT_DELAY);
            Assert.IsTrue(broker.Suspended, "Should remain suspsended after Send is called");
            Assert.IsTrue(client.EventCount == 0, "Should NOT send any message while it's being suspended");

            broker.Suspended = false;
            Assert.IsFalse(broker.Suspended);
            Thread.Sleep(WAIT_DELAY * 5);
            Assert.IsTrue(client.EventCount == events.Length, "All messages should be sent after it's no longer suspended");

            const int MinBatchSizeRequired = 2;

            Assert.IsTrue(client.EventsSetSent.Count <= (events.Length / MinBatchSizeRequired), "Messages must be sent in batches");
        }
        public void TestDisposeBeforeSending()
        {
            MockClientCallback client = new MockClientCallback();
            MockApplication    app    = new MockApplication(client);
            EventBroker        broker = app.EventBroker;;

            Thread.Sleep(WAIT_DELAY);

            client.Reset();

            broker.Dispose();


            MockEvent @event = new MockEvent();

            broker.Send(@event);

            Assert.IsTrue(client.EventCount == 0);
            Thread.Sleep(WAIT_DELAY);
            Assert.IsTrue(client.EventCount == 0, "Should not send out any message after it is disposed");
        }
        public void TestDisposeAfterSending()
        {
            MockClientCallback client = new MockClientCallback();
            MockApplication    app    = new MockApplication(client);
            EventBroker        broker = app.EventBroker;

            Thread.Sleep(WAIT_DELAY);

            client.Reset();
            MockEvent @event = new MockEvent();

            broker.Send(@event);

            Assert.IsFalse(broker.Suspended);
            broker.Dispose();


            //Attempt to send an event after its disposed

            Thread.Sleep(WAIT_DELAY);
            Assert.IsTrue(client.EventCount == 1, "EventBroker shall deliver the last message which was sent before it was disposed");
        }
        public void TestSend_OtherException()
        {
            MockClientCallback client = new MockClientCallback();
            MockApplication    app    = new MockApplication(client);
            EventBroker        broker = app.EventBroker;

            Thread.Sleep(WAIT_DELAY);

            broker.Suspended = false;
            Assert.IsFalse(broker.Suspended);

            client.Reset();
            MockEvent @event = new MockEvent()
            {
                ExceptionToThrow = new Exception("Simulated")
            };

            broker.Send(@event);

            Thread.Sleep(WAIT_DELAY);
            Assert.IsFalse(app.IsRunning, "EventBroker should stop the application");
        }
        public void TestSend_CommunicationException()
        {
            MockClientCallback client = new MockClientCallback();

            MockApplication app = new MockApplication(client);

            EventBroker broker = new EventBroker(app, client);

            broker.Suspended = false;
            Assert.IsFalse(broker.Suspended);

            client.Reset();

            MockEvent @event = new MockEvent()
            {
                ExceptionToThrow = new CommunicationException("Simulated")
            };

            broker.Send(@event);

            Thread.Sleep(WAIT_DELAY);
            Assert.IsTrue(app.IsRunning, "EventBroker should eat any Communication Exception instead of stopping the application");
        }
示例#19
0
        public void TestBatchSending()
        {
            if (!BatchSendingSupported)
                Assert.Ignore("Broker does not appear to support batch sending");

            MockClientCallback client = new MockClientCallback();
            MockApplication app = new MockApplication(client);
            EventBroker broker = app.EventBroker;
            Thread.Sleep(WAIT_DELAY);

            broker.Suspended = true;
            Assert.IsTrue(broker.Suspended);
            client.Reset();

            Event[] events = new Event[50];
            for(int i=0; i<events.Length; i++)
            {
                events[i] = new MockEvent();
            }

            for (int i = 0; i < events.Length; i++)
            {
                broker.Send(events[i]);
            }

            Thread.Sleep(WAIT_DELAY);
            Assert.IsTrue(broker.Suspended, "Should remain suspsended after Send is called");
            Assert.IsTrue(client.EventCount == 0, "Should NOT send any message while it's being suspended");

            broker.Suspended = false;
            Assert.IsFalse(broker.Suspended);
            Thread.Sleep(WAIT_DELAY*5);
            Assert.IsTrue(client.EventCount == events.Length, "All messages should be sent after it's no longer suspended");
            
            const int MinBatchSizeRequired = 2;
            Assert.IsTrue(client.EventsSetSent.Count <= (events.Length/MinBatchSizeRequired), "Messages must be sent in batches");
        }
示例#20
0
        public void TestBatchSendingWithErrors()
        {
            if (!BatchSendingSupported)
                Assert.Ignore("Broker does not appear to support batch sending");

            // Based on the current implementation (implicit requirement), the Application.Stop() will be called if 
            // an exception is thrown when the broker tries to send an event.

            MockClientCallback client = new MockClientCallback();
            MockApplication app = new MockApplication(client);
            EventBroker broker = app.EventBroker;
            Thread.Sleep(WAIT_DELAY);

            broker.Suspended = true;
            Assert.IsTrue(broker.Suspended);
            client.Reset();

            // Prepare a batch of 50 msgs which will cause an exception on the 25th.
            MockEvent[] events = new MockEvent[50];
            for (int i = 0; i < events.Length; i++)
            {
                events[i] = new MockEvent();
                if (i == 24)
                    events[i].ExceptionToThrow = new Exception("Simulated");
            }

            for (int i = 0; i < events.Length; i++)
            {
                broker.Send(events[i]);
            }

            Thread.Sleep(WAIT_DELAY);
            Assert.IsTrue(broker.Suspended, "Should remain suspsended after Send is called");
            Assert.IsTrue(client.EventCount == 0, "Should NOT send any message while it's being suspended");

            broker.Suspended = false;
            Assert.IsFalse(broker.Suspended);
            Thread.Sleep(WAIT_DELAY * 5);

            Assert.IsFalse(app.IsRunning, "Application should be stopped");
            
            // TODO: Ideally broker should only send 26 messages (24 good msgs + bad msg + stop event)
            // However, it's not implemented to stop sending messages if the application is already stopped.
            // Decide to skip these checks since it's late in the game and didn't seem to cause any problem.
            Assert.Ignore("Message count is not verified.");
            //Assert.IsTrue(client.EventCount == 26 , "26 messages should be sent");
            //Assert.IsTrue(client.EventsSent[25] is ApplicationStoppedEvent, "Last event should be ApplicationStoppedEvent");
            
        }
示例#21
0
        public void TestAllowBatchSending()
        {
            // TODO: Do we need another test to check the behaviour when the broker does not support batch sending?
            if (!BatchSendingSupported)
                  Assert.Ignore("Broker does not appear to support batch sending");

            MockClientCallback client = new MockClientCallback();
            MockApplication app = new MockApplication(client);
            EventBroker broker = app.EventBroker;
            Assert.IsFalse(broker.Suspended);
            Thread.Sleep(WAIT_DELAY);

            
            client.Reset();

            broker.Suspended = true;

            int MSG_COUNT = 50;
            MockEvent[] events = new MockEvent[MSG_COUNT];

            for (int i = 0; i < events.Length; i++)
            {
                events[i] = new MockEvent(true);
            }

            foreach (Event e in events)
                broker.Send(e);


            broker.Suspended= false;

            Thread.Sleep(2000);

            // When all messages are AllowSendInBatch, there's nothing to check really except that all messages
            // will be sent in the right order. 
            
            Assert.IsTrue(client.EventsSent.Count == MSG_COUNT, "Not all messages were accounted for");
            for (int i = 0; i < client.EventsSent.Count; i++)
            {
                Assert.IsTrue(ReferenceEquals(client.EventsSent[i], events[i]), "Order of events was incorrect");
            }

        }
示例#22
0
        public void TestSuspendResume()
        {
            MockClientCallback client = new MockClientCallback();
            MockApplication app = new MockApplication(client);
            EventBroker broker = app.EventBroker;
            Thread.Sleep(WAIT_DELAY);
            
            broker.Suspended = true;
            Assert.IsTrue(broker.Suspended);

            client.Reset();

            MockEvent @event1 = new MockEvent();
            MockEvent @event2 = new MockEvent();
            broker.Send(@event1);
            broker.Send(@event2);

            Thread.Sleep(2 * WAIT_DELAY);
            Assert.IsTrue(broker.Suspended, "Should remain suspsended after Send is called");
            Assert.IsTrue(client.EventCount == 0, "Should NOT send any message while it's being suspended");

            broker.Suspended = false;
            Assert.IsFalse(broker.Suspended);
            Thread.Sleep(2 * WAIT_DELAY);
            Assert.IsTrue(client.EventCount == 2, "Should resume sending all messages after it's no longer suspended");
            Assert.IsTrue(ReferenceEquals(client.EventsSent[0], @event1), "Event order is incorrect");
            Assert.IsTrue(ReferenceEquals(client.EventsSent[1], @event2), "Event order is incorrect");
            
        }
示例#23
0
        public void TestDisposeWhileSuspendOn()
        {
            // What happens if Dispose is called while the broker is being suspended?
            // Based on the current implementation, existing messages in the queue
            // will be sent out when Dispose() is called, regardless of the value of the Suspended property.

            MockClientCallback client = new MockClientCallback();
            MockApplication app = new MockApplication(client);
            EventBroker broker = app.EventBroker;
            Assert.IsTrue(app.IsRunning);
            Thread.Sleep(WAIT_DELAY);

            broker.Suspended = true;
            Assert.IsTrue(broker.Suspended);

            client.Reset();

            MockEvent @event1 = new MockEvent();
            broker.Send(@event1);

            MockEvent @event2 = new MockEvent();
            broker.Send(@event2);

            Assert.IsTrue(broker.Suspended, "EventBroker Suspended should be true");

            broker.Dispose();

            // send more events after calling Dispose()
            MockEvent @event3 = new MockEvent();
            broker.Send(@event3);


            Thread.Sleep(WAIT_DELAY);

            Assert.IsTrue(app.IsRunning, "Application should not be stopped");
            Assert.IsTrue(client.EventCount == 2, "EventBroker should delivery all messages up to the point when Dispose() is called");
            Assert.IsTrue(ReferenceEquals(client.EventsSent[0], @event1), "Event order is incorrect");
            Assert.IsTrue(ReferenceEquals(client.EventsSent[1], @event2), "Event order is incorrect");
            
            // TODO: should Suspended be true or false if Dispose() is called?
            // On one hand, it should be false because messages have been sent out.
            // On another hand, this is an invalid use case because one should never try to use the broker once it is disposed of (bad coding).
            Assert.Ignore("EventBroker.Suspended property is not verified");
        }
示例#24
0
        public void TestDisposeBeforeSending()
        {
            MockClientCallback client= new MockClientCallback();
            MockApplication app = new MockApplication(client);
            EventBroker broker = app.EventBroker;;
            Thread.Sleep(WAIT_DELAY);
            
            client.Reset();
            
            broker.Dispose();

            
            MockEvent @event = new MockEvent();
            broker.Send(@event);

            Assert.IsTrue(client.EventCount == 0);
            Thread.Sleep(WAIT_DELAY);
            Assert.IsTrue(client.EventCount == 0, "Should not send out any message after it is disposed");
        }
示例#25
0
        public void TestMinSpeed()
        {
            // Make sure there's no significant delay caused by the broker
            
            MockClientCallback client = new MockClientCallback();
            MockApplication app = new MockApplication(client);
            EventBroker broker = app.EventBroker;
            Thread.Sleep(WAIT_DELAY);

            client.Reset();

            // use a thread to continously send messages
            Thread producer = new Thread((arg) =>
                                             {
                                                 ManualResetEvent stop = arg as ManualResetEvent;
                                                 while(!stop.WaitOne(1))
                                                 {
                                                     broker.Send(new MockEvent());
                                                 }
                                             });

            ManualResetEvent stopEvent = new ManualResetEvent(false);
            producer.Start(stopEvent);

            // In theory, without any other delay caused by external code, 
            // the broker should send msgs out at min rate of 20 msg per sec.
            // 
            int MIN_SPEED_MSG_PER_SEC= 20;
            TimeSpan duration = TimeSpan.FromSeconds(5);
            Thread.Sleep(duration);
            stopEvent.Set();
            producer.Join();

            int msgCount = client.EventsSent.Count;
            double minMsgCount = MIN_SPEED_MSG_PER_SEC*duration.TotalSeconds;
            Assert.Greater(msgCount, minMsgCount, "Messages must be sent at min rate of 20 mps");
            Trace.WriteLine(String.Format("Speed: {0} msg per sec", msgCount / duration.TotalSeconds));
        }
示例#26
0
        public void TestDisposeAfterSending()
        {
            MockClientCallback client = new MockClientCallback();
            MockApplication app = new MockApplication(client);
            EventBroker broker = app.EventBroker;
            Thread.Sleep(WAIT_DELAY);
            
            client.Reset();
            MockEvent @event = new MockEvent();
            broker.Send(@event);

            Assert.IsFalse(broker.Suspended);
            broker.Dispose();

            
            //Attempt to send an event after its disposed

            Thread.Sleep(WAIT_DELAY);
            Assert.IsTrue(client.EventCount == 1, "EventBroker shall deliver the last message which was sent before it was disposed"); 
        }
示例#27
0
        public void TestSend_OtherException()
        {
            MockClientCallback client = new MockClientCallback();
            MockApplication app = new MockApplication(client);
            EventBroker broker = app.EventBroker;
            Thread.Sleep(WAIT_DELAY);
            
            broker.Suspended = false;
            Assert.IsFalse(broker.Suspended);

            client.Reset();
            MockEvent @event = new MockEvent() { ExceptionToThrow = new Exception("Simulated") };
            broker.Send(@event);

            Thread.Sleep(WAIT_DELAY);
            Assert.IsFalse(app.IsRunning, "EventBroker should stop the application");
        }
示例#28
0
        public void TestSingleSend()
        {
            MockClientCallback client = new MockClientCallback();
            MockApplication app = new MockApplication(client);
            EventBroker broker = app.EventBroker;
            Assert.IsFalse(broker.Suspended);

            Thread.Sleep(WAIT_DELAY);
            client.Reset();
             
            MockEvent @event = new MockEvent();
            broker.Send(@event);
            Thread.Sleep(WAIT_DELAY);
            Assert.IsTrue(client.EventCount == 1, "Should send a single message");

            Assert.IsTrue(client.LastEventSet.Events.Length == 1);
            Assert.IsTrue(ReferenceEquals(client.LastEventSet.Events[0], @event));
        }
        public void TestSequenceNumber()
        {
            // Make sure the sequence number generated
            // by the broker is correct
            MockClientCallback client = new MockClientCallback();
            MockApplication    app    = new MockApplication(client);
            EventBroker        broker = app.EventBroker;

            Thread.Sleep(WAIT_DELAY);

            client.Reset();

            // use threads to continously send messages
            Thread[] producers = new Thread[5];

            for (int i = 0; i < producers.Length; i++)
            {
                producers[i] = new Thread((arg) =>
                {
                    Random ran            = new Random();
                    ManualResetEvent stop = arg as ManualResetEvent;
                    while (!stop.WaitOne(1))
                    {
                        while (ran.Next() % 10 == 0)
                        {
                            broker.Send(new MockEvent());
                        }

                        Thread.Sleep(ran.Next(0, 20));
                    }
                });
            }

            ManualResetEvent stopEvent = new ManualResetEvent(false);

            foreach (Thread producer in producers)
            {
                producer.Start(stopEvent);
            }

            TimeSpan duration = TimeSpan.FromSeconds(5);

            Thread.Sleep(duration);
            stopEvent.Set();

            foreach (Thread producer in producers)
            {
                producer.Join();
            }


            int?expectedSQ = null;

            for (int i = 0; i < client.EventsSetSent.Count; i++)
            {
                EventSet eventSet = client.EventsSetSent[i];

                if (!expectedSQ.HasValue)
                {
                    expectedSQ = eventSet.Number;
                }
                else
                {
                    Assert.AreEqual(expectedSQ.Value, eventSet.Number, "Wrong sequence number");
                }
                expectedSQ++;
            }
        }
示例#30
0
        public void TestSequenceNumber()
        {
			// Make sure the sequence number generated
			// by the broker is correct
            MockClientCallback client = new MockClientCallback();
            MockApplication app = new MockApplication(client);
            EventBroker broker = app.EventBroker;
            Thread.Sleep(WAIT_DELAY);

            client.Reset();

            // use threads to continously send messages
            Thread[] producers = new Thread[5];

            for (int i = 0; i < producers.Length; i++)
            {
                producers[i] = new Thread((arg) =>
                                                 {
                                                     Random ran = new Random();
                                                     ManualResetEvent stop = arg as ManualResetEvent;
                                                     while (!stop.WaitOne(1))
                                                     {
                                                         while(ran.Next()%10==0)
                                                            broker.Send(new MockEvent());

                                                         Thread.Sleep(ran.Next(0, 20));
                                                     }
                                                 });
            }

            ManualResetEvent stopEvent = new ManualResetEvent(false);
            foreach(Thread producer in producers)
                producer.Start(stopEvent);

            TimeSpan duration = TimeSpan.FromSeconds(5);
            Thread.Sleep(duration);
            stopEvent.Set();

            foreach (Thread producer in producers)
                producer.Join();


            int? expectedSQ=null;

            for(int i=0; i<client.EventsSetSent.Count; i++)
            {
                EventSet eventSet = client.EventsSetSent[i];

                if (!expectedSQ.HasValue)
                    expectedSQ = eventSet.Number;
                else
                {
                    Assert.AreEqual(expectedSQ.Value, eventSet.Number, "Wrong sequence number");
                }
                expectedSQ++;
            }
            
        }
示例#31
0
        public void TestMultipleSend()
        {
            MockClientCallback client = new MockClientCallback();
            MockApplication app =new MockApplication(client);
            EventBroker broker = app.EventBroker;
            Assert.IsFalse(broker.Suspended);
            Thread.Sleep(WAIT_DELAY);
            
            client.Reset();

            int MSG_COUNT = 5;
            MockEvent[] events = new MockEvent[MSG_COUNT];

            for(int i=0; i<events.Length; i++)
                events[i] = new MockEvent();

            foreach(Event e in events)
                broker.Send(e);

            int MAX_MSG_DELAY = 200; //200ms per msg
            Thread.Sleep(MAX_MSG_DELAY * MSG_COUNT);
            Assert.IsTrue(client.EventsSent.Count == MSG_COUNT, "All messages should be sent within the required period");
            for (int i = 0; i < client.EventsSent.Count; i++)
            {
                Assert.IsTrue(ReferenceEquals(client.EventsSent[i], events[i]), "Order of events is incorrect");
            }
        }
        // For this case, if the batch contains a special event with AllowSendInBatch=false,
        // we want to make sure that the event is the last event in the batch.
        public void TestAllowBatchSending3()
        {
            if (!BatchSendingSupported)
            {
                Assert.Ignore("Broker does not appear to support batch sending");
            }

            int MSG_COUNT = 50;

            MockClientCallback client = new MockClientCallback();
            MockApplication    app    = new MockApplication(client);
            EventBroker        broker = app.EventBroker;

            Assert.IsFalse(broker.Suspended);
            Thread.Sleep(WAIT_DELAY);


            client.Reset();

            MockEvent[]  events = new MockEvent[MSG_COUNT];
            List <Event> eventsNotAllowSendInBatch = new List <Event>();
            Random       ran = new Random();

            for (int i = 0; i < events.Length; i++)
            {
                if (i % 10 == 0 || ran.Next() % 10 == 0)
                {
                    events[i] = new MockEvent(false);
                    eventsNotAllowSendInBatch.Add(events[i]);
                }
                else
                {
                    events[i] = new MockEvent(true);
                }
            }
            foreach (Event e in events)
            {
                broker.Send(e);
            }


            broker.Suspended = false;

            Thread.Sleep(2000);
            Assert.AreEqual(MSG_COUNT, client.EventsSent.Count, "Not all messages were accounted for");

            Trace.WriteLine("Checking order of the events..");
            int notAllowInBatchEventsFoundCounter = 0;

            for (int i = 0; i < client.EventsSetSent.Count; i++)
            {
                EventSet set = client.EventsSetSent[i];
                for (int eventIndex = 0; eventIndex < set.Events.Length; eventIndex++)
                {
                    // If the batch contains a special event with AllowSendInBatch=false,
                    // the event should be the last event in the batch.
                    Event e = set.Events[eventIndex];
                    if (!e.AllowSendInBatch)
                    {
                        Assert.IsTrue(ReferenceEquals(e, eventsNotAllowSendInBatch[notAllowInBatchEventsFoundCounter]),
                                      "Special events were sent in the wrong order");
                        Assert.AreEqual(eventIndex, set.Events.Length - 1,
                                        "Special event must be the last message in the set");

                        notAllowInBatchEventsFoundCounter++;
                        break;
                    }
                }
            }
            Assert.AreEqual(eventsNotAllowSendInBatch.Count, notAllowInBatchEventsFoundCounter,
                            "Not all specially events were accounted for.");

            for (int i = 0; i < client.EventsSent.Count; i++)
            {
                Assert.IsTrue(ReferenceEquals(client.EventsSent[i], events[i]), "Order of all events was incorrect");
            }
        }
示例#33
0
        public void TestSuspendResumeWithErrors()
        {
            // Based on the current implementation (implicit requirement), the Application.Stop() will be called if 
            // an exception is thrown when the broker tries to send an event.

            MockClientCallback client = new MockClientCallback();
            MockApplication app = new MockApplication(client);
            EventBroker broker = app.EventBroker;
            Assert.IsTrue(app.IsRunning);
            Thread.Sleep(WAIT_DELAY);
            
            broker.Suspended = true;
            Assert.IsTrue(broker.Suspended);

            client.Reset();

            MockEvent @event1 = new MockEvent();
            broker.Send(@event1);

            MockEvent @event2 = new MockEvent() { ExceptionToThrow = new Exception("Simulated") };
            broker.Send(@event2);

            Thread.Sleep(WAIT_DELAY);
            Assert.IsTrue(broker.Suspended, "Should remain suspsended");
            Assert.IsTrue(client.EventCount == 0, "Should NOT send any message while it's being suspended");
            Assert.IsTrue(app.IsRunning, "Application should still be running because the broker is being suspended"); 
            
            broker.Suspended = false;
            Assert.IsFalse(broker.Suspended);
            Thread.Sleep(WAIT_DELAY);
            
            Assert.IsTrue(client.EventCount == 3 /* including the app stop event */, "EventBroker should send all messages + AppStopEvent when it's no longer suspended");
            Assert.IsTrue(ReferenceEquals(client.EventsSent[0], @event1), "Event order is incorrect");
            Assert.IsTrue(ReferenceEquals(client.EventsSent[1], @event2), "Event order is incorrect");
            Assert.IsTrue(client.EventsSent[2] is ApplicationStoppedEvent, "Last event must be ApplicationStoppedEvent");
            
            Assert.IsFalse(app.IsRunning, "Application should be stopped because of the exception");

        }
示例#34
0
        public void TestAllowBatchSending2()
        {
            if (!BatchSendingSupported)
                Assert.Ignore("Broker does not appear to support batch sending");

            int MSG_COUNT = 50;

            MockClientCallback client = new MockClientCallback();
            MockApplication app = new MockApplication(client);
            EventBroker broker = app.EventBroker;
            Assert.IsFalse(broker.Suspended);
            Thread.Sleep(WAIT_DELAY);

            client.Reset();

            broker.Suspended = true;
            MockEvent[] events = new MockEvent[MSG_COUNT];
            for (int i = 0; i < events.Length; i++)
            {
                events[i] = new MockEvent(false);
            }
            foreach (Event e in events)
                broker.Send(e);

            
            broker.Suspended = false;

            Thread.Sleep(2000);
            Assert.AreEqual(MSG_COUNT, client.EventsSent.Count, "Not all messages were accounted for");

            Assert.AreEqual(MSG_COUNT, client.EventsSetSent.Count, "Each message should be sent individually");

            for (int i = 0; i < client.EventsSent.Count; i++)
            {
                Assert.IsTrue(ReferenceEquals(client.EventsSent[i], events[i]), "Order of all events was incorrect");
            }

        }
示例#35
0
        // For this case, if the batch contains a special event with AllowSendInBatch=false,
        // we want to make sure that the event is the last event in the batch.
        public void TestAllowBatchSending3()
        {
            if (!BatchSendingSupported)
                Assert.Ignore("Broker does not appear to support batch sending");

            int MSG_COUNT = 50;

            MockClientCallback client = new MockClientCallback();
            MockApplication app = new MockApplication(client);
            EventBroker broker = app.EventBroker;
            Assert.IsFalse(broker.Suspended);
            Thread.Sleep(WAIT_DELAY);

            
            client.Reset();

            MockEvent[] events = new MockEvent[MSG_COUNT];
            List<Event> eventsNotAllowSendInBatch = new List<Event>();
            Random ran = new Random();
            for (int i = 0; i < events.Length; i++)
            {
                if (i%10 == 0 || ran.Next()%10 == 0)
                {
                    events[i] = new MockEvent(false);
                    eventsNotAllowSendInBatch.Add(events[i]);
                }
                else
                {
                    events[i] = new MockEvent(true);
                }
            }
            foreach (Event e in events)
                broker.Send(e);


            broker.Suspended = false;

            Thread.Sleep(2000);
            Assert.AreEqual(MSG_COUNT, client.EventsSent.Count, "Not all messages were accounted for");

            Trace.WriteLine("Checking order of the events..");
            int notAllowInBatchEventsFoundCounter = 0;
            for (int i = 0; i < client.EventsSetSent.Count; i++)
            {
                EventSet set = client.EventsSetSent[i];
                for (int eventIndex = 0; eventIndex < set.Events.Length; eventIndex++)
                {
                    // If the batch contains a special event with AllowSendInBatch=false,
                    // the event should be the last event in the batch.
                    Event e = set.Events[eventIndex];
                    if (!e.AllowSendInBatch)
                    {
                        Assert.IsTrue(ReferenceEquals(e, eventsNotAllowSendInBatch[notAllowInBatchEventsFoundCounter]),
                                      "Special events were sent in the wrong order");
                        Assert.AreEqual(eventIndex, set.Events.Length - 1,
                                        "Special event must be the last message in the set");

                        notAllowInBatchEventsFoundCounter++;
                        break;
                    }
                }
            }
            Assert.AreEqual(eventsNotAllowSendInBatch.Count, notAllowInBatchEventsFoundCounter,
                            "Not all specially events were accounted for.");

            for (int i = 0; i < client.EventsSent.Count; i++)
            {
                Assert.IsTrue(ReferenceEquals(client.EventsSent[i], events[i]), "Order of all events was incorrect");
            }

        }
示例#36
0
        public void TestDisposeWhileSuspendOff()
        {
            // What happens if Dispose is called while the broker is being suspended?
            // Based on the current implementation, existing messages in the queue
            // will be sent out when Dispose() is called, regardless of the value of the Suspended property.

            MockClientCallback client = new MockClientCallback();
            MockApplication app = new MockApplication(client);
            EventBroker broker = app.EventBroker;
            Assert.IsTrue(app.IsRunning);
            Thread.Sleep(WAIT_DELAY);

            broker.Suspended = false;
            Assert.IsFalse(broker.Suspended);

            client.Reset();

            MockEvent @event1 = new MockEvent();
            broker.Send(@event1);

            MockEvent @event2 = new MockEvent();
            broker.Send(@event2);

            Assert.IsFalse(broker.Suspended, "EventBroker Suspended should be false");

            broker.Dispose();

            // send more events after calling Dispose()
            MockEvent @event3 = new MockEvent();
            broker.Send(@event3);


            Thread.Sleep(WAIT_DELAY);

            Assert.IsFalse(broker.Suspended, "EventBroker Suspended should be false after Dispose() is called");
            Assert.IsTrue(client.EventCount == 2, "EventBroker should delivery all messages up to the point when Dispose() is called");
            Assert.IsTrue(ReferenceEquals(client.EventsSent[0], @event1), "Event order is incorrect");
            Assert.IsTrue(ReferenceEquals(client.EventsSent[1], @event2), "Event order is incorrect");

            Assert.IsTrue(app.IsRunning, "Application should not be stopped");

        }
示例#37
0
 public void TestConstructor2()
 {
     MockApplication ap = new MockApplication(null);
     EventBroker broker = new EventBroker(ap, null);
 }
示例#38
0
        public void TestSend_CommunicationException()
        {
            MockClientCallback client = new MockClientCallback();
            
            MockApplication app = new MockApplication(client);

            EventBroker broker = new EventBroker(app, client);
            
            broker.Suspended = false;
            Assert.IsFalse(broker.Suspended);

            client.Reset();

            MockEvent @event = new MockEvent() {ExceptionToThrow = new CommunicationException("Simulated")};
            broker.Send(@event);

            Thread.Sleep(WAIT_DELAY);
            Assert.IsTrue(app.IsRunning, "EventBroker should eat any Communication Exception instead of stopping the application");

        }
 public void TestConstructor2()
 {
     MockApplication ap     = new MockApplication(null);
     EventBroker     broker = new EventBroker(ap, null);
 }
示例#40
0
        public void TestSendWithNullArgument()
        {
            MockClientCallback client = new MockClientCallback();
            MockApplication app = new MockApplication(client);
            EventBroker broker = app.EventBroker;

            Assert.IsFalse(broker.Suspended);

            client.Reset();
            MockEvent @event = null;
            broker.Send(@event);
        }