コード例 #1
0
        public void ShouldKeepCountOfSubscriptions()
        {
            var coursier = new Coursier();

            var subOne = coursier.Subscribe <TestMessageOne>(msg => { });

            coursier.Subscribe <TestMessageOne>(msg => { });

            Assert.Equal(2, coursier.SubscriptionCount <TestMessageOne>());

            coursier.Unsubscribe <TestMessageOne>(subOne);

            Assert.Equal(1, coursier.SubscriptionCount <TestMessageOne>());
        }
コード例 #2
0
        public void ShouldClearAllSubscriptions()
        {
            var coursier = new Coursier();

            var msgOneReceived = false;
            var msgTwoReceived = false;

            coursier.Subscribe <TestMessageOne>(msg => { msgOneReceived = true; });
            coursier.Subscribe <TestMessageTwo>(msg => { msgTwoReceived = true; });

            coursier.ClearAllSubscriptions();

            coursier.Publish(new TestMessageOne(this));
            coursier.Publish(new TestMessageTwo(this));

            Assert.False(msgOneReceived);
            Assert.False(msgTwoReceived);
        }
コード例 #3
0
        public void ShouldPublishMessage()
        {
            var coursier    = new Coursier();
            var msgReceived = false;

            coursier.Subscribe <TestMessageOne>(msg =>
            {
                msgReceived = true;
            });

            coursier.Publish(new TestMessageOne(this));

            Assert.True(msgReceived);
        }
コード例 #4
0
        public void ShouldNotReceiveMessageIfUnSubscribed()
        {
            var coursier    = new Coursier();
            var msgReceived = false;

            var token = coursier.Subscribe <TestMessageOne>(msg =>
            {
                msgReceived = true;
            });

            coursier.Unsubscribe <TestMessageOne>(token);

            coursier.Publish(new TestMessageOne(this));

            Assert.False(msgReceived);
        }
コード例 #5
0
        public void ShouldRegisterAndInvokeAsyncHandler()
        {
            var coursier    = new Coursier();
            var msgReceived = false;

            coursier.Subscribe <TestMessageOne>(async msg =>
            {
                await Task.Run(() =>
                {
                    msgReceived = true;
                });
            });

            coursier.Publish(new TestMessageOne(this));

            Thread.Sleep(1000);

            Assert.True(msgReceived);
        }
コード例 #6
0
        public void ShouldThrowIfSubscribingToBaseMessage()
        {
            var coursier = new Coursier();

            Assert.Throws <InvalidOperationException>(() => coursier.Subscribe <BaseMessage>(msg => { }));
        }