Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test IncrementalLoadingCollection itemsPerPage ctor param #4023

Merged
merged 1 commit into from
Aug 30, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ namespace UnitTests.UI
[TestClass]
public class Test_IncrementalLoadingCollection
{
private const int PageSize = 20;
private const int Pages = 5;

private static readonly DataSource<int>.PageOperation[] FailPassSequence
= new DataSource<int>.PageOperation[]
{
Expand All @@ -28,76 +25,83 @@ private static readonly DataSource<int>.PageOperation[] FailPassSequence
DataSource<int>.ThrowException, DataSource<int>.PassThrough,
};

private static readonly int[] AllData
= Enumerable.Range(0, Pages * PageSize).ToArray();

[DataRow]
[DataRow(2500, 1000, 1000, 1000, 1000)]
[TestMethod]
public async Task Requests(params int[] pageDelays)
[DataRow(20, 5)]
[DataRow(5, 5)]
[DataRow(20, 5, 2500, 1000, 1000, 1000, 1000)]
public async Task Requests(int pageSize, int pages, params int[] pageDelays)
{
var source = new DataSource<int>(AllData, pageDelays.Select(DataSource<int>.MakeDelayOp));
var collection = new IncrementalLoadingCollection<DataSource<int>, int>(source, PageSize);
var allData = Enumerable.Range(0, pages * pageSize).ToArray();
var source = new DataSource<int>(allData, pageDelays.Select(DataSource<int>.MakeDelayOp));
var collection = new IncrementalLoadingCollection<DataSource<int>, int>(source, pageSize);

for (int pageNum = 1; pageNum <= Pages; pageNum++)
for (var pageNum = 1; pageNum <= pages; pageNum++)
{
await collection.LoadMoreItemsAsync(0);
CollectionAssert.AreEqual(Enumerable.Range(0, PageSize * pageNum).ToArray(), collection);
CollectionAssert.AreEqual(Enumerable.Range(0, pageSize * pageNum).ToArray(), collection);
}
}

[DataRow]
[DataRow(2500, 1000, 1000, 1000, 1000)]
[TestMethod]
public async Task RequestsAsync(params int[] pageDelays)
[DataRow(20, 5)]
[DataRow(5, 5)]
[DataRow(20, 5, 2500, 1000, 1000, 1000, 1000)]
public async Task RequestsAsync(int pageSize, int pages, params int[] pageDelays)
{
var source = new DataSource<int>(AllData, pageDelays.Select(DataSource<int>.MakeDelayOp));
var collection = new IncrementalLoadingCollection<DataSource<int>, int>(source, PageSize);
var allData = Enumerable.Range(0, pages * pageSize).ToArray();
var source = new DataSource<int>(allData, pageDelays.Select(DataSource<int>.MakeDelayOp));
var collection = new IncrementalLoadingCollection<DataSource<int>, int>(source, pageSize);

var requests = new List<Task>();

for (int pageNum = 1; pageNum <= Pages; pageNum++)
for (var pageNum = 1; pageNum <= pages; pageNum++)
{
requests.Add(collection.LoadMoreItemsAsync(0).AsTask()
.ContinueWith(t => Assert.IsTrue(t.IsCompletedSuccessfully)));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we check the interim results for pageSize here as well as we're passing that into the test now?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure its that easy since these calls are happening asynchronously and we would need to test ageist collection which will be changing as these tasks complete.

}

await Task.WhenAll(requests);

CollectionAssert.AreEqual(AllData, collection);
CollectionAssert.AreEqual(allData, collection);
}

[TestMethod]
public async Task FirstRequestFails()
[DataRow(5, 5)]
[DataRow(20, 5)]
public async Task FirstRequestFails(int pageSize, int pages)
{
var source = new DataSource<int>(AllData, DataSource<int>.ThrowException);
var collection = new IncrementalLoadingCollection<DataSource<int>, int>(source, PageSize);
var allData = Enumerable.Range(0, pages * pageSize).ToArray();
var source = new DataSource<int>(allData, DataSource<int>.ThrowException);
var collection = new IncrementalLoadingCollection<DataSource<int>, int>(source, pageSize);

await Assert.ThrowsExceptionAsync<AggregateException>(collection.LoadMoreItemsAsync(0).AsTask);

Assert.IsTrue(!collection.Any());

var requests = new List<Task>();

for (int pageNum = 1; pageNum <= Pages; pageNum++)
for (var pageNum = 1; pageNum <= pages; pageNum++)
{
requests.Add(collection.LoadMoreItemsAsync(0).AsTask()
.ContinueWith(t => Assert.IsTrue(t.IsCompletedSuccessfully)));
}

await Task.WhenAll(requests);

CollectionAssert.AreEqual(AllData, collection);
CollectionAssert.AreEqual(allData, collection);
}

[TestMethod]
public async Task EveryOtherRequestFails()
[DataRow(5, 5)]
[DataRow(20, 5)]
public async Task EveryOtherRequestFails(int pageSize, int pages)
{
var source = new DataSource<int>(AllData, FailPassSequence);
var collection = new IncrementalLoadingCollection<DataSource<int>, int>(source, PageSize);
var allData = Enumerable.Range(0, pages * pageSize).ToArray();
var source = new DataSource<int>(allData, FailPassSequence);
var collection = new IncrementalLoadingCollection<DataSource<int>, int>(source, pageSize);

var willFail = true;
for (int submitedRequests = 0; submitedRequests < Pages * 2; submitedRequests++)
for (var submitedRequests = 0; submitedRequests < pages * 2; submitedRequests++)
{
if (willFail)
{
Expand All @@ -111,19 +115,22 @@ public async Task EveryOtherRequestFails()
willFail = !willFail;
}

CollectionAssert.AreEqual(AllData, collection);
CollectionAssert.AreEqual(allData, collection);
}

[TestMethod]
public async Task EveryOtherRequestFailsAsync()
[DataRow(5, 5)]
[DataRow(20, 5)]
public async Task EveryOtherRequestFailsAsync(int pageSize, int pages)
{
var source = new DataSource<int>(AllData, FailPassSequence);
var collection = new IncrementalLoadingCollection<DataSource<int>, int>(source, PageSize);
var allData = Enumerable.Range(0, pages * pageSize).ToArray();
var source = new DataSource<int>(allData, FailPassSequence);
var collection = new IncrementalLoadingCollection<DataSource<int>, int>(source, pageSize);

var requests = new List<Task>();

var willFail = true;
for (int submitedRequests = 0; submitedRequests < Pages * 2; submitedRequests++)
for (var submitedRequests = 0; submitedRequests < pages * 2; submitedRequests++)
{
if (willFail)
{
Expand All @@ -139,7 +146,7 @@ public async Task EveryOtherRequestFailsAsync()

await Task.WhenAll(requests);

CollectionAssert.AreEqual(AllData, collection);
CollectionAssert.AreEqual(allData, collection);
}
}
}