Skip to content

Commit

Permalink
Added unit tests for #6: Potential Memory Leak when using IInterceptor.
Browse files Browse the repository at this point in the history
  • Loading branch information
hikalkan committed Mar 22, 2017
1 parent 48b985f commit 3b24c1a
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"projects": [ "src", "test" ],
"sdk": {

"version": "1.0.0-preview2-1-003177"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,21 @@ public class BaseTestClass : IDisposable

public virtual void Dispose()
{
var type = GetType();

if (type.Namespace.StartsWith("Castle.Proxies"))
{
type = type.BaseType;
}

if (IsDisposed)
{
throw new ObjectDisposedException(GetType().FullName, "This object is already disposed!");
throw new ObjectDisposedException(type.FullName, "This object is already disposed!");
}

IsDisposed = true;

DisposeCounter.Increment(GetType());
DisposeCounter.Increment(type);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using Castle.Core;
using Castle.DynamicProxy;
using Castle.MicroKernel.Registration;
using Castle.Windsor.MsDependencyInjection.Tests.TestClasses;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -113,9 +115,58 @@ public void Should_Resolve_Same_Object_In_Same_Scope_For_Scoped_Lifestyle()
}
}

[Fact]
public void ResolvingAndDisposingWithIInterceptorShouldWork()
{
var collection = new ServiceCollection();

collection.AddScoped<MyTestClass1>();

var serviceProvider = CreateServiceProvider(collection);
var windsorContainer = serviceProvider.GetService<IWindsorContainer>();

windsorContainer.Register(Component.For<MyTestInterceptor>().LifestyleTransient());

windsorContainer.Register(Component.For<MyTestClass2>().LifestyleTransient());

windsorContainer.Register(
Component
.For<MyTestClass3>()
.Interceptors<MyTestInterceptor>()
.LifestyleTransient()
);

using (var scope = serviceProvider.CreateScope())
{
var test1 = scope.ServiceProvider.GetService<MyTestClass1>();

_disposeCounter.Get<MyTestClass1>().ShouldBe(0);
_disposeCounter.Get<MyTestClass2>().ShouldBe(0);
_disposeCounter.Get<MyTestClass3>().ShouldBe(0);

windsorContainer.Release(test1);
}

_disposeCounter.Get<MyTestClass1>().ShouldBe(1);
_disposeCounter.Get<MyTestClass2>().ShouldBe(1);
_disposeCounter.Get<MyTestClass3>().ShouldBe(1);
}

public void Dispose()
{
Assert.Null(MsLifetimeScope.Current);
}
}

public class MyTestInterceptor : IInterceptor, IDisposable
{
public void Intercept(IInvocation invocation)
{
invocation.Proceed();
}

public void Dispose()
{
}
}
}

0 comments on commit 3b24c1a

Please sign in to comment.