Skip to content

Commit

Permalink
Merge branch '10-forward-pipereader-seekasync-on-completed-readresult…
Browse files Browse the repository at this point in the history
…-ends-with-exception' into 'master'

Resolve "Forward PipeReader.SeekAsync on completed ReadResult ends with exception"

Closes kaitai-io#10

See merge request marta/kaitai_struct_csharp_runtime!20
  • Loading branch information
pluskal committed Dec 8, 2020
2 parents 3f11b47 + c36377d commit 17da91f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
19 changes: 19 additions & 0 deletions Kaitai.Struct.Runtime.Async.Tests/KaitaiAsyncStreamBaseTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.IO;
using System.IO.Pipelines;
using System.Threading;
using System.Threading.Tasks;
using Kaitai.Async;
using Xunit;
Expand Down Expand Up @@ -115,6 +116,24 @@ await EvaluateMaybeCancelled(async () =>
});
}

[Fact]
public async Task ForwardSeek_AfterReadToEndAndBackwardSeek_Test()
{
const int toRead = 1;

var kaitaiStreamSUT = Create(new byte[2]);

await EvaluateMaybeCancelled(async () =>
{
// Simulates kaitai compiler generated code for multiple fields defined as `instances`
await kaitaiStreamSUT.ReadBytesFullAsync(CancellationToken.None);
await kaitaiStreamSUT.SeekAsync(0);
await kaitaiStreamSUT.SeekAsync(toRead);

Assert.Equal(toRead, kaitaiStreamSUT.Pos);
});
}

[Theory]
[InlineData(0)]
[InlineData(1)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public virtual async ValueTask SeekAsync(long position, CancellationToken cancel
ReadResult = await PipeReader.ReadAsync(cancellationToken);
}

if (ReadResult.Buffer.Length <= position)
if (ReadResult.Buffer.Length >= position)
{
Position = position;
return;
Expand Down

0 comments on commit 17da91f

Please sign in to comment.