Skip to content

Commit 29a8fee

Browse files
committed
Cleanup use of static state
- related to aspnet#11 (1 of 4 additions) - do not use `GlobalFacebookConfiguration` - group tests that read / write `static`s (`ScopeStorage` and `ViewEngines`) together - one `[Xunit.Collection]` for all readers and writers - won't run in parallel, avoiding reads while writers have messed up the state - `IDisposable` for all test classes that don't already reliably clean up their writes - enable the subset of above tests that were disabled due to previous use of `static`s
1 parent 2d8f677 commit 29a8fee

26 files changed

+198
-49
lines changed

test/Microsoft.AspNet.Facebook.Test/FacebookRealtimeControllerTest.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Text;
88
using System.Threading.Tasks;
99
using Microsoft.AspNet.Facebook.Models;
10+
using Microsoft.AspNet.Facebook.Providers;
1011
using Microsoft.AspNet.Facebook.Realtime;
1112
using Microsoft.TestCommon;
1213

@@ -111,7 +112,13 @@ public UserRealtimeCallbackController() : this(null, null) { }
111112

112113
public UserRealtimeCallbackController(string appSecret, string verifyToken)
113114
{
114-
FacebookConfiguration.AppSecret = appSecret;
115+
// Avoid base class's fallback to static GlobalFacebookConfiguration.
116+
var config = new FacebookConfiguration();
117+
config.ClientProvider = new DefaultFacebookClientProvider(config);
118+
config.PermissionService = new DefaultFacebookPermissionService(config);
119+
config.AppSecret = appSecret;
120+
121+
FacebookConfiguration = config;
115122
_verifyToken = verifyToken;
116123
}
117124

test/Microsoft.Web.Helpers.Test/FacebookTest.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
using System.Collections.Generic;
66
using System.Web;
77
using System.Web.TestUtil;
8+
using System.Web.WebPages.Scope;
89
using Microsoft.TestCommon;
910
using Moq;
1011

1112
namespace Microsoft.Web.Helpers.Test
1213
{
13-
public class FacebookTest
14+
[Xunit.Collection("Uses ScopeStorage or ViewEngines.Engines")]
15+
public class FacebookTest : IDisposable
1416
{
1517
public FacebookTest()
1618
{
@@ -267,5 +269,12 @@ private static HttpContextBase CreateHttpContext(IDictionary<string, string> coo
267269

268270
return context.Object;
269271
}
272+
273+
public void Dispose()
274+
{
275+
// Reset ScopeStorage (written via e.g. Facebook.AppId) between tests to avoid unexpected interactions.
276+
ScopeStorage.CurrentProvider = new StaticScopeStorageProvider();
277+
ScopeStorage.GlobalScope.Clear();
278+
}
270279
}
271280
}

test/Microsoft.Web.Helpers.Test/LinkShareTest.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111

1212
namespace Microsoft.Web.Helpers.Test
1313
{
14-
public class LinkShareTest
14+
[Xunit.Collection("Uses ScopeStorage or ViewEngines.Engines")]
15+
public class LinkShareTest : IDisposable
1516
{
1617
private static LinkShareSite[] _allLinkShareSites = new[]
1718
{
@@ -194,5 +195,12 @@ public void LinkshareRendersValidXhtml()
194195
HtmlString htmlResult = new HtmlString(result);
195196
XhtmlAssert.Validate1_0(htmlResult);
196197
}
198+
199+
public void Dispose()
200+
{
201+
// Reset ScopeStorage (written via e.g. LinkShare.BitlyApiKey) between tests to avoid unexpected interactions.
202+
ScopeStorage.CurrentProvider = new StaticScopeStorageProvider();
203+
ScopeStorage.GlobalScope.Clear();
204+
}
197205
}
198206
}

test/Microsoft.Web.Helpers.Test/ReCaptchaTest.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414

1515
namespace Microsoft.Web.Helpers.Test
1616
{
17-
public class ReCaptchaTest
17+
[Xunit.Collection("Uses ScopeStorage or ViewEngines.Engines")]
18+
public class ReCaptchaTest : IDisposable
1819
{
1920
[Fact]
2021
public void ReCaptchaOptionsMissingWhenNoOptionsAndDefaultRendering()
@@ -249,5 +250,12 @@ private static VirtualPathUtilityBase GetVirtualPathUtility()
249250

250251
return virtualPathUtility.Object;
251252
}
253+
254+
public void Dispose()
255+
{
256+
// Reset ScopeStorage (written via e.g. ReCaptcha.PrivateKey) between tests to avoid unexpected interactions.
257+
ScopeStorage.CurrentProvider = new StaticScopeStorageProvider();
258+
ScopeStorage.GlobalScope.Clear();
259+
}
252260
}
253261
}

test/Microsoft.Web.Helpers.Test/ThemesTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public void ThemesImplUsesScopeStorageToStoreCurrentTheme()
145145
var scope = new ScopeStorageDictionary();
146146
var themesImpl = new ThemesImplementation(GetVirtualPathProvider(themeDirectory, new Dir(defaultTheme), new Dir("custom-theme-dir")), scope);
147147

148-
// Act
148+
// Act
149149
themesImpl.Initialize(themeDirectory, defaultTheme);
150150
themesImpl.CurrentTheme = currentThemeDir;
151151

test/System.Web.Helpers.Test/WebMailTest.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111

1212
namespace System.Web.Helpers.Test
1313
{
14-
public class WebMailTest
14+
[Xunit.Collection("Uses ScopeStorage or ViewEngines.Engines")]
15+
public class WebMailTest : IDisposable
1516
{
1617
const string FromAddress = "[email protected]";
1718
const string Server = "myserver.com";
@@ -149,7 +150,7 @@ public void ParseHeaderParsesStringInKeyValueFormat()
149150
// Arrange
150151
string header = "foo: bar";
151152

152-
// Act
153+
// Act
153154
string key, value;
154155

155156
// Assert
@@ -164,7 +165,7 @@ public void ParseHeaderReturnsFalseIfHeaderIsNotInCorrectFormat()
164165
// Arrange
165166
string header = "foo bar";
166167

167-
// Act
168+
// Act
168169
string key, value;
169170

170171
// Assert
@@ -376,5 +377,12 @@ public void ArgumentsToSendTakePriorityOverHeader()
376377
Assert.Equal("[email protected]", message.CC.Last().Address);
377378
Assert.Equal(MailPriority.High, message.Priority);
378379
}
380+
381+
public void Dispose()
382+
{
383+
// Reset ScopeStorage (written via e.g. WebMail.SmtpServer) between tests to avoid unexpected interactions.
384+
ScopeStorage.CurrentProvider = new StaticScopeStorageProvider();
385+
ScopeStorage.GlobalScope.Clear();
386+
}
379387
}
380388
}

test/System.Web.Mvc.Test/Ajax/Test/AjaxExtensionsTest.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@
88
using System.Threading;
99
using System.Web.Mvc.Html;
1010
using System.Web.Routing;
11+
using System.Web.WebPages.Scope;
1112
using Microsoft.TestCommon;
1213
using Microsoft.Web.UnitTestUtil;
1314
using Moq;
1415

1516
namespace System.Web.Mvc.Ajax.Test
1617
{
17-
public class AjaxExtensionsTest
18+
[Xunit.Collection("Uses ScopeStorage or ViewEngines.Engines")]
19+
public class AjaxExtensionsTest : IDisposable
1820
{
1921
// Guards
2022

@@ -1973,5 +1975,12 @@ private static AjaxHelper GetAjaxHelper(bool unobtrusiveJavaScript = false)
19731975

19741976
return new AjaxHelper(viewContext, new Mock<IViewDataContainer>().Object, routes);
19751977
}
1978+
1979+
public void Dispose()
1980+
{
1981+
// Reset ScopeStorage (written via e.g. ViewContext.UnobtrusiveJavaScriptEnabled) between tests to avoid unexpected interactions.
1982+
ScopeStorage.CurrentProvider = new StaticScopeStorageProvider();
1983+
ScopeStorage.GlobalScope.Clear();
1984+
}
19761985
}
19771986
}

test/System.Web.Mvc.Test/Html/Test/DefaultEditorTemplatesTest.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616

1717
namespace System.Web.Mvc.Html.Test
1818
{
19-
public class DefaultEditorTemplatesTest
19+
[Xunit.Collection("Uses ScopeStorage or ViewEngines.Engines")]
20+
public class DefaultEditorTemplatesTest : IDisposable
2021
{
2122
// BooleanTemplate
2223

@@ -92,7 +93,7 @@ public void BooleanTemplate_AddsHtmlAttributes(object htmlAttributes, string exp
9293

9394
[Theory]
9495
[PropertyData("BooleanTemplateHtmlAttributeData")]
95-
public void BooleanTemplate_AddsHtmlAttributesDictionary(object htmlAttributes, string expectedHtml)
96+
public void BooleanTemplate_AddsHtmlAttributesDictionary(object htmlAttributes, string expectedHtml)
9697
{
9798
var htmlHelper = MakeHtmlHelper<bool>(true);
9899
htmlHelper.ViewContext.ViewBag.htmlAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
@@ -1533,5 +1534,12 @@ private string RunWithoutViewEngine(Func<string> testCode)
15331534
}
15341535
}
15351536
}
1537+
1538+
public void Dispose()
1539+
{
1540+
// Reset ScopeStorage (written via e.g. ScopeStorage.CurrentProvider.CurrentScope) between tests to avoid unexpected interactions.
1541+
ScopeStorage.CurrentProvider = new StaticScopeStorageProvider();
1542+
ScopeStorage.GlobalScope.Clear();
1543+
}
15361544
}
15371545
}

test/System.Web.Mvc.Test/Html/Test/DisplayExtensionsTest.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace System.Web.Mvc.Html.Test
1010
{
11+
[Xunit.Collection("Uses ScopeStorage or ViewEngines.Engines")]
1112
public class DisplayExtensionsTest
1213
{
1314
[Fact]
@@ -272,7 +273,7 @@ public void DisplayFor_FindsModel()
272273
{
273274
// Act
274275
result = html.DisplayFor(m => m.Property1);
275-
}
276+
}
276277

277278
// Assert
278279
Assert.Equal("Model string", result.ToString());

test/System.Web.Mvc.Test/Html/Test/EditorExtensionsTest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace System.Web.Mvc.Html.Test
1010
{
11+
[Xunit.Collection("Uses ScopeStorage or ViewEngines.Engines")]
1112
public class EditorExtensionsTest
1213
{
1314
[Fact]

0 commit comments

Comments
 (0)