Permalink
Browse files

Merge branch 'razor-race-condition' of https://github.com/thefringeni…

…nja/Nancy into thefringeninja-razor-race-condition
  • Loading branch information...
2 parents 2ed7a04 + 03935a6 commit 17b3c0e60708a22487df091684fb8c78e6560d24 @grumpydev grumpydev committed Oct 3, 2012
@@ -1,4 +1,6 @@
-namespace Nancy.ViewEngines.Razor.Tests
+using System.Threading;
+
+namespace Nancy.ViewEngines.Razor.Tests
{
using System;
using System.Dynamic;
@@ -468,6 +470,40 @@ public void Should_use_custom_view_base_with_vb_views()
output.ShouldEqual("<h1>Hi, Nancy!</h1>");
}
+ [Fact]
+ public void should_work_on_multiple_threads()
+ {
+ // Given
+ var location = new ViewLocationResult(
+ string.Empty,
+ string.Empty,
+ "cshtml",
+ () =>
+ {
+ Thread.Sleep(500);
+ return new StringReader(@"@{var x = ""test"";}<h1>Hello Mr. @x</h1>");
+ });
+
+ var wait = new ManualResetEvent(false);
+
+ var stream = new MemoryStream();
+
+ // When
+ ThreadPool.QueueUserWorkItem(_ =>
+ {
+ var response2 = this.engine.RenderView(location, null, this.renderContext);
+ response2.Contents.Invoke(new MemoryStream());
+ wait.Set();
+ });
+ var response = this.engine.RenderView(location, null, this.renderContext);
+ response.Contents.Invoke(stream);
+
+ wait.WaitOne(1000).ShouldBeTrue();
+
+ // Then
+ stream.ShouldEqual("<h1>Hello Mr. test</h1>");
+ }
+
private static string ReadAll(Stream stream)
{
stream.Position = 0;
@@ -32,9 +32,12 @@ public string Extension
public RazorEngineHost Host { get; private set; }
/// <summary>
- /// Gets the provider that is used to generate code.
+ /// Creates the provider that is used to generate code.
/// </summary>
- public CodeDomProvider Provider { get; private set; }
+ public CodeDomProvider CreateProvider()
+ {
+ return new CSharpCodeProvider();
+ }
/// <summary>
/// Initializes a new instance of the <see cref="CSharpRazorViewRenderer"/> class.
@@ -46,8 +49,6 @@ public CSharpRazorViewRenderer()
typeof(Microsoft.CSharp.RuntimeBinder.Binder).GetAssemblyPath()
};
- this.Provider = new CSharpCodeProvider();
-
this.Host = new NancyRazorEngineHost(new CSharpRazorCodeLanguage());
this.Host.NamespaceImports.Add("Microsoft.CSharp.RuntimeBinder");
@@ -27,6 +27,6 @@ public interface IRazorViewRenderer
/// <summary>
/// Gets the provider that is used to generate code.
/// </summary>
- CodeDomProvider Provider { get; }
+ CodeDomProvider CreateProvider();
}
}
@@ -134,7 +134,7 @@ private Func<NancyRazorViewBase> GetCompiledViewFactory(string extension, TextRe
var razorResult = engine.GenerateCode(reader, sourceFileName:"placeholder");
- var viewFactory = this.GenerateRazorViewFactory(renderer.Provider, razorResult, referencingAssembly, renderer.Assemblies, passedModelType, viewLocationResult);
+ var viewFactory = this.GenerateRazorViewFactory(renderer.CreateProvider(), razorResult, referencingAssembly, renderer.Assemblies, passedModelType, viewLocationResult);
return viewFactory;
}
@@ -32,7 +32,10 @@ public string Extension
/// <summary>
/// Gets the provider that is used to generate code.
/// </summary>
- public CodeDomProvider Provider { get; private set; }
+ public CodeDomProvider CreateProvider()
+ {
+ return new VBCodeProvider();
+ }
/// <summary>
/// Initializes a new instance of the <see cref="VisualBasicRazorViewRenderer"/> class.
@@ -41,8 +44,6 @@ public VisualBasicRazorViewRenderer()
{
this.Assemblies = new List<string>();
- this.Provider = new VBCodeProvider();
-
this.Host = new NancyRazorEngineHost(new VBRazorCodeLanguage());
}

0 comments on commit 17b3c0e

Please sign in to comment.