Files
Umbraco-CMS/tests/Umbraco.Tests.UnitTests/Umbraco.PublishedCache.NuCache/SnapDictionaryTests.cs
Paul Johnson 01d2d0326c Merge release/10.0.0 into v10/dev
commit e0aa430d4c
Author: Paul Johnson <pmj@umbraco.com>
Date:   Thu May 19 10:00:57 2022 +0100

    Fix typo in pipeline yaml

commit 2ec450f2d6
Author: Paul Johnson <pmj@umbraco.com>
Date:   Thu May 19 09:14:47 2022 +0100

    Fix yaml conditions

commit c2d548039a
Author: Paul Johnson <pmj@umbraco.com>
Date:   Thu May 19 09:02:50 2022 +0100

    Azure pipeline refactor (#12428)

    * Skip symbols for Umbraco.Templates

    * Resolve some test issues

    + Fixed whitespace dependant tests to pass regardless of build OS vs
    run OS.

    + Snap dictionary tests were failing when Configuration was release

    + Removed hardcoded baseUrl from one of the acceptance tests

    * Move docfx setup to ./build and fix

    * Update UI docs title

    * Added dockerfile that can be used when running the acceptance tests.

    * Take explicit dependency on System.Security.Cryptography.Pkcs

    * Refactor ci/cd pipeline

commit ee8359af75
Author: Mole <nikolajlauridsen@protonmail.ch>
Date:   Thu May 19 09:57:21 2022 +0200

    V10: Reintroduce appsettings-schema.json (#12416)

    * Reintroduce language files tests (#12367)

    * Reintroducing language files tests

    * Fix casing

    * Update tests/Umbraco.Tests.UnitTests/Umbraco.Core/EmbeddedResources/LanguageXmlTests.cs

    Co-authored-by: Mole <nikolajlauridsen@protonmail.ch>

    * Change Umbraco.Cms and Umbraco.Templates nuspecs to csproj

    * Remove Umbraco.Templates from VerifyNuGet step

    * Remove duplicate and unnecessary properties

    * Generate json schema on build

    * Add targets file

    * Gitignore auto generated appsettings schema

    * Fix build not copying file

    * Use the new path in appsettings

    * Update copy message

    * Build json schema as release

    * Update json schema options

    Otherwise just running the project will put the file in the wrong place

    * Generate schema if it doesn't exist in Web.Ui

    * Update json schema options

    Otherwise just running the project will put the file in the wrong place

    Co-authored-by: Elitsa Marinovska <21998037+elit0451@users.noreply.github.com>
    Co-authored-by: Ronald Barendse <ronald@barend.se>

commit dd617ede80
Author: Ronald Barendse <ronald@barend.se>
Date:   Thu May 19 09:51:11 2022 +0200

    v10: Change Umbraco.Cms and Umbraco.Templates nuspecs to csproj (#12413)

    * Reintroduce language files tests (#12367)

    * Reintroducing language files tests

    * Fix casing

    * Update tests/Umbraco.Tests.UnitTests/Umbraco.Core/EmbeddedResources/LanguageXmlTests.cs

    Co-authored-by: Mole <nikolajlauridsen@protonmail.ch>

    * Change Umbraco.Cms and Umbraco.Templates nuspecs to csproj

    * Remove Umbraco.Templates from VerifyNuGet step

    * Remove duplicate and unnecessary properties

    Co-authored-by: Elitsa Marinovska <21998037+elit0451@users.noreply.github.com>
    Co-authored-by: Mole <nikolajlauridsen@protonmail.ch>

commit b83216876f
Author: Ronald Barendse <ronald@barend.se>
Date:   Thu May 19 08:36:04 2022 +0200

    v10: Project template database/connection string improvements (#12407)

    * Add new connection-string-provider-name parameter

    * Use template value forms to correctly encode JSON values

    * Add new development-database-type parameter

    * Update package template and fix App_Plugins directory rename

    * Remove conflicting short parameter name

    * Lowercase framework parameter to align with MS templates

    * Cleanup default template settings

    * Write unattended install parameters when either connection string or development database is set

    * Include RootNamespace in UmbracoPackage template

    * Update Umbraco specific gitignore rules

    * Revert "Lowercase framework parameter to align with MS templates"

    This reverts commit 22de389272a7e119df569ec2e54190265f6d0ae0.

    * Add exclude-gitignore parameter

    * Update template schemas

    * Add minimal-gitignore parameter

commit e40049dcf1
Author: Mole <nikolajlauridsen@protonmail.ch>
Date:   Wed May 18 15:22:51 2022 +0200

    Fix domain for invariant content nodes (#12405)

    Co-authored-by: Elitsa Marinovska <elm@umbraco.dk>

commit a3692b887a
Author: Elitsa Marinovska <21998037+elit0451@users.noreply.github.com>
Date:   Mon May 9 11:42:10 2022 +0200

    Use SnippetCollection to when working with snippets  (#12355)

    * Introducing a new Snippet type

    * Adding a SnippetCollection and SnippetCollectionBuilder

    * Using snippetCollection to get the snippets instead of fileService

    * Fixed fetching the correct content

    * Make ISnippet non-discoverable

    * Split the SnippetCollection into PartialViewSnippetCollection and PartialViewMacroSnippetCollection

    * Update CodeFileController to use the 2 snippet collections

    * Display the names with Empty.cshtml on top

    * Remove merging embedded snippets with custom snippets from ~\Umbraco.Web.UI\umbraco\PartialViewMacros\Templates folder for the Partial View Collection

    * Fix naming

    * Fix another naming

    * Cleanup + Use base items

    Co-authored-by: Bjarke Berg <mail@bergmania.dk>
    (cherry picked from commit 9326cc5fc6)

commit 4f48a4937b
Author: patrickdemooij9 <patrickdemooij98@hotmail.com>
Date:   Fri Oct 8 11:18:00 2021 +0200

    Cherry picked from 4c08b44684

commit 4fdbfee597
Author: Bjarne Fyrstenborg <bjarne_fyrstenborg@hotmail.com>
Date:   Wed May 11 00:51:37 2022 +0200

    Show nicer overlay when clicking block card for deleted element type (#12140)

    * Show nicer overlay when clicking block card for deleted element type

    * Cleanup

    * Remove stop-scrolling container

    * Use flex-start instead on start

    * Remove legacy flexbox fallback

    * Remove unnecessary hack

    * Use standard gap property instead

    * Localization of message

    * Fix translation

    * End sentence with a dot

    (cherry picked from commit ebb1dc21a9)

commit 3856bf8288
Author: Henk Jan Pluim <henkjan.pluim@greenchoice.nl>
Date:   Mon Apr 25 10:02:06 2022 +0200

    #fix 12254 return emptyresult

    (cherry picked from commit 7993d19c1b)

commit 7087c3d9f6
Author: Ronald Barendse <ronald@barend.se>
Date:   Tue May 17 12:59:01 2022 +0200

    v10: Use ForceCreateDatabase during unattended install and extend GetUmbracoConnectionString extension methods (#12397)

    * Add extension methods to get the Umbraco connection string/provider name from configuration

    * Added tests for configuration extension methods.

    * Fix issue with InstallMissingDatabase and ForceCreateDatabase

    * Fix comments

    * Revert casing change in GenerateConnectionString

    * Re-add AddOptions (without config binding) to fix test

    * Update src/Umbraco.Core/Configuration/Models/ConnectionStrings.cs

    Co-authored-by: Ronald Barendse <ronald@barend.se>

    * Update src/Umbraco.Core/Configuration/Models/ConnectionStrings.cs

    * Update src/Umbraco.Infrastructure/Runtime/RuntimeState.cs

    * Whitespace and documentation updates

    * Add DatabaseProviderMetadataExtensions

    * Filter before ordering

    * Replace DataDirectory placeholder when setting connection string

    Co-authored-by: Andy Butland <abutland73@gmail.com>
    Co-authored-by: Bjarke Berg <mail@bergmania.dk>
    (cherry picked from commit 8e6e262c7f)

commit e90bf26577
Author: Ronald Barendse <ronald@barend.se>
Date:   Tue May 17 07:33:54 2022 +0200

    v10: Support System.Data.SqlClient provider name (#12408)

    * Add support for System.Data.SqlClient provider name

    * Only update connection string when required

    (cherry picked from commit e82bcb1b76)

commit fd0637c96d
Author: Elitsa Marinovska <21998037+elit0451@users.noreply.github.com>
Date:   Mon May 16 14:23:59 2022 +0200

    Reintroduce language files tests (#12367)

    * Reintroducing language files tests

    * Fix casing

    * Update tests/Umbraco.Tests.UnitTests/Umbraco.Core/EmbeddedResources/LanguageXmlTests.cs

    Co-authored-by: Mole <nikolajlauridsen@protonmail.ch>
    (cherry picked from commit 2ed71a64ec)
2022-05-19 10:25:44 +01:00

1183 lines
39 KiB
C#

// Copyright (c) Umbraco.
// See LICENSE for more details.
using System;
using System.Linq;
using System.Threading.Tasks;
using Moq;
using NUnit.Framework;
using Umbraco.Cms.Core.Scoping;
using Umbraco.Cms.Infrastructure.PublishedCache;
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.PublishedCache.NuCache
{
[TestFixture]
public class SnapDictionaryTests
{
[Test]
public void LiveGenUpdate()
{
var d = new SnapDictionary<int, string>();
d.Test.CollectAuto = false;
Assert.AreEqual(0, d.Test.GetValues(1).Length);
// gen 1
d.Set(1, "one");
Assert.AreEqual(1, d.Test.GetValues(1).Length);
d.Clear(1);
Assert.AreEqual(0, d.Test.GetValues(1).Length); // gone
Assert.AreEqual(1, d.Test.LiveGen);
Assert.IsTrue(d.Test.NextGen);
Assert.AreEqual(0, d.Test.FloorGen);
}
[Test]
public void OtherGenUpdate()
{
var d = new SnapDictionary<int, string>();
d.Test.CollectAuto = false;
Assert.AreEqual(0, d.Test.GetValues(1).Length);
Assert.AreEqual(0, d.Test.LiveGen);
Assert.IsFalse(d.Test.NextGen);
// gen 1
d.Set(1, "one");
Assert.AreEqual(1, d.Test.GetValues(1).Length);
Assert.AreEqual(1, d.Test.LiveGen);
Assert.IsTrue(d.Test.NextGen);
SnapDictionary<int, string>.Snapshot s = d.CreateSnapshot();
Assert.AreEqual(1, s.Gen);
Assert.AreEqual(1, d.Test.LiveGen);
Assert.IsFalse(d.Test.NextGen);
// gen 2
d.Clear(1);
Assert.AreEqual(2, d.Test.GetValues(1).Length); // there
Assert.AreEqual(2, d.Test.LiveGen);
Assert.IsTrue(d.Test.NextGen);
Assert.AreEqual(0, d.Test.FloorGen);
GC.KeepAlive(s);
}
[Test]
public void MissingReturnsNull()
{
var d = new SnapDictionary<int, string>();
SnapDictionary<int, string>.Snapshot s = d.CreateSnapshot();
Assert.IsNull(s.Get(1));
}
[Test]
public void DeletedReturnsNull()
{
var d = new SnapDictionary<int, string>();
// gen 1
d.Set(1, "one");
SnapDictionary<int, string>.Snapshot s1 = d.CreateSnapshot();
Assert.AreEqual("one", s1.Get(1));
// gen 2
d.Clear(1);
SnapDictionary<int, string>.Snapshot s2 = d.CreateSnapshot();
Assert.IsNull(s2.Get(1));
Assert.AreEqual("one", s1.Get(1));
}
[Retry(5)] // TODO make this test non-flaky.
[Test]
public async Task CollectValues()
{
var d = new SnapDictionary<int, string>();
d.Test.CollectAuto = false;
// gen 1
d.Set(1, "one");
Assert.AreEqual(1, d.Test.GetValues(1).Length);
d.Set(1, "one");
Assert.AreEqual(1, d.Test.GetValues(1).Length);
d.Set(1, "uno");
Assert.AreEqual(1, d.Test.GetValues(1).Length);
Assert.AreEqual(1, d.Test.LiveGen);
Assert.IsTrue(d.Test.NextGen);
SnapDictionary<int, string>.Snapshot s1 = d.CreateSnapshot();
Assert.AreEqual(1, d.Test.LiveGen);
Assert.IsFalse(d.Test.NextGen);
// gen 2
Assert.AreEqual(1, d.Test.GetValues(1).Length);
d.Set(1, "one");
Assert.AreEqual(2, d.Test.GetValues(1).Length);
d.Set(1, "uno");
Assert.AreEqual(2, d.Test.GetValues(1).Length);
Assert.AreEqual(2, d.Test.LiveGen);
Assert.IsTrue(d.Test.NextGen);
SnapDictionary<int, string>.Snapshot s2 = d.CreateSnapshot();
Assert.AreEqual(2, d.Test.LiveGen);
Assert.IsFalse(d.Test.NextGen);
// gen 3
Assert.AreEqual(2, d.Test.GetValues(1).Length);
d.Set(1, "one");
Assert.AreEqual(3, d.Test.GetValues(1).Length);
d.Set(1, "uno");
Assert.AreEqual(3, d.Test.GetValues(1).Length);
Assert.AreEqual(3, d.Test.LiveGen);
Assert.IsTrue(d.Test.NextGen);
SnapDictionary<int, string>.TestHelper.GenVal[] tv = d.Test.GetValues(1);
Assert.AreEqual(3, tv[0].Gen);
Assert.AreEqual(2, tv[1].Gen);
Assert.AreEqual(1, tv[2].Gen);
Assert.AreEqual(0, d.Test.FloorGen);
// nothing to collect
await d.CollectAsync();
GC.KeepAlive(s1);
GC.KeepAlive(s2);
Assert.AreEqual(0, d.Test.FloorGen);
Assert.AreEqual(3, d.Test.LiveGen);
Assert.IsTrue(d.Test.NextGen);
Assert.AreEqual(2, d.SnapCount);
Assert.AreEqual(3, d.Test.GetValues(1).Length);
// one snapshot to collect
s1 = null;
GC.Collect();
GC.KeepAlive(s2);
await d.CollectAsync();
Assert.AreEqual(1, d.Test.FloorGen);
Assert.AreEqual(3, d.Test.LiveGen);
Assert.IsTrue(d.Test.NextGen);
Assert.AreEqual(1, d.SnapCount);
Assert.AreEqual(2, d.Test.GetValues(1).Length);
// another snapshot to collect
s2 = null;
GC.Collect();
await d.CollectAsync();
Assert.AreEqual(2, d.Test.FloorGen);
Assert.AreEqual(3, d.Test.LiveGen);
Assert.IsTrue(d.Test.NextGen);
Assert.AreEqual(0, d.SnapCount);
Assert.AreEqual(1, d.Test.GetValues(1).Length);
}
[Test]
public async Task ProperlyCollects()
{
var d = new SnapDictionary<int, string>();
d.Test.CollectAuto = false;
for (int i = 0; i < 32; i++)
{
d.Set(i, i.ToString());
d.CreateSnapshot().Dispose();
}
Assert.AreEqual(32, d.GenCount);
Assert.AreEqual(0, d.SnapCount); // because we've disposed them
await d.CollectAsync();
Assert.AreEqual(32, d.Test.LiveGen);
Assert.IsFalse(d.Test.NextGen);
Assert.AreEqual(0, d.GenCount);
Assert.AreEqual(0, d.SnapCount);
Assert.AreEqual(32, d.Count);
for (int i = 0; i < 32; i++)
{
d.Set(i, null);
}
d.CreateSnapshot().Dispose();
// because we haven't collected yet, but disposed nevertheless
Assert.AreEqual(1, d.GenCount);
Assert.AreEqual(0, d.SnapCount);
Assert.AreEqual(32, d.Count);
// once we collect, they are all gone
// since noone is interested anymore
await d.CollectAsync();
Assert.AreEqual(0, d.GenCount);
Assert.AreEqual(0, d.SnapCount);
Assert.AreEqual(0, d.Count);
}
[Retry(5)] // TODO make this test non-flaky.
[Test]
public async Task CollectNulls()
{
var d = new SnapDictionary<int, string>();
d.Test.CollectAuto = false;
// gen 1
d.Set(1, "one");
Assert.AreEqual(1, d.Test.GetValues(1).Length);
d.Set(1, "one");
Assert.AreEqual(1, d.Test.GetValues(1).Length);
d.Set(1, "uno");
Assert.AreEqual(1, d.Test.GetValues(1).Length);
Assert.AreEqual(1, d.Test.LiveGen);
Assert.IsTrue(d.Test.NextGen);
SnapDictionary<int, string>.Snapshot s1 = d.CreateSnapshot();
Assert.AreEqual(1, d.Test.LiveGen);
Assert.IsFalse(d.Test.NextGen);
// gen 2
Assert.AreEqual(1, d.Test.GetValues(1).Length);
d.Set(1, "one");
Assert.AreEqual(2, d.Test.GetValues(1).Length);
d.Set(1, "uno");
Assert.AreEqual(2, d.Test.GetValues(1).Length);
Assert.AreEqual(2, d.Test.LiveGen);
Assert.IsTrue(d.Test.NextGen);
SnapDictionary<int, string>.Snapshot s2 = d.CreateSnapshot();
Assert.AreEqual(2, d.Test.LiveGen);
Assert.IsFalse(d.Test.NextGen);
// gen 3
Assert.AreEqual(2, d.Test.GetValues(1).Length);
d.Set(1, "one");
Assert.AreEqual(3, d.Test.GetValues(1).Length);
d.Set(1, "uno");
Assert.AreEqual(3, d.Test.GetValues(1).Length);
d.Clear(1);
Assert.AreEqual(3, d.Test.GetValues(1).Length);
Assert.AreEqual(3, d.Test.LiveGen);
Assert.IsTrue(d.Test.NextGen);
SnapDictionary<int, string>.TestHelper.GenVal[] tv = d.Test.GetValues(1);
Assert.AreEqual(3, tv[0].Gen);
Assert.AreEqual(2, tv[1].Gen);
Assert.AreEqual(1, tv[2].Gen);
Assert.AreEqual(0, d.Test.FloorGen);
// nothing to collect
await d.CollectAsync();
GC.KeepAlive(s1);
GC.KeepAlive(s2);
Assert.AreEqual(0, d.Test.FloorGen);
Assert.AreEqual(3, d.Test.LiveGen);
Assert.IsTrue(d.Test.NextGen);
Assert.AreEqual(2, d.SnapCount);
Assert.AreEqual(3, d.Test.GetValues(1).Length);
// one snapshot to collect
s1 = null;
GC.Collect();
GC.KeepAlive(s2);
await d.CollectAsync();
Assert.AreEqual(1, d.Test.FloorGen);
Assert.AreEqual(3, d.Test.LiveGen);
Assert.IsTrue(d.Test.NextGen);
Assert.AreEqual(1, d.SnapCount);
Assert.AreEqual(2, d.Test.GetValues(1).Length);
// another snapshot to collect
s2 = null;
GC.Collect();
await d.CollectAsync();
Assert.AreEqual(2, d.Test.FloorGen);
Assert.AreEqual(3, d.Test.LiveGen);
Assert.IsTrue(d.Test.NextGen);
Assert.AreEqual(0, d.SnapCount);
// and everything is gone?
// no, cannot collect the live gen because we'd need to lock
Assert.AreEqual(1, d.Test.GetValues(1).Length);
d.CreateSnapshot();
GC.Collect();
await d.CollectAsync();
// poof, gone
Assert.AreEqual(0, d.Test.GetValues(1).Length);
}
[Test]
[Retry(5)] // TODO make this test non-flaky.
public async Task EventuallyCollectNulls()
{
var d = new SnapDictionary<int, string>();
d.Test.CollectAuto = false;
Assert.AreEqual(0, d.Test.GetValues(1).Length);
// gen 1
d.Set(1, "one");
Assert.AreEqual(1, d.Test.GetValues(1).Length);
Assert.AreEqual(1, d.Test.LiveGen);
Assert.IsTrue(d.Test.NextGen);
await d.CollectAsync();
SnapDictionary<int, string>.TestHelper.GenVal[] tv = d.Test.GetValues(1);
Assert.AreEqual(1, tv.Length);
Assert.AreEqual(1, tv[0].Gen);
SnapDictionary<int, string>.Snapshot s = d.CreateSnapshot();
Assert.AreEqual("one", s.Get(1));
Assert.AreEqual(1, d.Test.LiveGen);
Assert.IsFalse(d.Test.NextGen);
Assert.AreEqual(1, d.Count);
Assert.AreEqual(1, d.SnapCount);
Assert.AreEqual(1, d.GenCount);
// gen 2
d.Clear(1);
tv = d.Test.GetValues(1);
Assert.AreEqual(2, tv.Length);
Assert.AreEqual(2, tv[0].Gen);
Assert.AreEqual(2, d.Test.LiveGen);
Assert.IsTrue(d.Test.NextGen);
Assert.AreEqual(1, d.Count);
Assert.AreEqual(1, d.SnapCount);
Assert.AreEqual(1, d.GenCount);
// nothing to collect
await d.CollectAsync();
GC.KeepAlive(s);
Assert.AreEqual(2, d.Test.GetValues(1).Length);
Assert.AreEqual(1, d.Count);
Assert.AreEqual(1, d.SnapCount);
Assert.AreEqual(1, d.GenCount);
Assert.AreEqual(2, d.Test.LiveGen);
Assert.IsTrue(d.Test.NextGen);
// collect snapshot
// don't collect liveGen+
s = null; // without being disposed
GC.Collect(); // should release the generation reference
await d.CollectAsync();
Assert.AreEqual(1, d.Test.GetValues(1).Length); // "one" value is gone
Assert.AreEqual(1, d.Count); // still have 1 item
Assert.AreEqual(0, d.SnapCount); // snapshot is gone
Assert.AreEqual(0, d.GenCount); // and generation has been dequeued
// liveGen/nextGen
s = d.CreateSnapshot();
s = null;
// collect liveGen
GC.Collect();
Assert.IsTrue(d.Test.GenObjs.TryPeek(out global::Umbraco.Cms.Infrastructure.PublishedCache.Snap.GenObj genObj));
genObj = null;
// in Release mode, it works, but in Debug mode, the weak reference is still alive
// and for some reason we need to do this to ensure it is collected
#if DEBUG
await d.CollectAsync();
GC.Collect();
#endif
Assert.IsTrue(d.Test.GenObjs.TryPeek(out genObj));
Assert.IsFalse(genObj.WeakGenRef.IsAlive); // snapshot is gone, along with its reference
await d.CollectAsync();
Assert.AreEqual(0, d.Test.GetValues(1).Length); // null value is gone
Assert.AreEqual(0, d.Count); // item is gone
Assert.AreEqual(0, d.Test.GenObjs.Count);
Assert.AreEqual(0, d.SnapCount); // snapshot is gone
Assert.AreEqual(0, d.GenCount); // and generation has been dequeued
}
[Test]
public async Task CollectDisposedSnapshots()
{
var d = new SnapDictionary<int, string>();
d.Test.CollectAuto = false;
// gen 1
d.Set(1, "one");
Assert.AreEqual(1, d.Test.GetValues(1).Length);
Assert.AreEqual(1, d.Test.LiveGen);
Assert.IsTrue(d.Test.NextGen);
SnapDictionary<int, string>.Snapshot s1 = d.CreateSnapshot();
Assert.AreEqual(1, d.Test.LiveGen);
Assert.IsFalse(d.Test.NextGen);
// gen 2
d.Set(1, "two");
Assert.AreEqual(2, d.Test.GetValues(1).Length);
Assert.AreEqual(2, d.Test.LiveGen);
Assert.IsTrue(d.Test.NextGen);
SnapDictionary<int, string>.Snapshot s2 = d.CreateSnapshot();
Assert.AreEqual(2, d.Test.LiveGen);
Assert.IsFalse(d.Test.NextGen);
// gen 3
d.Set(1, "three");
Assert.AreEqual(3, d.Test.GetValues(1).Length);
Assert.AreEqual(3, d.Test.LiveGen);
Assert.IsTrue(d.Test.NextGen);
SnapDictionary<int, string>.Snapshot s3 = d.CreateSnapshot();
Assert.AreEqual(3, d.Test.LiveGen);
Assert.IsFalse(d.Test.NextGen);
Assert.AreEqual(3, d.SnapCount);
s1.Dispose();
await d.CollectAsync();
Assert.AreEqual(2, d.SnapCount);
Assert.AreEqual(2, d.Test.GetValues(1).Length);
s2.Dispose();
await d.CollectAsync();
Assert.AreEqual(1, d.SnapCount);
Assert.AreEqual(1, d.Test.GetValues(1).Length);
s3.Dispose();
await d.CollectAsync();
Assert.AreEqual(0, d.SnapCount);
Assert.AreEqual(1, d.Test.GetValues(1).Length);
}
[Retry(5)] // TODO make this test non-flaky.
[Test]
public async Task CollectGcSnapshots()
{
var d = new SnapDictionary<int, string>();
d.Test.CollectAuto = false;
// gen 1
d.Set(1, "one");
Assert.AreEqual(1, d.Test.GetValues(1).Length);
Assert.AreEqual(1, d.Test.LiveGen);
Assert.IsTrue(d.Test.NextGen);
SnapDictionary<int, string>.Snapshot s1 = d.CreateSnapshot();
Assert.AreEqual(1, d.Test.LiveGen);
Assert.IsFalse(d.Test.NextGen);
// gen 2
d.Set(1, "two");
Assert.AreEqual(2, d.Test.GetValues(1).Length);
Assert.AreEqual(2, d.Test.LiveGen);
Assert.IsTrue(d.Test.NextGen);
SnapDictionary<int, string>.Snapshot s2 = d.CreateSnapshot();
Assert.AreEqual(2, d.Test.LiveGen);
Assert.IsFalse(d.Test.NextGen);
// gen 3
d.Set(1, "three");
Assert.AreEqual(3, d.Test.GetValues(1).Length);
Assert.AreEqual(3, d.Test.LiveGen);
Assert.IsTrue(d.Test.NextGen);
SnapDictionary<int, string>.Snapshot s3 = d.CreateSnapshot();
Assert.AreEqual(3, d.Test.LiveGen);
Assert.IsFalse(d.Test.NextGen);
Assert.AreEqual(3, d.SnapCount);
s1 = s2 = s3 = null;
await d.CollectAsync();
Assert.AreEqual(3, d.SnapCount);
Assert.AreEqual(3, d.Test.GetValues(1).Length);
GC.Collect();
await d.CollectAsync();
Assert.AreEqual(0, d.SnapCount);
Assert.AreEqual(1, d.Test.GetValues(1).Length);
}
[Retry(5)] // TODO make this test non-flaky.
[Test]
public async Task RandomTest1()
{
var d = new SnapDictionary<int, string>();
d.Test.CollectAuto = false;
d.Set(1, "one");
d.Set(2, "two");
SnapDictionary<int, string>.Snapshot s1 = d.CreateSnapshot();
string v1 = s1.Get(1);
Assert.AreEqual("one", v1);
d.Set(1, "uno");
SnapDictionary<int, string>.Snapshot s2 = d.CreateSnapshot();
string v2 = s2.Get(1);
Assert.AreEqual("uno", v2);
v1 = s1.Get(1);
Assert.AreEqual("one", v1);
Assert.AreEqual(2, d.SnapCount);
s1 = null;
GC.Collect();
await d.CollectAsync();
GC.Collect();
await d.CollectAsync();
Assert.AreEqual(1, d.SnapCount);
v2 = s2.Get(1);
Assert.AreEqual("uno", v2);
s2 = null;
GC.Collect();
await d.CollectAsync();
Assert.AreEqual(0, d.SnapCount);
}
[Retry(5)] // TODO make this test non-flaky.
[Test]
public async Task RandomTest2()
{
var d = new SnapDictionary<int, string>();
d.Test.CollectAuto = false;
d.Set(1, "one");
d.Set(2, "two");
SnapDictionary<int, string>.Snapshot s1 = d.CreateSnapshot();
string v1 = s1.Get(1);
Assert.AreEqual("one", v1);
d.Clear(1);
SnapDictionary<int, string>.Snapshot s2 = d.CreateSnapshot();
string v2 = s2.Get(1);
Assert.AreEqual(null, v2);
v1 = s1.Get(1);
Assert.AreEqual("one", v1);
Assert.AreEqual(2, d.SnapCount);
s1 = null;
GC.Collect();
await d.CollectAsync();
GC.Collect();
await d.CollectAsync();
Assert.AreEqual(1, d.SnapCount);
v2 = s2.Get(1);
Assert.AreEqual(null, v2);
s2 = null;
GC.Collect();
await d.CollectAsync();
Assert.AreEqual(0, d.SnapCount);
}
[Test]
public void WriteLockingFirstSnapshot()
{
var d = new SnapDictionary<int, string>();
d.Test.CollectAuto = false;
// gen 1
d.Set(1, "one");
Assert.AreEqual(1, d.Test.GetValues(1).Length);
Assert.AreEqual(1, d.Test.LiveGen);
Assert.IsTrue(d.Test.NextGen);
using (d.GetScopedWriteLock(GetScopeProvider()))
{
SnapDictionary<int, string>.Snapshot s1 = d.CreateSnapshot();
Assert.AreEqual(0, s1.Gen);
Assert.AreEqual(1, d.Test.LiveGen);
Assert.IsTrue(d.Test.NextGen);
Assert.IsNull(s1.Get(1));
}
SnapDictionary<int, string>.Snapshot s2 = d.CreateSnapshot();
Assert.AreEqual(1, s2.Gen);
Assert.AreEqual(1, d.Test.LiveGen);
Assert.IsFalse(d.Test.NextGen);
Assert.AreEqual("one", s2.Get(1));
}
[Test]
public void WriteLocking()
{
var d = new SnapDictionary<int, string>();
d.Test.CollectAuto = false;
// gen 1
d.Set(1, "one");
Assert.AreEqual(1, d.Test.GetValues(1).Length);
Assert.AreEqual(1, d.Test.LiveGen);
Assert.IsTrue(d.Test.NextGen);
SnapDictionary<int, string>.Snapshot s1 = d.CreateSnapshot();
Assert.AreEqual(1, s1.Gen);
Assert.AreEqual(1, d.Test.LiveGen);
Assert.IsFalse(d.Test.NextGen);
Assert.AreEqual("one", s1.Get(1));
// gen 2
Assert.AreEqual(1, d.Test.GetValues(1).Length);
d.Set(1, "uno");
Assert.AreEqual(2, d.Test.GetValues(1).Length);
Assert.AreEqual(2, d.Test.LiveGen);
Assert.IsTrue(d.Test.NextGen);
SnapDictionary<int, string>.Snapshot s2 = d.CreateSnapshot();
Assert.AreEqual(2, s2.Gen);
Assert.AreEqual(2, d.Test.LiveGen);
Assert.IsFalse(d.Test.NextGen);
Assert.AreEqual("uno", s2.Get(1));
using (d.GetScopedWriteLock(GetScopeProvider()))
{
// gen 3
Assert.AreEqual(2, d.Test.GetValues(1).Length);
d.SetLocked(1, "ein");
Assert.AreEqual(3, d.Test.GetValues(1).Length);
Assert.AreEqual(3, d.Test.LiveGen);
Assert.IsTrue(d.Test.NextGen);
SnapDictionary<int, string>.Snapshot s3 = d.CreateSnapshot();
Assert.AreEqual(2, s3.Gen);
Assert.AreEqual(3, d.Test.LiveGen);
Assert.IsTrue(d.Test.NextGen); // has NOT changed when (non) creating snapshot
Assert.AreEqual("uno", s3.Get(1));
}
SnapDictionary<int, string>.Snapshot s4 = d.CreateSnapshot();
Assert.AreEqual(3, s4.Gen);
Assert.AreEqual(3, d.Test.LiveGen);
Assert.IsFalse(d.Test.NextGen);
Assert.AreEqual("ein", s4.Get(1));
}
[Test]
public void NestedWriteLocking1()
{
var d = new SnapDictionary<int, string>();
SnapDictionary<int, string>.TestHelper t = d.Test;
t.CollectAuto = false;
Assert.AreEqual(0, d.CreateSnapshot().Gen);
// no scope context: writers nest, last one to be disposed commits
ICoreScopeProvider scopeProvider = GetScopeProvider();
using (IDisposable w1 = d.GetScopedWriteLock(scopeProvider))
{
Assert.AreEqual(1, t.LiveGen);
Assert.IsTrue(t.IsLocked);
Assert.IsTrue(t.NextGen);
Assert.Throws<InvalidOperationException>(() =>
{
using (IDisposable w2 = d.GetScopedWriteLock(scopeProvider))
{
}
});
Assert.AreEqual(1, t.LiveGen);
Assert.IsTrue(t.IsLocked);
Assert.IsTrue(t.NextGen);
Assert.AreEqual(0, d.CreateSnapshot().Gen);
}
Assert.AreEqual(1, t.LiveGen);
Assert.IsFalse(t.IsLocked);
Assert.IsTrue(t.NextGen);
Assert.AreEqual(1, d.CreateSnapshot().Gen);
}
[Test]
public void NestedWriteLocking2()
{
var d = new SnapDictionary<int, string>();
d.Test.CollectAuto = false;
Assert.AreEqual(0, d.CreateSnapshot().Gen);
// scope context: writers enlist
var scopeContext = new ScopeContext();
ICoreScopeProvider scopeProvider = GetScopeProvider(scopeContext);
using (IDisposable w1 = d.GetScopedWriteLock(scopeProvider))
{
// This one is interesting, although we don't allow recursive locks, since this is
// using the same ScopeContext/key, the lock acquisition is only done once.
using (IDisposable w2 = d.GetScopedWriteLock(scopeProvider))
{
Assert.AreSame(w1, w2);
d.SetLocked(1, "one");
}
}
}
[Test]
public void NestedWriteLocking3()
{
var d = new SnapDictionary<int, string>();
SnapDictionary<int, string>.TestHelper t = d.Test;
t.CollectAuto = false;
Assert.AreEqual(0, d.CreateSnapshot().Gen);
var scopeContext = new ScopeContext();
ICoreScopeProvider scopeProvider1 = GetScopeProvider();
ICoreScopeProvider scopeProvider2 = GetScopeProvider(scopeContext);
using (IDisposable w1 = d.GetScopedWriteLock(scopeProvider1))
{
Assert.AreEqual(1, t.LiveGen);
Assert.IsTrue(t.IsLocked);
Assert.IsTrue(t.NextGen);
Assert.Throws<InvalidOperationException>(() =>
{
using (IDisposable w2 = d.GetScopedWriteLock(scopeProvider2))
{
}
});
}
}
[Test]
public void WriteLocking2()
{
var d = new SnapDictionary<int, string>();
d.Test.CollectAuto = false;
// gen 1
d.Set(1, "one");
Assert.AreEqual(1, d.Test.GetValues(1).Length);
Assert.AreEqual(1, d.Test.LiveGen);
Assert.IsTrue(d.Test.NextGen);
SnapDictionary<int, string>.Snapshot s1 = d.CreateSnapshot();
Assert.AreEqual(1, s1.Gen);
Assert.AreEqual(1, d.Test.LiveGen);
Assert.IsFalse(d.Test.NextGen);
Assert.AreEqual("one", s1.Get(1));
// gen 2
Assert.AreEqual(1, d.Test.GetValues(1).Length);
d.Set(1, "uno");
Assert.AreEqual(2, d.Test.GetValues(1).Length);
Assert.AreEqual(2, d.Test.LiveGen);
Assert.IsTrue(d.Test.NextGen);
SnapDictionary<int, string>.Snapshot s2 = d.CreateSnapshot();
Assert.AreEqual(2, s2.Gen);
Assert.AreEqual(2, d.Test.LiveGen);
Assert.IsFalse(d.Test.NextGen);
Assert.AreEqual("uno", s2.Get(1));
ICoreScopeProvider scopeProvider = GetScopeProvider();
using (d.GetScopedWriteLock(scopeProvider))
{
// gen 3
Assert.AreEqual(2, d.Test.GetValues(1).Length);
d.SetLocked(1, "ein");
Assert.AreEqual(3, d.Test.GetValues(1).Length);
Assert.AreEqual(3, d.Test.LiveGen);
Assert.IsTrue(d.Test.NextGen);
SnapDictionary<int, string>.Snapshot s3 = d.CreateSnapshot();
Assert.AreEqual(2, s3.Gen);
Assert.AreEqual(3, d.Test.LiveGen);
Assert.IsTrue(d.Test.NextGen); // has NOT changed when (non) creating snapshot
Assert.AreEqual("uno", s3.Get(1));
}
SnapDictionary<int, string>.Snapshot s4 = d.CreateSnapshot();
Assert.AreEqual(3, s4.Gen);
Assert.AreEqual(3, d.Test.LiveGen);
Assert.IsFalse(d.Test.NextGen);
Assert.AreEqual("ein", s4.Get(1));
}
[Test]
public void WriteLocking3()
{
var d = new SnapDictionary<int, string>();
d.Test.CollectAuto = false;
// gen 1
d.Set(1, "one");
SnapDictionary<int, string>.Snapshot s1 = d.CreateSnapshot();
Assert.AreEqual(1, s1.Gen);
Assert.AreEqual("one", s1.Get(1));
d.Set(1, "uno");
SnapDictionary<int, string>.Snapshot s2 = d.CreateSnapshot();
Assert.AreEqual(2, s2.Gen);
Assert.AreEqual("uno", s2.Get(1));
ICoreScopeProvider scopeProvider = GetScopeProvider();
using (d.GetScopedWriteLock(scopeProvider))
{
// creating a snapshot in a write-lock does NOT return the "current" content
// it uses the previous snapshot, so new snapshot created only on release
d.SetLocked(1, "ein");
SnapDictionary<int, string>.Snapshot s3 = d.CreateSnapshot();
Assert.AreEqual(2, s3.Gen);
Assert.AreEqual("uno", s3.Get(1));
// but live snapshot contains changes
SnapDictionary<int, string>.Snapshot ls = d.Test.LiveSnapshot;
Assert.AreEqual("ein", ls.Get(1));
Assert.AreEqual(3, ls.Gen);
}
SnapDictionary<int, string>.Snapshot s4 = d.CreateSnapshot();
Assert.AreEqual(3, s4.Gen);
Assert.AreEqual("ein", s4.Get(1));
}
[Test]
public void ScopeLocking1()
{
var d = new SnapDictionary<int, string>();
d.Test.CollectAuto = false;
// gen 1
d.Set(1, "one");
SnapDictionary<int, string>.Snapshot s1 = d.CreateSnapshot();
Assert.AreEqual(1, s1.Gen);
Assert.AreEqual("one", s1.Get(1));
d.Set(1, "uno");
SnapDictionary<int, string>.Snapshot s2 = d.CreateSnapshot();
Assert.AreEqual(2, s2.Gen);
Assert.AreEqual("uno", s2.Get(1));
var scopeContext = new ScopeContext();
ICoreScopeProvider scopeProvider = GetScopeProvider(scopeContext);
using (d.GetScopedWriteLock(scopeProvider))
{
// creating a snapshot in a write-lock does NOT return the "current" content
// it uses the previous snapshot, so new snapshot created only on release
d.SetLocked(1, "ein");
SnapDictionary<int, string>.Snapshot s3 = d.CreateSnapshot();
Assert.AreEqual(2, s3.Gen);
Assert.AreEqual("uno", s3.Get(1));
// but live snapshot contains changes
SnapDictionary<int, string>.Snapshot ls = d.Test.LiveSnapshot;
Assert.AreEqual("ein", ls.Get(1));
Assert.AreEqual(3, ls.Gen);
}
SnapDictionary<int, string>.Snapshot s4 = d.CreateSnapshot();
Assert.AreEqual(2, s4.Gen);
Assert.AreEqual("uno", s4.Get(1));
scopeContext.ScopeExit(true);
SnapDictionary<int, string>.Snapshot s5 = d.CreateSnapshot();
Assert.AreEqual(3, s5.Gen);
Assert.AreEqual("ein", s5.Get(1));
}
[Test]
public void ScopeLocking2()
{
var d = new SnapDictionary<int, string>();
SnapDictionary<int, string>.TestHelper t = d.Test;
t.CollectAuto = false;
// gen 1
d.Set(1, "one");
SnapDictionary<int, string>.Snapshot s1 = d.CreateSnapshot();
Assert.AreEqual(1, s1.Gen);
Assert.AreEqual("one", s1.Get(1));
d.Set(1, "uno");
SnapDictionary<int, string>.Snapshot s2 = d.CreateSnapshot();
Assert.AreEqual(2, s2.Gen);
Assert.AreEqual("uno", s2.Get(1));
Assert.AreEqual(2, t.LiveGen);
Assert.IsFalse(t.NextGen);
var scopeContext = new ScopeContext();
ICoreScopeProvider scopeProvider = GetScopeProvider(scopeContext);
using (d.GetScopedWriteLock(scopeProvider))
{
// creating a snapshot in a write-lock does NOT return the "current" content
// it uses the previous snapshot, so new snapshot created only on release
d.SetLocked(1, "ein");
SnapDictionary<int, string>.Snapshot s3 = d.CreateSnapshot();
Assert.AreEqual(2, s3.Gen);
Assert.AreEqual("uno", s3.Get(1));
// we made some changes, so a next gen is required
Assert.AreEqual(3, t.LiveGen);
Assert.IsTrue(t.NextGen);
Assert.IsTrue(t.IsLocked);
// but live snapshot contains changes
SnapDictionary<int, string>.Snapshot ls = t.LiveSnapshot;
Assert.AreEqual("ein", ls.Get(1));
Assert.AreEqual(3, ls.Gen);
}
// nothing is committed until scope exits
Assert.AreEqual(3, t.LiveGen);
Assert.IsTrue(t.NextGen);
Assert.IsTrue(t.IsLocked);
// no changes until exit
SnapDictionary<int, string>.Snapshot s4 = d.CreateSnapshot();
Assert.AreEqual(2, s4.Gen);
Assert.AreEqual("uno", s4.Get(1));
scopeContext.ScopeExit(false);
// now things have changed
Assert.AreEqual(2, t.LiveGen);
Assert.IsFalse(t.NextGen);
Assert.IsFalse(t.IsLocked);
// no changes since not completed
SnapDictionary<int, string>.Snapshot s5 = d.CreateSnapshot();
Assert.AreEqual(2, s5.Gen);
Assert.AreEqual("uno", s5.Get(1));
}
[Test]
public void GetAll()
{
var d = new SnapDictionary<int, string>();
d.Test.CollectAuto = false;
Assert.AreEqual(0, d.Test.GetValues(1).Length);
d.Set(1, "one");
d.Set(2, "two");
d.Set(3, "three");
d.Set(4, "four");
SnapDictionary<int, string>.Snapshot s1 = d.CreateSnapshot();
string[] all = s1.GetAll().ToArray();
Assert.AreEqual(4, all.Length);
Assert.AreEqual("one", all[0]);
Assert.AreEqual("four", all[3]);
d.Set(1, "uno");
SnapDictionary<int, string>.Snapshot s2 = d.CreateSnapshot();
all = s1.GetAll().ToArray();
Assert.AreEqual(4, all.Length);
Assert.AreEqual("one", all[0]);
Assert.AreEqual("four", all[3]);
all = s2.GetAll().ToArray();
Assert.AreEqual(4, all.Length);
Assert.AreEqual("uno", all[0]);
Assert.AreEqual("four", all[3]);
}
[Test]
public void DontPanic()
{
var d = new SnapDictionary<int, string>();
d.Test.CollectAuto = false;
Assert.IsNull(d.Test.GenObj);
// gen 1
d.Set(1, "one");
Assert.IsTrue(d.Test.NextGen);
Assert.AreEqual(1, d.Test.LiveGen);
Assert.IsNull(d.Test.GenObj);
SnapDictionary<int, string>.Snapshot s1 = d.CreateSnapshot();
Assert.IsFalse(d.Test.NextGen);
Assert.AreEqual(1, d.Test.LiveGen);
Assert.IsNotNull(d.Test.GenObj);
Assert.AreEqual(1, d.Test.GenObj.Gen);
Assert.AreEqual(1, s1.Gen);
Assert.AreEqual("one", s1.Get(1));
d.Set(1, "uno");
Assert.IsTrue(d.Test.NextGen);
Assert.AreEqual(2, d.Test.LiveGen);
Assert.IsNotNull(d.Test.GenObj);
Assert.AreEqual(1, d.Test.GenObj.Gen);
var scopeContext = new ScopeContext();
ICoreScopeProvider scopeProvider = GetScopeProvider(scopeContext);
// scopeProvider.Context == scopeContext -> writer is scoped
// writer is scope contextual and scoped
// when disposed, nothing happens
// when the context exists, the writer is released
using (d.GetScopedWriteLock(scopeProvider))
{
d.SetLocked(1, "ein");
Assert.IsTrue(d.Test.NextGen);
Assert.AreEqual(3, d.Test.LiveGen);
Assert.IsNotNull(d.Test.GenObj);
Assert.AreEqual(2, d.Test.GenObj.Gen);
}
// writer has not released
Assert.IsTrue(d.Test.IsLocked);
Assert.IsNotNull(d.Test.GenObj);
Assert.AreEqual(2, d.Test.GenObj.Gen);
// nothing changed
Assert.IsTrue(d.Test.NextGen);
Assert.AreEqual(3, d.Test.LiveGen);
// panic!
SnapDictionary<int, string>.Snapshot s2 = d.CreateSnapshot();
Assert.IsTrue(d.Test.IsLocked);
Assert.IsNotNull(d.Test.GenObj);
Assert.AreEqual(2, d.Test.GenObj.Gen);
Assert.AreEqual(3, d.Test.LiveGen);
Assert.IsTrue(d.Test.NextGen);
// release writer
scopeContext.ScopeExit(true);
Assert.IsFalse(d.Test.IsLocked);
Assert.IsNotNull(d.Test.GenObj);
Assert.AreEqual(2, d.Test.GenObj.Gen);
Assert.AreEqual(3, d.Test.LiveGen);
Assert.IsTrue(d.Test.NextGen);
SnapDictionary<int, string>.Snapshot s3 = d.CreateSnapshot();
Assert.IsFalse(d.Test.IsLocked);
Assert.IsNotNull(d.Test.GenObj);
Assert.AreEqual(3, d.Test.GenObj.Gen);
Assert.AreEqual(3, d.Test.LiveGen);
Assert.IsFalse(d.Test.NextGen);
}
private ICoreScopeProvider GetScopeProvider(ScopeContext scopeContext = null)
{
ICoreScopeProvider scopeProvider = Mock.Of<ICoreScopeProvider>();
Mock.Get(scopeProvider)
.Setup(x => x.Context).Returns(scopeContext);
return scopeProvider;
}
}
/// <summary>
/// Used for tests so that we don't have to wrap every Set/Clear call in locks
/// </summary>
public static class SnapDictionaryExtensions
{
internal static void Set<TKey, TValue>(this SnapDictionary<TKey, TValue> d, TKey key, TValue value)
where TValue : class
{
using (d.GetScopedWriteLock(GetScopeProvider()))
{
d.SetLocked(key, value);
}
}
internal static void Clear<TKey, TValue>(this SnapDictionary<TKey, TValue> d)
where TValue : class
{
using (d.GetScopedWriteLock(GetScopeProvider()))
{
d.ClearLocked();
}
}
internal static void Clear<TKey, TValue>(this SnapDictionary<TKey, TValue> d, TKey key)
where TValue : class
{
using (d.GetScopedWriteLock(GetScopeProvider()))
{
d.ClearLocked(key);
}
}
private static IScopeProvider GetScopeProvider()
{
IScopeProvider scopeProvider = Mock.Of<IScopeProvider>();
Mock.Get(scopeProvider)
.Setup(x => x.Context).Returns(() => null);
return scopeProvider;
}
}
}