Esempio n. 1
0
        public async Task SubscribeToRestrictedChannelWhenAuthorized()
        {
            var client = new RtmClientBuilder(Config.Endpoint, Config.AppKey)
                         .SetRoleSecretAuthenticator(Config.Role, Config.RoleSecretKey)
                         .Build();

            var queue = client.CreateStateQueue();

            client.Start();

            var channel = GenerateRandomChannelName();

            var subObs = new TestSubscriptionObserverQueue(queue);

            subObs.ObserveSubscriptionState();
            subObs.ObserveSubscriptionPdu();
            client.CreateSubscription(channel, SubscriptionModes.Advanced, subObs);

            Assert.That(await queue.Dequeue(), Is.EqualTo("leave-stopped"));
            Assert.That(await queue.Dequeue(), Is.EqualTo("enter-connecting"));
            Assert.That(await queue.Dequeue(), Is.EqualTo("rtm:created"));
            Assert.That(await queue.Dequeue(), Is.EqualTo("rtm:enter-unsubscribed"));
            Assert.That(await queue.Dequeue(), Is.EqualTo("leave-connecting"));
            Assert.That(await queue.Dequeue(), Is.EqualTo("rtm:leave-unsubscribed"));
            Assert.That(await queue.Dequeue(), Is.EqualTo("rtm:enter-subscribing"));
            Assert.That(await queue.Dequeue(), Is.EqualTo("enter-connected"));
            Assert.That(await queue.Dequeue(), Is.EqualTo("rtm:leave-subscribing"));
            Assert.That(await queue.Dequeue(), Is.EqualTo("rtm:enter-subscribed"));

            await client.Dispose();
        }
        public async Task WriteNullShouldBeOk()
        {
            var channel = GenerateRandomChannelName();
            var client  = new RtmClientBuilder(Config.Endpoint, Config.AppKey).Build();

            client.Start();

            var queue = new TestSubscriptionObserverQueue();
            await client.CreateSubscriptionAndWaitSubscribed(channel, null, queue);

            await client.Yield();

            queue.ObserveSubscriptionState();
            queue.ObserveSubscriptionPdu();

            await client.Write(channel, (string)null, Ack.Yes);

            Assert.That(await queue.Dequeue(), Is.EqualTo("rtm:subscription-data:"));

            await Task.Delay(200);

            await client.Yield();

            Assert.That(queue.TryDequeue() == null);

            await client.Dispose();
        }
Esempio n. 3
0
        public async Task TwoSubscriptionsWithDifferentNames()
        {
            var client = new RtmClientBuilder(Config.Endpoint, Config.AppKey).Build();

            var channel = GenerateRandomChannelName();
            var subId1  = "s1_" + channel;
            var subId2  = "s2_" + channel;

            await client.StartAndWaitConnected();

            var subCfg = new SubscriptionConfig(SubscriptionModes.Advanced)
            {
                Filter = $"select * FROM `{channel}`"
            };

            var queue1 = new TestSubscriptionObserverQueue();

            queue1.ObserveSubscriptionState();
            queue1.ObserveSubscriptionPdu();
            subCfg.Observer = queue1;
            client.CreateSubscription(subId1, subCfg);

            Assert.That(await queue1.Dequeue(), Is.EqualTo("rtm:created"));
            Assert.That(await queue1.Dequeue(), Is.EqualTo("rtm:enter-unsubscribed"));
            Assert.That(await queue1.Dequeue(), Is.EqualTo("rtm:leave-unsubscribed"));
            Assert.That(await queue1.Dequeue(), Is.EqualTo("rtm:enter-subscribing"));
            Assert.That(await queue1.Dequeue(), Is.EqualTo("rtm:leave-subscribing"));
            Assert.That(await queue1.Dequeue(), Is.EqualTo("rtm:enter-subscribed"));

            var queue2 = new TestSubscriptionObserverQueue();

            queue2.ObserveSubscriptionState();
            queue2.ObserveSubscriptionPdu();
            subCfg.Observer = queue2;
            client.CreateSubscription(subId2, subCfg);

            Assert.That(await queue2.Dequeue(), Is.EqualTo("rtm:created"));
            Assert.That(await queue2.Dequeue(), Is.EqualTo("rtm:enter-unsubscribed"));
            Assert.That(await queue2.Dequeue(), Is.EqualTo("rtm:leave-unsubscribed"));
            Assert.That(await queue2.Dequeue(), Is.EqualTo("rtm:enter-subscribing"));
            Assert.That(await queue2.Dequeue(), Is.EqualTo("rtm:leave-subscribing"));
            Assert.That(await queue2.Dequeue(), Is.EqualTo("rtm:enter-subscribed"));

            var msg = new JObject(new JProperty("filed", "value"));

            await client.Publish(channel, msg, Ack.Yes);

            Assert.That(await queue1.Dequeue(), Is.EqualTo($"rtm:subscription-data:{msg}"));
            Assert.That(await queue2.Dequeue(), Is.EqualTo($"rtm:subscription-data:{msg}"));

            await Task.Delay(200);

            await client.Yield();

            Assert.That(queue1.TryDequeue(), Is.EqualTo(null));
            Assert.That(queue2.TryDequeue(), Is.EqualTo(null));

            await client.Dispose();
        }
        public async Task FailOnOutOfSyncWhenAdvancedMode()
        {
            var channel = GenerateRandomChannelName();

            var client = new RtmClientBuilder(Config.Endpoint, Config.AppKey)
                         .SetConnector(ConnectAndOutOfSyncOnData)
                         .Build();

            var queue = client.CreateStateQueue();

            var obs = new TestSubscriptionObserverQueue(queue);

            obs.ObserveSubscriptionState();
            obs.ObserveSubscriptionPdu();

            client.OnError += ex =>
            {
                queue.Enqueue($"error:{ex.Message}");
            };

            client.CreateSubscription(channel, SubscriptionModes.Advanced, obs);

            RtmSubscriptionError subError = null;

            obs.OnSubscriptionError += (_, error) => subError = error;

            client.Start();

            await queue.AssertDequeue(
                "rtm:created",
                "rtm:enter-unsubscribed",
                "leave-stopped",
                "enter-connecting",
                "leave-connecting",
                "rtm:leave-unsubscribed",
                "rtm:enter-subscribing",
                "enter-connected",
                "rtm:leave-subscribing",
                "rtm:enter-subscribed");

            // trigger subscription/info
            await client.Publish(channel, "msg", Ack.Yes);

            await queue.AssertDequeue(
                $"rtm:leave-subscribed",
                "rtm:enter-failed",
                $"rtm:subscription-error:{RtmSubscriptionError.OutOfSync}");

            var sub = await client.GetSubscription(channel);

            Assert.That(subError.Position, Is.EqualTo(sub.Position));

            await queue.AssertEmpty(client, millis : 200);

            await client.Dispose();
        }
        public async Task FastForwardOnOutOfSyncWhenSimpleMode()
        {
            var channel = GenerateRandomChannelName();

            var client = new RtmClientBuilder(Config.Endpoint, Config.AppKey)
                         .SetConnector(ConnectAndFastForwardOnData)
                         .Build();

            var queue = client.CreateStateQueue();

            var obs = new TestSubscriptionObserverQueue(queue);

            obs.ObserveSubscriptionState();
            obs.ObserveSubscriptionPdu();

            client.OnError += ex =>
            {
                queue.Enqueue($"error:{ex.Message}");
            };

            RtmSubscriptionInfo subInfo = null;

            obs.OnSubscriptionInfo += (_, info) => subInfo = info;

            client.CreateSubscription(channel, SubscriptionModes.Simple, obs);

            client.Start();

            await queue.AssertDequeue(
                "rtm:created",
                "rtm:enter-unsubscribed",
                "leave-stopped",
                "enter-connecting",
                "leave-connecting",
                "rtm:leave-unsubscribed",
                "rtm:enter-subscribing",
                "enter-connected",
                "rtm:leave-subscribing",
                "rtm:enter-subscribed");

            // trigger subscription/info
            await client.Publish(channel, "msg", Ack.Yes);

            // on connected info
            Assert.That(await queue.Dequeue(), Is.EqualTo($"rtm:subscription-info:{RtmSubscriptionInfo.FastForward}"));

            var sub = await client.GetSubscription(channel);

            Assert.That(sub.Position, Is.EqualTo(null));
            Assert.That(subInfo.Position, Is.Not.EqualTo(null));

            await queue.AssertEmpty(client, millis : 200);

            await client.Dispose();
        }
Esempio n. 6
0
        public async Task AutoDeleteSubscriptionOnDispose()
        {
            var channel = GenerateRandomChannelName();
            var client  = new RtmClientBuilder(Config.Endpoint, Config.AppKey).Build();
            await client.Yield();

            var queue = new TestSubscriptionObserverQueue();

            queue.ObserveSubscriptionState();

            client.Start();
            client.CreateSubscription(channel, SubscriptionModes.Advanced, queue);

            Assert.That(await queue.Dequeue(), Is.EqualTo("rtm:created"));
            Assert.That(await queue.Dequeue(), Is.EqualTo("rtm:enter-unsubscribed"));
            Assert.That(await queue.Dequeue(), Is.EqualTo("rtm:leave-unsubscribed"));
            Assert.That(await queue.Dequeue(), Is.EqualTo("rtm:enter-subscribing"));
            Assert.That(await queue.Dequeue(), Is.EqualTo("rtm:leave-subscribing"));
            Assert.That(await queue.Dequeue(), Is.EqualTo("rtm:enter-subscribed"));

            await client.Dispose();

            Assert.That(await queue.Dequeue(), Is.EqualTo("rtm:leave-subscribed"));
            Assert.That(await queue.Dequeue(), Is.EqualTo("rtm:enter-unsubscribed"));
            Assert.That(await queue.Dequeue(), Is.EqualTo("rtm:leave-unsubscribed"));
            Assert.That(await queue.Dequeue(), Is.EqualTo("rtm:deleted"));

            try
            {
                await client.GetSubscription(channel);

                Assert.Fail("Subscription wasn't removed");
            }
            catch (InvalidOperationException)
            {
            }
        }
Esempio n. 7
0
        public async Task RestartOnUnsubscribeError()
        {
            var channel = GenerateRandomChannelName();
            var client  = new RtmClientBuilder(Config.Endpoint, Config.AppKey)
                          .SetConnector(
                (url, opts, ct) => TestConnection.Connect(
                    url,
                    opts,
                    ct,
                    transform: res =>
            {
                var reply = res.AsReply()?.AsPositive();
                if (reply != null && reply.pdu.Action.StartsWith(RtmActions.Unsubscribe, StringComparison.InvariantCulture))
                {
                    var oldPdu = reply.pdu.As <RtmUnsubscribeReply>();
                    var newPdu = Pdu.Create <RtmUnsubscribeError>(
                        action: $"{RtmActions.Unsubscribe}/{RtmOutcomes.Error}",
                        id: oldPdu.Id,
                        body: new RtmUnsubscribeError {
                        SubscriptionId = oldPdu.Body.SubscriptionId
                    });
                    return(new ConnectionOperationResult.Reply.Negative(newPdu));
                }

                return(res);
            })).Build();

            var queue  = client.CreateStateQueue();
            var subObs = new TestSubscriptionObserverQueue(queue);

            subObs.ObserveSubscriptionState();

            client.Start();
            await queue.AssertDequeue(
                "leave-stopped",
                "enter-connecting",
                "leave-connecting",
                "enter-connected");

            client.CreateSubscription(channel, SubscriptionModes.Advanced, subObs);
            await queue.AssertDequeue(
                "rtm:created",
                "rtm:enter-unsubscribed",
                "rtm:leave-unsubscribed",
                "rtm:enter-subscribing",
                "rtm:leave-subscribing",
                "rtm:enter-subscribed");

            client.RemoveSubscription(channel);

            await queue.AssertDequeue(
                "rtm:leave-subscribed",
                "rtm:enter-unsubscribing",
                "rtm:leave-unsubscribing",
                "rtm:enter-unsubscribed",
                "rtm:leave-unsubscribed",
                "rtm:deleted",
                "leave-connected",
                "enter-awaiting",
                "leave-awaiting",
                "enter-connecting",
                "leave-connecting",
                "enter-connected");

            await Task.Delay(200);

            await client.Yield();

            Assert.That(queue.TryDequeue(), Is.EqualTo(null));

            await client.Dispose();
        }
Esempio n. 8
0
        public async Task ManualResubscribe()
        {
            var channel = GenerateRandomChannelName();
            var client  = (RtmClient) new RtmClientBuilder(Config.Endpoint, Config.AppKey).Build();
            var queue   = client.CreateStateQueue();
            var subObs  = new TestSubscriptionObserverQueue(queue);

            subObs.ObserveSubscriptionState();
            subObs.ObserveSubscriptionPdu();

            await client.Yield();

            client.Start();
            Assert.That(await queue.Dequeue(), Is.EqualTo("leave-stopped"));
            Assert.That(await queue.Dequeue(), Is.EqualTo("enter-connecting"));
            Assert.That(await queue.Dequeue(), Is.EqualTo("leave-connecting"));
            Assert.That(await queue.Dequeue(), Is.EqualTo("enter-connected"));
            client.CreateSubscription(channel, SubscriptionModes.Advanced, subObs);
            Assert.That(await queue.Dequeue(), Is.EqualTo("rtm:created"));
            Assert.That(await queue.Dequeue(), Is.EqualTo("rtm:enter-unsubscribed"));
            Assert.That(await queue.Dequeue(), Is.EqualTo("rtm:leave-unsubscribed"));
            Assert.That(await queue.Dequeue(), Is.EqualTo("rtm:enter-subscribing"));
            Assert.That(await queue.Dequeue(), Is.EqualTo("rtm:leave-subscribing"));
            Assert.That(await queue.Dequeue(), Is.EqualTo("rtm:enter-subscribed"));

            await client.Publish(channel, "message-1", Ack.No).ConfigureAwait(false);

            await client.Yield();

            Assert.That(await queue.Dequeue(), Is.EqualTo("rtm:subscription-data:message-1"));
            await Task.Delay(200);

            await client.Yield();

            Assert.That(queue.TryDequeue(), Is.EqualTo(null));

            var cn = await client.GetConnection();

            await cn.Close();

            Assert.That(await queue.Dequeue(), Is.EqualTo("rtm:leave-subscribed"));
            Assert.That(await queue.Dequeue(), Is.EqualTo("rtm:enter-unsubscribed"));
            Assert.That(await queue.Dequeue(), Is.EqualTo("leave-connected"));
            Assert.That(await queue.Dequeue(), Is.EqualTo("enter-awaiting"));
            client.Start();
            var sw = new Stopwatch();

            sw.Start();
            Assert.That(await queue.Dequeue(), Is.EqualTo("leave-awaiting"));
            sw.Stop();
            Assert.That(sw.ElapsedMilliseconds, Is.LessThan(50));

            Assert.That(await queue.Dequeue(), Is.EqualTo("enter-connecting"));
            Assert.That(await queue.Dequeue(), Is.EqualTo("leave-connecting"));
            Assert.That(await queue.Dequeue(), Is.EqualTo("rtm:leave-unsubscribed"));
            Assert.That(await queue.Dequeue(), Is.EqualTo("rtm:enter-subscribing"));
            Assert.That(await queue.Dequeue(), Is.EqualTo("enter-connected"));
            Assert.That(await queue.Dequeue(), Is.EqualTo("rtm:leave-subscribing"));
            Assert.That(await queue.Dequeue(), Is.EqualTo("rtm:enter-subscribed"));

            await client.Publish(channel, "message-2", Ack.No).ConfigureAwait(false);

            await client.Yield();

            Assert.That(await queue.Dequeue(), Is.EqualTo("rtm:subscription-data:message-2"));
            await Task.Delay(200);

            await client.Yield();

            Assert.That(queue.TryDequeue(), Is.EqualTo(null));

            await client.Dispose();

            Assert.That(await queue.Dequeue(), Is.EqualTo("rtm:leave-subscribed"));
            Assert.That(await queue.Dequeue(), Is.EqualTo("rtm:enter-unsubscribed"));
            Assert.That(await queue.Dequeue(), Is.EqualTo("leave-connected"));
            Assert.That(await queue.Dequeue(), Is.EqualTo("enter-disposed"));
            Assert.That(await queue.Dequeue(), Is.EqualTo("rtm:leave-unsubscribed"));
            Assert.That(await queue.Dequeue(), Is.EqualTo("rtm:deleted"));

            await Task.Delay(200);

            await client.Yield();

            Assert.That(queue.TryDequeue(), Is.EqualTo(null));
        }