public void TheCurrentThreadUsesTheThreadDefaultContextInsteadOfInheritingTheGlobalContext()
        {
            mgr.GlobalContext = new StubTestContext();

            StubTestContext context = new StubTestContext();
            mgr.SetThreadDefaultContext(Thread.CurrentThread, context);

            Assert.AreSame(context, mgr.GetThreadDefaultContext(Thread.CurrentThread));
            Assert.AreSame(context, mgr.CurrentContext);
            Assert.AreNotSame(context, mgr.GlobalContext);
        }
        public void TheCurrentThreadUsesTheEnteredContextInsteadOfItsDefaultContext()
        {
            StubTestContext enteredContext = new StubTestContext();
            mgr.EnterContext(enteredContext);

            StubTestContext context = new StubTestContext();
            mgr.SetThreadDefaultContext(Thread.CurrentThread, context);

            Assert.AreSame(context, mgr.GetThreadDefaultContext(Thread.CurrentThread));
            Assert.AreSame(enteredContext, mgr.CurrentContext);
            Assert.IsNull(mgr.GlobalContext);
        }
        public void TheCurrentThreadInheritsTheGlobalContext()
        {
            StubTestContext context = new StubTestContext();
            mgr.GlobalContext = context;

            Assert.AreSame(context, mgr.GlobalContext);
            Assert.AreSame(context, mgr.CurrentContext);

            mgr.GlobalContext = null;
            Assert.IsNull(mgr.GlobalContext);
            Assert.IsNull(mgr.CurrentContext);
        }
        public void TheEnteredContextPropagatesAcrossThreadsWithIndependentStacks()
        {
            StubTestContext rootContext = new StubTestContext();
            mgr.EnterContext(rootContext);
            Assert.AreSame(rootContext, mgr.CurrentContext);

            Tasks.StartThreadTask("A", delegate
            {
                Assert.AreSame(rootContext, mgr.CurrentContext);

                StubTestContext leafContext = new StubTestContext();
                mgr.EnterContext(leafContext);
                Assert.AreSame(leafContext, mgr.CurrentContext);

                Tasks.StartThreadTask("B", delegate
                {
                    Assert.AreSame(leafContext, mgr.CurrentContext);

                    StubTestContext leafContext2 = new StubTestContext();
                    mgr.EnterContext(leafContext2);
                    Assert.AreSame(leafContext2, mgr.CurrentContext);
                }).Join(new TimeSpan(0, 0, 1));

                Assert.AreSame(leafContext, mgr.CurrentContext);
            }).Join(new TimeSpan(0, 0, 1));

            Assert.AreSame(rootContext, mgr.CurrentContext);

            Tasks.JoinAndVerify(new TimeSpan(0, 0, 5));
        }
        public void TheEnteredContextCanBeNullWhichWillOverrideEvenTheGlobalContext()
        {
            StubTestContext rootContext = new StubTestContext();
            mgr.GlobalContext = rootContext;

            using (mgr.EnterContext(null))
            {
                Assert.IsNull(mgr.CurrentContext);

                StubTestContext context1 = new StubTestContext();
                using (mgr.EnterContext(context1))
                {
                    Assert.AreSame(context1, mgr.CurrentContext);

                    using (mgr.EnterContext(null))
                    {
                        Assert.IsNull(mgr.CurrentContext);

                        StubTestContext context2 = new StubTestContext();
                        using (mgr.EnterContext(context2))
                        {
                            Assert.AreSame(context2, mgr.CurrentContext);
                        }

                        Assert.IsNull(mgr.CurrentContext);
                    }

                    Assert.AreSame(context1, mgr.CurrentContext);
                }

                Assert.IsNull(mgr.CurrentContext);
            }

            Assert.AreSame(rootContext, mgr.CurrentContext);
        }
        public void TheEnteredContextIsExitedWhenTheCookieIsDisposed()
        {
            StubTestContext context1 = new StubTestContext();
            using (mgr.EnterContext(context1))
            {
                Assert.AreSame(context1, mgr.CurrentContext);

                StubTestContext context2 = new StubTestContext();
                using (mgr.EnterContext(context2))
                {
                    Assert.AreSame(context2, mgr.CurrentContext);
                }

                Assert.AreSame(context1, mgr.CurrentContext);
            }

            Assert.IsNull(mgr.CurrentContext);
        }