コード例 #1
0
        public async Task Test_Unsubscribe()
        {
            var dutPublisher  = new AsyncRedisClient();
            var dutSubscriber = new AsyncRedisSubscriptionClient();

            await dutPublisher.Connect(LocalHostDefaultPort.AsConnectionSettings());

            await dutSubscriber.Connect(LocalHostDefaultPort.AsConnectionSettings());

            string receivedChannel = null;
            string receivedMessage = null;

            dutSubscriber.OnMessageReceived += (ch, msg) =>
            {
                receivedChannel = ch;
                receivedMessage = msg;
            };

            await dutSubscriber.Subscribe(Channel);

            await dutPublisher.Publish(Channel, Message);

            Thread.Sleep(100);

            await dutSubscriber.Unsubscribe(Channel);

            await dutPublisher.Publish(Channel, SecondMessage);

            Thread.Sleep(100);

            Assert.AreEqual(Channel, receivedChannel);
            Assert.AreEqual(Message, receivedMessage);
        }
コード例 #2
0
        public async Task Test_MultipleSubscribe()
        {
            var dutPublisher  = new AsyncRedisClient();
            var dutSubscriber = new AsyncRedisSubscriptionClient();

            await dutPublisher.Connect(LocalHostDefaultPort.AsConnectionSettings());

            await dutSubscriber.Connect(LocalHostDefaultPort.AsConnectionSettings());

            var received = new List <Tuple <string, string> >();

            dutSubscriber.OnMessageReceived += (ch, msg) =>
            {
                received.Add(Tuple.Create(ch, msg));
            };

            await dutSubscriber.Subscribe(new[] { Channel, OtherChannel });

            await dutPublisher.Publish(Channel, Message);

            Thread.Sleep(100);

            await dutPublisher.Publish(OtherChannel, OtherMessage);

            Thread.Sleep(100);

            Assert.AreEqual(2, received.Count);
            Assert.AreEqual(Channel, received[0].Item1);
            Assert.AreEqual(Message, received[0].Item2);
            Assert.AreEqual(OtherChannel, received[1].Item1);
            Assert.AreEqual(OtherMessage, received[1].Item2);
        }
コード例 #3
0
        public async Task TestAlreadySubscribed_SubscribeThrowsException()
        {
            Exception thrownException = null;
            var       dut             = new AsyncRedisSubscriptionClient();

            try
            {
                await dut.Connect(LocalHostDefaultPort.AsConnectionSettings());

                await dut.Subscribe("channel");

                await dut.Subscribe("other-channel");
            }
            catch (Exception ex)
            {
                thrownException = ex;
            }

            Assert.IsNotNull(thrownException);
            Assert.IsInstanceOfType(thrownException, typeof(InvalidOperationException));
        }