Exemplo n.º 1
0
        public async Task CloudQueueClearMessageAsync()
        {
            CloudQueueClient client = GenerateCloudQueueClient();
            string           name   = GenerateNewQueueName();
            CloudQueue       queue  = client.GetQueueReference(name);
            await queue.CreateAsync();

            string            msgContent = Guid.NewGuid().ToString("N");
            CloudQueueMessage message    = new CloudQueueMessage(msgContent);
            await queue.AddMessageAsync(message);

            VerifyAddMessageResult(message);
            CloudQueueMessage receivedMessage1 = await queue.PeekMessageAsync();

            Assert.IsTrue(receivedMessage1.AsString == message.AsString);
            await queue.ClearAsync();

            Assert.IsNull(await queue.PeekMessageAsync());
            await queue.DeleteAsync();
        }
Exemplo n.º 2
0
        public async Task CloudQueueAddMessageFullParameterAsync()
        {
            CloudQueueMessage futureMessage  = new CloudQueueMessage("This message is for the future.");
            CloudQueueMessage presentMessage = new CloudQueueMessage("This message is for the present.");

            CloudQueueClient client = GenerateCloudQueueClient();
            CloudQueue       queue  = client.GetQueueReference(GenerateNewQueueName());

            try
            {
                await queue.CreateIfNotExistsAsync();

                await queue.AddMessageAsync(futureMessage, null, TimeSpan.FromDays(2), null, null);

                VerifyAddMessageResult(futureMessage);

                // We should not be able to see the future message yet.
                CloudQueueMessage retrievedMessage = await queue.GetMessageAsync();

                Assert.IsNull(retrievedMessage);

                await queue.AddMessageAsync(presentMessage, null, TimeSpan.Zero, null, null);

                VerifyAddMessageResult(presentMessage);
                await queue.AddMessageAsync(presentMessage, TimeSpan.FromDays(1), null, null, null);

                VerifyAddMessageResult(presentMessage);

                // We should be able to see the present message.
                retrievedMessage = await queue.GetMessageAsync();

                Assert.IsNotNull(retrievedMessage);
                Assert.AreEqual <string>(presentMessage.AsString, retrievedMessage.AsString);

                await queue.AddMessageAsync(futureMessage, TimeSpan.FromDays(2), TimeSpan.FromDays(1), null, null);

                VerifyAddMessageResult(futureMessage);

                await queue.ClearAsync();

                // -1 seconds should set an infinite ttl
                await queue.AddMessageAsync(presentMessage, TimeSpan.FromSeconds(-1), null, null, null);

                retrievedMessage = await queue.PeekMessageAsync();

                Assert.AreEqual(DateTime.MaxValue.Year, retrievedMessage.ExpirationTime.Value.Year);

                // There should be no upper bound on ttl
                await queue.AddMessageAsync(presentMessage, TimeSpan.MaxValue, null, null, null);

                // Check other edge cases
                await queue.AddMessageAsync(presentMessage, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(0), null, null);

                await queue.AddMessageAsync(presentMessage, TimeSpan.FromSeconds(7 * 24 * 60 * 60), TimeSpan.FromSeconds(7 * 24 * 60 * 60 - 1), null, null);

                await queue.AddMessageAsync(presentMessage, TimeSpan.FromSeconds(-1), TimeSpan.FromSeconds(1), null, null);

                await TestHelper.ExpectedExceptionAsync <ArgumentException>(
                    async() => await queue.AddMessageAsync(futureMessage, TimeSpan.FromDays(1), TimeSpan.FromDays(2), null, null),
                    "Using a visibility timeout longer than the time to live should fail");

                await TestHelper.ExpectedExceptionAsync <ArgumentException>(
                    async() => await queue.AddMessageAsync(futureMessage, null, TimeSpan.FromDays(8), null, null),
                    "Using a visibility longer than the maximum visibility timeout should fail");

                await TestHelper.ExpectedExceptionAsync <ArgumentException>(
                    async() => await queue.AddMessageAsync(futureMessage, null, TimeSpan.FromMinutes(-1), null, null),
                    "Using a negative visibility should fail");

                await TestHelper.ExpectedExceptionAsync <ArgumentException>(
                    async() => await queue.AddMessageAsync(futureMessage, TimeSpan.FromMinutes(-1), null, null, null),
                    "Using a negative TTL other than -1 seconds (infinite) should fail");

                await TestHelper.ExpectedExceptionAsync <ArgumentException>(
                    () => queue.AddMessageAsync(futureMessage, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(1), null, null),
                    "Visibility timeout must be strictly less than the TTL");

                await TestHelper.ExpectedExceptionAsync <ArgumentException>(
                    () => queue.AddMessageAsync(presentMessage, null, CloudQueueMessage.MaxVisibilityTimeout, null, null),
                    "Null TTL will default to 7 days, which is the max visibility timeout. They cannot be equal.");
            }
            finally
            {
                await queue.DeleteAsync();
            }
        }