Skip to content

Commit

Permalink
Adding failing unit test for issue where disposable objects are not d…
Browse files Browse the repository at this point in the history
…isposed by DI when decorated.
  • Loading branch information
DanHarltey authored and khellang committed May 23, 2022
1 parent ffef7cd commit 45b6cef
Showing 1 changed file with 38 additions and 48 deletions.
86 changes: 38 additions & 48 deletions test/Scrutor.Tests/DecorationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,9 @@ public void CanDecorateMultipleLevels()

var instance = provider.GetRequiredService<IDecoratedService>();

var decorator = Assert.IsType<Decorator>(instance);
var outerDecorator = Assert.IsType<Decorator>(decorator.Inner);

Assert.IsType<Decorated>(outerDecorator.Inner);
var outerDecorator = Assert.IsType<Decorator>(instance);
var innerDecorator = Assert.IsType<Decorator>(outerDecorator.Inner);
_ = Assert.IsType<Decorated>(innerDecorator.Inner);
}

[Fact]
Expand Down Expand Up @@ -228,10 +227,10 @@ public void Issue148_Decorate_IsAbleToDecorateConcreateTypes()
Assert.NotNull(result);
var inner = Assert.IsType<DecoratedService>(result.Inner);
Assert.NotNull(inner.Dependency);
}

#region Individual functions tests

}

#region Individual functions tests

[Fact]
public void DecorationFunctionsDoDecorateRegisteredService()
{
Expand All @@ -242,9 +241,9 @@ public void DecorationFunctionsDoDecorateRegisteredService()
sc => sc.Decorate(typeof(IDecoratedService), typeof(Decorator)),
sc => sc.TryDecorate(typeof(IDecoratedService), typeof(Decorator)),
sc => sc.Decorate((IDecoratedService obj, IServiceProvider sp) => new Decorator(obj)),
sc => sc.TryDecorate((IDecoratedService obj, IServiceProvider sp) => new Decorator(obj)),
sc => sc.TryDecorate((IDecoratedService obj, IServiceProvider sp) => new Decorator(obj)),
sc => sc.Decorate((IDecoratedService obj) => new Decorator(obj)),
sc => sc.TryDecorate((IDecoratedService obj) => new Decorator(obj)),
sc => sc.TryDecorate((IDecoratedService obj) => new Decorator(obj)),
sc => sc.Decorate(typeof(IDecoratedService), (object obj, IServiceProvider sp) => new Decorator((IDecoratedService)obj)),
sc => sc.TryDecorate(typeof(IDecoratedService), (object obj, IServiceProvider sp) => new Decorator((IDecoratedService)obj)),
sc => sc.Decorate(typeof(IDecoratedService), (object obj) => new Decorator((IDecoratedService)obj)),
Expand All @@ -263,33 +262,33 @@ public void DecorationFunctionsDoDecorateRegisteredService()
var decorator = Assert.IsType<Decorator>(instance);
Assert.IsType<Decorated>(decorator.Inner);
}
}

}

[Fact]
public void DecorationFunctionsProvideScopedServiceProvider()
{
IServiceProvider actual = default;

var decorationFunctions = new Action<IServiceCollection>[]
{
sc => sc.Decorate((IDecoratedService obj, IServiceProvider sp) =>
sc => sc.Decorate((IDecoratedService obj, IServiceProvider sp) =>
{
actual = sp;
return null;
return null;
}),
sc => sc.TryDecorate((IDecoratedService obj, IServiceProvider sp) =>
{
actual = sp;
return null;
}),
return null;
}),
sc => sc.Decorate(typeof(IDecoratedService), (object obj, IServiceProvider sp) =>
{
actual = sp;
return null;
actual = sp;
return null;
}),
sc => sc.TryDecorate(typeof(IDecoratedService), (object obj, IServiceProvider sp) =>
{
actual = sp;
actual = sp;
return null;
}),
};
Expand All @@ -300,12 +299,12 @@ public void DecorationFunctionsProvideScopedServiceProvider()
{
services.AddScoped<IDecoratedService, Decorated>();
decorationMethod(services);
});

using var scope = provider.CreateScope();
var expected = scope.ServiceProvider;
_ = scope.ServiceProvider.GetService<IDecoratedService>();
Assert.Same(expected, actual);
});

using var scope = provider.CreateScope();
var expected = scope.ServiceProvider;
_ = scope.ServiceProvider.GetService<IDecoratedService>();
Assert.Same(expected, actual);
}
}

Expand All @@ -327,8 +326,8 @@ public void TryDecorateReturnsBoolResult()
{
sc => sc.TryDecorate<IDecoratedService, Decorator>(),
sc => sc.TryDecorate(typeof(IDecoratedService), typeof(Decorator)),
sc => sc.TryDecorate((IDecoratedService obj, IServiceProvider sp) => new Decorator(obj)),
sc => sc.TryDecorate((IDecoratedService obj) => new Decorator(obj)),
sc => sc.TryDecorate((IDecoratedService obj, IServiceProvider sp) => new Decorator(obj)),
sc => sc.TryDecorate((IDecoratedService obj) => new Decorator(obj)),
sc => sc.TryDecorate(typeof(IDecoratedService), (object obj, IServiceProvider sp) => new Decorator((IDecoratedService)obj)),
sc => sc.TryDecorate(typeof(IDecoratedService), (object obj) => new Decorator((IDecoratedService)obj))
};
Expand All @@ -344,14 +343,14 @@ public void TryDecorateReturnsBoolResult()
isDecorated = decorationMethod(services);
Assert.True(isDecorated);
});
});
}
}

#endregion

}

#endregion

#region DI Scope test


[Fact]
public void DecoratedTransientServiceRetainsScope()
{
Expand Down Expand Up @@ -435,12 +434,12 @@ public void DependentServicesRetainTheirOwnScope()
Assert.NotEqual(decorator1, decorator2);
Assert.NotEqual(decorator1.Inner, decorator2.Inner);
Assert.Equal(decorator1.Inner.Dependency, decorator2.Inner.Dependency);
}

}

#endregion


#region Mocks


public interface IDecoratedService { }

public class DecoratedService
Expand Down Expand Up @@ -482,12 +481,7 @@ public class Decorator : IDecoratedService
{
public Decorator(IDecoratedService inner, IService injectedService = null)
{
if (inner == null)
{
throw new ArgumentNullException(nameof(inner));
}

Inner = inner;
Inner = inner ?? throw new ArgumentNullException(nameof(inner));
InjectedService = injectedService;
}

Expand Down Expand Up @@ -524,11 +518,7 @@ public DisposableServiceDecorator(IDisposableService inner)

public bool WasDisposed { get; private set; }

public void Dispose()
{
Inner.Dispose();
WasDisposed = true;
}
public void Dispose() => WasDisposed = true;
}

public interface IEvent
Expand Down

0 comments on commit 45b6cef

Please sign in to comment.