Skip to content

Commit

Permalink
HostFactoryResolver - Increase default timeout and add env var option (
Browse files Browse the repository at this point in the history
…#61688)

* HostFactoryResolver - Increase default timeout and add env var option

* Add GeneratePackageOnBuild on source csproj

* Add ServicingVersion property

* Update src/libraries/Microsoft.Extensions.HostFactoryResolver/tests/TopLevelStatementsTestsTimeout/TopLevelStatementsTestsTimeout.csproj
  • Loading branch information
maryamariyan committed Nov 17, 2021
1 parent 1aaf3bf commit c91170a
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 3 deletions.
Expand Up @@ -20,9 +20,25 @@ internal sealed class HostFactoryResolver
public const string BuildWebHost = nameof(BuildWebHost);
public const string CreateWebHostBuilder = nameof(CreateWebHostBuilder);
public const string CreateHostBuilder = nameof(CreateHostBuilder);
private const string TimeoutEnvironmentKey = "DOTNET_HOST_FACTORY_RESOLVER_DEFAULT_TIMEOUT_IN_SECONDS";

// The amount of time we wait for the diagnostic source events to fire
private static readonly TimeSpan s_defaultWaitTimeout = Debugger.IsAttached ? Timeout.InfiniteTimeSpan : TimeSpan.FromSeconds(5);
private static readonly TimeSpan s_defaultWaitTimeout = SetupDefaultTimout();

private static TimeSpan SetupDefaultTimout()
{
if (Debugger.IsAttached)
{
return Timeout.InfiniteTimeSpan;
}

if (uint.TryParse(Environment.GetEnvironmentVariable(TimeoutEnvironmentKey), out uint timeoutInSeconds))
{
return TimeSpan.FromSeconds((int)timeoutInSeconds);
}

return TimeSpan.FromMinutes(5);
}

public static Func<string[], TWebHost>? ResolveWebHostFactory<TWebHost>(Assembly assembly)
{
Expand Down Expand Up @@ -229,7 +245,7 @@ public object CreateHost()
// Try to set an exception if the entry point returns gracefully, this will force
// build to throw
_hostTcs.TrySetException(new InvalidOperationException("Unable to build IHost"));
_hostTcs.TrySetException(new InvalidOperationException("The entry point exited without ever building an IHost."));
}
catch (TargetInvocationException tie) when (tie.InnerException is StopTheHostException)
{
Expand Down Expand Up @@ -268,7 +284,7 @@ public object CreateHost()
// Wait before throwing an exception
if (!_hostTcs.Task.Wait(_waitTimeout))
{
throw new InvalidOperationException("Unable to build IHost");
throw new InvalidOperationException($"Timed out waiting for the entry point to build the IHost after {s_defaultWaitTimeout}. This timeout can be modified using the '{TimeoutEnvironmentKey}' environment variable.");
}
}
catch (AggregateException) when (_hostTcs.Task.IsCompleted)
Expand Down
Expand Up @@ -9,6 +9,8 @@
<IsSourcePackage>true</IsSourcePackage>
<!-- This is non-shipping package. -->
<DisablePackageBaselineValidation>true</DisablePackageBaselineValidation>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<ServicingVersion>1</ServicingVersion>
<PackageDescription>Internal package for sharing Microsoft.Extensions.Hosting.HostFactoryResolver type.</PackageDescription>
</PropertyGroup>
</Project>
Expand Up @@ -253,6 +253,16 @@ public void TopLevelStatements()
Assert.IsAssignableFrom<IServiceProvider>(factory(Array.Empty<string>()));
}

[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
public void TopLevelStatementsTestsTimeout()
{
var assembly = Assembly.Load("TopLevelStatementsTestsTimeout");
var factory = HostFactoryResolver.ResolveServiceProviderFactory(assembly, s_WaitTimeout);

Assert.NotNull(factory);
Assert.Throws<InvalidOperationException>(() => factory(Array.Empty<string>()));
}

[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
public void ApplicationNameSetFromAgrument()
{
Expand Down
Expand Up @@ -28,6 +28,7 @@
<ProjectReference Include="NoSpecialEntryPointPatternHangs\NoSpecialEntryPointPatternHangs.csproj" />
<ProjectReference Include="NoSpecialEntryPointPatternMainNoArgs\NoSpecialEntryPointPatternMainNoArgs.csproj" />
<ProjectReference Include="TopLevelStatements\TopLevelStatements.csproj" />
<ProjectReference Include="TopLevelStatementsTestsTimeout\TopLevelStatementsTestsTimeout.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
@@ -0,0 +1,10 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Threading;
using Microsoft.Extensions.Hosting;

var hostBuilder = new HostBuilder();
Thread.Sleep(TimeSpan.FromSeconds(30));
hostBuilder.Build();
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>$(NetCoreAppCurrent);net461</TargetFrameworks>
<EnableDefaultItems>true</EnableDefaultItems>
<OutputType>Exe</OutputType>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\MockHostTypes\MockHostTypes.csproj" />
</ItemGroup>

</Project>

0 comments on commit c91170a

Please sign in to comment.