-
-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #542 from qmfrederik/fixes/packfiles
GitPackMemoryCache: Don't have multiple callers reuse the same stream
- Loading branch information
Showing
5 changed files
with
148 additions
and
7 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
src/NerdBank.GitVersioning.Tests/ManagedGit/GitPackMemoryCacheTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using System.IO; | ||
using Nerdbank.GitVersioning.ManagedGit; | ||
using Xunit; | ||
|
||
namespace NerdBank.GitVersioning.Tests.ManagedGit | ||
{ | ||
/// <summary> | ||
/// Tests the <see cref="GitPackMemoryCache"/> class. | ||
/// </summary> | ||
public class GitPackMemoryCacheTests | ||
{ | ||
[Fact] | ||
public void StreamsAreIndependent() | ||
{ | ||
using (MemoryStream stream = new MemoryStream( | ||
new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 })) | ||
{ | ||
var cache = new GitPackMemoryCache(); | ||
|
||
var stream1 = cache.Add(0, stream); | ||
Assert.True(cache.TryOpen(0, out Stream stream2)); | ||
|
||
using (stream1) | ||
using (stream2) | ||
{ | ||
stream1.Seek(5, SeekOrigin.Begin); | ||
Assert.Equal(5, stream1.Position); | ||
Assert.Equal(0, stream2.Position); | ||
Assert.Equal(5, stream1.ReadByte()); | ||
|
||
Assert.Equal(6, stream1.Position); | ||
Assert.Equal(0, stream2.Position); | ||
|
||
Assert.Equal(0, stream2.ReadByte()); | ||
Assert.Equal(6, stream1.Position); | ||
Assert.Equal(1, stream2.Position); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
src/NerdBank.GitVersioning/ManagedGit/GitPackMemoryCacheViewStream.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Text; | ||
|
||
namespace Nerdbank.GitVersioning.ManagedGit | ||
{ | ||
internal class GitPackMemoryCacheViewStream : Stream | ||
{ | ||
private readonly GitPackMemoryCacheStream baseStream; | ||
|
||
public GitPackMemoryCacheViewStream(GitPackMemoryCacheStream baseStream) | ||
{ | ||
this.baseStream = baseStream ?? throw new ArgumentNullException(nameof(baseStream)); | ||
} | ||
|
||
public override bool CanRead => true; | ||
|
||
public override bool CanSeek => true; | ||
|
||
public override bool CanWrite => false; | ||
|
||
public override long Length => this.baseStream.Length; | ||
|
||
private long position; | ||
|
||
public override long Position | ||
{ | ||
get => this.position; | ||
set => throw new NotSupportedException(); | ||
} | ||
|
||
public override void Flush() => throw new NotImplementedException(); | ||
|
||
public override int Read(byte[] buffer, int offset, int count) | ||
{ | ||
return this.Read(buffer.AsSpan(offset, count)); | ||
} | ||
|
||
#if NETSTANDARD | ||
public int Read(Span<byte> buffer) | ||
#else | ||
/// <inheritdoc/> | ||
public override int Read(Span<byte> buffer) | ||
#endif | ||
{ | ||
int read = 0; | ||
|
||
lock (this.baseStream) | ||
{ | ||
if (this.baseStream.Position != this.position) | ||
{ | ||
this.baseStream.Seek(this.position, SeekOrigin.Begin); | ||
} | ||
|
||
read = this.baseStream.Read(buffer); | ||
} | ||
|
||
this.position += read; | ||
return read; | ||
} | ||
|
||
public override long Seek(long offset, SeekOrigin origin) | ||
{ | ||
if (origin != SeekOrigin.Begin) | ||
{ | ||
throw new NotSupportedException(); | ||
} | ||
|
||
this.position = Math.Min(offset, this.Length); | ||
return this.position; | ||
} | ||
|
||
public override void SetLength(long value) => throw new NotSupportedException(); | ||
public override void Write(byte[] buffer, int offset, int count) => throw new NotSupportedException(); | ||
} | ||
} |