Example #1
0
        public void To_qd_returns_correct_input_values()
        {
            var createdDt = DateTime.UtcNow.ToUnixTime();
            var rdPolicy = new SqsRedrivePolicy
            {
                DeadLetterTargetArn = "http://test.dlq.targetarn.com",
                MaxReceiveCount = 11
            };
            var qn = new SqsQueueName(Guid.NewGuid().ToString("N"));

            var attributes = new Dictionary<string, string>
            {
                { QueueAttributeName.VisibilityTimeout, "17" },
                { QueueAttributeName.ReceiveMessageWaitTimeSeconds, "16" },
                { QueueAttributeName.CreatedTimestamp, createdDt.ToString(CultureInfo.InvariantCulture) },
                { QueueAttributeName.ApproximateNumberOfMessages, "123456789" },
                { QueueAttributeName.QueueArn, "http://test.queue.arn.com" },
                { QueueAttributeName.RedrivePolicy, rdPolicy.ToJson() }
            };

            var qd = attributes.ToQueueDefinition(qn, "testQueueUrl", disableBuffering: true);

            Assert.IsTrue(ReferenceEquals(qd.SqsQueueName, qn), "SqsQueueName");
            Assert.AreEqual(qd.QueueUrl, "testQueueUrl", "QueueUrl");
            Assert.AreEqual(qd.QueueArn, "http://test.queue.arn.com", "QueueArn");
            Assert.AreEqual(qd.ApproximateNumberOfMessages, 123456789, "ApproxNumMessages");
            Assert.AreEqual(qd.VisibilityTimeout, 17, "VisibilityTimeout");
            Assert.AreEqual(qd.ReceiveWaitTime, 16, "ReceiveWaitTime");
            Assert.AreEqual(qd.RedrivePolicy.ToJson(), rdPolicy.ToJson(), "RedrivePolicy");
            Assert.AreEqual(qd.CreatedTimestamp, createdDt, "CreatedTimestamp");
            Assert.IsTrue(qd.DisableBuffering, "DisableBuffering");
        }
        public void To_qd_returns_correct_input_values()
        {
            var createdDt = DateTime.UtcNow.ToUnixTime();
            var rdPolicy = new SqsRedrivePolicy
            {
                DeadLetterTargetArn = "http://test.dlq.targetarn.com",
                MaxReceiveCount = 11
            };
            var qn = new SqsQueueName(Guid.NewGuid().ToString("N"));

            var attributes = new Dictionary<string, string>
            {
                { QueueAttributeName.VisibilityTimeout, "17" },
                { QueueAttributeName.ReceiveMessageWaitTimeSeconds, "16" },
                { QueueAttributeName.CreatedTimestamp, createdDt.ToString(CultureInfo.InvariantCulture) },
                { QueueAttributeName.ApproximateNumberOfMessages, "123456789" },
                { QueueAttributeName.QueueArn, "http://test.queue.arn.com" },
                { QueueAttributeName.RedrivePolicy, rdPolicy.ToJson() }
            };

            var qd = attributes.ToQueueDefinition(qn, "testQueueUrl", disableBuffering: true);

            Assert.IsTrue(ReferenceEquals(qd.SqsQueueName, qn), "SqsQueueName");
            Assert.AreEqual(qd.QueueUrl, "testQueueUrl", "QueueUrl");
            Assert.AreEqual(qd.QueueArn, "http://test.queue.arn.com", "QueueArn");
            Assert.AreEqual(qd.ApproximateNumberOfMessages, 123456789, "ApproxNumMessages");
            Assert.AreEqual(qd.VisibilityTimeout, 17, "VisibilityTimeout");
            Assert.AreEqual(qd.ReceiveWaitTime, 16, "ReceiveWaitTime");
            Assert.AreEqual(qd.RedrivePolicy.ToJson(), rdPolicy.ToJson(), "RedrivePolicy");
            Assert.AreEqual(qd.CreatedTimestamp, createdDt, "CreatedTimestamp");
            Assert.IsTrue(qd.DisableBuffering, "DisableBuffering");
        }
Example #3
0
        public void polling_client_pulls_from_sqs_and_calls_callback_but_does_not_automatically_deletes_messages()
        {
            var mockSqsClient = new MockSqsClient();

            mockSqsClient.FillQueue(15);
            var pollster = new SqsPollingClient(mockSqsClient);

            Assert.AreEqual(15, mockSqsClient.Queued.Count, "queue was accessed prematurely");
            var posted    = new List <SqsMessage>();
            var queueName = new SqsQueueName("foo");

            pollster.Listen(new SqsPollingClientSettings(
                                queueName: queueName,
                                callback: posted.AddRange,
                                longPollInterval: 10.Seconds(),
                                maxNumberOfMessages: SqsUtils.MAX_NUMBER_OF_MESSAGES_TO_FETCH,
                                waitTimeOnError: 1.Seconds()));
            Assert.IsTrue(Wait.For(() => mockSqsClient.Queued.Count == 0, 10.Seconds()), "queue did not get depleted in time");
            Assert.IsTrue(
                Wait.For(() => mockSqsClient.ReceiveCalled > 0, 5.Seconds()),
                string.Format("receive called the wrong number of times: {0} != {1}", 3, mockSqsClient.ReceiveCalled)
                );
            Assert.AreEqual(15, mockSqsClient.Delivered.Count, "delivered has the wrong number of messages");
            Assert.AreNotEqual(
                mockSqsClient.Delivered.Count,
                mockSqsClient.Deleted.Count,
                "delivered and deleted don't match");
            Assert.AreEqual(
                mockSqsClient.Delivered.Count,
                posted.Count,
                "delivered and posted don't match");
        }
Example #4
0
        public void sending_a_message_to_unknown_queue_throws_an_amazon_sqs_exception()
        {
            // Setup
            var queue = new SqsQueueName("unknown-queue");

            // Act
            _client.SendMessage(queue, new XDoc("event").Attr("id", 1).ToCompactString());
        }
Example #5
0
 public IEnumerable <SqsMessageId> DeleteMessages(SqsQueueName queueName, IEnumerable <SqsMessage> messages)
 {
     foreach (var message in messages)
     {
         DeleteMessage(queueName, message.MessageReceipt);
     }
     return(new SqsMessageId[0]);
 }
Example #6
0
 //--- Methods ---
 public IEnumerable <SqsMessage> ReceiveMessages(SqsQueueName queueName, TimeSpan waitTimeSeconds, uint maxNumberOfMessages)
 {
     if (Calls < 3)
     {
         Calls++;
         Threw = true;
         throw new Exception();
     }
     Calls++;
     return(new SqsMessage[0]);
 }
Example #7
0
        public IEnumerable <SqsMessage> ReceiveMessages(SqsQueueName queueName, TimeSpan waitTimeSeconds, uint maxNumberOfMessages)
        {
            ReceiveCalled++;
            var take  = (int)Math.Min(10, maxNumberOfMessages);
            var taken = Queued.Take(take).ToArray();

            _log.DebugFormat("receive returning {0} messages", taken.Length);
            Delivered.AddRange(taken);
            Queued.RemoveRange(0, taken.Length);
            return(taken);
        }
Example #8
0
        public void Can_create_and_delete_queues()
        {
            var sqs       = new InMemorySqsClient();
            var queueName = new SqsQueueName("bar");

            sqs.CreateQueue(queueName);
            var queues = sqs.ListQueues(null);

            Assert.AreEqual(1, queues.Count());
            Assert.AreEqual("bar", queues.First());
            sqs.DeleteQueue(queueName);
            queues = sqs.ListQueues(null);
            Assert.IsFalse(queues.Any());
        }
Example #9
0
        public void Can_round_trip_message_through_queue()
        {
            var sqs       = new InMemorySqsClient();
            var queueName = new SqsQueueName("bar");

            sqs.CreateQueue(queueName);
            sqs.SendMessage(queueName, "msg body");
            var messages = sqs.ReceiveMessages(queueName, 0.Seconds(), 10);

            Assert.AreEqual(1, messages.Count());
            var received = messages.First();

            Assert.AreEqual("msg body", received.Body);
        }
Example #10
0
        public void sending_a_message_with_a_delay_works()
        {
            // Setup
            var queue = new SqsQueueName("unknown-queue");

            // Act
            for (int i = 0; i < 10; i++)
            {
                _client.SendMessage(queue, new XDoc("event")
                                    .Start("page")
                                    .Elem("page", "/a/b/c")
                                    .End().ToCompactString(), 30.Seconds());
            }
        }
Example #11
0
        public void polling_client_pulls_from_sqs_and_calls_callback()
        {
            var mockSqsClient = new MockSqsClient();

            mockSqsClient.FillQueue(15);
            var pollster = new SqsPollingClient(mockSqsClient);

            Assert.AreEqual(15, mockSqsClient.Queued.Count, "queue was accessed prematurely");
            var posted   = new List <SqsMessage>();
            var fooQueue = new SqsQueueName("foo");

            pollster.Listen(new SqsPollingClientSettings(
                                queueName: fooQueue,
                                callback: messages => {
                posted.AddRange(messages);
                foreach (var msg in messages)
                {
                    mockSqsClient.DeleteMessage(fooQueue, msg.MessageReceipt);
                }
            },
                                longPollInterval: 0.Seconds(),
                                maxNumberOfMessages: SqsUtils.MAX_NUMBER_OF_MESSAGES_TO_FETCH,
                                waitTimeOnError: 1.Seconds()));
            Assert.IsTrue(Wait.For(() => mockSqsClient.Queued.Count == 0, 10.Seconds()), "queue did not get depleted in time");
            Assert.IsTrue(
                Wait.For(() => mockSqsClient.ReceiveCalled > 0, 5.Seconds()),
                string.Format("receive called the wrong number of times: Expected {0} != {1}", 3, mockSqsClient.ReceiveCalled)
                );
            Assert.AreEqual(15, mockSqsClient.Delivered.Count, "delivered has the wrong number of messages");

            // Compare delivered and deleted
            Assert.AreEqual(mockSqsClient.Delivered.Count(), mockSqsClient.Deleted.Count(), "The count of delivered messages and deleted messages does not match and it must");
            for (var i = 0; i < mockSqsClient.Delivered.Count(); i++)
            {
                Assert.AreEqual(
                    mockSqsClient.Delivered[i].MessageReceipt,
                    mockSqsClient.Deleted[i],
                    "delivered message and deleted message don't match on index " + i);
            }
            Assert.AreEqual(mockSqsClient.Delivered.Count(), posted.Count(), "The number of delivered messages and posted messages does not match and it should");
            for (var i = 0; i < mockSqsClient.Delivered.Count(); i++)
            {
                Assert.AreEqual(
                    mockSqsClient.Delivered[i],
                    posted[i],
                    "delivered and posted don't match");
            }
        }
Example #12
0
        public void creating_a_queue_that_is_already_created_multiple_times_always_returns_http_200_responses()
        {
            // This assumes that'test-queue' is a queue that has already been created
            // And that you can create queues in SQS
            var queueName = new SqsQueueName("test-queue");

            // Create the queue for the first time
            var queue1Success = _client.CreateQueue(queueName);

            Assert.IsTrue(queue1Success, "Creating queue first time failed");

            // Try to create the queue for the second time
            var queue2Success = _client.CreateQueue(queueName);

            Assert.IsTrue(queue2Success, "Creating queue second time failed");
        }
Example #13
0
        public void To_qd_uses_defaults_without_input_values()
        {
            var attributes = new Dictionary<string, string>();
            var qn = new SqsQueueName(Guid.NewGuid().ToString("N"));
            var startedAtUtc = DateTime.UtcNow.ToUnixTime();

            var qd = attributes.ToQueueDefinition(qn, "testQueueUrl", disableBuffering: false);

            Assert.That(qd.CreatedTimestamp >= startedAtUtc && qd.CreatedTimestamp <= DateTime.UtcNow.ToUnixTime(), "CreatedTimestamp");
            Assert.IsTrue(ReferenceEquals(qd.SqsQueueName, qn), "SqsQueueName");
            Assert.AreEqual(qd.QueueUrl, "testQueueUrl", "QueueUrl");
            Assert.IsFalse(qd.DisableBuffering, "DisableBuffering");

            Assert.AreEqual(qd.VisibilityTimeout, SqsQueueDefinition.DefaultVisibilityTimeoutSeconds, "VisibilityTimeout");
            Assert.AreEqual(qd.ReceiveWaitTime, SqsQueueDefinition.DefaultWaitTimeSeconds, "ReceiveWaitTime");
            Assert.AreEqual(qd.ApproximateNumberOfMessages, 0, "ApproxNumMessages");
            Assert.IsNull(qd.QueueArn, "QueueArn");
            Assert.IsNull(qd.RedrivePolicy, "RedrivePolicy");
        }
Example #14
0
        public void Init()
        {
            var accountId = Environment.GetEnvironmentVariable("SQS_ACCOUNT_ID");

            if (string.IsNullOrEmpty(accountId))
            {
                accountId = "accountid";
            }
            var publicKey = Environment.GetEnvironmentVariable("AWS_ACCESS_KEY_ID");

            if (string.IsNullOrEmpty(publicKey))
            {
                publicKey = "publickey";
            }
            var privateKey = Environment.GetEnvironmentVariable("AWS_SECRET_ACCESS_KEY");

            if (string.IsNullOrEmpty("privatekey"))
            {
                privateKey = "privatekey";
            }
            _client = new SqsClient(SqsClientConfig.From(new XDoc("sqs-config")
                                                         .Elem("endpoint", "default")
                                                         .Elem("accountid", accountId)
                                                         .Elem("publickey", publicKey)
                                                         .Elem("privatekey", privateKey)));
            TEST_QUEUE = new SqsQueueName("steveb-events");

            // purge the queue
            while (true)
            {
                var messages = _client.ReceiveMessages(TEST_QUEUE, 1.Seconds(), SqsUtils.MAX_NUMBER_OF_MESSAGES_TO_FETCH);
                if (messages.None())
                {
                    break;
                }
                _client.DeleteMessages(TEST_QUEUE, messages);
            }
        }
        private bool QueueExists(SqsQueueName queueName, bool forceRecheck = false)
        {
            if (!forceRecheck && queueNameMap.ContainsKey(queueName.QueueName))
                return true;

            try
            {
                var definition = GetQueueDefinition(queueName, forceRecheck);
                return definition != null;
            }
            catch (QueueDoesNotExistException)
            {
                log.DebugFormat("SQS Queue named [{0}] does not exist", queueName);
                return false;
            }
            catch (AmazonSQSException sqsex)
            {
                if (!sqsex.Message.Contains("specified queue does not exist"))
                    throw;

                log.DebugFormat("SQS Queue named [{0}] does not exist", queueName);
                return false;
            }
        }
        private void DeleteQueue(SqsQueueName queueName, string queueUrl)
        {
            var request = new DeleteQueueRequest
            {
                QueueUrl = queueUrl
            };

            var response = SqsClient.DeleteQueue(request);

            SqsQueueDefinition qd;
            queueNameMap.TryRemove(queueName.QueueName, out qd);
        }
        private SqsQueueDefinition GetQueueDefinition(SqsQueueName queueName, string queueUrl)
        {
            var response = SqsClient.GetQueueAttributes(new GetQueueAttributesRequest
            {
                QueueUrl = queueUrl,
                AttributeNames = new List<string> {
                    "All"
                }
            });

            var qd = response.Attributes.ToQueueDefinition(queueName, queueUrl, DisableBuffering);

            queueNameMap[queueName.QueueName] = qd;

            return qd;
        }
        private SqsQueueDefinition GetQueueDefinition(SqsQueueName queueName, bool forceRecheck = false)
        {
            SqsQueueDefinition qd;

            if (!forceRecheck && queueNameMap.TryGetValue(queueName.QueueName, out qd))
                return qd;

            var queueUrl = GetQueueUrl(queueName);
            return GetQueueDefinition(queueName, queueUrl);
        }
Example #19
0
 public IEnumerable <SqsMessageId> DeleteMessages(SqsQueueName queueName, IEnumerable <SqsMessage> messages)
 {
     throw new NotImplementedException();
 }
Example #20
0
 public bool DeleteMessage(SqsQueueName queueName, SqsMessageReceipt messageReceipt)
 {
     _log.DebugFormat("deleting {0}", messageReceipt);
     Deleted.Add(messageReceipt);
     return(true);
 }
Example #21
0
 public void SendMessage(SqsQueueName queueName, string messageBody)
 {
     throw new NotImplementedException();
 }
Example #22
0
 public IEnumerable <string> SendMessages(SqsQueueName queueName, IEnumerable <string> messageBodies)
 {
     throw new NotImplementedException();
 }
        private SqsQueueDefinition CreateQueue(SqsQueueName queueName, int? visibilityTimeoutSeconds = null,
                                               int? receiveWaitTimeSeconds = null, bool? disasbleBuffering = null,
                                               SqsRedrivePolicy redrivePolicy = null)
        {
            SqsQueueDefinition queueDefinition = null;

            var request = new CreateQueueRequest
            {
                QueueName = queueName.AwsQueueName,
                Attributes = new Dictionary<string, string>
                {
                    {
                        QueueAttributeName.ReceiveMessageWaitTimeSeconds,
                        TimeSpan.FromSeconds(receiveWaitTimeSeconds ?? DefaultReceiveWaitTime)
                            .TotalSeconds
                            .ToString(CultureInfo.InvariantCulture)
                    },
                    {
                        QueueAttributeName.VisibilityTimeout,
                        TimeSpan.FromSeconds(visibilityTimeoutSeconds ?? DefaultVisibilityTimeout)
                            .TotalSeconds
                            .ToString(CultureInfo.InvariantCulture)
                    },
                    {
                        QueueAttributeName.MessageRetentionPeriod,
                        (QueueNames.IsTempQueue(queueName.QueueName)
                            ? SqsQueueDefinition.DefaultTempQueueRetentionSeconds
                            : SqsQueueDefinition.DefaultPermanentQueueRetentionSeconds).ToString(CultureInfo.InvariantCulture)
                    }
                }
            };

            if (redrivePolicy != null)
            {
                var json = AwsClientUtils.ToJson(redrivePolicy);
                request.Attributes.Add(QueueAttributeName.RedrivePolicy, json);
            }

            try
            {
                var createResponse = SqsClient.CreateQueue(request);

                // Note - must go fetch the attributes from the server after creation, as the request attributes do not include
                // anything assigned by the server (i.e. the ARN, etc.).
                queueDefinition = GetQueueDefinition(queueName, createResponse.QueueUrl);

                queueDefinition.DisableBuffering = disasbleBuffering ?? DisableBuffering;

                queueNameMap[queueDefinition.QueueName] = queueDefinition;
            }
            catch (QueueNameExistsException)
            {   // Queue exists with different attributes, instead of creating, alter those attributes to match what was requested
                queueDefinition = UpdateQueue(queueName, request.ToSetAttributesRequest(null), disasbleBuffering);
            }

            return queueDefinition;
        }
Example #24
0
 public bool DeleteMessage(SqsQueueName queueName, SqsMessageReceipt messageReceipt)
 {
     throw new NotImplementedException();
 }
        private SqsQueueDefinition UpdateQueue(SqsQueueName sqsQueueName, SetQueueAttributesRequest request,
                                               bool? disasbleBuffering = null)
        {
            if (string.IsNullOrEmpty(request.QueueUrl))
            {
                request.QueueUrl = GetQueueUrl(sqsQueueName);
            }

            var response = SqsClient.SetQueueAttributes(request);

            // Note - must go fetch the attributes from the server after creation, as the request attributes do not include
            // anything assigned by the server (i.e. the ARN, etc.).
            var queueDefinition = GetQueueDefinition(sqsQueueName, request.QueueUrl);

            queueDefinition.DisableBuffering = disasbleBuffering ?? DisableBuffering;

            queueNameMap[queueDefinition.QueueName] = queueDefinition;

            return queueDefinition;
        }
        public void To_qd_uses_defaults_without_input_values()
        {
            var attributes = new Dictionary<string, string>();
            var qn = new SqsQueueName(Guid.NewGuid().ToString("N"));
            var startedAtUtc = DateTime.UtcNow.ToUnixTime();

            var qd = attributes.ToQueueDefinition(qn, "testQueueUrl", disableBuffering: false);

            Assert.That(qd.CreatedTimestamp >= startedAtUtc && qd.CreatedTimestamp <= DateTime.UtcNow.ToUnixTime(), "CreatedTimestamp");
            Assert.IsTrue(ReferenceEquals(qd.SqsQueueName, qn), "SqsQueueName");
            Assert.AreEqual(qd.QueueUrl, "testQueueUrl", "QueueUrl");
            Assert.IsFalse(qd.DisableBuffering, "DisableBuffering");

            Assert.AreEqual(qd.VisibilityTimeout, SqsQueueDefinition.DefaultVisibilityTimeoutSeconds, "VisibilityTimeout");
            Assert.AreEqual(qd.ReceiveWaitTime, SqsQueueDefinition.DefaultWaitTimeSeconds, "ReceiveWaitTime");
            Assert.AreEqual(qd.ApproximateNumberOfMessages, 0, "ApproxNumMessages");
            Assert.IsNull(qd.QueueArn, "QueueArn");
            Assert.IsNull(qd.RedrivePolicy, "RedrivePolicy");
        }
        private string GetQueueUrl(SqsQueueName queueName, bool forceRecheck = false)
        {
            SqsQueueDefinition qd = null;

            if (!forceRecheck && queueNameMap.TryGetValue(queueName.QueueName, out qd))
            {
                if (!string.IsNullOrEmpty(qd.QueueUrl))
                    return qd.QueueUrl;
            }

            var response = SqsClient.GetQueueUrl(queueName.AwsQueueName);
            return response.QueueUrl;
        }
Example #28
0
 public bool DeleteQueue(SqsQueueName queueName)
 {
     throw new NotImplementedException();
 }