Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,26 @@ public void SerialStreamTransport_Stream_WhenNotConnected_ThrowsTransportNotConn
Assert.Contains("COM1", ex.Message);
}

[Fact]
public void SerialStreamTransport_ApplyOperationalTimeouts_BoundsBothDirections()
{
// #399: only ReadTimeout used to be lowered after open, so WriteTimeout kept the connect
// timeout for the life of the port. SerialPort.Write takes no CancellationToken, so a
// device that stops draining its receive buffer parks the write for that whole duration
// with nothing able to interrupt it. Both directions must end up bounded and short.
using var port = new SerialPort("COM1")
{
ReadTimeout = 30000,
WriteTimeout = 30000
};

SerialStreamTransport.ApplyOperationalTimeouts(port);

Assert.Equal(500, port.ReadTimeout);
Assert.Equal(2000, port.WriteTimeout);
Assert.NotEqual(SerialPort.InfiniteTimeout, port.WriteTimeout);
}

[Fact]
public void SerialStreamTransport_SetSerialPortForTesting_TakesOwnershipAndDisposesPreviousPort()
{
Expand Down
52 changes: 52 additions & 0 deletions src/Daqifi.Core.Tests/Device/SdCard/SdCardFileReceiverTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,27 @@ await Assert.ThrowsAnyAsync<OperationCanceledException>(
cancellationToken: cts.Token));
}

[Fact]
public async Task ReceiveAsync_WhenCancelledAndStreamIgnoresTheToken_ReportsCancellationNotTimeout()
{
// #399: System.IO.Ports' SerialStream ignores the token once a read is in flight, and on a
// device that is sending nothing it just returns 0 bytes when its (500 ms) ReadTimeout
// elapses. The loop translated that into "transport stream closed" — a timeout — even when
// the caller had already cancelled, which is precisely the case where a consumer's stall
// watchdog wants to see its own cancellation come back. Cancellation has to win.
using var sourceStream = new TokenIgnoringSilentStream();
using var destinationStream = new MemoryStream();
var receiver = new SdCardFileReceiver(sourceStream);
using var cts = new CancellationTokenSource();
await cts.CancelAsync();

await Assert.ThrowsAnyAsync<OperationCanceledException>(
() => receiver.ReceiveAsync(
destinationStream, "test.bin",
timeout: TimeSpan.FromMinutes(5),
cancellationToken: cts.Token));
}

[Fact]
public async Task ReceiveAsync_ProgressReporting_BytesReceivedIncreases()
{
Expand Down Expand Up @@ -376,6 +397,37 @@ public override void Flush() { }
public override void Write(byte[] buffer, int offset, int count) => throw new NotSupportedException();
}

/// <summary>
/// A silent device: every read returns 0 bytes (a serial read timeout) and the cancellation
/// token is accepted and then ignored, exactly as System.IO.Ports' SerialStream behaves for a
/// read already in flight.
/// </summary>
private sealed class TokenIgnoringSilentStream : Stream
{
public override bool CanRead => true;
public override bool CanSeek => false;
public override bool CanWrite => false;
public override long Length => throw new NotSupportedException();
public override long Position
{
get => throw new NotSupportedException();
set => throw new NotSupportedException();
}

public override int Read(byte[] buffer, int offset, int count) => 0;

public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
// Deliberately no ThrowIfCancellationRequested: the token is accepted, never acted on.
return Task.FromResult(0);
}

public override void Flush() { }
public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException();
public override void SetLength(long value) => throw new NotSupportedException();
public override void Write(byte[] buffer, int offset, int count) => throw new NotSupportedException();
}

/// <summary>
/// A stream that blocks on read until cancellation is requested.
/// Used to test cancellation behavior.
Expand Down
Loading