| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. See License.txt in the project root for |
| | 3 | | // license information. |
| | 4 | |
|
| | 5 | | namespace Microsoft.Azure.Search.Tests.Utilities |
| | 6 | | { |
| | 7 | | using System; |
| | 8 | | using System.Linq; |
| | 9 | | using System.Net.Http; |
| | 10 | | using System.Threading; |
| | 11 | | using Microsoft.Azure.Test.HttpRecorder; |
| | 12 | | using Microsoft.Rest.ClientRuntime.Azure.TestFramework; |
| | 13 | |
|
| | 14 | | public static class SearchTestUtilities |
| | 15 | | { |
| 0 | 16 | | private static readonly HttpClient _httpClient = new HttpClient(); |
| | 17 | |
|
| | 18 | | public static string GenerateServiceName() |
| | 19 | | { |
| 602 | 20 | | return TestUtilities.GenerateName(prefix: "azs-"); |
| | 21 | | } |
| | 22 | |
|
| | 23 | | public static string GenerateName() |
| | 24 | | { |
| 1994 | 25 | | return TestUtilities.GenerateName(); |
| | 26 | | } |
| | 27 | |
|
| | 28 | | public static void WaitForIndexing() |
| | 29 | | { |
| 418 | 30 | | TestUtilities.Wait(TimeSpan.FromSeconds(2)); |
| 418 | 31 | | } |
| | 32 | |
|
| | 33 | | public static void WaitForSynonymMapUpdate() |
| | 34 | | { |
| 4 | 35 | | TestUtilities.Wait(TimeSpan.FromSeconds(5)); |
| 4 | 36 | | } |
| | 37 | |
|
| | 38 | | public static void WaitForServiceProvisioning() |
| | 39 | | { |
| 0 | 40 | | TestUtilities.Wait(TimeSpan.FromSeconds(10)); |
| 0 | 41 | | } |
| | 42 | |
|
| | 43 | | public static bool WaitForSearchServiceDns(string searchServiceName, TimeSpan maxDelay) |
| | 44 | | { |
| 602 | 45 | | if (HttpMockServer.Mode == HttpRecorderMode.Playback) |
| | 46 | | { |
| | 47 | | // Nothing to wait for when we're running mocked. |
| 602 | 48 | | return true; |
| | 49 | | } |
| | 50 | |
|
| 0 | 51 | | TestEnvironment testEnvironment = TestEnvironmentFactory.GetTestEnvironment(); |
| 0 | 52 | | Uri baseUri = testEnvironment.GetBaseSearchUri(searchServiceName); |
| | 53 | |
|
| 0 | 54 | | TimeSpan retryDelay = TimeSpan.FromSeconds(1); |
| | 55 | |
|
| 0 | 56 | | long maxRetries = (long)(maxDelay.TotalSeconds / retryDelay.TotalSeconds); |
| 0 | 57 | | int retries = 0; |
| | 58 | |
|
| 0 | 59 | | while (retries < maxRetries) |
| | 60 | | { |
| 0 | 61 | | if (CanResolve(baseUri)) |
| | 62 | | { |
| 0 | 63 | | return true; |
| | 64 | | } |
| | 65 | | else |
| | 66 | | { |
| 0 | 67 | | Thread.Sleep(retryDelay); |
| 0 | 68 | | retries++; |
| | 69 | | } |
| | 70 | | } |
| | 71 | |
|
| 0 | 72 | | return false; |
| | 73 | | } |
| | 74 | |
|
| | 75 | | // Workaround since the Dns class is not available in PCLs. |
| | 76 | | private static bool CanResolve(Uri url) |
| | 77 | | { |
| | 78 | | try |
| | 79 | | { |
| 0 | 80 | | return _httpClient.GetAsync(url).Wait(TimeSpan.FromSeconds(5)); |
| | 81 | | } |
| 0 | 82 | | catch (AggregateException e) |
| | 83 | | { |
| 0 | 84 | | HttpRequestException httpEx = e.InnerExceptions.FirstOrDefault() as HttpRequestException; |
| 0 | 85 | | if (httpEx != null) |
| | 86 | | { |
| 0 | 87 | | if (httpEx.InnerException != null && |
| 0 | 88 | | httpEx.InnerException.Message.Contains("could not be resolved")) |
| | 89 | | { |
| 0 | 90 | | return false; |
| | 91 | | } |
| | 92 | | } |
| | 93 | |
|
| 0 | 94 | | throw; |
| | 95 | | } |
| 0 | 96 | | } |
| | 97 | | } |
| | 98 | | } |