ThreadStaticTests ClassDelta Engine Documentation
Inheritance Hierarchy

System Object
  DeltaEngine.Tests.Extensions ThreadStaticTests

Namespace: DeltaEngine.Tests.Extensions
Assembly: DeltaEngine.Tests (in DeltaEngine.Tests.dll) Version: 1.1.1.0 (1.1.1)
Syntax

public class ThreadStaticTests

The ThreadStaticTests type exposes the following members.

Constructors

  NameDescription
Public methodThreadStaticTests
Initializes a new instance of the ThreadStaticTests class
Top
Methods

  NameDescription
Public methodCurrentIsAvailableOnlyWithinScopeIfNoFallbackSet
Public methodCurrentIsAvailableOutsideScopeIfFallbackSet
Public methodDisposingInnerScopeTwiceHasNoEffect
Public methodDisposingMidScopeJumpsToOuterScope
Public methodDisposingOnADifferentThreadToCreationThrowsException
Public methodMultipleScopesPushAndPopCorrectly
Public methodNewThreadStaticVariableHasADefaultValue
Public methodNewThreadStaticVariableHasNoCurrentValue
Top
Examples

[Test]
public void NewThreadStaticVariableHasNoCurrentValue()
{
    var threadStatic = new ThreadStatic<string>();
    Assert.IsFalse(threadStatic.HasCurrent);
    Assert.Throws<ThreadStatic<string>.NoValueAvailable>(
        () => Assert.IsNotNull(threadStatic.Current));
}
[Test]
public void NewThreadStaticVariableHasADefaultValue()
{
    var threadStatic = new ThreadStatic<int>();
    Assert.AreEqual(0, threadStatic.CurrentOrDefault);
}
[Test]
public void CurrentIsAvailableOnlyWithinScopeIfNoFallbackSet()
{
    var threadStatic = new ThreadStatic<string>();
    using (threadStatic.Use("abc"))
        Assert.AreEqual("abc", threadStatic.Current);
    Assert.IsFalse(threadStatic.HasCurrent);
}
[Test]
public void CurrentIsAvailableOutsideScopeIfFallbackSet()
{
    var threadStatic = new ThreadStatic<Randomizer>(new PseudoRandom());
    using (threadStatic.Use(new FixedRandom()))
        Assert.IsTrue(threadStatic.Current is FixedRandom);
    Assert.IsTrue(threadStatic.Current is PseudoRandom);
}
[Test]
public void MultipleScopesPushAndPopCorrectly()
{
    var threadStatic = new ThreadStatic<int>();
    using (threadStatic.Use(1))
    {
        Assert.AreEqual(1, threadStatic.Current);
        TestMidScope(threadStatic);
        Assert.AreEqual(1, threadStatic.Current);
    }
}
[Test]
public void DisposingMidScopeJumpsToOuterScope()
{
    var threadStatic = new ThreadStatic<int>();
    threadStatic.Use(1);
    var midScope = threadStatic.Use(2);
    threadStatic.Use(3);
    Assert.AreEqual(3, threadStatic.Current);
    midScope.Dispose();
    Assert.AreEqual(1, threadStatic.Current);
}
[Test]
public void DisposingInnerScopeTwiceHasNoEffect()
{
    var threadStatic = new ThreadStatic<int>();
    threadStatic.Use(1);
    var innerScope = threadStatic.Use(2);
    Assert.AreEqual(2, threadStatic.Current);
    innerScope.Dispose();
    innerScope.Dispose();
    Assert.AreEqual(1, threadStatic.Current);
}
[Test, Ignore]
public void DisposingOnADifferentThreadToCreationThrowsException()
{
    var worker = new BackgroundWorker();
    worker.DoWork += CreateScope;
    worker.RunWorkerCompleted += DisposeScope;
    worker.RunWorkerAsync();
    Thread.Sleep(1);
}
See Also