azure-functions-dotnet-worker/test/TestUtility/TestUtility.cs

49 строки
1.8 KiB
C#

// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using System;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
namespace Microsoft.Azure.Functions.Tests
{
public static class TestUtility
{
public static IConfiguration GetTestConfiguration()
{
return new ConfigurationBuilder()
.AddEnvironmentVariables()
.AddTestSettings()
.Build();
}
public static IConfigurationBuilder AddTestSettings(this IConfigurationBuilder builder)
{
string configPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".azurefunctions", "appsettings.tests.json");
return builder.AddJsonFile(configPath, true);
}
public static async Task RetryAsync(Func<Task<bool>> condition, int timeout = 60 * 1000, int pollingInterval = 2 * 1000, bool throwWhenDebugging = false, Func<string> userMessageCallback = null)
{
DateTime start = DateTime.Now;
while (!await condition())
{
await Task.Delay(pollingInterval);
bool shouldThrow = !Debugger.IsAttached || (Debugger.IsAttached && throwWhenDebugging);
if (shouldThrow && (DateTime.Now - start).TotalMilliseconds > timeout)
{
string error = "Condition not reached within timeout.";
if (userMessageCallback != null)
{
error += " " + userMessageCallback();
}
throw new ApplicationException(error);
}
}
}
}
}