Skip to content

Commit 593786e

Browse files
committed
Add unit test
1 parent aef0f44 commit 593786e

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System.IO;
2+
using Nerdbank.GitVersioning.ManagedGit;
3+
using Xunit;
4+
5+
namespace NerdBank.GitVersioning.Tests.ManagedGit
6+
{
7+
/// <summary>
8+
/// Tests the <see cref="GitPackMemoryCache"/> class.
9+
/// </summary>
10+
public class GitPackMemoryCacheTests
11+
{
12+
[Fact]
13+
public void StreamsAreIndependent()
14+
{
15+
using (MemoryStream stream = new MemoryStream(
16+
new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }))
17+
{
18+
var cache = new GitPackMemoryCache();
19+
20+
var stream1 = cache.Add(0, stream);
21+
Assert.True(cache.TryOpen(0, out Stream stream2));
22+
23+
using (stream1)
24+
using (stream2)
25+
{
26+
stream1.Seek(5, SeekOrigin.Begin);
27+
Assert.Equal(5, stream1.Position);
28+
Assert.Equal(0, stream2.Position);
29+
Assert.Equal(5, stream1.ReadByte());
30+
31+
Assert.Equal(6, stream1.Position);
32+
Assert.Equal(0, stream2.Position);
33+
34+
Assert.Equal(0, stream2.ReadByte());
35+
Assert.Equal(6, stream1.Position);
36+
Assert.Equal(1, stream2.Position);
37+
}
38+
}
39+
}
40+
}
41+
}

src/NerdBank.GitVersioning/ManagedGit/GitPackMemoryCache.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,37 @@
77

88
namespace Nerdbank.GitVersioning.ManagedGit
99
{
10-
internal class GitPackMemoryCache : GitPackCache
10+
/// <summary>
11+
/// <para>
12+
/// The <see cref="GitPackMemoryCache"/> implements the <see cref="GitPackCache"/> abstract class.
13+
/// When a <see cref="Stream"/> is added to the <see cref="GitPackMemoryCache"/>, it is wrapped in a
14+
/// <see cref="GitPackMemoryCacheStream"/>. This stream allows for just-in-time, random, read-only
15+
/// access to the underlying data (which may deltafied and/or compressed).
16+
/// </para>
17+
/// <para>
18+
/// Whenever data is read from a <see cref="GitPackMemoryCacheStream"/>, the call is forwarded to the
19+
/// underlying <see cref="Stream"/> and cached in a <see cref="MemoryStream"/>. If the same data is read
20+
/// twice, it is read from the <see cref="MemoryStream"/>, rather than the underlying <see cref="Stream"/>.
21+
/// </para>
22+
/// <para>
23+
/// <see cref="Add(long, Stream)"/> and <see cref="TryOpen(long, out Stream?)"/> return <see cref="Stream"/>
24+
/// objects which may operate on the same underlying <see cref="Stream"/>, but independently maintain
25+
/// their state.
26+
/// </para>
27+
/// </summary>
28+
public class GitPackMemoryCache : GitPackCache
1129
{
1230
private readonly Dictionary<long, GitPackMemoryCacheStream> cache = new Dictionary<long, GitPackMemoryCacheStream>();
1331

32+
/// <inheritdoc/>
1433
public override Stream Add(long offset, Stream stream)
1534
{
1635
var cacheStream = new GitPackMemoryCacheStream(stream);
1736
this.cache.Add(offset, cacheStream);
1837
return new GitPackMemoryCacheViewStream(cacheStream);
1938
}
2039

40+
/// <inheritdoc/>
2141
public override bool TryOpen(long offset, [NotNullWhen(true)] out Stream? stream)
2242
{
2343
if (this.cache.TryGetValue(offset, out GitPackMemoryCacheStream? cacheStream))
@@ -30,6 +50,7 @@ public override bool TryOpen(long offset, [NotNullWhen(true)] out Stream? stream
3050
return false;
3151
}
3252

53+
/// <inheritdoc/>
3354
public override void GetCacheStatistics(StringBuilder builder)
3455
{
3556
builder.AppendLine($"{this.cache.Count} items in cache");

0 commit comments

Comments
 (0)