public void CheckSubscribeThenPublishFromAnotherThreadShouldReceiveEvent()
        {
            // Arrange
            var eventAggregator = new EventAggregator();
            var source          = new EventSource(eventAggregator);
            var receiver        = new EventReceiver(eventAggregator);

            var data = EventData.Random();

            // Act
            var unsubscriber = receiver.Subscribe();

            Task.Run(() =>
            {
                source.Emit(data);
            });

            receiver.Wait();

            // Assert
            Assert.True(receiver.EventReceived);
            Assert.Equal(data, receiver.Data);
            Assert.Equal(data.TestGuid, receiver.Data.TestGuid);
            Assert.Equal(data.TestInteger, receiver.Data.TestInteger);
            Assert.Equal(data.TestStr, receiver.Data.TestStr);

            // Cleanup
            unsubscriber.Dispose();
            eventAggregator.Dispose();
            receiver.Dispose();
        }
        public void CheckSubscribeThenUnsubscribeShouldNotReceivedEvent()
        {
            // Arrange
            var eventAggregator = new EventAggregator();
            var source          = new EventSource(eventAggregator);
            var receiver        = new EventReceiver(eventAggregator);

            var data = EventData.Random();

            // Act
            var unsubscriber = receiver.Subscribe();

            unsubscriber.Dispose();

            source.Emit(data);

            receiver.Wait();

            // Assert
            Assert.False(receiver.EventReceived);
            Assert.Null(receiver.Data);

            // Cleanup
            eventAggregator.Dispose();
            receiver.Dispose();
        }
        public void CheckSubscriberWithoutPublisherShouldNotReceivedEvent()
        {
            // Arrange
            var eventAggregator = new EventAggregator();
            var receiver        = new EventReceiver(eventAggregator);

            // Act
            var unsubscriber = receiver.Subscribe();

            receiver.Wait();

            // Assert
            Assert.False(receiver.EventReceived);
            Assert.Null(receiver.Data);

            // Cleanup
            unsubscriber.Dispose();
            eventAggregator.Dispose();
            receiver.Dispose();
        }
        public void CheckTwoSubscribersShouldReceiveSameEvent()
        {
            // Arrange
            var eventAggregator = new EventAggregator();
            var source          = new EventSource(eventAggregator);
            var firstReceiver   = new EventReceiver(eventAggregator);
            var secondReceiver  = new EventReceiver(eventAggregator);

            var data = EventData.Random();

            // Act
            var firstUnsubscriber  = firstReceiver.Subscribe();
            var secondUnsubscriber = secondReceiver.Subscribe();

            source.Emit(data);

            firstReceiver.Wait();
            secondReceiver.Wait();

            // Assert
            Assert.True(firstReceiver.EventReceived);
            Assert.Equal(data, firstReceiver.Data);
            Assert.Equal(data.TestGuid, firstReceiver.Data.TestGuid);
            Assert.Equal(data.TestInteger, firstReceiver.Data.TestInteger);
            Assert.Equal(data.TestStr, firstReceiver.Data.TestStr);

            Assert.True(secondReceiver.EventReceived);
            Assert.Equal(data, secondReceiver.Data);
            Assert.Equal(data.TestGuid, secondReceiver.Data.TestGuid);
            Assert.Equal(data.TestInteger, secondReceiver.Data.TestInteger);
            Assert.Equal(data.TestStr, secondReceiver.Data.TestStr);

            // Cleanup
            firstUnsubscriber.Dispose();
            secondUnsubscriber.Dispose();
            eventAggregator.Dispose();
            firstReceiver.Dispose();
            secondReceiver.Dispose();
        }
        public void CheckLongRunningHandlerShouldNotBlockOtherHandler()
        {
            // Arrange
            var timerElapsed = new ManualResetEventSlim(false);
            var timer        = new Timer(
                _ =>
            {
                timerElapsed.Set();
            },
                null,
                1000,
                0);

            var eventAggregator = new EventAggregator();
            var source          = new EventSource(eventAggregator);
            var firstReceiver   = new EventReceiver(eventAggregator, () =>
            {
                timerElapsed.Wait();
            });
            var secondReceiver = new EventReceiver(eventAggregator);

            var data = EventData.Random();

            // Act
            var firstUnsubscriber  = firstReceiver.Subscribe();
            var secondUnsubscriber = secondReceiver.Subscribe();

            source.Emit(data);

            firstReceiver.Wait();
            secondReceiver.Wait();

            // Assert
            Assert.False(firstReceiver.EventReceived);
            Assert.True(secondReceiver.EventReceived);

            Assert.Null(firstReceiver.Data);
            Assert.Equal(data, secondReceiver.Data);
            Assert.Equal(data.TestGuid, secondReceiver.Data.TestGuid);
            Assert.Equal(data.TestInteger, secondReceiver.Data.TestInteger);
            Assert.Equal(data.TestStr, secondReceiver.Data.TestStr);

            firstReceiver.Wait(1000);

            Assert.True(firstReceiver.EventReceived);

            Assert.Equal(data, firstReceiver.Data);
            Assert.Equal(data.TestGuid, firstReceiver.Data.TestGuid);
            Assert.Equal(data.TestInteger, firstReceiver.Data.TestInteger);
            Assert.Equal(data.TestStr, firstReceiver.Data.TestStr);

            // Cleanup
            firstUnsubscriber.Dispose();
            secondUnsubscriber.Dispose();
            eventAggregator.Dispose();
            firstReceiver.Dispose();
            secondReceiver.Dispose();

            timer.Dispose();
            timerElapsed.Dispose();
        }