コード例 #1
0
 private static async Task WaitForHandlerCompletion(WaitForResult waitForResult, int timeoutInMs)
 {
     await using var timer = new Timer(TimedOutCallback, waitForResult, timeoutInMs, Timeout.Infinite);
     while (!waitForResult.HasCompleted && !waitForResult.HasTimedOut)
     {
         await Task.Delay(100);
     }
 }
コード例 #2
0
        public async Task <WaitForResult> WaitFor <T>(
            Func <Task> func,
            bool assertOnTimeout = true,
            bool assertOnError   = false,
            int timeoutInMs      = 15000)
        {
            var waitForResult = new WaitForResult();

            var hook = _testContext.Hooks.SingleOrDefault(h => h is Hook <T>) as Hook <T>;

            hook.OnReceived  += (message) => { waitForResult.SetHasStarted(); };
            hook.OnProcessed += (message) => { waitForResult.SetHasCompleted(); };
            hook.OnErrored   += (ex, message) => { waitForResult.SetHasErrored(ex); };

            try
            {
                await func();
            }
            catch (Exception ex)
            {
                waitForResult.SetHasErrored(ex);
            }
            await WaitForHandlerCompletion(waitForResult, timeoutInMs);

            if (assertOnTimeout)
            {
                waitForResult.HasTimedOut.Should().Be(false, "handler should not have timed out");
            }

            if (assertOnError)
            {
                waitForResult.HasErrored.Should().Be(false, $"handler should not have failed with error '{waitForResult.LastException?.Message}'");
            }

            return(waitForResult);
        }