public void PushMultipleToSubscribedTopic()
        {
            string topicName = TestDataFactory.RandomTopicName();

            T("Topic " + topicName);
            Queue queue = validClient.CreateQueue("csharp_notify_queue_" + TestDataFactory.RandomQueueName());
            Topic topic = validClient.CreateTopic(topicName);
            QueueTopicSubscription sub = validClient.CreateTopicSubscription(
                topic, new QueueTopicSubscription(queue.Name));

            Assert.NotNull(queue);
            Assert.NotNull(topic);
            Assert.NotNull(sub);
            Assert.AreEqual(topicName, topic.Name);
            Assert.AreEqual(sub.QueueName, queue.Name);

            List<Message> originalMsgs = new List<Message>();
            List<Message> pushedMsgs = new List<Message>();

            Stopwatch timer = Stopwatch.StartNew();
            for (int ii = 0; ii < 100; ii++) {
                string randomBody = "(" + ii + ") " + TestDataFactory.RandomHexString(20);

                Message msg = new Message();
                msg.Body = randomBody;
                originalMsgs.Add(msg);

                Assert.AreEqual(randomBody, msg.Body);

                Message pushedMsg = validClient.PublishToTopic(topic, msg);

                Console.Write(".");
                if (ii > 0 && ii % 10 == 0) {
                    Console.Write(ii + "\n");
                }

                Assert.IsNotNull(pushedMsg);
                Assert.IsNotEmpty(pushedMsg.ID);
                Assert.AreEqual(pushedMsg.Body, msg.Body);

                pushedMsgs.Add(pushedMsg);
            }

            timer.Stop();

            Assert.IsNotEmpty(originalMsgs);
            Assert.IsNotEmpty(pushedMsgs);
            Assert.AreEqual(pushedMsgs.Count, originalMsgs.Count);

            T("Generated " + originalMsgs.Count + " messages");
            T(String.Format(
                "Pushed {0} messages in {1:f}sec ({2:f}/sec)",
                pushedMsgs.Count,
                timer.Elapsed.TotalSeconds,
                pushedMsgs.Count / timer.Elapsed.TotalSeconds));

            for (int ii = 0; ii < pushedMsgs.Count; ii++) {
                Message pushedMsg = pushedMsgs[ii];
                Message originalMsg = originalMsgs.Find(
                    delegate(Message tmpMsg)
                    {
                        return (tmpMsg.Body == pushedMsg.Body);
                    });

                Assert.IsNotNull(originalMsg);

                T(String.Format("Pushed message {0:d}: {1} -> {2}",
                    ii, pushedMsg.ID, originalMsg.Body));

                Assert.AreEqual(originalMsg.Body, pushedMsg.Body);
            }

            int poppedCount = 0, expectedPopCount = pushedMsgs.Count;
            int perIterationWaitMsec = 100, totalWait = 0;
            List<Message> poppedMessages = new List<Message>();

            do {
                System.Threading.Thread.Sleep(perIterationWaitMsec);
                totalWait += perIterationWaitMsec;

                List<Message> tmpPoppedMessages = validClient.PopMessages(queue, 10);
                poppedCount += tmpPoppedMessages.Count;

                if (tmpPoppedMessages.Count > 0) {
                    T("Popped " + poppedMessages.Count + " messages:");
                    tmpPoppedMessages.ForEach(delegate(Message m)
                    {
                        validClient.DeleteMessage(queue, m);
                        T(String.Format(" - {0}: {1}", m.ID, m.Body));
                    });
                }

                poppedMessages.AddRange(tmpPoppedMessages);
            } while (poppedCount < expectedPopCount);

            Assert.Greater(poppedCount, 0);
            Assert.AreEqual(poppedCount, pushedMsgs.Count);

            T(String.Format("Total of {0}msec for {1} messages to reach queue.",
                totalWait, poppedCount));

            for (int ii = 0; ii < pushedMsgs.Count; ii++) {
                Message pushedMsg = pushedMsgs[ii];
                Message poppedMsg = poppedMessages.Find(
                    delegate(Message tmpMsg)
                    {
                        return (tmpMsg.Body == pushedMsg.Body);
                    });

                Assert.IsNotNull(poppedMsg);
                Assert.AreEqual(pushedMsg.Body, poppedMsg.Body);
                T(String.Format("Popped message {0:d}: {1} -> {2} {3}",
                    ii, pushedMsg.ID, pushedMsg.ID, poppedMsg.Body));
            }
        }
        public void PushSingleToTopic()
        {
            string topicName = TestDataFactory.RandomTopicName();

            T("Topic " + topicName);
            Topic newTopic = new Topic(topicName);
            Topic topic = validClient.CreateTopic(newTopic);

            Assert.NotNull(topic);
            Assert.AreEqual(topicName, topic.Name);

            string randomBody = TestDataFactory.RandomHexString();
            Message msg = new Message();
            msg.Fields["test Key"] = TestDataFactory.RandomHexString(10);
            msg.Body = randomBody;

            Assert.AreEqual(randomBody, msg.Body);

            Message returnMsg = validClient.PublishToTopic(topic, msg);
            Assert.IsNotEmpty(returnMsg.ID);
            Assert.AreEqual(returnMsg.Body, msg.Body);
            Assert.AreEqual(returnMsg.Fields["test Key"], msg.Fields["test Key"]);

            T("Msg ID:    " + returnMsg.ID);
            T("Msg Body:  " + returnMsg.Body);
            T("Msg Field: testKey=" + returnMsg.Fields["test Key"]);
        }
        public void PushMultipleToTopic()
        {
            string topicName = TestDataFactory.RandomTopicName();

            T("Topic " + topicName);
            Topic topic = validClient.CreateTopic(topicName);

            Assert.NotNull(topic);
            Assert.AreEqual(topicName, topic.Name);

            List<Message> originalMsgs = new List<Message>();
            List<Message> pushedMsgs = new List<Message>();

            Stopwatch timer = Stopwatch.StartNew();
            for (int ii = 0; ii < 100; ii++) {
                string randomBody = TestDataFactory.RandomHexString();

                Message msg = new Message();
                msg.Body = randomBody;
                originalMsgs.Add(msg);

                Assert.AreEqual(randomBody, msg.Body);

                Message pushedMsg = validClient.PublishToTopic(topic, msg);

                Console.Write(".");
                if (ii > 0 && ii % 10 == 0) {
                    Console.Write(ii + "\n");
                }

                Assert.IsNotNull(pushedMsg);
                Assert.IsNotEmpty(pushedMsg.ID);
                Assert.AreEqual(pushedMsg.Body, msg.Body);

                pushedMsgs.Add(pushedMsg);
            }

            timer.Stop();

            Assert.IsNotEmpty(originalMsgs);
            Assert.IsNotEmpty(pushedMsgs);
            Assert.AreEqual(pushedMsgs.Count, originalMsgs.Count);

            T("Generated " + originalMsgs.Count + " messages");
            T(String.Format(
                "Pushed {0} messages in {1:f}sec ({2:f}/sec)",
                pushedMsgs.Count,
                timer.Elapsed.TotalSeconds,
                pushedMsgs.Count / timer.Elapsed.TotalSeconds));

            for (int ii = 0; ii < pushedMsgs.Count; ii++) {
                Message pushedMsg = pushedMsgs[ii];
                Message originalMsg = originalMsgs.Find(
                    delegate(Message tmpMsg)
                    {
                        return (tmpMsg.Body == pushedMsg.Body);
                    });

                Assert.IsNotNull(originalMsg);

                T(String.Format("Pushed message {0:d}: {1} -> {2}",
                    ii, pushedMsg.ID, originalMsg.Body));

                Assert.AreEqual(originalMsg.Body, pushedMsg.Body);
            }
        }
        public MessageList PopMessages(string queueName, int maxMessages = 1)
        {
            if (maxMessages <= 0 || maxMessages > Queue.MaxMessagesPerPop) {
                throw new ArgumentOutOfRangeException("maxMessages");
            }

            MessageList messages = new MessageList();

            MessagingRequest request = new MessagingRequest("queues/{queueName}/messages", Method.GET);
            request.AddUrlSegment("queueName", queueName);
            request.AddParameter("batch", maxMessages, ParameterType.GetOrPost);
            request.HttpStatusSuccessCodes.Add(200);
            request.HttpStatusExceptionMap.Add(404, typeof(QueueNotFoundException));

            MessageListResponse response = client.Execute<MessageListResponse>(request);

            response.items.ForEach(delegate(MessageResponse tmpMessage)
            {
                Message message = new Message();
                message.ID = tmpMessage.id;
                message.Body = tmpMessage.body;
                message.SetInitialEntryTime(tmpMessage.initial_entry_time);
                message.VisibilityDelay = tmpMessage.visibility_delay;
                message.VisibilityInterval = tmpMessage.visibility_interval;

                if (tmpMessage.fields != null) {
                    message.Fields = new FieldList(tmpMessage.fields);
                }

                messages.Add(message);
            });

            return messages;
        }
 public bool DeleteMessage(Queue queue, Message message)
 {
     return DeleteMessage(queue.Name, message.ID);
 }
 public Message PublishToTopic(Topic topic, Message message)
 {
     return PublishToTopic(topic.Name, message);
 }
        public Message PublishToTopic(string topicName, Message message)
        {
            MessagingRequest request = new MessagingRequest("topics/{topicName}/messages", Method.POST);
            request.AddUrlSegment("topicName", topicName);
            request.HttpStatusSuccessCodes.Add(201);
            request.HttpStatusExceptionMap.Add(404, typeof(TopicNotFoundException));

            object postObj = new {
                body = message.Body,
                fields = message.Fields,
                visibility_delay = message.VisibilityDelay
            };

            if (DebugRequests) {
                Console.WriteLine("-> " + request.JsonSerializer.Serialize(postObj));
            }

            request.AddBody(postObj);

            MessageResponse response = client.Execute<MessageResponse>(request);
            Message tmpMessage = new Message();
            tmpMessage.ID = response.id;
            tmpMessage.Body = response.body;
            tmpMessage.Fields = new FieldList(response.fields);
            tmpMessage.VisibilityDelay = response.visibility_delay;
            tmpMessage.SetInitialEntryTime(response.initial_entry_time);

            return tmpMessage;
        }
 public Message PublishToQueue(Queue queue, Message message)
 {
     return PublishToQueue(queue.Name, message);
 }
        public void PopMultipleFromQueue()
        {
            string queueName = TestDataFactory.RandomQueueName();
            int numberOfTestMessages = 1000;

            T("Queue " + queueName);
            Queue queue = validClient.CreateQueue(queueName);
            T(" - interval: " + queue.VisibilityInterval + " sec");

            Assert.NotNull(queue);
            Assert.AreEqual(queueName, queue.Name);

            List<Message> originalMsgs = new List<Message>();
            List<Message> pushedMsgs = new List<Message>();
            for (int ii = 0; ii < numberOfTestMessages; ii++) {
                string randomBody = "(" + ii + ") " + TestDataFactory.RandomHexString(15);

                Message msg = new Message();
                msg.Body = randomBody;
                originalMsgs.Add(msg);

                Assert.AreEqual(randomBody, msg.Body);

                Message pushedMsg = validClient.PublishToQueue(queue, msg);

                Assert.IsNotNull(pushedMsg);
                Assert.IsNotEmpty(pushedMsg.ID);
                Assert.AreEqual(pushedMsg.Body, msg.Body);

                pushedMsgs.Add(pushedMsg);

                TC(".");
                if (ii > 0 && ii % 100 == 0) {
                    TC(ii + "\n");
                }
            }

            Assert.IsNotEmpty(originalMsgs);
            Assert.IsNotEmpty(pushedMsgs);

            T("Generated " + originalMsgs.Count + " messages");
            T("Pushed " + pushedMsgs.Count + " messages");

            List<Message> poppedMsgs = new List<Message>();
            Assert.DoesNotThrow(
                delegate
                {
                    int iterPoppedCount = 0;
                    do {
                        T("Popping...");
                        List<Message> tmpMsgs = validClient.PopMessages(queue, Queue.MaxMessagesPerPop);
                        iterPoppedCount = tmpMsgs.Count;
                        poppedMsgs.AddRange(tmpMsgs);

                        T(" - Popped " + tmpMsgs.Count);
                        T(String.Format(" - i:{0}, p:{1}, t:{2}",
                            iterPoppedCount, pushedMsgs.Count, poppedMsgs.Count));
                    } while (iterPoppedCount > 0);
                });
            Assert.IsNotEmpty(poppedMsgs);
            Assert.AreEqual(poppedMsgs.Count, pushedMsgs.Count);

            for (int ii = 0; ii < poppedMsgs.Count; ii++) {
                Message poppedMsg = poppedMsgs[ii];
                Message pushedMsg = pushedMsgs.Find(
                    delegate(Message tmpMsg)
                    {
                        return (tmpMsg.ID == poppedMsg.ID);
                    });

                Assert.IsNotNull(pushedMsg);

                T(String.Format("Popped message {0:d}: {1} / {2}",
                    ii, poppedMsg.ID, pushedMsg.ID));

                Assert.AreEqual(poppedMsg.Body, pushedMsg.Body);
            }
        }
        public void PopSingleFromQueue()
        {
            string queueName = TestDataFactory.RandomQueueName();

            T("Queue " + queueName);
            Queue queue = validClient.CreateQueue(queueName);

            Assert.NotNull(queue);
            Assert.AreEqual(queueName, queue.Name);

            string randomBody = TestDataFactory.RandomHexString();

            Message originalMsg = new Message();
            originalMsg.Body = randomBody;

            Assert.AreEqual(randomBody, originalMsg.Body);

            Message pushedMsg = validClient.PublishToQueue(queue, originalMsg);
            Assert.IsNotEmpty(pushedMsg.ID);
            Assert.AreEqual(pushedMsg.Body, originalMsg.Body);

            T("Pushed Msg ID:   " + pushedMsg.ID);
            T("Pushed Msg Body: " + pushedMsg.Body);

            Message poppedMsg = null;

            Assert.DoesNotThrow(
                delegate { poppedMsg = validClient.PopMessage(queue); });
            Assert.IsNotNull(poppedMsg);
            Assert.IsNotEmpty(poppedMsg.ID);
            Assert.AreEqual(poppedMsg.ID, pushedMsg.ID);
            Assert.AreEqual(poppedMsg.Body, pushedMsg.Body);

            T("Popped Msg ID:   " + poppedMsg.ID);
            T("Popped Msg Body: " + poppedMsg.Body);
        }
        public void PushMultipleToQueue()
        {
            string queueName = TestDataFactory.RandomQueueName();

            T("Queue " + queueName);
            Queue queue = validClient.CreateQueue(queueName);
            T(" - interval: " + queue.VisibilityInterval + " sec");

            Assert.NotNull(queue);
            Assert.AreEqual(queueName, queue.Name);

            List<Message> originalMsgs = new List<Message>();
            List<Message> pushedMsgs = new List<Message>();

            Stopwatch timer = Stopwatch.StartNew();
            for (int ii = 0; ii < 1000; ii++) {
                string randomBody = TestDataFactory.RandomHexString();

                Message msg = new Message();
                msg.Body = randomBody;
                originalMsgs.Add(msg);

                Assert.AreEqual(randomBody, msg.Body);

                Message pushedMsg = validClient.PublishToQueue(queue, msg);

                TC(".");
                if (ii > 0 && ii % 100 == 0) {
                    TC(ii + "\n");
                }

                Assert.IsNotNull(pushedMsg);
                Assert.IsNotEmpty(pushedMsg.ID);
                Assert.AreEqual(pushedMsg.Body, msg.Body);

                pushedMsgs.Add(pushedMsg);
            }

            timer.Stop();

            Assert.IsNotEmpty(originalMsgs);
            Assert.IsNotEmpty(pushedMsgs);
            Assert.AreEqual(pushedMsgs.Count, originalMsgs.Count);

            T("Generated " + originalMsgs.Count + " messages");
            T(String.Format(
                "Pushed {0} messages in {1:f}sec ({2:f}/sec)",
                pushedMsgs.Count,
                timer.Elapsed.TotalSeconds,
                pushedMsgs.Count / timer.Elapsed.TotalSeconds));

            for (int ii = 0; ii < pushedMsgs.Count; ii++) {
                Message pushedMsg = pushedMsgs[ii];
                Message originalMsg = originalMsgs.Find(
                    delegate(Message tmpMsg)
                    {
                        return (tmpMsg.Body == pushedMsg.Body);
                    });

                Assert.IsNotNull(originalMsg);

                T(String.Format("Pushed message {0:d}: {1} -> {2}",
                    ii, pushedMsg.ID, originalMsg.Body));

                Assert.AreEqual(originalMsg.Body, pushedMsg.Body);
            }
        }
        public void PushSingleToQueue()
        {
            string queueName = TestDataFactory.RandomQueueName();

            T("Queue " + queueName);
            Queue queue = validClient.CreateQueue(queueName);

            Assert.NotNull(queue);
            Assert.AreEqual(queueName, queue.Name);

            string randomBody = TestDataFactory.RandomHexString();
            Message msg = new Message();
            msg.Fields["test Key"] = TestDataFactory.RandomHexString(10);
            msg.Body = randomBody;

            Assert.AreEqual(randomBody, msg.Body);

            Message returnMsg = validClient.PublishToQueue(queue, msg);
            Assert.IsNotEmpty(returnMsg.ID);
            Assert.AreEqual(returnMsg.Body, msg.Body);
            Assert.AreEqual(returnMsg.Fields["test Key"], msg.Fields["test Key"]);

            T("Msg ID:    " + returnMsg.ID);
            T("Msg Body:  " + returnMsg.Body);
            T("Msg Field: testKey=" + returnMsg.Fields["test Key"]);
        }