Пример #1
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
            {
                queue.DeleteIfExistsAsync().Wait();
            }
        }
Пример #2
0
        public async Task QueueRegionalSASTestAsync()
        {
#if NETCORE
            //CultureInfo currentCulture = CultureInfo.CurrentCulture;
            //CultureInfo.CurrentCulture = new CultureInfo("it");
#else
            string currentPrimaryLanguage = ApplicationLanguages.PrimaryLanguageOverride;
            ApplicationLanguages.PrimaryLanguageOverride = "it";
#endif

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

            try
            {
                await queue.CreateAsync();

                string            messageContent = Guid.NewGuid().ToString();
                CloudQueueMessage message        = new CloudQueueMessage(messageContent);
                await queue.AddMessageAsync(message);

                // Prepare SAS authentication with full permissions
                string                       id          = Guid.NewGuid().ToString();
                DateTime                     start       = DateTime.UtcNow;
                DateTime                     expiry      = start.AddMinutes(30);
                QueuePermissions             permissions = new QueuePermissions();
                SharedAccessQueuePermissions queuePerm   = SharedAccessQueuePermissions.Add | SharedAccessQueuePermissions.ProcessMessages | SharedAccessQueuePermissions.Read | SharedAccessQueuePermissions.Update;
                permissions.SharedAccessPolicies.Add(id, new SharedAccessQueuePolicy()
                {
                    SharedAccessStartTime  = start,
                    SharedAccessExpiryTime = expiry,
                    Permissions            = queuePerm
                });

                await queue.SetPermissionsAsync(permissions);

                await Task.Delay(30 * 1000);

                string             sasTokenFromId   = queue.GetSharedAccessSignature(null, id);
                StorageCredentials sasCredsFromId   = new StorageCredentials(sasTokenFromId);
                CloudQueue         sasQueueFromId   = new CloudQueue(queue.Uri, sasCredsFromId);
                CloudQueueMessage  receivedMessage1 = await sasQueueFromId.PeekMessageAsync();

                Assert.AreEqual(messageContent, receivedMessage1.AsString);

                string             sasTokenFromPolicy = queue.GetSharedAccessSignature(permissions.SharedAccessPolicies[id], null);
                StorageCredentials sasCredsFromPolicy = new StorageCredentials(sasTokenFromPolicy);
                CloudQueue         sasQueueFromPolicy = new CloudQueue(queue.Uri, sasCredsFromPolicy);
                CloudQueueMessage  receivedMessage2   = await sasQueueFromPolicy.PeekMessageAsync();

                Assert.AreEqual(messageContent, receivedMessage2.AsString);
            }
            finally
            {
#if NETCORE
                //CultureInfo.CurrentCulture = currentCulture;
#else
                ApplicationLanguages.PrimaryLanguageOverride = currentPrimaryLanguage;
#endif
                queue.DeleteAsync().Wait();
            }
        }