public async Task TestListExtensions() { using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) { cancellationTokenSource.CancelAfter(TestTimeout(TimeSpan.FromSeconds(10))); using (IIdentityService service = CreateService()) { ListExtensionsApiCall apiCall = await service.PrepareListExtensionsAsync(cancellationTokenSource.Token); Tuple <HttpResponseMessage, ReadOnlyCollectionPage <Extension> > response = await apiCall.SendAsync(cancellationTokenSource.Token); Assert.IsNotNull(response); Assert.IsNotNull(response.Item2); ReadOnlyCollectionPage <Extension> extensions = response.Item2; Assert.IsNotNull(extensions); Assert.AreNotEqual(0, extensions.Count); Assert.IsFalse(extensions.CanHaveNextPage); foreach (Extension extension in extensions) { CheckExtension(extension); } } } }
public async Task TestGetExtensionSimple() { using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) { cancellationTokenSource.CancelAfter(TestTimeout(TimeSpan.FromSeconds(10))); using (IIdentityService service = CreateService()) { ReadOnlyCollectionPage <Extension> extensions = await service.ListExtensionsAsync(cancellationTokenSource.Token); Assert.IsNotNull(extensions); Assert.AreNotEqual(0, extensions.Count); Assert.IsFalse(extensions.CanHaveNextPage); foreach (Extension listedExtension in extensions) { Assert.IsNotNull(listedExtension); Assert.IsNotNull(listedExtension.Alias); Extension extension = await service.GetExtensionAsync(listedExtension.Alias, cancellationTokenSource.Token); CheckExtension(extension); Assert.AreEqual(listedExtension.Alias, extension.Alias); } } } }
public async Task ListQueuesAsyncAwait() { #region ListQueuesAsync (await) IQueueingService queueingService = new CloudQueuesProvider(identity, region, clientId, internalUrl, identityProvider); ReadOnlyCollectionPage <CloudQueue> queuesPage = await queueingService.ListQueuesAsync(null, null, true, CancellationToken.None); ReadOnlyCollection <CloudQueue> queues = await queuesPage.GetAllPagesAsync(CancellationToken.None, null); #endregion }
public async Task TestListApiVersions() { using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) { cancellationTokenSource.CancelAfter(TestTimeout(TimeSpan.FromSeconds(10))); using (IBaseIdentityService service = CreateService()) { ListApiVersionsApiCall apiCall = await service.PrepareListApiVersionsAsync(cancellationTokenSource.Token); Tuple <HttpResponseMessage, ReadOnlyCollectionPage <ApiVersion> > response = await apiCall.SendAsync(cancellationTokenSource.Token); Assert.IsNotNull(response); Assert.IsNotNull(response.Item1); ReadOnlyCollectionPage <ApiVersion> versions = response.Item2; Assert.IsNotNull(versions); Assert.AreNotEqual(0, versions.Count); Assert.IsFalse(versions.CanHaveNextPage); Assert.IsFalse(versions.Contains(null)); foreach (ApiVersion version in versions) { Assert.IsNotNull(version); Assert.IsNotNull(version.Id); Assert.IsNotNull(version.LastModified); Assert.IsFalse(version.MediaTypes.IsDefault); Assert.IsFalse(version.Links.IsDefault); Assert.IsNotNull(version.Status); Assert.AreNotEqual(0, version.MediaTypes.Length); foreach (MediaType mediaType in version.MediaTypes) { Assert.IsNotNull(mediaType); Assert.IsNotNull(mediaType.Base); Assert.IsNotNull(mediaType.Type); } Assert.AreNotEqual(0, version.Links.Length); foreach (Link link in version.Links) { Assert.IsNotNull(link); Assert.IsNotNull(link.Target); Assert.IsNotNull(link.Relation); Assert.IsTrue(link.Target.IsAbsoluteUri); } } } } }
/// <inheritdoc/> public Task <ReadOnlyCollectionPage <CloudQueue> > ListQueuesAsync(QueueName marker, int?limit, bool detailed, CancellationToken cancellationToken) { if (limit <= 0) { throw new ArgumentOutOfRangeException("limit"); } UriTemplate template = new UriTemplate("/queues?marker={marker}&limit={limit}&detailed={detailed}"); var parameters = new Dictionary <string, string> { { "detailed", detailed.ToString().ToLowerInvariant() }, }; if (marker != null) { parameters.Add("marker", marker.Value); } if (limit.HasValue) { parameters.Add("limit", limit.ToString()); } Func <Task <Tuple <IdentityToken, Uri> >, HttpWebRequest> prepareRequest = PrepareRequestAsyncFunc(HttpMethod.GET, template, parameters); Func <Task <HttpWebRequest>, Task <ListCloudQueuesResponse> > requestResource = GetResponseAsyncFunc <ListCloudQueuesResponse>(cancellationToken); Func <Task <ListCloudQueuesResponse>, ReadOnlyCollectionPage <CloudQueue> > resultSelector = task => { ReadOnlyCollectionPage <CloudQueue> page = null; if (task.Result != null && task.Result.Queues != null) { CloudQueue lastQueue = task.Result.Queues.LastOrDefault(); QueueName nextMarker = lastQueue != null ? lastQueue.Name : marker; Func <CancellationToken, Task <ReadOnlyCollectionPage <CloudQueue> > > getNextPageAsync = nextCancellationToken => ListQueuesAsync(nextMarker, limit, detailed, nextCancellationToken); page = new BasicReadOnlyCollectionPage <CloudQueue>(task.Result.Queues, getNextPageAsync); } return(page ?? ReadOnlyCollectionPage <CloudQueue> .Empty); }; return(AuthenticateServiceAsync(cancellationToken) .Select(prepareRequest) .Then(requestResource) .Select(resultSelector)); }
public async Task TestListApiVersionsSimple() { using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) { cancellationTokenSource.CancelAfter(TestTimeout(TimeSpan.FromSeconds(10))); using (IBaseIdentityService service = CreateService()) { // test using the simple extension method ReadOnlyCollectionPage <ApiVersion> versions = await service.ListApiVersionsAsync(cancellationTokenSource.Token); Assert.IsNotNull(versions); Assert.AreNotEqual(0, versions.Count); Assert.IsFalse(versions.CanHaveNextPage); Assert.IsFalse(versions.Contains(null)); } } }
public static ReadOnlyCollectionPage <T> GetNextPage <T>(this ReadOnlyCollectionPage <T> page) { if (page == null) { throw new ArgumentNullException("page"); } try { return(page.GetNextPageAsync(CancellationToken.None).Result); } catch (AggregateException ex) { ReadOnlyCollection <Exception> innerExceptions = ex.Flatten().InnerExceptions; if (innerExceptions.Count == 1) { throw innerExceptions[0]; } throw; } }
public async Task TestListTenants() { using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) { cancellationTokenSource.CancelAfter(TestTimeout(TimeSpan.FromSeconds(10))); TestCredentials credentials = Credentials; Assert.IsNotNull(credentials); AuthenticationRequest request = credentials.AuthenticationRequest; Assert.IsNotNull(request); IdentityV2AuthenticationService authenticationService = new IdentityV2AuthenticationService(CreateService(), request); using (IIdentityService service = CreateService(authenticationService)) { ListTenantsApiCall apiCall = await service.PrepareListTenantsAsync(cancellationTokenSource.Token); Tuple <HttpResponseMessage, ReadOnlyCollectionPage <Tenant> > response = await apiCall.SendAsync(cancellationTokenSource.Token); Assert.IsNotNull(response); Assert.IsNotNull(response.Item2); ReadOnlyCollectionPage <Tenant> tenants = response.Item2; Assert.IsNotNull(tenants); Assert.AreNotEqual(0, tenants.Count); Assert.IsFalse(tenants.CanHaveNextPage); foreach (Tenant tenant in tenants) { CheckTenant(tenant); } Assert.IsTrue(tenants.Any(i => i.Enabled ?? false)); } } }
/// <summary> /// Get all pages in a paginated collection. /// </summary> /// <remarks> /// If <paramref name="progress"/> is non-<see langword="null"/>, the first call to /// <see cref="IProgress{T}.Report"/> will specify the <paramref name="page"/> /// argument. After each task to obtain to the next page of results completes, /// the <see cref="IProgress{T}.Report"/> method will be called again with the /// new page of results. /// <para> /// This method determines that the end of the collection is reached when either of /// the following conditions is true. /// </para> /// <list type="bullet"> /// <item>The <see cref="ReadOnlyCollectionPage{T}.CanHaveNextPage"/> property returns <see langword="false"/>.</item> /// <item>An empty page is reached.</item> /// </list> /// </remarks> /// <typeparam name="T">The type of elements in the collection.</typeparam> /// <param name="page">The first page in the collection.</param> /// <param name="cancellationToken">The <see cref="CancellationToken"/> that the task will observe.</param> /// <param name="progress">An optional callback object to receive progress notifications. If this is <see langword="null"/>, no progress notifications are sent.</param> /// <returns> /// A <see cref="Task"/> object representing the asynchronous operation. When the operation /// completes successfully, the <see cref="Task{TResult}.Result"/> property will contain a /// read-only collection containing the complete set of results from the paginated collection. /// </returns> /// <exception cref="ArgumentNullException">If <paramref name="page"/> is <see langword="null"/>.</exception> public static Task <ReadOnlyCollection <T> > GetAllPagesAsync <T>(this ReadOnlyCollectionPage <T> page, CancellationToken cancellationToken, IProgress <ReadOnlyCollectionPage <T> > progress) { if (page == null) { throw new ArgumentNullException("page"); } if (progress != null) { progress.Report(page); } if (!page.CanHaveNextPage || page.Count == 0) { return(InternalTaskExtensions.CompletedTask <ReadOnlyCollection <T> >(page)); } TaskCompletionSource <ReadOnlyCollection <T> > taskCompletionSource = new TaskCompletionSource <ReadOnlyCollection <T> >(); List <T> result = new List <T>(page); ReadOnlyCollectionPage <T> currentPage = page; Func <Task <ReadOnlyCollectionPage <T> > > getNextPage = () => currentPage.GetNextPageAsync(cancellationToken); Task <ReadOnlyCollectionPage <T> > currentTask = getNextPage(); Action <Task <ReadOnlyCollectionPage <T> > > continuation = null; continuation = previousTask => { if (previousTask.Status != TaskStatus.RanToCompletion) { taskCompletionSource.SetFromTask(previousTask); return; } currentPage = previousTask.Result; if (currentPage == null) { // TODO: should we throw an exception instead? taskCompletionSource.SetResult(result.AsReadOnly()); return; } if (progress != null) { progress.Report(currentPage); } result.AddRange(currentPage); if (!currentPage.CanHaveNextPage || currentPage.Count == 0) { taskCompletionSource.SetResult(result.AsReadOnly()); return; } // continue with the next page currentTask = getNextPage(); // use ContinueWith since the continuation handles cancellation and faulted antecedent tasks currentTask.ContinueWith(continuation, TaskContinuationOptions.ExecuteSynchronously); }; // use ContinueWith since the continuation handles cancellation and faulted antecedent tasks currentTask.ContinueWith(continuation, TaskContinuationOptions.ExecuteSynchronously); return(taskCompletionSource.Task); }
private async Task CheckServicesAsync(ReadOnlyCollectionPage<Service> firstPage, CancellationToken cancellationToken) { ReadOnlyCollection<Service> allServices = await firstPage.GetAllPagesAsync(cancellationToken, null); Assert.IsNotNull(allServices); if (allServices.Count == 0) Assert.Inconclusive("No services were provided by the service."); foreach (Service service in allServices) { Assert.IsNotNull(service); Assert.IsNotNull(service.Id); Assert.IsFalse(service.Links.IsDefault); Assert.IsTrue(service.Links.Length > 0); Assert.IsTrue(service.Links.Any(i => "self".Equals(i.Relation, StringComparison.OrdinalIgnoreCase))); } }
private async Task CheckFlavorsAsync(ReadOnlyCollectionPage<Flavor> firstPage, CancellationToken cancellationToken) { ReadOnlyCollection<Flavor> allFlavors = await firstPage.GetAllPagesAsync(cancellationToken, null); Assert.IsNotNull(allFlavors); if (allFlavors.Count == 0) Assert.Inconclusive("No flavors were provided by the service."); foreach (Flavor flavor in allFlavors) { Assert.IsNotNull(flavor); Assert.IsNotNull(flavor.Id); //Assert.IsNotNull(flavor.Limits); Assert.IsFalse(flavor.Providers.IsDefault); Assert.IsTrue(flavor.Providers.Length > 0); foreach (FlavorProvider provider in flavor.Providers) { Assert.IsNotNull(provider.Provider); Assert.IsFalse(provider.Links.IsDefault); Assert.IsTrue(provider.Links.Length > 0); Assert.IsTrue(provider.Links.Any(i => "provider_url".Equals(i.Relation, StringComparison.OrdinalIgnoreCase))); } Assert.IsFalse(flavor.Links.IsDefault); Assert.IsTrue(flavor.Links.Length > 0); Assert.IsTrue(flavor.Links.Any(i => "self".Equals(i.Relation, StringComparison.OrdinalIgnoreCase))); } }