TimeoutHelper.cs 928 字节
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Threading.Tasks;

namespace System.Device.Gpio.Tests
{
    public static class TimeoutHelper
    {
        public static void CompletesInTime(Action test, TimeSpan timeout)
        {
            Task task = Task.Run(test);
            bool completedInTime = Task.WaitAll(new[] { task }, timeout);

            if (task.Exception != null)
            {
                if (task.Exception.InnerExceptions.Count == 1)
                {
                    throw task.Exception.InnerExceptions[0];
                }

                throw task.Exception;
            }

            if (!completedInTime)
            {
                throw new TimeoutException($"Test did not complete in the specified timeout: {timeout.TotalSeconds} seconds.");
            }
        }
    }
}