Skip to content
Open
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
38 changes: 38 additions & 0 deletions source/Handlebars.Test/BindingContextPoolTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.Collections.Generic;
using Xunit;

namespace HandlebarsDotNet.Test
{
[CollectionDefinition(nameof(BindingContextPoolTests), DisableParallelization = true)]
public class BindingContextPoolCollection
{
}

// Binding contexts are pooled process-wide and shared by every IHandlebars instance. A value one
// render binds must never be visible to a later render, however many renders come between.
[Collection(nameof(BindingContextPoolTests))]
public class BindingContextPoolTests
{
[Fact]
public void BlockParamBoundInOneRenderIsNotSeenByLaterRenders()
{
var first = Handlebars.Create().Compile("{{#each secrets as |x|}}{{/each}}");
// `x` is read in two nested frames, so whichever pooled context holds the earlier
// binding, it is read wherever that context lands except as the root
var second = Handlebars.Create().Compile("{{#with this as |y|}}{{#with this as |z|}}[{{x}}]{{/with}}[{{x}}]{{/with}}");

// A context returned at a version that is a multiple of 10 is fully cleared, so bind `x`
// in several renders to leave it in more than one pooled context
for (var i = 0; i < 20; i++) first(new { secrets = new[] { "from the first render" } });

var leaked = new List<string>();
for (var i = 0; i < 100000 && leaked.Count < 5; i++)
{
var output = second(new { x = "own value" });
if (output != "[own value][own value]") leaked.Add(i + ": " + output);
}

Assert.Empty(leaked);
}
}
}
29 changes: 29 additions & 0 deletions source/Handlebars.Test/Collections/FixedSizeDictionaryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,35 @@ public void CanAddAfterClear()
}
}

[Theory]
[InlineData(nameof(FixedSizeDictionary<object, object, EqualityComparer<object>>.Reset))]
[InlineData(nameof(FixedSizeDictionary<object, object, EqualityComparer<object>>.OptionalClear))]
public void EntryFromAnEarlierUseStaysGoneOnceTheVersionWraps(string clear)
{
var dictionary = new FixedSizeDictionary<string, string, StringComparerAdapter>(16, 7, new StringComparerAdapter());
dictionary.AddOrReplace("earlier", "earlier value", out _);

// The version is a byte: 255 resets bring it back to the value it had when "earlier" was added
for (var i = 0; i < 255; i++)
{
if (clear == nameof(dictionary.Reset)) dictionary.Reset();
else dictionary.OptionalClear();
}

dictionary.AddOrReplace("current", "current value", out _);

Assert.False(dictionary.TryGetValue("earlier", out var value), $"found \"{value}\" from before the resets");
Assert.False(dictionary.ContainsKey("earlier"));
Assert.Single(dictionary);
}

private readonly struct StringComparerAdapter : IEqualityComparer<string>
{
public bool Equals(string? x, string? y) => string.Equals(x, y, StringComparison.Ordinal);

public int GetHashCode(string obj) => StringComparer.Ordinal.GetHashCode(obj);
}

public void Dispose()
{
FixedSizeDictionary.OptionalClear();
Expand Down
21 changes: 17 additions & 4 deletions source/Handlebars/Collections/FixedSizeDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -393,9 +393,7 @@ public void Reset()
{
_count = 0;

unchecked { ++_version; }

if (_version == 0) _version = 1;
NextVersion();
}

/// <summary>
Expand All @@ -415,9 +413,24 @@ public void Clear()

_count = 0;

NextVersion();
}

/// <summary>
/// Moves to the next version. Entries left from earlier versions are kept for speed and
/// count as absent only while their version differs from the current one, so when the
/// version wraps every slot is wiped: an entry must never come back as current.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void NextVersion()
{
unchecked { ++_version; }

if (_version == 0) _version = 1;
if (_version != 0) return;

Array.Clear(_entries, 0, _entries.Length);
Array.Clear(_indexes, 0, _indexes.Length);
_version = 1;
}

/// <summary>
Expand Down
Loading