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

Ensure stream position to be reset before returning task #306

Merged
merged 1 commit into from
May 22, 2018
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
36 changes: 36 additions & 0 deletions Neo4j.Driver/Neo4j.Driver.Tests/IO/ChunkReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,24 @@ public void ShouldResetInternalBufferPositionsAfterOneChunkSpanningMessageIsRead
count.Should().Be(1);
logger.Verify(l => l.Trace(It.IsRegex("^\\d+ bytes left in chunk buffer.*compacting\\.$"), It.IsAny<object[]>()), Times.AtLeast(size / Constants.ChunkBufferSize));
}

[Fact]
public void ShouldResetBufferStreamPosition()
{
var data = IOExtensions.GenerateBoltMessages(1000, 128 * 1024);

var logger = new Mock<ILogger>();
var reader = new ChunkReader(new MemoryStream(data.ToArray()), logger.Object);

var bufferStream = new MemoryStream();
bufferStream.Write(IOExtensions.GenerateBoltMessage(1035));

var bufferPosition = bufferStream.Position;

var count = reader.ReadNextMessages(bufferStream);

bufferStream.Position.Should().Be(bufferPosition);
}

}

Expand Down Expand Up @@ -328,6 +346,24 @@ public async void ShouldResetInternalBufferPositionsAfterOneChunkSpanningMessage
logger.Verify(l => l.Trace(It.IsRegex("^\\d+ bytes left in chunk buffer.*compacting\\.$"), It.IsAny<object[]>()), Times.AtLeast(size / Constants.ChunkBufferSize));
}

[Fact]
public async void ShouldResetBufferStreamPosition()
{
var data = IOExtensions.GenerateBoltMessages(1000, 128 * 1024);

var logger = new Mock<ILogger>();
var reader = new ChunkReader(new MemoryStream(data.ToArray()), logger.Object);

var bufferStream = new MemoryStream();
bufferStream.Write(IOExtensions.GenerateBoltMessage(1035));

var bufferPosition = bufferStream.Position;

var count = await reader.ReadNextMessagesAsync(bufferStream);

bufferStream.Position.Should().Be(bufferPosition);
}

}

}
Expand Down
13 changes: 8 additions & 5 deletions Neo4j.Driver/Neo4j.Driver/Internal/IO/ChunkReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
// limitations under the License.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Neo4j.Driver.V1;

Expand Down Expand Up @@ -150,18 +152,19 @@ public Task<int> ReadNextMessagesAsync(Stream messageStream)

// Track and manage target stream's positions.
var previousPosition = messageStream.Position;
taskCompletionSource.Task.ContinueWith(t =>
{
messageStream.Position = previousPosition;
}, TaskContinuationOptions.ExecuteSynchronously);
messageStream.Position = messageStream.Length;

ReadNextChunkLoopAsync(
messageStream,
taskCompletionSource,
TaskExtensions.GetCompletedTask());

return taskCompletionSource.Task;
return taskCompletionSource.Task.ContinueWith(t =>
{
messageStream.Position = previousPosition;

return t;
}, TaskContinuationOptions.ExecuteSynchronously).Unwrap();
}

private Task ReadNextChunkLoopAsync(Stream messageStream, TaskCompletionSource<int> taskCompletionSource,
Expand Down