public async Task DelayedPublish() { var channel = GenerateRandomChannelName(); var subClient = new RtmClientBuilder(Config.Endpoint, Config.AppKey).Build(); await subClient.Yield(); subClient.Start(); var subData = new TestSubscriptionObserverQueue(); subData.ObserveSubscriptionPdu(); await subClient.CreateSubscriptionAndWaitSubscribed(channel, null, subData); var pubClient = new RtmClientBuilder(Config.Endpoint, Config.AppKey).Build(); await pubClient.Yield(); var noAckTask = pubClient.Publish(channel, "no-ack", Ack.No); var withAckTask = pubClient.Publish(channel, "with-ack", Ack.Yes); await Task.Delay(100); Assert.That(noAckTask.Status, Is.EqualTo(TaskStatus.WaitingForActivation)); Assert.That(withAckTask.Status, Is.EqualTo(TaskStatus.WaitingForActivation)); pubClient.Start(); await noAckTask.ConfigureAwait(false); await withAckTask.ConfigureAwait(false); Assert.That(await subData.Dequeue(), Is.EqualTo("rtm:subscription-data:no-ack")); Assert.That(await subData.Dequeue(), Is.EqualTo("rtm:subscription-data:with-ack")); await subClient.Dispose(); await pubClient.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(); }
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 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(); }
public async Task AutoResubscribe() { var channel = GenerateRandomChannelName(); var client = (RtmClient) new RtmClientBuilder(Config.Endpoint, Config.AppKey).Build(); var queue = client.CreateStateQueue(); 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")); var subData = new TestSubscriptionObserverQueue(); subData.ObserveSubscriptionPdu(); await client.CreateSubscriptionAndWaitSubscribed(channel, null, subData).ConfigureAwait(false); await client.Publish(channel, "message-1", Ack.No).ConfigureAwait(false); await client.Yield(); Assert.That(await subData.Dequeue(), Is.EqualTo("rtm:subscription-data:message-1")); var cn = await client.GetConnection(); await cn.Close(); Assert.That(await queue.Dequeue(), Is.EqualTo("leave-connected")); Assert.That(await queue.Dequeue(), Is.EqualTo("enter-awaiting")); Assert.That(await queue.Dequeue(), Is.EqualTo("leave-awaiting")); 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")); await client.Publish(channel, "message-2", Ack.No).ConfigureAwait(false); await client.Yield(); Assert.That(await subData.Dequeue(), Is.EqualTo("rtm:subscription-data:message-2")); await client.Dispose(); Assert.That(await queue.Dequeue(), Is.EqualTo("leave-connected")); Assert.That(await queue.Dequeue(), Is.EqualTo("enter-disposed")); await Task.Delay(200); await client.Yield(); Assert.That(queue.TryDequeue() == null); Assert.That(subData.TryDequeue() == null); }
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)); }