Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

Commit e39c84c

Browse files
committed
Work around inotify limits with Core CLR on Linux
- #3066 - cache `HostingEnvironment` instances per application root - avoid creating a system-limited `FileSystemWatcher` per test
1 parent b574561 commit e39c84c

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

test/Microsoft.AspNet.Mvc.FunctionalTests/TestHelper.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5+
using System.Collections.Concurrent;
56
using System.IO;
67
using System.Reflection;
78
using Microsoft.AspNet.Builder;
89
using Microsoft.AspNet.Hosting;
910
using Microsoft.AspNet.TestHost;
10-
using Microsoft.Framework.DependencyInjection;
1111
using Microsoft.Dnx.Runtime;
1212
using Microsoft.Dnx.Runtime.Infrastructure;
13+
using Microsoft.Framework.DependencyInjection;
1314

1415
namespace Microsoft.AspNet.Mvc.FunctionalTests
1516
{
@@ -18,6 +19,13 @@ public static class TestHelper
1819
// Path from Mvc\\test\\Microsoft.AspNet.Mvc.FunctionalTests
1920
private static readonly string WebsitesDirectoryPath = Path.Combine("..", "WebSites");
2021

22+
// Minimize PhysicalFileProviders allocated. Otherwise tests create far more than would ever be needed in
23+
// production. On some platforms this aberration causes IOExceptions and irrelevant test failures. One example
24+
// is Core CLR on Linux, where the functional tests exceed the default fs.inotify.max_user_instances and
25+
// fs.inotify.max_user_watches.
26+
private static readonly ConcurrentDictionary<string, HostingEnvironment> HostingEnvironments =
27+
new ConcurrentDictionary<string, HostingEnvironment>(StringComparer.Ordinal);
28+
2129
public static TestServer CreateServer(Action<IApplicationBuilder> builder, string applicationWebSiteName)
2230
{
2331
return CreateServer(builder, applicationWebSiteName, applicationPath: null);
@@ -114,8 +122,8 @@ private static void AddTestServices(
114122
applicationBasePath,
115123
applicationWebSiteName);
116124
services.AddInstance<IApplicationEnvironment>(environment);
117-
var hostingEnvironment = new HostingEnvironment();
118-
hostingEnvironment.Initialize(applicationBasePath, environmentName: null);
125+
126+
var hostingEnvironment = HostingEnvironments.GetOrAdd(applicationBasePath, CreateHostingEnvironment);
119127
services.AddInstance<IHostingEnvironment>(hostingEnvironment);
120128

121129
// Injecting a custom assembly provider. Overrides AddMvc() because that uses TryAdd().
@@ -139,6 +147,14 @@ private static string CalculateApplicationBasePath(
139147
Path.Combine(appEnvironment.ApplicationBasePath, websitePath, applicationWebSiteName));
140148
}
141149

150+
private static HostingEnvironment CreateHostingEnvironment(string applicationBasePath)
151+
{
152+
var hostingEnvironment = new HostingEnvironment();
153+
hostingEnvironment.Initialize(applicationBasePath, environmentName: null);
154+
155+
return hostingEnvironment;
156+
}
157+
142158
private static IAssemblyProvider CreateAssemblyProvider(string siteName)
143159
{
144160
// Creates a service type that will limit MVC to only the controllers in the test site.

0 commit comments

Comments
 (0)