Example #1
0
        public void TestNoWait()
        {
            Context.RunInJsServer(c =>
            {
                // create the stream.
                CreateDefaultTestStream(c);

                // Create our JetStream context.
                IJetStream js = c.CreateJetStreamContext();

                // Build our subscription options. Durable is REQUIRED for pull based subscriptions
                PullSubscribeOptions options = PullSubscribeOptions.Builder().WithDurable(DURABLE).Build();

                // Pull Subscribe.
                IJetStreamPullSubscription sub = js.PullSubscribe(SUBJECT, options);
                AssertSubscription(sub, STREAM, DURABLE, null, true);
                c.Flush(DefaultTimeout); // flush outgoing communication with/to the server

                // publish 10 messages
                // no wait, batch size 10, there are 10 messages, we will read them all and not trip nowait
                JsPublish(js, SUBJECT, "A", 10);
                sub.PullNoWait(10);
                IList <Msg> messages = ReadMessagesAck(sub);
                Assert.Equal(10, messages.Count);
                AssertAllJetStream(messages);

                // publish 20 messages
                // no wait, batch size 10, there are 20 messages, we will read 10
                JsPublish(js, SUBJECT, "B", 20);
                sub.PullNoWait(10);
                messages = ReadMessagesAck(sub);
                Assert.Equal(10, messages.Count);

                // there are still ten messages
                // no wait, batch size 10, there are 20 messages, we will read 10
                sub.PullNoWait(10);
                messages = ReadMessagesAck(sub);
                Assert.Equal(10, messages.Count);

                // publish 5 messages
                // no wait, batch size 10, there are 5 messages, we WILL trip nowait
                JsPublish(js, SUBJECT, "C", 5);
                sub.PullNoWait(10);
                messages = ReadMessagesAck(sub);
                Assert.Equal(5, messages.Count);

                // publish 12 messages
                // no wait, batch size 10, there are more than batch messages we will read 10
                JsPublish(js, SUBJECT, "D", 12);
                sub.PullNoWait(10);
                messages = ReadMessagesAck(sub);
                Assert.Equal(10, messages.Count);

                // 2 messages left
                // no wait, less than batch size will WILL trip nowait
                sub.PullNoWait(10);
                messages = ReadMessagesAck(sub);
                Assert.Equal(2, messages.Count);
            });
        }
Example #2
0
        private void _testDurable(IJetStream js, PullSubSupplier supplier)
        {
            JsPublish(js, SUBJECT, 2);

            IJetStreamPullSubscription sub = supplier.Invoke();

            // start the pull
            sub.PullNoWait(4);

            IList <Msg> messages = ReadMessagesAck(sub);

            ValidateRedAndTotal(2, messages.Count, 2, 2);

            sub.Unsubscribe();
        }
        public static void Main(string[] args)
        {
            ArgumentHelper helper = new ArgumentHelperBuilder("Pull Subscription using primitive No Wait, Use Cases", args, Usage)
                                    .DefaultStream("nowait-uc-stream")
                                    .DefaultSubject("nowait-uc-subject")
                                    .DefaultDurable("nowait-uc-durable")
                                    .Build();

            try
            {
                using (IConnection c = new ConnectionFactory().CreateConnection(helper.MakeOptions()))
                {
                    // Create a JetStreamManagement context.
                    IJetStreamManagement jsm = c.CreateJetStreamManagementContext();

                    // Use the utility to create a stream stored in memory.
                    JsUtils.CreateStreamExitWhenExists(jsm, helper.Stream, helper.Subject);

                    // Create our JetStream context.
                    IJetStream js = c.CreateJetStreamContext();

                    // Build our subscription options. Durable is REQUIRED for pull based subscriptions
                    PullSubscribeOptions pullOptions = PullSubscribeOptions.Builder()
                                                       .WithDurable(helper.Durable) // required
                                                       .Build();

                    // 0.1 Initialize. subscription
                    // 0.2 DO NOT start the pull, no wait works differently than regular pull.
                    //     With no wait, we have to start the pull the first time and every time the
                    //     batch size is exhausted or no waits out.
                    // 0.3 Flush outgoing communication with/to the server, useful when app is both publishing and subscribing.
                    Console.WriteLine("\n----------\n0. Initialize the subscription and pull.");
                    IJetStreamPullSubscription sub = js.PullSubscribe(helper.Subject, pullOptions);
                    c.Flush(1000);

                    // 1. Start the pull, but there are no messages yet.
                    // -  Read the messages
                    // -  Since there are less than the batch size, we get them all (0)
                    Console.WriteLine("----------\n1. There are no messages yet");
                    sub.PullNoWait(10);
                    IList <Msg> messages = JsUtils.ReadMessagesAck(sub);
                    Console.WriteLine("We should have received 0 total messages, we received: " + messages.Count);

                    // 2. Publish 10 messages
                    // -  Start the pull
                    // -  Read the messages
                    // -  Since there are exactly the batch size we get them all
                    Console.WriteLine("----------\n2. Publish 10 which satisfies the batch");
                    JsUtils.Publish(js, helper.Subject, "A", 10);
                    sub.PullNoWait(10);
                    messages = JsUtils.ReadMessagesAck(sub);
                    Console.WriteLine("We should have received 10 total messages, we received: " + messages.Count);

                    // 3. Publish 20 messages
                    // -  Start the pull
                    // -  Read the messages
                    Console.WriteLine("----------\n3. Publish 20 which is larger than the batch size.");
                    JsUtils.Publish(js, helper.Subject, "B", 20);
                    sub.PullNoWait(10);
                    messages = JsUtils.ReadMessagesAck(sub);
                    Console.WriteLine("We should have received 10 total messages, we received: " + messages.Count);

                    // 4. There are still messages left from the last
                    // -  Start the pull
                    // -  Read the messages
                    Console.WriteLine("----------\n4. Get the rest of the publish.");
                    sub.PullNoWait(10);
                    messages = JsUtils.ReadMessagesAck(sub);
                    Console.WriteLine("We should have received 10 total messages, we received: " + messages.Count);

                    // 5. Publish 5 messages
                    // -  Start the pull
                    // -  Read the messages
                    Console.WriteLine("----------\n5. Publish 5 which is less than batch size.");
                    JsUtils.Publish(js, helper.Subject, "C", 5);
                    sub.PullNoWait(10);
                    messages = JsUtils.ReadMessagesAck(sub);
                    Console.WriteLine("We should have received 5 total messages, we received: " + messages.Count);

                    // 6. Publish 14 messages
                    // -  Start the pull
                    // -  Read the messages
                    // -  we do NOT get a nowait status message if there are more or equals messages than the batch
                    Console.WriteLine("----------\n6. Publish 14 which is more than the batch size.");
                    JsUtils.Publish(js, helper.Subject, "D", 14);
                    sub.PullNoWait(10);
                    messages = JsUtils.ReadMessagesAck(sub);
                    Console.WriteLine("We should have received 10 total messages, we received: " + messages.Count);

                    // 7. There are 4 messages left
                    // -  Start the pull
                    // -  Read the messages
                    // -  Since there are less than batch size the last message we get will be a status 404 message.
                    Console.WriteLine("----------\n7. There are 4 messages left, which is less than the batch size.");
                    sub.PullNoWait(10);
                    messages = JsUtils.ReadMessagesAck(sub);
                    Console.WriteLine("We should have received 4 messages, we received: " + messages.Count);

                    Console.WriteLine("----------\n");

                    // delete the stream since we are done with it.
                    jsm.DeleteStream(helper.Stream);
                }
            }
            catch (Exception ex)
            {
                helper.ReportException(ex);
            }
        }
Example #4
0
        public void TestPullExpires()
        {
            Context.RunInJsServer(c =>
            {
                // create the stream.
                CreateDefaultTestStream(c);

                // Create our JetStream context.
                IJetStream js = c.CreateJetStreamContext();

                // Build our subscription options. Durable is REQUIRED for pull based subscriptions
                PullSubscribeOptions options = PullSubscribeOptions.Builder().WithDurable(DURABLE).Build();

                // Subscribe synchronously.
                IJetStreamPullSubscription sub = js.PullSubscribe(SUBJECT, options);
                AssertSubscription(sub, STREAM, DURABLE, null, true);
                c.Flush(DefaultTimeout); // flush outgoing communication with/to the server

                int expires = 250;       // millis

                // publish 10 messages
                JsPublish(js, SUBJECT, "A", 5);
                sub.PullExpiresIn(10, expires);
                IList <Msg> messages = ReadMessagesAck(sub);
                Assert.Equal(5, messages.Count);
                AssertAllJetStream(messages);
                Thread.Sleep(expires); // make sure the pull actually expires

                JsPublish(js, SUBJECT, "B", 10);
                sub.PullExpiresIn(10, expires);
                messages = ReadMessagesAck(sub);
                Assert.Equal(10, messages.Count);
                Thread.Sleep(expires); // make sure the pull actually expires

                JsPublish(js, SUBJECT, "C", 5);
                sub.PullExpiresIn(10, expires);
                messages = ReadMessagesAck(sub);
                Assert.Equal(5, messages.Count);
                AssertAllJetStream(messages);
                Thread.Sleep(expires); // make sure the pull actually expires

                JsPublish(js, SUBJECT, "D", 10);
                sub.Pull(10);
                messages = ReadMessagesAck(sub);
                Assert.Equal(10, messages.Count);

                JsPublish(js, SUBJECT, "E", 5);
                sub.PullExpiresIn(10, expires); // using millis version here
                messages = ReadMessagesAck(sub);
                Assert.Equal(5, messages.Count);
                AssertAllJetStream(messages);
                Thread.Sleep(expires); // make sure the pull actually expires

                JsPublish(js, SUBJECT, "F", 10);
                sub.PullNoWait(10);
                messages = ReadMessagesAck(sub);
                Assert.Equal(10, messages.Count);

                JsPublish(js, SUBJECT, "G", 5);
                sub.PullExpiresIn(10, expires); // using millis version here
                messages = ReadMessagesAck(sub);
                Assert.Equal(5, messages.Count);
                AssertAllJetStream(messages);

                JsPublish(js, SUBJECT, "H", 10);
                messages = sub.Fetch(10, expires);
                Assert.Equal(10, messages.Count);
                AssertAllJetStream(messages);

                JsPublish(js, SUBJECT, "I", 5);
                sub.PullExpiresIn(10, expires);
                messages = ReadMessagesAck(sub);
                Assert.Equal(5, messages.Count);
                AssertAllJetStream(messages);
                Thread.Sleep(expires); // make sure the pull actually expires
            });
        }