public void TestNoSubscription()
 {
     var hub = this.GetAsyncEventHub();
     var args = new EventMock(10);
     hub.Raise(args);
     this.app.Stop();
 }
 public void TestEventInterface()
 {
     var hub = this.GetAsyncEventHub();
     var args = new EventMock(10);
     int called = 0;
     hub.Subscribe<IEvent>(e =>
     {
         e.Should().BeSameAs(args, "the passed arguments should be the same as provided");
         called++;
     });
     hub.Raise(args);
     this.app.Stop();
     called.Should().Be(1, "event handler should be called exactly once");
 }
 public void TestParentAndChildSubscription()
 {
     var hub = this.GetAsyncEventHub();
     var args = new EventMock(10);
     int called = 0;
     Action<EventMock> handler = (e => called++);
     hub.Subscribe(handler);
     hub.Subscribe<EventMockChild>(handler);
     hub.Raise(args);
     this.app.Stop();
     called.Should().Be(1, "event handler should be called exactly once");
 }
        public void TestHandlerUnsubscribingItself()
        {
            var hub = this.GetAsyncEventHub();
            var args = new EventMock(10);

            int beforeHandlerCalled = 0;
            Action<IEvent> beforeHandler = (e => beforeHandlerCalled++);
            hub.Subscribe(beforeHandler);

            int unsubscribingHandlerCalled = 0;
            Action<IEvent> unsubscribingHandler = null;
            unsubscribingHandler = (e => { unsubscribingHandlerCalled++; hub.Unsubscribe(unsubscribingHandler); });
            hub.Subscribe(unsubscribingHandler);

            int afterHandlerCalled = 0;
            Action<IEvent> afterHandler = (e => afterHandlerCalled++);
            hub.Subscribe(afterHandler);

            hub.Raise(args);
            hub.Raise(args);
            this.app.Stop();
            beforeHandlerCalled.Should().Be(2, "before event handler should be called exactly twice");
            unsubscribingHandlerCalled.Should().Be(1, "unsubscribing event handler should not be called after the second raise since it has unsubscribed itself during the first");
            afterHandlerCalled.Should().Be(2, "after event handler should be called exactly twice");
        }
 public void TestUninitialize()
 {
     var hub = this.GetAsyncEventHub();
     var args = new EventMock(10);
     int called = 0;
     Action<EventMock> handler = (e => called++);
     hub.Subscribe(handler);
     hub.Raise(args);
     this.app.Stop();
     called.Should().Be(1, "event handler should be called exactly once after the first raise");
 }
 public void TestUnsubscribe()
 {
     var hub = this.GetAsyncEventHub();
     var args = new EventMock(10);
     int called = 0;
     Action<EventMock> handler = (e => called++);
     hub.Subscribe(handler);
     hub.Unsubscribe(handler);
     hub.Raise(args);
     this.app.Stop();
     called.Should().Be(0, "event handler should not be called after the unsubscription");
 }
 public void TestDoubleRaise()
 {
     var hub = this.GetAsyncEventHub();
     var args = new EventMock(10);
     int called = 0;
     Action<IEvent> handler = (e => called++);
     hub.Subscribe(handler);
     hub.Raise(args);
     hub.Raise(args);
     this.app.Stop();
     called.Should().Be(2, "event handler should be called exactly twice after the second raise");
 }