Skip to content

Commit

Permalink
Allow tests to pass without optimizations.
Browse files Browse the repository at this point in the history
Fixes #302.
  • Loading branch information
drieseng authored and scott-xu committed Jan 15, 2022
1 parent f613624 commit 7edad9d
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 53 deletions.
14 changes: 10 additions & 4 deletions src/Ninject.Test/Integration/ConstructorArgumentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,22 @@ public void WeakConstructorArgument()

var barracks = this.kernel.Get<Barracks>();

barracks.Weapon.Should().BeOfType<Sword>();
barracks.Warrior.Weapon.Should().BeOfType<Dagger>();
barracks.Weapon.Should().BeSameAs(weakReference.Target);
barracks.Weapon = null;
// Assert in separate method to allow weapon to be finalized
AssertWeapons(barracks, weakReference);

GC.Collect();

weakReference.IsAlive.Should().BeFalse();
}

private void AssertWeapons(Barracks barracks, WeakReference weaponReference)
{
barracks.Weapon.Should().BeOfType<Sword>();
barracks.Warrior.Weapon.Should().BeOfType<Dagger>();
barracks.Weapon.Should().BeSameAs(weaponReference.Target);
barracks.Weapon = null;
}


private WeakReference Process()
{
Expand Down
16 changes: 11 additions & 5 deletions src/Ninject.Test/Integration/PropertyInjectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,8 @@ public void WeakPropertyValue()

var weakReference = this.Process();

var warrior = this.kernel.Get<FootSoldier>();

warrior.Weapon.Should().BeOfType<Sword>();
warrior.Weapon.Should().BeSameAs(weakReference.Target);
warrior.Weapon = null;
// Assert Weapon property in separate method to allow weapon to be finalized
AssertProperty(weakReference);

GC.Collect();
GC.WaitForPendingFinalizers();
Expand All @@ -94,6 +91,15 @@ public void WeakPropertyValue()
weakReference.IsAlive.Should().BeFalse();
}

private void AssertProperty(WeakReference weaponReference)
{
var warrior = this.kernel.Get<FootSoldier>();

warrior.Weapon.Should().BeOfType<Sword>();
warrior.Weapon.Should().BeSameAs(weaponReference.Target);
warrior.Weapon = null;
}

private WeakReference Process()
{
var sword = new Sword();
Expand Down
26 changes: 16 additions & 10 deletions src/Ninject.Test/Integration/ThreadScopeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,8 @@ public void InstancesActivatedWithinScopeAreDeactivatedAfterThreadIsGarbageColle
this.kernel.Bind<NotifiesWhenDisposed>().ToSelf().InThreadScope();
var cache = this.kernel.Components.Get<ICache>();

NotifiesWhenDisposed instance = null;

ThreadStart callback = () => instance = this.kernel.Get<NotifiesWhenDisposed>();

var thread = new Thread(callback);

thread.Start();
thread.Join();

thread = null;
// Use separate method to allow thread/scope to be finalized
NotifiesWhenDisposed instance = GetInstanceFromSeparateThread();

GC.Collect();
GC.WaitForPendingFinalizers();
Expand All @@ -95,5 +87,19 @@ public void InstancesActivatedWithinScopeAreDeactivatedAfterThreadIsGarbageColle
instance.Should().NotBeNull();
instance.IsDisposed.Should().BeTrue();
}

private NotifiesWhenDisposed GetInstanceFromSeparateThread()
{
NotifiesWhenDisposed instance = null;

ThreadStart callback = () => instance = this.kernel.Get<NotifiesWhenDisposed>();

var thread = new Thread(callback);

thread.Start();
thread.Join();

return instance;
}
}
}
54 changes: 35 additions & 19 deletions src/Ninject.Test/Integration/TransientScopeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,21 @@ public void InstancesAreGarbageCollectedIfAllExternalReferencesAreDropped()
{
this.kernel.Bind<IWeapon>().To<Sword>().InTransientScope();

var instance = this.kernel.Get<IWeapon>();
var reference = new WeakReference(instance);

instance = null;
// Use separate method to allow instance to be finalized
var reference = GetWeakReference<IWeapon>();

GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();

reference.IsAlive.Should().BeFalse();
}

private WeakReference GetWeakReference<T>()
{
var instance = this.kernel.Get<T>();
return new WeakReference(instance);
}
}

public class WhenServiceIsBoundToSelfInTransientScope : TransientScopeContext
Expand All @@ -71,22 +75,26 @@ public void InstancesAreGarbageCollectedIfAllExternalReferencesAreDropped()
{
this.kernel.Bind<Sword>().ToSelf().InTransientScope();

var instance = this.kernel.Get<Sword>();
var reference = new WeakReference(instance);

instance = null;
// Use separate method to allow instance to be finalized
var weakReference = GetWeakReference<Sword>();

GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();

reference.IsAlive.Should().BeFalse();
weakReference.IsAlive.Should().BeFalse();

var cache = this.kernel.Components.Get<ICache>();
cache.Prune();

cache.Count.Should().Be(0);
}

private WeakReference GetWeakReference<T>()
{
var instance = this.kernel.Get<T>();
return new WeakReference(instance);
}
}

public class WhenServiceIsBoundToProviderInTransientScope : TransientScopeContext
Expand All @@ -107,16 +115,20 @@ public void InstancesAreGarbageCollectedIfAllExternalReferencesAreDropped()
{
this.kernel.Bind<IWeapon>().ToProvider<SwordProvider>().InTransientScope();

var instance = this.kernel.Get<IWeapon>();
var reference = new WeakReference(instance);

instance = null;
// Use separate method to allow instance to be finalized
var weakReference = GetWeakReference<IWeapon>();

GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();

reference.IsAlive.Should().BeFalse();
weakReference.IsAlive.Should().BeFalse();
}

private WeakReference GetWeakReference<T>()
{
var instance = this.kernel.Get<IWeapon>();
return new WeakReference(instance);
}
}

Expand All @@ -138,16 +150,20 @@ public void InstancesAreGarbageCollectedIfAllExternalReferencesAreDropped()
{
this.kernel.Bind<IWeapon>().ToMethod(x => new Sword()).InTransientScope();

var instance = this.kernel.Get<IWeapon>();
var reference = new WeakReference(instance);

instance = null;
// Use separate method to allow instance to be finalized
var weakReference = GetWeakReference<IWeapon>();

GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();

reference.IsAlive.Should().BeFalse();
weakReference.IsAlive.Should().BeFalse();
}

private WeakReference GetWeakReference<T>()
{
var instance = this.kernel.Get<IWeapon>();
return new WeakReference(instance);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Ninject.Test/Ninject.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFrameworks>netcoreapp2.0;net46</TargetFrameworks>
<IsPackable>false</IsPackable>
<Optimize>true</Optimize>
<Optimize>false</Optimize>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>..\Ninject.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>
Expand Down
10 changes: 8 additions & 2 deletions src/Ninject.Test/Unit/ActivationCacheTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ public void IsDeactivatedReturnsTrueForObjectsInTheDeactivationCache()
[Fact]
public void DeadObjectsAreRemoved()
{
this.testee.AddActivatedInstance(new TestObject(42));
this.testee.AddDeactivatedInstance(new TestObject(42));
// Use separate method to allow instances to be finalized
AddActivatedAndDeactivatedInstance();

GC.Collect();
GC.WaitForPendingFinalizers();
Expand All @@ -87,5 +87,11 @@ public void ImplementationDoesNotRelyOnObjectHashCode()

isActivated.Should().BeTrue();
}

private void AddActivatedAndDeactivatedInstance()
{
this.testee.AddActivatedInstance(new TestObject(42));
this.testee.AddDeactivatedInstance(new TestObject(42));
}
}
}
25 changes: 13 additions & 12 deletions src/Ninject.Test/Unit/CachePruningTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,8 @@ public WhenPruneIsCalled()
[Fact]
public void CollectedScopeInstancesAreRemoved()
{
var sword = new Sword();
var swordWeakReference = new WeakReference(sword);
var context = CreateContextMock(new TestObject(42), this.bindingConfigurationMock.Object);
this.Remember(sword, context);

sword = null;
context = null;
// Use separate method to allow scope to be finalized
var swordWeakReference = Remember();

GC.Collect();
GC.WaitForPendingFinalizers();
Expand All @@ -54,17 +49,14 @@ public void CollectedScopeInstancesAreRemoved()
[Fact]
public void UncollectedScopeInstancesAreNotRemoved()
{
var sword = new Sword();
var swordWeakReference = new WeakReference(sword);
var context = CreateContextMock(new TestObject(42), this.bindingConfigurationMock.Object);
this.Remember(sword, context);
// Use separate method to allow scope to be finalized
var swordWeakReference = Remember();

GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();

bool swordCollected = !swordWeakReference.IsAlive;

swordCollected.Should().BeFalse();
}

Expand All @@ -75,6 +67,15 @@ private static IContext CreateContextMock(object scope, IBindingConfiguration bi
return new ContextMock(scope, bindingMock.Object, genericArguments);
}

private WeakReference Remember()
{
var sword = new Sword();
var swordWeakReference = new WeakReference(sword);
var context = CreateContextMock(new TestObject(42), this.bindingConfigurationMock.Object);
this.Remember(sword, context);
return swordWeakReference;
}

private void Remember(Sword sword, IContext context)
{
this.cache.Remember(context, new InstanceReference { Instance = sword });
Expand Down

0 comments on commit 7edad9d

Please sign in to comment.