Skip to content

Commit

Permalink
Fix digital twins query pagination test (#16087)
Browse files Browse the repository at this point in the history
This test frequently failed claiming that only one page of results was returned from a query. In order to fix this, the test will now create more than just the minimum amount of queryable twins, and it will wait until at least pageSize + 1 twins can be queried before testing that we can control the page size
  • Loading branch information
timtay-microsoft authored Oct 19, 2020
1 parent 898a107 commit bbf79d1
Show file tree
Hide file tree
Showing 3 changed files with 117,624 additions and 26,834 deletions.
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, null, queryTimeoutCancellationToken.Token);
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

0 comments on commit bbf79d1

Please sign in to comment.