public static void EnqueueCompletion(this UnitTestBase testBase, Func <OperationBase> func, int timeoutInSeconds)
        {
            testBase.Enqueue(() =>
            {
                var operation = func();
                if (!operation.IsComplete)
                {
                    using var semaphore = new SemaphoreSlim(0);
                    // The awaiter has some extra code to wait a bit more after Completed event
                    // so more property changes etc will have time to trigger
                    operation.Completed += (op, args) =>
                    {
                        var task = OperationExtensions.OperationAwaiter.GetCurrentExecutingTask();
                        if (task != null)
                        {
                            task.ConfigureAwait(false)
                            .GetAwaiter()
                            .UnsafeOnCompleted(() => semaphore.Release());
                        }
                        else
                        {
                            Debug.Assert(false, "no task");
                            semaphore.Release();
                        }
                    };

                    if (!semaphore.Wait(TimeSpan.FromSeconds(timeoutInSeconds)))
                    {
                        Assert.Fail(UnitTestBase.ComposeTimeoutMessage(timeoutInSeconds, string.Empty));
                    }
                }

                Assert.IsTrue(operation.IsComplete);
            });
        }
Esempio n. 2
0
        public void EnqueueConditional(Func <bool> conditionalDelegate, int timeoutInSeconds, string timeoutMessage)
        {
            DateTime endTime = DateTime.Now.AddSeconds(timeoutInSeconds);

            this.Enqueue(
                () =>
            {
                while (!conditionalDelegate())
                {
                    if (DateTime.Now >= endTime)
                    {
                        UnitTestBase.NumberOfTimeouts++;
                        Assert.Fail(UnitTestBase.ComposeTimeoutMessage(timeoutInSeconds, timeoutMessage));
                    }

                    Thread.Sleep(UnitTestBase.DefaultStepInMilliseconds);
                }
            });
        }
Esempio n. 3
0
        public virtual void EnqueueConditional(Func <bool> conditionalDelegate, int timeoutInSeconds, string timeoutMessage)
        {
            DateTime endTime = DateTime.Now.AddSeconds(timeoutInSeconds);

            base.EnqueueConditional(() =>
            {
                bool conditionSatisfied = conditionalDelegate();

                if (!conditionSatisfied)
                {
                    if (DateTime.Now >= endTime)
                    {
                        UnitTestBase.NumberOfTimeouts++;
                        Assert.Fail(UnitTestBase.ComposeTimeoutMessage(timeoutInSeconds, timeoutMessage));
                    }
                }

                return(conditionSatisfied);
            });
        }
 public static void EnqueueCompletion(this UnitTestBase testBase, Func <Task> func, int timeoutInSeconds)
 {
     testBase.Enqueue(() =>
     {
         var task = func();
         if (!task.IsCompleted)
         {
             try
             {
                 if (!task.Wait(TimeSpan.FromSeconds(timeoutInSeconds)))
                 {
                     Assert.Fail(UnitTestBase.ComposeTimeoutMessage(timeoutInSeconds, string.Empty));
                 }
             }
             catch (OperationCanceledException)
             {
                 // cancelled tasks are also completed
             }
             Assert.IsTrue(task.IsCompleted);
         }
     });
 }