Skip to content

Commit

Permalink
fix stalling on PagedCollection, when value is empty but nextLink is …
Browse files Browse the repository at this point in the history
…valid (#999)

* fix stalling when values is empty but nextLink is valid

* comments
  • Loading branch information
weidongxu-microsoft authored Mar 17, 2020
1 parent d44b63d commit 53fd15e
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 3 deletions.
62 changes: 62 additions & 0 deletions Tests/Fluent.Tests/PagedCollectionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using Microsoft.Azure.KeyVault.Models;
using Microsoft.Azure.Management.ResourceManager.Fluent.Core;
using Microsoft.Rest.Azure;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Xunit;

namespace Fluent.Tests
{
// For serialzation to Page<T>
[JsonObject]
public class MockPage<T>
{
[JsonProperty("nextLink")]
public string NextPageLink { get; set; }

[JsonProperty("value")]
public IList<T> Items { get; set; }
}

public class PagedCollection
{
[Fact]
public void CanLoadEmptyPageWithNextLink()
{
var taskLoadPage = PagedCollection<string, string>.LoadPage(
// first page, valid nextLink
(cancellation) => Task.FromResult(ConvertToPage(new MockPage<string> { Items = new List<string> { "1", "2" }, NextPageLink = "2" })),
(nextLink, cancellation) =>
{
if (nextLink == "2")
{
// empty values, valid nextLink
return Task.FromResult(ConvertToPage(new MockPage<string> { Items = new List<string>(), NextPageLink = "3" }));
}
else if (nextLink == "3")
{
// non-empty values, null nextLink
return Task.FromResult(ConvertToPage(new MockPage<string> { Items = new List<string> { "3", "4", "5" }, NextPageLink = null }));
}
else
{
Assert.False(true);
return Task.FromResult(ConvertToPage(new MockPage<string> { Items = new List<string> (), NextPageLink = null }));
}
},
(str) => str, true, CancellationToken.None);

Assert.Equal(new List<string> { "1", "2", "3", "4", "5" }, taskLoadPage.Result);
}

private static IPage<T> ConvertToPage<T>(MockPage<T> mockPage)
{
return Microsoft.Rest.Serialization.SafeJsonConvert.DeserializeObject<Page<T>>(Microsoft.Rest.Serialization.SafeJsonConvert.SerializeObject(mockPage));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,14 @@ IEnumerator IEnumerable.GetEnumerator()
private async Task AddCollection(IPage<InnerResourceT> currentPage, PagedCollection<IFluentResourceT, InnerResourceT> pagedCollection,
CancellationToken cancellationToken)
{
if (currentPage != null && currentPage.Any())
if (currentPage != null)
{
pagedCollection.NextPageLink = currentPage.NextPageLink;
var resources = await Task.WhenAll(currentPage.Select(async (inner) => await this.WrapModelAsyncDelegate(inner, cancellationToken)));
((List<IFluentResourceT>)pagedCollection.innerCollection).AddRange(resources);
if (currentPage.Any())
{
var resources = await Task.WhenAll(currentPage.Select(async (inner) => await this.WrapModelAsyncDelegate(inner, cancellationToken)));
((List<IFluentResourceT>)pagedCollection.innerCollection).AddRange(resources);
}
}
}

Expand Down

0 comments on commit 53fd15e

Please sign in to comment.