<commit_before>﻿namespace Sitecore.FakeDb.Tests
{
  using System;
  using System.Threading;
  using System.Threading.Tasks;
  using FluentAssertions;
  using Xunit;

  public class DbConcurrencyTest
  {
    [Fact]
    public void ShouldBeThreadSafe()
    {
      // arrange
      var create = new Task(Create);
      var assert = new Task(Assert);

      create.Start();
      assert.Start();

      create.Wait();
      assert.Wait();
    }

    private static void Create()
    {
      var random = new Random();

      using (var db = new Db { new DbItem("Home") })
      {
        Thread.Sleep(random.Next(1000));
        db.GetItem("/sitecore/content/home").Should().NotBeNull("the Home item is expected");
      }
    }

    private static void Assert()
    {
      var random = new Random();

      using (var db = new Db())
      {
        Thread.Sleep(random.Next(1000));
        db.GetItem("/sitecore/content/home").Should().BeNull("the Home item is not expected");
      }
    }
  }
}<commit_msg>Added concurrency tests for pipelines and settings<commit_after>﻿namespace Sitecore.FakeDb.Tests
{
  using System;
  using System.Threading;
  using System.Threading.Tasks;
  using FluentAssertions;
  using Xunit;
  using Sitecore.Xml;
  using Sitecore.Configuration;
  using Sitecore.Pipelines;

  public class DbConcurrencyTest
  {
    [Fact]
    public void ShouldBeThreadLocalDb()
    {
      // arrange
      var t1 = Task.Factory.StartNew(() =>
        {
          var random = new Random();
          using (var db = new Db { new DbItem("Home") })
          {
            Thread.Sleep(random.Next(1000));
            db.GetItem("/sitecore/content/home").Should().NotBeNull("the Home item is expected");
          }
        });

      var t2 = Task.Factory.StartNew(() =>
        {
          var random = new Random();
          using (var db = new Db())
          {
            Thread.Sleep(random.Next(1000));
            db.GetItem("/sitecore/content/home").Should().BeNull("the Home item is not expected");
          }
        });

      t1.Wait();
      t2.Wait();
    }

    [Fact(Skip = "To be implemented.")]
    public void ShouldBeThreadLocalPipelines()
    {
      var t1 = Task.Factory.StartNew(() =>
      {
        using (var db = new Db())
        {
          db.PipelineWatcher.Expects("mypipeline");

          Thread.Sleep(1000);

          CorePipeline.Run("mypipeline", new PipelineArgs());
          db.PipelineWatcher.EnsureExpectations();
        }
      });

      var t2 = Task.Factory.StartNew(() =>
      {
        using (var db = new Db()) { }
      });

      t1.Wait();
    }

    [Fact(Skip = "To be implemented.")]
    public void ShouldBeThreadLocalSettings()
    {
      var t1 = Task.Factory.StartNew(() =>
      {
        using (var db = new Db())
        {
          db.Configuration.Settings["mysetting"] = "123";

          Thread.Sleep(1000);

          Settings.GetSetting("mysetting").Should().Be("123");
        }
      });

      var t2 = Task.Factory.StartNew(() =>
      {
        using (var db = new Db())
        {
          db.Configuration.Settings["mysetting"] = "abc";
          Settings.GetSetting("mysetting").Should().Be("abc");
        }
      });

      t1.Wait();
    }
  }
}
{'fraction_non_alphanumeric': 0.14017252002464572, 'fraction_numerical': 0.012322858903265557, 'mean_word_length': 1.9788990825688073, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3131604', 'n_tokens_mistral': 1006, 'n_tokens_neox': 948, 'n_words': 207}