Skip to content

Commit

Permalink
start on cancellationToken work
Browse files Browse the repository at this point in the history
  • Loading branch information
nietras committed Jan 21, 2025
1 parent 206384a commit 1063a20
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 34 deletions.
6 changes: 4 additions & 2 deletions src/Sep.Test/SepWriterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,14 @@ public async ValueTask SepWriterTest_EndRowWithoutNewRow_Throws()
"I.e. prefer 'using var row = writer.NewRow();'";
{
using var writer = CreateWriter();
var e = Assert.ThrowsException<InvalidOperationException>(() => writer.EndRow());
var e = Assert.ThrowsException<InvalidOperationException>(
() => writer.EndRow());
Assert.AreEqual(expected, e.Message);
}
{
await using var writer = CreateWriter();
var e = await Assert.ThrowsExceptionAsync<InvalidOperationException>(async () => await writer.EndRowAsync());
var e = await Assert.ThrowsExceptionAsync<InvalidOperationException>(
async () => await writer.EndRowAsync(default));
Assert.AreEqual(expected, e.Message);
}
}
Expand Down
30 changes: 15 additions & 15 deletions src/Sep/SepWriter.IO.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public Task FlushAsync(CancellationToken cancellationToken = default) =>
#if SYNC
internal void EndRow()
#else
internal async ValueTask EndRowAsync()
internal async ValueTask EndRowAsync(CancellationToken cancellationToken)
#endif
{
if (!_newRowActive) { SepThrow.InvalidOperationException_WriterDoesNotHaveActiveRow(); }
Expand All @@ -33,7 +33,7 @@ internal async ValueTask EndRowAsync()
#if SYNC
WriteHeader();
#else
await WriteHeaderAsync();
await WriteHeaderAsync(cancellationToken);
#endif
}
else if (_colNotSetOption == SepColNotSetOption.Throw || !_disableColCountCheck)
Expand Down Expand Up @@ -77,15 +77,15 @@ internal async ValueTask EndRowAsync()
#if SYNC
WriteEscaped(span);
#else
await WriteEscapedAsync(span);
await WriteEscapedAsync(span, cancellationToken);
#endif
}
else
{
#if SYNC
_writer.Write(span);
#else
await _writer.WriteAsync(span);
await _writer.WriteAsync(span, cancellationToken);
#endif
}
notFirst = true;
Expand All @@ -107,7 +107,7 @@ internal async ValueTask EndRowAsync()
#if SYNC
internal void WriteHeader()
#else
internal async ValueTask WriteHeaderAsync()
internal async ValueTask WriteHeaderAsync(CancellationToken cancellationToken)
#endif
{
if (_writeHeader)
Expand Down Expand Up @@ -138,8 +138,8 @@ internal async ValueTask WriteHeaderAsync()
if (_escape) { WriteEscaped(name); }
else { _writer.Write(name); }
#else
if (_escape) { await WriteEscapedAsync(name.AsMemory()); }
else { await _writer.WriteAsync(name); }
if (_escape) { await WriteEscapedAsync(name.AsMemory(), cancellationToken); }
else { await _writer.WriteAsync(name.AsMemory(), cancellationToken); }
#endif
_colNamesHeader[colIndex] = name;
notFirstHeader = true;
Expand All @@ -157,7 +157,7 @@ internal async ValueTask WriteHeaderAsync()
#if SYNC
void WriteEscaped(ReadOnlySpan<char> span)
#else
async ValueTask WriteEscapedAsync(ReadOnlyMemory<char> span)
async ValueTask WriteEscapedAsync(ReadOnlyMemory<char> span, CancellationToken cancellationToken)
#endif
{
#if SYNC
Expand All @@ -172,7 +172,7 @@ async ValueTask WriteEscapedAsync(ReadOnlyMemory<char> span)
WriteQuotesEscaped(span);
#else
await _writer.WriteAsync(SepDefaults.Quote);
await WriteQuotesEscapedAsync(span);
await WriteQuotesEscapedAsync(span, cancellationToken);
#endif
#if SYNC
_writer.Write(SepDefaults.Quote);
Expand All @@ -185,7 +185,7 @@ async ValueTask WriteEscapedAsync(ReadOnlyMemory<char> span)
#if SYNC
_writer.Write(span);
#else
await _writer.WriteAsync(span);
await _writer.WriteAsync(span, cancellationToken);
#endif
}
}
Expand All @@ -194,7 +194,7 @@ async ValueTask WriteEscapedAsync(ReadOnlyMemory<char> span)
#if SYNC
void WriteQuotesEscaped(ReadOnlySpan<char> span)
#else
async ValueTask WriteQuotesEscapedAsync(ReadOnlyMemory<char> span)
async ValueTask WriteQuotesEscapedAsync(ReadOnlyMemory<char> span, CancellationToken cancellationToken)
#endif
{
var start = 0;
Expand All @@ -211,7 +211,7 @@ async ValueTask WriteQuotesEscapedAsync(ReadOnlyMemory<char> span)
#if SYNC
_writer.Write(remainingSpan);
#else
await _writer.WriteAsync(remainingSpan);
await _writer.WriteAsync(remainingSpan, cancellationToken);
#endif
break;
}
Expand All @@ -221,7 +221,7 @@ async ValueTask WriteQuotesEscapedAsync(ReadOnlyMemory<char> span)
_writer.Write(remainingSpan.Slice(0, quoteIndex + 1));
_writer.Write(SepDefaults.Quote);
#else
await _writer.WriteAsync(remainingSpan.Slice(0, quoteIndex + 1));
await _writer.WriteAsync(remainingSpan.Slice(0, quoteIndex + 1), cancellationToken);
await _writer.WriteAsync(SepDefaults.Quote);
#endif
start += quoteIndex + 1;
Expand All @@ -241,15 +241,15 @@ async ValueTask WriteQuotesEscapedAsync(ReadOnlyMemory<char> span)
#if SYNC
void DisposeManaged()
#else
async ValueTask DisposeManagedAsync()
async ValueTask DisposeManagedAsync(CancellationToken cancellationToken)
#endif
{
if (!_headerWrittenOrSkipped && _cols.Count > 0)
{
#if SYNC
WriteHeader();
#else
await WriteHeaderAsync();
await WriteHeaderAsync(cancellationToken);
#endif
}

Expand Down
30 changes: 15 additions & 15 deletions src/Sep/SepWriter.IO.Sync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public Task FlushAsync(CancellationToken cancellationToken = default) =>
#if SYNC
internal void EndRow()
#else
internal async ValueTask EndRowAsync()
internal async ValueTask EndRowAsync(CancellationToken cancellationToken)
#endif
{
if (!_newRowActive) { SepThrow.InvalidOperationException_WriterDoesNotHaveActiveRow(); }
Expand All @@ -33,7 +33,7 @@ internal async ValueTask EndRowAsync()
#if SYNC
WriteHeader();
#else
await WriteHeaderAsync();
await WriteHeaderAsync(cancellationToken);
#endif
}
else if (_colNotSetOption == SepColNotSetOption.Throw || !_disableColCountCheck)
Expand Down Expand Up @@ -77,15 +77,15 @@ internal async ValueTask EndRowAsync()
#if SYNC
WriteEscaped(span);
#else
await WriteEscapedAsync(span);
await WriteEscapedAsync(span, cancellationToken);
#endif
}
else
{
#if SYNC
_writer.Write(span);
#else
await _writer.WriteAsync(span);
await _writer.WriteAsync(span, cancellationToken);
#endif
}
notFirst = true;
Expand All @@ -107,7 +107,7 @@ internal async ValueTask EndRowAsync()
#if SYNC
internal void WriteHeader()
#else
internal async ValueTask WriteHeaderAsync()
internal async ValueTask WriteHeaderAsync(CancellationToken cancellationToken)
#endif
{
if (_writeHeader)
Expand Down Expand Up @@ -138,8 +138,8 @@ internal async ValueTask WriteHeaderAsync()
if (_escape) { WriteEscaped(name); }
else { _writer.Write(name); }
#else
if (_escape) { await WriteEscapedAsync(name.AsMemory()); }
else { await _writer.WriteAsync(name); }
if (_escape) { await WriteEscapedAsync(name.AsMemory(), cancellationToken); }
else { await _writer.WriteAsync(name.AsMemory(), cancellationToken); }
#endif
_colNamesHeader[colIndex] = name;
notFirstHeader = true;
Expand All @@ -157,7 +157,7 @@ internal async ValueTask WriteHeaderAsync()
#if SYNC
void WriteEscaped(ReadOnlySpan<char> span)
#else
async ValueTask WriteEscapedAsync(ReadOnlyMemory<char> span)
async ValueTask WriteEscapedAsync(ReadOnlyMemory<char> span, CancellationToken cancellationToken)
#endif
{
#if SYNC
Expand All @@ -172,7 +172,7 @@ async ValueTask WriteEscapedAsync(ReadOnlyMemory<char> span)
WriteQuotesEscaped(span);
#else
await _writer.WriteAsync(SepDefaults.Quote);
await WriteQuotesEscapedAsync(span);
await WriteQuotesEscapedAsync(span, cancellationToken);
#endif
#if SYNC
_writer.Write(SepDefaults.Quote);
Expand All @@ -185,7 +185,7 @@ async ValueTask WriteEscapedAsync(ReadOnlyMemory<char> span)
#if SYNC
_writer.Write(span);
#else
await _writer.WriteAsync(span);
await _writer.WriteAsync(span, cancellationToken);
#endif
}
}
Expand All @@ -194,7 +194,7 @@ async ValueTask WriteEscapedAsync(ReadOnlyMemory<char> span)
#if SYNC
void WriteQuotesEscaped(ReadOnlySpan<char> span)
#else
async ValueTask WriteQuotesEscapedAsync(ReadOnlyMemory<char> span)
async ValueTask WriteQuotesEscapedAsync(ReadOnlyMemory<char> span, CancellationToken cancellationToken)
#endif
{
var start = 0;
Expand All @@ -211,7 +211,7 @@ async ValueTask WriteQuotesEscapedAsync(ReadOnlyMemory<char> span)
#if SYNC
_writer.Write(remainingSpan);
#else
await _writer.WriteAsync(remainingSpan);
await _writer.WriteAsync(remainingSpan, cancellationToken);
#endif
break;
}
Expand All @@ -221,7 +221,7 @@ async ValueTask WriteQuotesEscapedAsync(ReadOnlyMemory<char> span)
_writer.Write(remainingSpan.Slice(0, quoteIndex + 1));
_writer.Write(SepDefaults.Quote);
#else
await _writer.WriteAsync(remainingSpan.Slice(0, quoteIndex + 1));
await _writer.WriteAsync(remainingSpan.Slice(0, quoteIndex + 1), cancellationToken);
await _writer.WriteAsync(SepDefaults.Quote);
#endif
start += quoteIndex + 1;
Expand All @@ -241,15 +241,15 @@ async ValueTask WriteQuotesEscapedAsync(ReadOnlyMemory<char> span)
#if SYNC
void DisposeManaged()
#else
async ValueTask DisposeManagedAsync()
async ValueTask DisposeManagedAsync(CancellationToken cancellationToken)
#endif
{
if (!_headerWrittenOrSkipped && _cols.Count > 0)
{
#if SYNC
WriteHeader();
#else
await WriteHeaderAsync();
await WriteHeaderAsync(cancellationToken);
#endif
}

Expand Down
2 changes: 1 addition & 1 deletion src/Sep/SepWriter.Row.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public ValueTask DisposeAsync()
{
if (_writer is not null)
{
return _writer.EndRowAsync();
return _writer.EndRowAsync(default);
}
_writer = null;
return ValueTask.CompletedTask;
Expand Down
2 changes: 1 addition & 1 deletion src/Sep/SepWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ static uint ContainsSpecialCharacters(ReadOnlySpan<char> span, char separator)
//static ulong IndexOfFirstSet(ulong v) => ((((v - 1) & 0x0001000100010001ul) * 0x0001000100010001ul) >> 60) - 1;
}

public ValueTask DisposeAsync() => DisposeManagedAsync();
public ValueTask DisposeAsync() => DisposeManagedAsync(default);

#region Dispose
bool _disposed;
Expand Down

0 comments on commit 1063a20

Please sign in to comment.