Skip to content
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
15 changes: 14 additions & 1 deletion Terminal.Gui/Drivers/UnixHelpers/UnixIOHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,20 @@ public static bool IsInputAvailable (Pollfd [] pollMap, int timeoutMs = 0)
{
int n = poll (pollMap, (uint)pollMap.Length, timeoutMs);

return n > 0;
if (n <= 0)
{
return false;
}

foreach (Pollfd pollfd in pollMap)
{
if ((pollfd.revents & (short)Condition.PollIn) != 0)
{
return true;
}
}

return false;
}
catch
{
Expand Down
85 changes: 85 additions & 0 deletions Tests/UnitTestsParallelizable/Drivers/UnixIOHelperTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using System.Runtime.InteropServices;
using Terminal.Gui.Drivers;

namespace DriverTests;

/// <summary>
/// Tests for <see cref="UnixIOHelper"/> polling semantics on Unix-like platforms.
/// </summary>
[Collection ("Driver Tests")]
public class UnixIOHelperTests
{
[DllImport ("libc", SetLastError = true)]
private static extern int pipe (int [] pipefd);

[DllImport ("libc", SetLastError = true)]
private static extern int close (int fd);

[Fact]
// Copilot
public void IsInputAvailable_ReturnsTrue_WhenPollReportsReadableData ()
{
if (!OperatingSystem.IsLinux () && !OperatingSystem.IsMacOS () && !OperatingSystem.IsFreeBSD ())
{
return;
}

int [] pipeFds = new int [2];
Assert.Equal (0, pipe (pipeFds));

try
{
byte [] payload = [0x41];
Assert.Equal (1, UnixIOHelper.write (pipeFds [1], payload, payload.Length));

UnixIOHelper.Pollfd [] pollMap =
[
new ()
{
fd = pipeFds [0],
events = (short)UnixIOHelper.Condition.PollIn
}
];

Assert.True (UnixIOHelper.IsInputAvailable (pollMap, 0));
}
finally
{
close (pipeFds [0]);
close (pipeFds [1]);
}
}

[Fact]
// Copilot
public void IsInputAvailable_ReturnsFalse_WhenPollReportsNonReadableEvent ()
{
if (!OperatingSystem.IsLinux () && !OperatingSystem.IsMacOS () && !OperatingSystem.IsFreeBSD ())
{
return;
}

int [] pipeFds = new int [2];
Assert.Equal (0, pipe (pipeFds));

try
{
Assert.Equal (0, close (pipeFds [0]));

UnixIOHelper.Pollfd [] pollMap =
[
new ()
{
fd = pipeFds [0],
events = (short)UnixIOHelper.Condition.PollIn
}
];

Assert.False (UnixIOHelper.IsInputAvailable (pollMap, 0));
}
finally
{
close (pipeFds [1]);
}
}
}
Loading