Skip to content
This repository has been archived by the owner on Apr 26, 2020. It is now read-only.

Commit

Permalink
Merge pull request #3 from thecodejunkie/registrationfix
Browse files Browse the repository at this point in the history
Sorted out registration issue
  • Loading branch information
grumpydev committed Feb 21, 2012
2 parents 00c71c2 + 0eb247d commit 7a121d0
Show file tree
Hide file tree
Showing 17 changed files with 192 additions and 121 deletions.
2 changes: 1 addition & 1 deletion .gitmodules
@@ -1,3 +1,3 @@
[submodule "dependencies/Nancy"]
path = dependencies/Nancy
url = git://github.com/NancyFx/Nancy.git
url = https://github.com/NancyFx/Nancy.git
2 changes: 1 addition & 1 deletion dependencies/Nancy
Submodule Nancy updated 318 files
@@ -0,0 +1,36 @@
namespace Nancy.Bootstrappers.Windsor.Tests
{
using BootStrappers.Windsor.Tests.Fakes;
using Bootstrapper;
using Castle.MicroKernel.Registration;
using Castle.Windsor;

public class FakeWindsorNancyBootstrapper : WindsorNancyBootstrapper
{
public IWindsorContainer Container
{
get { return this.ApplicationContainer; }
}

public bool ApplicationContainerConfigured { get; set; }

public bool RequestContainerConfigured { get; set; }

protected override void ApplicationStartup(IWindsorContainer container, IPipelines pipelines)
{
base.ApplicationStartup(container, pipelines);
this.RequestContainerConfigured = true;
}

protected override void ConfigureApplicationContainer(IWindsorContainer existingContainer)
{
this.ApplicationContainerConfigured = true;
base.ConfigureApplicationContainer(existingContainer);

existingContainer.Register(
Component.For<Foo, IFoo>(),
Component.For<FakeDependency, IDependency>()
);
}
}
}
16 changes: 16 additions & 0 deletions src/Nancy.BootStrappers.Windsor.Tests/Helpers.cs
@@ -0,0 +1,16 @@
namespace Nancy.Bootstrappers.Windsor.Tests
{
using System.IO;

public static class Helpers
{
public static string GetContentsAsString(this Response r)
{
var stream = new MemoryStream();
r.Contents.Invoke(stream);
stream.Position = 0;
var reader = new StreamReader(stream);
return reader.ReadToEnd();
}
}
}
Expand Up @@ -55,11 +55,16 @@
</ItemGroup>
<ItemGroup>
<Compile Include="..\..\dependencies\Nancy\src\Nancy.Tests\ShouldExtensions.cs">
<Link>ShouldExtensions.cs</Link>
<Link>Properties\ShouldExtensions.cs</Link>
</Compile>
<Compile Include="..\..\dependencies\Nancy\src\SharedAssemblyInfo.cs">
<Link>SharedAssemblyInfo.cs</Link>
</Compile>
<Compile Include="Fakes\FakeNancyModuleWithBasePath.cs" />
<Compile Include="Fakes\FakeNancyModuleWithDependency.cs" />
<Compile Include="WindsorNancyBootStrapperFixture.cs" />
<Compile Include="FakeWindsorNancyBootstrapper.cs" />
<Compile Include="Helpers.cs" />
<Compile Include="WindsorNancyBootstrapperFixture.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Nancy.BootStrappers.Windsor\Nancy.Bootstrappers.Windsor.csproj">
Expand All @@ -74,9 +79,7 @@
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<Folder Include="Properties\" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
@@ -1,62 +1,15 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Castle.MicroKernel.Registration;
using Castle.Windsor;
using Nancy.BootStrappers.Windsor.Tests.Fakes;
using Nancy.Bootstrapper;
using Nancy.Tests;
using Xunit;

namespace Nancy.Bootstrappers.Windsor.Tests
{
public static class Helpers
{
public static string GetContentsAsString(this Response r)
{
var stream = new MemoryStream();
r.Contents.Invoke(stream);
stream.Position = 0;
var reader = new StreamReader(stream);
return reader.ReadToEnd();
}
}


public class FakeWindsorNancyBootstrapper : WindsorNancyBootstrapper
{
public bool ApplicationContainerConfigured { get; set; }

public IWindsorContainer Container
{
get { return this.ApplicationContainer; }
}

public bool RequestContainerConfigured { get; set; }

protected override void ApplicationStartup(IWindsorContainer container, IPipelines pipelines)
{
base.ApplicationStartup(container, pipelines);

RequestContainerConfigured = true;

container.Register(
Component.For<Foo, IFoo>(),
Component.For<FakeDependency, IDependency>()
);
}

protected override void ConfigureApplicationContainer(IWindsorContainer existingContainer)
{
ApplicationContainerConfigured = true;
base.ConfigureApplicationContainer(existingContainer);
}
}

public class WindsorNancyBootstrapperFixture
{
private readonly FakeWindsorNancyBootstrapper bootstrapper;
Expand All @@ -70,26 +23,38 @@ public WindsorNancyBootstrapperFixture()
[Fact]
public void the_bootstrapper_returns_an_instance_of_INancyEngine()
{
// Given
// When
var result = this.bootstrapper.GetEngine();

// Then
result.ShouldNotBeNull();
result.ShouldBeOfType<INancyEngine>();
}

[Fact]
public void GetEngine_ConfigureApplicationContainer_Should_Be_Called()
{
// Given
// When
this.bootstrapper.GetEngine();

// Then
this.bootstrapper.ApplicationContainerConfigured.ShouldBeTrue();
}

[Fact]
public void GetAllModules_Returns_As_MultiInstance()
{
// Given
this.bootstrapper.GetEngine();
var context = new NancyContext();

// When
var output1 = this.bootstrapper.GetAllModules(context).FirstOrDefault(nm => nm.GetType() == typeof(FakeNancyModuleWithBasePath));
var output2 = this.bootstrapper.GetAllModules(context).FirstOrDefault(nm => nm.GetType() == typeof(FakeNancyModuleWithBasePath));

// Then
output1.ShouldNotBeNull();
output2.ShouldNotBeNull();
output1.ShouldNotBeSameAs(output2);
Expand All @@ -98,11 +63,15 @@ public void GetAllModules_Returns_As_MultiInstance()
[Fact]
public void GetModuleByKey_Returns_As_MultiInstance()
{
// Given
this.bootstrapper.GetEngine();
var context = new NancyContext();

// When
var output1 = this.bootstrapper.GetModuleByKey(typeof(FakeNancyModuleWithBasePath).FullName, context);
var output2 = this.bootstrapper.GetModuleByKey(typeof(FakeNancyModuleWithBasePath).FullName, context);

// Then
output1.ShouldNotBeNull();
output2.ShouldNotBeNull();
output1.ShouldNotBeSameAs(output2);
Expand All @@ -111,74 +80,96 @@ public void GetModuleByKey_Returns_As_MultiInstance()
[Fact]
public void GetEngine_Defaults_Registered_In_Container()
{
// Given
this.bootstrapper.GetEngine();
var defaults = NancyInternalConfiguration.WithOverrides(x =>
{
x.ModuleKeyGenerator = typeof(WindsorModuleKeyGenerator);
});

// When
foreach (var registration in defaults.GetTypeRegistations().Where(x => x.RegistrationType != typeof(INancyEngine)))
{
this.bootstrapper.Container.Resolve(registration.RegistrationType)
.ShouldBeOfType(registration.ImplementationType);
}
// NancyEngine is being proxied by Castle to intercept the HandleRequest call

// Then
this.bootstrapper.Container.Resolve<INancyEngine>()
.GetType().Name.ShouldEqual("INancyEngineProxy");
}

[Fact]
public void can_make_request_from_module_with_no_dependency()
{
// Given
var engine = this.bootstrapper.GetEngine();

// When
var ctx = engine.HandleRequest(new Request("GET", "/fake/route/with/some/parts", "http"));

// Then
ctx.Response.StatusCode.ShouldEqual(HttpStatusCode.OK);
ctx.Dispose();
}

[Fact]
public void can_make_request_from_module_with_dependencies()
{
// Given
var engine = this.bootstrapper.GetEngine();

// When
var ctx = engine.HandleRequest(new Request("GET", "/with-dependency", "http"));

// Then
ctx.Response.StatusCode.ShouldEqual(HttpStatusCode.OK);
ctx.Dispose();
}

[Fact]
public void simultaneous_requests_use_different_module_instances()
{
// Given
var engine = this.bootstrapper.GetEngine();
var ctx1 = engine.HandleRequest(new Request("GET", "/fake/unique", "http"));
var ctx2 = engine.HandleRequest(new Request("GET", "/fake/unique", "http"));

// When
var response1 = ctx1.Response.GetContentsAsString();
var response2= ctx2.Response.GetContentsAsString();

// Then
response1.ShouldNotEqual(response2);
ctx1.Dispose();
ctx2.Dispose();
}


[Fact(Skip = "For testing memory leaks only")]
public void Check_windsor_memory_leak()
{
var engine = this.bootstrapper.GetEngine();
var ctx = engine.HandleRequest(new Request("GET", "/fake/route/with/some/parts", "http"));
ctx.Dispose();

Console.WriteLine("Start - " + GC.GetTotalMemory(false).ToString("#,###,##0") + " Bytes");
for (int i = 0; i < 1000000; i++)

for (var i = 0; i < 1000000; i++)
{
engine = this.bootstrapper.GetEngine();
ctx = engine.HandleRequest(new Request("GET", "/fake/route/with/some/parts", "http"));
ctx.Dispose();
}

Console.WriteLine("End - " + GC.GetTotalMemory(false).ToString("#,###,##0") + " Bytes");
}

[Fact(Skip = "For testing memory leaks with ASP.NET hosting only")]
public void Check_windsor_memory_leak_with_aspnet_hosting()
{
var tasks = new List<Task>(100000);

for (var i = 0; i < 100000; i++)
{
tasks.Add(Task.Factory.StartNew(() => {
Expand All @@ -190,6 +181,7 @@ public void Check_windsor_memory_leak_with_aspnet_hosting()
} catch(Exception ex) {}
}));
}

Task.WaitAll(tasks.ToArray());
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/Nancy.BootStrappers.Windsor.sln
@@ -1,13 +1,13 @@

Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nancy.BootStrappers.Windsor", "Nancy.BootStrappers.Windsor\Nancy.BootStrappers.Windsor.csproj", "{F4F084BE-E3B3-4BC3-925A-5EB4FC7FA46B}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nancy.Bootstrappers.Windsor", "Nancy.BootStrappers.Windsor\Nancy.Bootstrappers.Windsor.csproj", "{F4F084BE-E3B3-4BC3-925A-5EB4FC7FA46B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nancy.BootStrappers.Windsor.Tests", "Nancy.BootStrappers.Windsor.Tests\Nancy.BootStrappers.Windsor.Tests.csproj", "{C490CBDA-78BA-42BF-98F4-CCA06D09532D}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nancy.Bootstrappers.Windsor.Tests", "Nancy.BootStrappers.Windsor.Tests\Nancy.Bootstrappers.Windsor.Tests.csproj", "{C490CBDA-78BA-42BF-98F4-CCA06D09532D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nancy", "..\dependencies\Nancy\src\Nancy\Nancy.csproj", "{34576216-0DCA-4B0F-A0DC-9075E75A676F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebDemo", "WebDemo\WebDemo.csproj", "{E3EAC4A4-46BA-4FE6-9A2C-116A2A2CD598}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nancy.Demo.Bootstrappers.Windsor", "WebDemo\Nancy.Demo.Bootstrappers.Windsor.csproj", "{E3EAC4A4-46BA-4FE6-9A2C-116A2A2CD598}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nancy.Hosting.Aspnet", "..\dependencies\Nancy\src\Nancy.Hosting.Aspnet\Nancy.Hosting.Aspnet.csproj", "{15B7F794-0BB2-4B66-AD78-4A951F1209B2}"
EndProject
Expand Down
Expand Up @@ -8,8 +8,8 @@
<ProjectGuid>{F4F084BE-E3B3-4BC3-925A-5EB4FC7FA46B}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Nancy.BootStrappers.Windsor</RootNamespace>
<AssemblyName>Nancy.BootStrappers.Windsor</AssemblyName>
<RootNamespace>Nancy.Bootstrappers.Windsor</RootNamespace>
<AssemblyName>Nancy.Bootstrappers.Windsor</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
Expand Down

0 comments on commit 7a121d0

Please sign in to comment.