2
2
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3
3
4
4
using System ;
5
+ using System . Collections . Concurrent ;
5
6
using System . IO ;
6
7
using System . Reflection ;
7
8
using Microsoft . AspNet . Builder ;
8
9
using Microsoft . AspNet . Hosting ;
9
10
using Microsoft . AspNet . TestHost ;
10
- using Microsoft . Framework . DependencyInjection ;
11
11
using Microsoft . Dnx . Runtime ;
12
12
using Microsoft . Dnx . Runtime . Infrastructure ;
13
+ using Microsoft . Framework . DependencyInjection ;
13
14
14
15
namespace Microsoft . AspNet . Mvc . FunctionalTests
15
16
{
@@ -18,6 +19,13 @@ public static class TestHelper
18
19
// Path from Mvc\\test\\Microsoft.AspNet.Mvc.FunctionalTests
19
20
private static readonly string WebsitesDirectoryPath = Path . Combine ( ".." , "WebSites" ) ;
20
21
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
+
21
29
public static TestServer CreateServer ( Action < IApplicationBuilder > builder , string applicationWebSiteName )
22
30
{
23
31
return CreateServer ( builder , applicationWebSiteName , applicationPath : null ) ;
@@ -114,8 +122,8 @@ private static void AddTestServices(
114
122
applicationBasePath ,
115
123
applicationWebSiteName ) ;
116
124
services . AddInstance < IApplicationEnvironment > ( environment ) ;
117
- var hostingEnvironment = new HostingEnvironment ( ) ;
118
- hostingEnvironment . Initialize ( applicationBasePath , environmentName : null ) ;
125
+
126
+ var hostingEnvironment = HostingEnvironments . GetOrAdd ( applicationBasePath , CreateHostingEnvironment ) ;
119
127
services . AddInstance < IHostingEnvironment > ( hostingEnvironment ) ;
120
128
121
129
// Injecting a custom assembly provider. Overrides AddMvc() because that uses TryAdd().
@@ -139,6 +147,14 @@ private static string CalculateApplicationBasePath(
139
147
Path . Combine ( appEnvironment . ApplicationBasePath , websitePath , applicationWebSiteName ) ) ;
140
148
}
141
149
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
+
142
158
private static IAssemblyProvider CreateAssemblyProvider ( string siteName )
143
159
{
144
160
// Creates a service type that will limit MVC to only the controllers in the test site.
0 commit comments