public void AllowsForGarbageCollection() { var mortal = new LifetimeSource(); var lifes = new[] { Lifetime.Immortal, Lifetime.Dead, mortal.Lifetime }; // callbacks are not kept when the lifetime is not mortal var f = ValidCallbackMaker; foreach (var life in lifes) { // pre-finished f.AssertCollectedAfter(e => Lifetime.Immortal.WhenDead(e, life)); f.AssertCollectedAfter(e => Lifetime.Dead.WhenDead(e, life)); f.AssertCollectedAfter(e => LimboedLifetime.WhenDead(e, life)); // post-finished f.AssertCollectedAfter(e => { using (var limbod = new LimboLife()) limbod.Lifetime.WhenDead(e, life); }); f.AssertCollectedAfter(e => { using (var blessed = new BlessedLife()) blessed.Lifetime.WhenDead(e, life); }); f.AssertCollectedAfter(e => { using (var doomed = new DoomedLife()) doomed.Lifetime.WhenDead(e, life); }); } // callbacks are not kept when the subscription lifetime is dead or dies f.AssertCollectedAfter(e => { using (var doomed = new DoomedLife()) mortal.Lifetime.WhenDead(e, doomed.Lifetime); }); f.AssertCollectedAfter(e => mortal.Lifetime.WhenDead(e, Lifetime.Dead)); // callbacks are kept when the lifetime is mortal and the subscription lifetime does not die f.AssertNotCollectedAfter(e => mortal.Lifetime.WhenDead(e, Lifetime.Immortal)); f.AssertNotCollectedAfter(e => mortal.Lifetime.WhenDead(e, mortal.Lifetime)); GC.KeepAlive(mortal); }
public void Status() { // status of constants Lifetime.Immortal.IsMortal.AssertIsFalse(); Lifetime.Immortal.IsImmortal.AssertIsTrue(); Lifetime.Immortal.IsDead.AssertIsFalse(); Lifetime.Dead.IsMortal.AssertIsFalse(); Lifetime.Dead.IsImmortal.AssertIsFalse(); Lifetime.Dead.IsDead.AssertIsTrue(); // state before transition var mortal = new LifetimeSource(); mortal.Lifetime.IsMortal.AssertIsTrue(); mortal.Lifetime.IsImmortal.AssertIsFalse(); mortal.Lifetime.IsDead.AssertIsFalse(); // transition to dead var doomed = new LifetimeSource(); doomed.EndLifetime(); doomed.Lifetime.IsMortal.AssertIsFalse(); doomed.Lifetime.IsImmortal.AssertIsFalse(); doomed.Lifetime.IsDead.AssertIsTrue(); // transition to immortal var blessed = new LifetimeSource(); blessed.ImmortalizeLifetime(); blessed.Lifetime.IsMortal.AssertIsFalse(); blessed.Lifetime.IsImmortal.AssertIsTrue(); blessed.Lifetime.IsDead.AssertIsFalse(); // transition to immortal via limbo var limbo = new LimboLife(); limbo.Dispose(); GC.Collect(); limbo.Lifetime.IsMortal.AssertIsFalse(); limbo.Lifetime.IsImmortal.AssertIsTrue(); limbo.Lifetime.IsDead.AssertIsFalse(); }
public void WhenSet() { // called when immortal? var blessed = new LifetimeSource(); var preBlessedLife = blessed.Lifetime.WhenDeadTask(); blessed.ImmortalizeLifetime(); var bt = new[] { Lifetime.Immortal.WhenDeadTask(), blessed.Lifetime.WhenDeadTask(), preBlessedLife }; Task.WhenAny(bt).AssertNotCompleted(); // called when dead? var doomed = new LifetimeSource(); var preDoomedLife = doomed.Lifetime.WhenDeadTask(); doomed.EndLifetime(); var dt = new[] { preDoomedLife, doomed.Lifetime.WhenDeadTask(), Lifetime.Dead.WhenDeadTask() }; Task.WhenAll(dt).AssertRanToCompletion(); // never called from limbo var limboed = new LimboLife(); var preLimboLife = limboed.Lifetime.WhenDeadTask(); limboed.Dispose(); Task.WhenAny( preLimboLife, limboed.Lifetime.WhenDeadTask() ).AssertNotCompleted(); }