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

Fix digital twins query pagination test #16087

Merged
merged 3 commits into from
Oct 19, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
28 changes: 25 additions & 3 deletions sdk/digitaltwins/Azure.DigitalTwins.Core/tests/QueryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Azure.Core.TestFramework;
using FluentAssertions;
Expand Down Expand Up @@ -76,6 +78,7 @@ public async Task Query_PaginationWorks()
int pageSize = 5;
string floorModelId = await GetUniqueModelIdAsync(client, TestAssetDefaults.FloorModelIdPrefix).ConfigureAwait(false);
string roomModelId = await GetUniqueModelIdAsync(client, TestAssetDefaults.RoomModelIdPrefix).ConfigureAwait(false);
TimeSpan QueryWaitTimeout = TimeSpan.FromMinutes(1); // Wait at most one minute for the created twins to become queryable

try
{
Expand All @@ -86,7 +89,7 @@ public async Task Query_PaginationWorks()
// Create a room twin, with property "IsOccupied": true
string roomTwin = TestAssetsHelper.GetRoomTwinPayload(roomModelId);

for (int i = 0; i < pageSize + 1; i++)
for (int i = 0; i < pageSize * 2; i++)
{
string roomTwinId = await GetUniqueTwinIdAsync(client, TestAssetDefaults.RoomTwinIdPrefix).ConfigureAwait(false);
await client.CreateDigitalTwinAsync(roomTwinId, roomTwin).ConfigureAwait(false);
Expand All @@ -97,13 +100,32 @@ public async Task Query_PaginationWorks()
// act
var options = new QueryOptions();
options.MaxItemsPerPage = pageSize;
AsyncPageable<string> asyncPageableResponse = client.QueryAsync(queryString, options);

CancellationTokenSource queryTimeoutCancellationToken = new CancellationTokenSource(QueryWaitTimeout);
bool queryHasExpectedCount = false;
while (!queryHasExpectedCount)
{
if (queryTimeoutCancellationToken.IsCancellationRequested)
{
throw new AssertionException($"Timed out waiting for at least {pageSize + 1} twins to be queryable");
}

AsyncPageable<string> asyncPageableResponse = client.QueryAsync(queryString);
Copy link
Contributor

Choose a reason for hiding this comment

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

do you want to pass the cancellation token into this or the AsPages methods?

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure, just in case

int count = 0;
await foreach (Page<string> queriedTwinPage in asyncPageableResponse.AsPages())
{
count += queriedTwinPage.Values.Count;
}

// Once at least (page + 1) twins are query-able, then page size control can be tested.
queryHasExpectedCount = count >= pageSize + 1;
}

// assert
// Test that page size hint works, and that all returned pages either have the page size hint amount of
// elements, or have no continuation token (signaling that it is the last page)
int pageCount = 0;
await foreach (Page<string> page in asyncPageableResponse.AsPages())
await foreach (Page<string> page in client.QueryAsync(queryString, options).AsPages())
{
pageCount++;
if (page.ContinuationToken != null)
Expand Down
Loading