Skip to content

Commit

Permalink
Fix Search live tests (Azure#11139)
Browse files Browse the repository at this point in the history
* Fix Search live tests

* Check indexer status before querying

* Add cancellation option to DelayAsync
  • Loading branch information
heaths authored Apr 8, 2020
1 parent bf4524d commit 95a8893
Show file tree
Hide file tree
Showing 5 changed files with 498 additions and 147 deletions.
76 changes: 54 additions & 22 deletions sdk/search/Azure.Search.Documents/tests/SearchServiceClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Azure.Core;
using Azure.Core.Pipeline;
Expand Down Expand Up @@ -214,10 +215,7 @@ public async Task CreateAzureBlobIndexer()

DataSource actualSource = await serviceClient.CreateDataSourceAsync(
dataSource,
new SearchRequestOptions
{
ClientRequestId = Recording.Random.NewGuid(),
});
GetOptions());

SearchIndexer indexer = new SearchIndexer(
Recording.Random.GetName(8),
Expand All @@ -226,10 +224,7 @@ public async Task CreateAzureBlobIndexer()

SearchIndexer actualIndexer = await serviceClient.CreateIndexerAsync(
indexer,
new SearchRequestOptions
{
ClientRequestId = Recording.Random.NewGuid(),
});
GetOptions());

// Update the indexer.
actualIndexer.Description = "Updated description";
Expand All @@ -239,33 +234,70 @@ await serviceClient.CreateOrUpdateIndexerAsync(
{
IfMatch = new ETag(actualIndexer.ETag),
},
new SearchRequestOptions
{
ClientRequestId = Recording.Random.NewGuid(),
});
GetOptions());

await WaitForIndexingAsync(serviceClient, actualIndexer.Name);

// Run the indexer.
await serviceClient.RunIndexerAsync(
indexer.Name,
new SearchRequestOptions
{
ClientRequestId = Recording.Random.NewGuid(),
});
GetOptions());

// Indexers may take longer than indexing documents uploaded to the Search service.
await DelayAsync(TimeSpan.FromSeconds(5));
await WaitForIndexingAsync(serviceClient, actualIndexer.Name);

// Query the index.
SearchIndexClient indexClient = serviceClient.GetSearchIndexClient(
resources.IndexName);

long count = await indexClient.GetDocumentCountAsync(
new SearchRequestOptions
{
ClientRequestId = Recording.Random.NewGuid(),
});
GetOptions());

Assert.AreEqual(SearchResources.TestDocuments.Length, count);
}

/// <summary>
/// Gets a new <see cref="SearchRequestOptions"/>.
/// </summary>
/// <returns>
/// A new <see cref="SearchRequestOptions"/> with a new <see cref="SearchRequestOptions.ClientRequestId"/>.
/// </returns>
private SearchRequestOptions GetOptions() => new SearchRequestOptions
{
ClientRequestId = Recording.Random.NewGuid(),
};

/// <summary>
/// Waits for an indexer to complete up to the given <paramref name="timeout"/>.
/// </summary>
/// <param name="client">The <see cref="SearchServiceClient"/> to use for requests.</param>
/// <param name="indexerName">The name of the <see cref="SearchIndexer"/> to check.</param>
/// <param name="timeout">The amount of time before being canceled. The default is 1 minute.</param>
/// <returns>A <see cref="Task"/> to await.</returns>
private async Task WaitForIndexingAsync(
SearchServiceClient client,
string indexerName,
TimeSpan? timeout = null)
{
TimeSpan delay = TimeSpan.FromSeconds(10);
timeout ??= TimeSpan.FromMinutes(1);

using CancellationTokenSource cts = new CancellationTokenSource(timeout.Value);

while (true)
{
await DelayAsync(delay, cancellationToken: cts.Token);

IndexerExecutionInfo status = await client.GetIndexerStatusAsync(
indexerName,
GetOptions(),
cts.Token);

if (status.Status == IndexerStatus.Running &&
status.LastResult?.Status == IndexerExecutionStatus.Success)
{
return;
}
}
}
}
}
Loading

0 comments on commit 95a8893

Please sign in to comment.