Ejemplo n.º 1
0
        public static QueueAsync <RtmSubscriptionData> CreateSubscriptionDataQueue(this ISubscriptionEventSource source)
        {
            var queue = new QueueAsync <RtmSubscriptionData>();

            source.SetSubscriptionDataQueue(queue);
            return(queue);
        }
Ejemplo n.º 2
0
        public static QueueAsync <string> CreateStateQueue(this IRtmClient client)
        {
            var queue = new QueueAsync <string>();

            client.SetStateObserver(queue);
            return(queue);
        }
Ejemplo n.º 3
0
 public static async Task AssertDequeue <T>(this QueueAsync <T> queue, params T[] items)
 {
     foreach (var item in items)
     {
         Assert.That(await queue.Dequeue(), Is.EqualTo(item));
     }
 }
Ejemplo n.º 4
0
        public async Task RepeatFirstMessage()
        {
            var channel = GenerateRandomChannelName();
            var client  = new RtmClientBuilder(Config.Endpoint, Config.AppKey).Build();
            await client.Yield();

            var subObs  = new SubscriptionObserver();
            var subData = subObs.CreateSubscriptionDataQueue();

            var subscription = new QueueAsync <string>();

            subObs.OnEnterSubscribed += s =>
            {
                subscription.Enqueue(s.Position);
            };

            client.Start();
            client.CreateSubscription(channel, SubscriptionModes.Advanced, subObs);
            var pos = await subscription.Dequeue().ConfigureAwait(false);

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

            await subData.DequeueAndVerify(channel, "message-1").ConfigureAwait(false);

            await client.Yield();

            client.RemoveSubscription(channel);
            client.CreateSubscription(
                channel,
                new SubscriptionConfig(SubscriptionModes.Advanced, position: pos, observer: subObs));
            await subData.DequeueAndVerify(channel, "message-1").ConfigureAwait(false);

            await client.Dispose();
        }
Ejemplo n.º 5
0
        public static T TryDequeue <T>(this QueueAsync <T> queue)
        {
            T res;

            queue.TryDequeue(out res);
            return(res);
        }
Ejemplo n.º 6
0
        public static async Task AssertEmpty <T>(this QueueAsync <T> queue, IRtmClient client, int millis)
        {
            await Task.Delay(millis);

            await client.Yield();

            Assert.That(queue.TryDequeue(), Is.EqualTo(null));
        }
Ejemplo n.º 7
0
        public static async Task <IConnection> Connect(
            string url,
            ConnectionOptions opts,
            CancellationToken ct,
            QueueAsync <Pdu <object> > requests,
            QueueAsync <Pdu> replies)
        {
            var con = await Connection.Connect(url, opts, ct).ConfigureAwait(false);

            return(new TestConnection(con, null, null, requests, replies));
        }
Ejemplo n.º 8
0
 public TestConnection(
     IConnection connection,
     Func <ConnectionOperationResult, ConnectionOperationResult> operationTransform,
     Func <ConnectionStepResult, ConnectionStepResult> stepTransform,
     QueueAsync <Pdu <object> > requests,
     QueueAsync <Pdu> replies)
 {
     _connection         = connection;
     _operationTransform = operationTransform;
     _stepTransform      = stepTransform;
     _requests           = requests;
     _replies            = replies;
 }
Ejemplo n.º 9
0
        public static async Task <RtmSubscriptionData> DequeueAndVerify <T>(this QueueAsync <RtmSubscriptionData> subscriptionData, string subscriptionId, T message)
        {
            var d = await subscriptionData.Dequeue().ConfigureAwait(false);

            if (d.SubscriptionId != subscriptionId)
            {
                throw new Exception("wrong channel");
            }

            if (d.Messages.Length != 1)
            {
                throw new Exception("only one message was expected");
            }

            if (!d.Messages[0].ToObject <T>().Equals(message))
            {
                throw new Exception("not equals");
            }

            return(d);
        }
Ejemplo n.º 10
0
 public TestSubscriptionObserverQueue(QueueAsync <string> queue)
 {
     _queue = queue;
 }
Ejemplo n.º 11
0
        public async Task SubscribeWithAdvancedModeAndPosition()
        {
            var channel = GenerateRandomChannelName();
            var reqs    = new QueueAsync <Pdu <object> >();
            var reps    = new QueueAsync <Pdu>();
            var client  = (RtmClient) new RtmClientBuilder(Config.Endpoint, Config.AppKey)
                          .SetConnector((url, opts, ct) => TestConnection.Connect(url, opts, ct, reqs, reps))
                          .Build();

            client.Start();

            // get position
            client.CreateSubscription(
                channel,
                new SubscriptionConfig(SubscriptionModes.Simple));

            Assert.That((await reqs.Dequeue()).Action, Is.EqualTo("rtm/subscribe"));

            var rep1 = await reps.Dequeue();

            Assert.That(rep1.Action, Is.EqualTo("rtm/subscribe/ok"));
            var pos1 = rep1.As <RtmSubscribeReply>().Body.Position;

            client.RemoveSubscription(channel);
            Assert.That((await reqs.Dequeue()).Action, Is.EqualTo("rtm/unsubscribe"));
            Assert.That((await reps.Dequeue()).Action, Is.EqualTo("rtm/unsubscribe/ok"));

            await client.Yield();

            var sub = await client.GetSubscription(channel);

            Assert.That(sub, Is.Null); // subscription is removed

            client.CreateSubscription(
                channel,
                new SubscriptionConfig(SubscriptionModes.Advanced, position: pos1));

            var req3 = await reqs.Dequeue();

            var body3 = (RtmSubscribeRequest)req3.Body;

            Assert.That(req3.Action, Is.EqualTo("rtm/subscribe"));
            Assert.That(body3.Position, Is.EqualTo(pos1));
            Assert.That(body3.FastForward, Is.False);

            var rep3 = await reps.Dequeue();

            Assert.That(rep3.Action, Is.EqualTo("rtm/subscribe/ok"));
            var pos2 = rep3.As <RtmSubscribeReply>().Body.Position;

            // cause resubsubscription
            var conn = await client.GetConnection();

            await conn.Close();

            var req4 = await reqs.Dequeue();

            var body4 = (RtmSubscribeRequest)req4.Body;

            Assert.That(req4.Action, Is.EqualTo("rtm/subscribe"));
            Assert.That(body4.Position, Is.EqualTo(pos2));
            Assert.That(body4.FastForward, Is.False);

            var rep4 = await reps.Dequeue();

            Assert.That(rep4.Action, Is.EqualTo("rtm/subscribe/ok"));

            await client.Dispose();
        }