Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not pass static zero IntPtr to epoll wait. Dispose driver in tests… #2291

Merged
merged 1 commit into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/System.Device.Gpio.Tests/GpioControllerTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protected GpioControllerTestBase(ITestOutputHelper testOutputHelper)
[InlineData(false)]
public void PinValueStaysSameAfterDispose(bool closeAsHigh)
{
var driver = GetTestDriver();
using var driver = GetTestDriver();
if (driver is SysFsDriver)
{
// This check fails on the SysFsDriver, because it always sets the value to 0 when the pin is opened (but on close, the value does stay high)
Expand Down Expand Up @@ -181,7 +181,7 @@ public void ThrowsIfWritingClosedPin()
[Trait("SkipOnTestRun", "Windows_NT")] // Currently, the Windows Driver is defaulting to InputPullDown, and it seems this cannot be changed
public void OpenPinDefaultsModeToLastMode(PinMode modeToTest)
{
var driver = GetTestDriver();
using var driver = GetTestDriver();
if (driver is SysFsDriver)
{
// See issue #1581. There seems to be a library-version issue or some other random cause for this test to act differently on different hardware.
Expand Down Expand Up @@ -230,7 +230,7 @@ void Callback(object sender, PinValueChangedEventArgs pinValueChangedEventArgs)
[Fact]
public void AddCallbackFallingEdgeNotDetectedTest()
{
var driver = GetTestDriver();
using var driver = GetTestDriver();
if (driver is SysFsDriver)
{
// This test is unreliable (flaky) with SysFs.
Expand Down Expand Up @@ -317,7 +317,7 @@ void Callback(object sender, PinValueChangedEventArgs e)
[Fact]
public void AddCallbackRemoveAllCallbackTest()
{
GpioDriver testDriver = GetTestDriver();
using GpioDriver testDriver = GetTestDriver();
// Skipping the test for now when using the SysFsDriver or the RaspberryPi3Driver given that this test is flaky for those drivers.
// Issue tracking this problem is https://github.com/dotnet/iot/issues/629
if (testDriver is SysFsDriver || testDriver is RaspberryPi3Driver)
Expand Down
5 changes: 1 addition & 4 deletions src/System.Device.Gpio/Interop/UnmanagedArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ internal sealed class UnmanagedArray<T> : SafeHandle
private readonly int _arrayLength;
private readonly int _typeSize;

public static readonly UnmanagedArray<T> Empty = new(0);

public UnmanagedArray(int arrayLength)
: base(IntPtr.Zero, true)
{
Expand All @@ -21,7 +19,7 @@ public UnmanagedArray(int arrayLength)
}

_arrayLength = arrayLength;
_typeSize = Marshal.SizeOf(typeof(T));
_typeSize = Marshal.SizeOf<T>();
SetHandle(Marshal.AllocHGlobal(_typeSize * arrayLength));
}

Expand Down Expand Up @@ -58,4 +56,3 @@ public static implicit operator IntPtr(UnmanagedArray<T> unmanagedArray)
return !unmanagedArray.IsInvalid ? unmanagedArray.handle : throw new InvalidOperationException("Invalid handle");
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,8 @@ private void AddPinToPoll(int pinNumber, ref int valueFileDescriptor, ref int po
}

// Ignore first time because it will always return the current state.
while (Interop.epoll_wait(pollFileDescriptor, UnmanagedArray<epoll_event>.Empty, 1, 0) == -1)
using var eventBuffer = new UnmanagedArray<epoll_event>(1);
while (Interop.epoll_wait(pollFileDescriptor, eventBuffer, 1, 0) == -1)
{
var errorCode = Marshal.GetLastWin32Error();
if (errorCode != ERROR_CODE_EINTR)
Expand All @@ -467,14 +468,15 @@ private void AddPinToPoll(int pinNumber, ref int valueFileDescriptor, ref int po
}
}

private unsafe bool WasEventDetected(int pollFileDescriptor, int valueFileDescriptor, out int pinNumber, CancellationToken cancellationToken)
private bool WasEventDetected(int pollFileDescriptor, int valueFileDescriptor, out int pinNumber, CancellationToken cancellationToken)
{
pinNumber = -1;

using var eventBuffer = new UnmanagedArray<epoll_event>(1);

while (!cancellationToken.IsCancellationRequested)
{
// Wait until something happens
using var eventBuffer = new UnmanagedArray<epoll_event>(1);
int waitResult = Interop.epoll_wait(pollFileDescriptor, eventBuffer, 1, PollingTimeout);
if (waitResult == -1)
{
Expand Down