public void LoadTest()
        {
            AddConfigValue("SQSWindow", "0.00:00:15");
            //QueueMessages(10000); uncomment to do a load test
            MockSQSService service = new MockSQSService();
            Stopwatch sw = new Stopwatch();
            sw.Start();
            service.Start();

            int count = 0;
            do
            {
                count = 0;
                List<NewWindowEventArgs> windows = new List<NewWindowEventArgs>();
                windows.AddRange(service.NewWindowEventArgsReceived);
                foreach (NewWindowEventArgs args in windows)
                {
                    count += args.Messages.Count();
                }
            }
            while ((count < 10000) &
                   (sw.Elapsed.TotalSeconds < 120));
            sw.Stop();
            service.Stop();
            Assert.IsTrue(sw.Elapsed.TotalSeconds <= 120);

            RemoveConfigValue("SQSWindow");
        }
        public void MessageDeleted()
        {
            AddConfigValue("SQSWindow", "0.00:00:15");

            int visibilityTimeout = -1;
            // change the visibility timeout of the queue
            using (Amazon.SQS.AmazonSQSClient client = new AmazonSQSClient(
                ConfigurationManager.AppSettings["AWSAccessKey"],
                ConfigurationManager.AppSettings["AWSSecretAccessKey"],
                new AmazonSQSConfig()))
            {
                // store the visibilitytimeout so we can reset it
                GetQueueAttributesRequest getRequest = new GetQueueAttributesRequest();
                getRequest.QueueUrl = ConfigurationManager.AppSettings["QueueUrl"];
                getRequest.AttributeName.Add("VisibilityTimeout");
                GetQueueAttributesResponse response = client.GetQueueAttributes(getRequest);
                visibilityTimeout = response.GetQueueAttributesResult.VisibilityTimeout;

                // set it to 1 second
                SetQueueAttributesRequest setRequest = new SetQueueAttributesRequest();
                setRequest.QueueUrl = ConfigurationManager.AppSettings["QueueUrl"];
                setRequest.Attribute.Add(new Amazon.SQS.Model.Attribute() { Name = "VisibilityTimeout", Value = "1" });
                client.SetQueueAttributes(setRequest);

                String[] messageIds = new String[] { QueueMessage() };
                MockSQSService service = new MockSQSService();
                service.Start();
                PollForMessages(messageIds, service);

                ReceiveMessageRequest receiveRequest = new ReceiveMessageRequest();
                receiveRequest.QueueUrl = ConfigurationManager.AppSettings["QueueUrl"];
                ReceiveMessageResponse receiveResponse = client.ReceiveMessage(receiveRequest);

                Assert.AreEqual(0, receiveResponse.ReceiveMessageResult.Message.Where((m) => m.MessageId == messageIds[0]).Count());

                // set the visibility timeout back
                setRequest = new SetQueueAttributesRequest();
                setRequest.QueueUrl = ConfigurationManager.AppSettings["QueueUrl"];
                setRequest.Attribute.Add(new Amazon.SQS.Model.Attribute() { Name = "VisibilityTimeout", Value = visibilityTimeout.ToString() });
                client.SetQueueAttributes(setRequest);

                RemoveConfigValue("SQSWindow");
            }
        }
        private static bool PollForMessages(String[] messageIds, MockSQSService service)
        {
            Stopwatch sw = new Stopwatch();
            HashSet<String> messages = new HashSet<string>();
            sw.Start();
            do
            {
                for (int i = 0; i < messageIds.Length; i++)
                {
                    if (!messages.Contains(messageIds[i]))
                    {
                        Message m = FindMessage(service, messageIds[i]);
                        if (m != null)
                        {
                            messages.Add(messageIds[i]);
                        }
                    }
                }
            }
            while ((sw.Elapsed.TotalSeconds <= 60) &
                  (messages.Count != messageIds.Length));

            return messages.Count == messageIds.Length;
        }
 private static Message FindMessage(MockSQSService service, String messageId)
 {
     List<NewWindowEventArgs> windows = new List<NewWindowEventArgs>();
     windows.AddRange(service.NewWindowEventArgsReceived);
     foreach (NewWindowEventArgs args in windows)
     {
         foreach (Message m in args.Messages)
         {
             if (String.Compare(m.MessageId, messageId) == 0)
             {
                 return m;
             }
         }
     }
     return null;
 }
 public void NewWindowWithNoEvents()
 {
     MockSQSService service = new MockSQSService();
     service.Start();
     Thread.Sleep(10000);
     service.Stop();
     Assert.AreEqual(1, service.NewWindowEventArgsReceived.Count);
 }
        public void MessagesPublished()
        {
            AddConfigValue("SQSWindow", "0.00:00:15");

            // add some messages to the queue
            String[] messageIds = new String[2];
            messageIds[0] = QueueMessage();
            messageIds[1] = QueueMessage();

            MockSQSService service = new MockSQSService();
            service.Start();
            Assert.IsTrue(PollForMessages(messageIds, service));

            RemoveConfigValue("SQSWindow");
        }
        public void MessagesOrderedASC()
        {
            AddConfigValue("SQSWindow", "0.00:00:15");

            // add some messages to the queue
            String[] messageIds = new String[2];
            messageIds[0] = QueueMessage();
            messageIds[1] = QueueMessage();

            MockSQSService service = new MockSQSService();
            service.Start();
            PollForMessages(messageIds, service);
            service.Stop();

            int message1Index = -1;
            int message2Index = -1;

            int count = 0;
            foreach (NewWindowEventArgs arg in service.NewWindowEventArgsReceived)
            {
                count++;
                foreach (Message m in arg.Messages)
                {
                    count++;
                    if (m.MessageId == messageIds[0])
                    {
                        message1Index = count;
                    }
                    if (m.MessageId == messageIds[1])
                    {
                        message2Index = count;
                    }
                }
            }

            Assert.IsTrue(message1Index < message2Index);

            RemoveConfigValue("SQSWindow");
        }