Esempio n. 1
0
    public void When_Deferrals_Disposed()
    {
        var deferralManager = new DeferralManager <Deferral>(h => new Deferral(h));

        var deferral1 = deferralManager.GetDeferral();
        var deferral2 = deferralManager.GetDeferral();

        bool isCompleted = false;

        deferralManager.Completed += (s, e) =>
        {
            isCompleted = true;
        };

        deferralManager.EventRaiseCompleted();

        Assert.IsFalse(isCompleted);

        deferral1.Dispose();

        Assert.IsFalse(isCompleted);

        deferral2.Dispose();

        Assert.IsTrue(isCompleted);
    }
Esempio n. 2
0
    public async Task When_Completed_Asynchronously_With_Requests()
    {
        var deferralManager = new DeferralManager <Deferral>(h => new Deferral(h));
        var deferral1       = deferralManager.GetDeferral();
        var deferral2       = deferralManager.GetDeferral();

        bool lastDeferralCompleting = false;

        deferralManager.Completed += (s, e) =>
        {
            Assert.IsTrue(lastDeferralCompleting);
            Assert.IsFalse(deferralManager.CompletedSynchronously);
        };
        var completedSynchronously = deferralManager.EventRaiseCompleted();

        Assert.IsFalse(completedSynchronously);
        Assert.IsFalse(deferralManager.CompletedSynchronously);

        await Task.Yield();

        deferral2.Complete();

        Assert.IsFalse(deferralManager.CompletedSynchronously);

        await Task.Yield();

        Assert.IsFalse(deferralManager.CompletedSynchronously);

        lastDeferralCompleting = true;
        deferral1.Complete();

        Assert.IsFalse(deferralManager.CompletedSynchronously);
    }
Esempio n. 3
0
 public void MultipleDeferralsWithOneIncomplete_PreventsCompletion()
 {
     AsyncContext.Run(async() =>
     {
         var dm        = new DeferralManager();
         var deferral1 = dm.GetDeferral();
         var deferral2 = dm.GetDeferral();
         var task      = dm.SignalAndWaitAsync();
         deferral1.Dispose();
         await AssertEx.NeverCompletesAsync(task);
     });
 }
Esempio n. 4
0
 public void MultipleDeferralsWithAllCompleted_Completes()
 {
     AsyncContext.Run(async() =>
     {
         var dm        = new DeferralManager();
         var deferral1 = dm.GetDeferral();
         var deferral2 = dm.GetDeferral();
         var task      = dm.SignalAndWaitAsync();
         deferral1.Dispose();
         deferral2.Dispose();
         await task;
     });
 }
Esempio n. 5
0
 public void TwoDeferralsWithOneCompletedTwice_PreventsCompletion()
 {
     Test.Async(async() =>
     {
         var dm        = new DeferralManager();
         var deferral1 = dm.GetDeferral();
         var deferral2 = dm.GetDeferral();
         var task      = dm.SignalAndWaitAsync();
         deferral1.Dispose();
         deferral1.Dispose();
         await AssertEx.NeverCompletesAsync(task);
     });
 }
Esempio n. 6
0
    public void When_Completed_Synchronously_With_Requests()
    {
        var deferralManager = new DeferralManager <Deferral>(h => new Deferral(h));
        var deferral1       = deferralManager.GetDeferral();
        var deferral2       = deferralManager.GetDeferral();

        deferral2.Complete();
        deferral1.Complete();
        deferralManager.Completed += (s, e) =>
        {
            Assert.IsTrue(deferralManager.CompletedSynchronously);
        };
        Assert.IsTrue(deferralManager.EventRaiseCompleted());
        Assert.IsTrue(deferralManager.CompletedSynchronously);
    }
Esempio n. 7
0
    public void When_Many_Deferrals()
    {
        var deferralManager = new DeferralManager <Deferral>(h => new Deferral(h));

        var random = new Random(42);

        var deferrals = new List <Deferral>();

        for (int i = 0; i < 20; i++)
        {
            deferrals.Add(deferralManager.GetDeferral());
        }

        bool isCompleted = false;

        deferralManager.Completed += (s, e) =>
        {
            isCompleted = true;
        };

        deferralManager.EventRaiseCompleted();

        while (deferrals.Count > 0)
        {
            Assert.IsFalse(isCompleted);
            var randomIndex    = random.Next(deferrals.Count);
            var randomDeferral = deferrals[randomIndex];
            randomDeferral.Complete();
            deferrals.RemoveAt(randomIndex);
        }

        Assert.IsTrue(isCompleted);
    }
Esempio n. 8
0
 public void IncompleteDeferral_PreventsCompletion()
 {
     AsyncContext.Run(async() =>
     {
         var dm       = new DeferralManager();
         var deferral = dm.GetDeferral();
         await AssertEx.NeverCompletesAsync(dm.SignalAndWaitAsync());
     });
 }
Esempio n. 9
0
 public SuspendingDeferral GetDeferral()
 {
     if (_deferralManager == null)
     {
         _deferralManager            = new DeferralManager <SuspendingDeferral>(h => new SuspendingDeferral(h));
         _deferralManager.Completed += (s, e) => _deferralDone?.Invoke();
     }
     return(_deferralManager.GetDeferral());
 }
Esempio n. 10
0
        public Deferral GetDeferral()
        {
            if (_deferralManager == null)
            {
                _deferralManager            = new DeferralManager <Deferral>(h => new Deferral(h));
                _deferralManager.Completed += (s, e) => _complete(this);
            }

            return(_deferralManager.GetDeferral());
        }
Esempio n. 11
0
 public void DeferralCompleted_Completes()
 {
     AsyncContext.Run(async() =>
     {
         var dm       = new DeferralManager();
         var deferral = dm.GetDeferral();
         var task     = dm.SignalAndWaitAsync();
         Assert.IsFalse(task.IsCompleted);
         deferral.Dispose();
         await task;
     });
 }
Esempio n. 12
0
    public void When_Incomplete_Deferral()
    {
        var deferralManager = new DeferralManager <Deferral>(h => new Deferral(h));

        var deferral = deferralManager.GetDeferral();

        bool testFinished = false;

        deferralManager.Completed += (s, e) =>
        {
            if (!testFinished)
            {
                Assert.Fail("Should not be called");
            }
        };

        var completedSynchronously = deferralManager.EventRaiseCompleted();

        // Set to avoid ghost assertion failure (deferral disposal will complete it).
        testFinished = true;
    }
Esempio n. 13
0
 public Deferral GetDeferral() => Manager.GetDeferral();
 public IDisposable GetDeferral() => _deferralManager.GetDeferral();
Esempio n. 15
0
 public IDisposable GetDeferral()
 {
     return(_deferrals.GetDeferral());
 }
 public IDisposable GetDeferral()
 {
     return(_deferralManager.GetDeferral());
 }