未验证 提交 36606c5e 编写于 作者: J Jose Perez Rodriguez 提交者: GitHub

Adding TimeoutHelper in order to make sure that long running test eventually times out. (#1160)

上级 21cf4914
......@@ -387,25 +387,28 @@ namespace System.Device.Gpio.Tests
[Fact]
public void WaitForEventFallingEdgeTest()
{
using (GpioController controller = new GpioController(GetTestNumberingScheme(), GetTestDriver()))
TimeoutHelper.CompletesInTime(() =>
{
CancellationTokenSource tokenSource = new CancellationTokenSource();
controller.OpenPin(InputPin, PinMode.Input);
controller.OpenPin(OutputPin, PinMode.Output);
controller.Write(OutputPin, PinValue.Low);
Task.Run(() =>
using (GpioController controller = new GpioController(GetTestNumberingScheme(), GetTestDriver()))
{
controller.Write(OutputPin, PinValue.High);
Thread.Sleep(WaitMilliseconds);
CancellationTokenSource tokenSource = new CancellationTokenSource();
controller.OpenPin(InputPin, PinMode.Input);
controller.OpenPin(OutputPin, PinMode.Output);
controller.Write(OutputPin, PinValue.Low);
});
WaitForEventResult result = controller.WaitForEvent(InputPin, PinEventTypes.Falling, tokenSource.Token);
Task.Run(() =>
{
controller.Write(OutputPin, PinValue.High);
Thread.Sleep(WaitMilliseconds);
controller.Write(OutputPin, PinValue.Low);
});
Assert.False(result.TimedOut);
Assert.Equal(PinEventTypes.Falling, result.EventTypes);
}
WaitForEventResult result = controller.WaitForEvent(InputPin, PinEventTypes.Falling, tokenSource.Token);
Assert.False(result.TimedOut);
Assert.Equal(PinEventTypes.Falling, result.EventTypes);
}
}, TimeSpan.FromSeconds(30));
}
[Fact]
......
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information
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.");
}
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册