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 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 CheckDisposeWithSingleSubscriberShouldNotRaiseException()
        {
            // Arrange
            var eventAggregator = new EventAggregator();

            eventAggregator.Subscribe <FakeEventArgs>(e => { /* Empty handler */ });

            // Act
            eventAggregator.Dispose();
        }
        public void CheckSubscribeNullHandlerShouldRaiseException()
        {
            // Arrange
            var eventAggregator = new EventAggregator();

            // Act
            Action action = () => eventAggregator.Subscribe <FakeEventArgs>(null);

            // Assert
            Assert.Throws <ArgumentNullException>(action);

            // Cleanup
            eventAggregator.Dispose();
        }
Exemplo n.º 5
0
        public void Dispose()
        {
            if (_aggregator != null)
            {
                _aggregator.Dispose();
                _aggregator = null;
            }

            if (_scheduler != null)
            {
                _scheduler.Dispose();
                _scheduler = null;
            }
        }
Exemplo n.º 6
0
        private bool disposedValue = false; // 要检测冗余调用

        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    // TODO: 释放托管状态(托管对象)。
                    eventPublisher.Dispose();
                }

                // TODO: 释放未托管的资源(未托管的对象)并在以下内容中替代终结器。
                // TODO: 将大型字段设置为 null。

                disposedValue = true;
            }
        }
        public void CheckSubscribeWithExceptionInHandlerShouldNotRaiseException()
        {
            // Arrange
            var eventAggregator = new EventAggregator();
            var source          = new EventSource(eventAggregator);
            var receiver        = new EventReceiver(eventAggregator, () => throw new Exception("Test"));

            var data = EventData.Random();

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

            source.Emit(data);

            // Cleanup
            unsubscriber.Dispose();
            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();
        }