Exemplo n.º 1
0
        public void SelectMany_WhenComposingDisposableFactoryWithDisposable_ThenUsableCallsDispose()
        {
            using (var trace = new TraceListenerScope())
            {
                var usable =
                    from outer in new Func <TraceSourceScope>(() => new TraceSourceScope("outer"))
                    from inner in new TraceSourceScope("inner")
                    select inner;
                var expectedTrace = new string[]
                {
                    "Enter: outer",
                    "    Enter: inner",
                    "    Leave: inner",
                    "Leave: outer",
                };

                Assert.Equal(new string[0], trace.ToLines());

                var value = usable.Value();

                Assert.Equal(expectedTrace, trace.ToLines());
                Assert.Throws <ObjectDisposedException>(() => value.Dispose());
            }

            Assert.Equal(0, Trace.IndentLevel);
        }
Exemplo n.º 2
0
        public void WhenUsableCreatedInFirstFrom_ThenInstanceExists()
        {
            using (var trace = new TraceListenerScope())
            {
                var usable =
                    from outer in new TraceSourceScope("outer").AsUsableOnece()
                    from inner in new TraceSourceScope($"{outer.Operation}/inner")
                    select $"{inner.Operation}/value".Trace();

                // TraceSourceScope is created and then converted to IUsable. This instance exists
                // whether usable was used or not. It gets disposed after usable is used.
                var expectedTraceBefore = new string[]
                {
                    "Enter: outer",
                };

                Assert.Equal(expectedTraceBefore, trace.ToLines());

                // Clean up.
                var value = usable.Value();


                var expectedTraceAfter = new string[]
                {
                    "Enter: outer",
                    "    Enter: outer/inner",
                    "        Value: outer/inner/value",
                    "    Leave: outer/inner",
                    "Leave: outer",
                };
                Assert.Equal(expectedTraceAfter, trace.ToLines());
            }
            Assert.Equal(0, Trace.IndentLevel);
        }
Exemplo n.º 3
0
        public void Stopwatch_WhenMultipleOperations_ThenElapsedIncrements()
        {
            using (var trace = new TraceListenerScope())
            {
                var usable =
                    // Stopwatch usable starts counting the moment somebody calls Use().
                    from stopwatch in Usable.Stopwatch()
                    from outer in Usable.Using(() => new TraceSourceScope("outer"))
                    // Snapshot elapsed to know how long it took to instantiate outer.
                    let elapsedOuter = stopwatch.Elapsed
                                       from inner in new TraceSourceScope("inner")
                                       // Snapshot elapsed to know how long it took to instantiate inner.
                                       let elapsedInner = stopwatch.Elapsed - elapsedOuter
                                       // Snapshot elapsed to know how long it took for the entire operation.
                                                          let elapsedTotal = stopwatch.Elapsed
                                       // Return computed value together with all the timing information.
                                                                             select new
                {
                    Value        = string.Join('/', outer.Operation, inner.Operation, "value").Trace(),
                    ElapsedOuter = elapsedOuter,
                    ElapsedInner = elapsedInner,
                    ElapsedTotal = elapsedTotal,
                };
                var expectedTrace = new string[]
                {
                    "Enter: outer",
                    "    Enter: inner",
                    "        Value: outer/inner/value",
                    "    Leave: inner",
                    "Leave: outer",
                };

                Assert.Equal(new string[0], trace.ToLines());

                var value = usable.Value();
                Assert.Equal("outer/inner/value", value.Value);
                Assert.True(value.ElapsedOuter < value.ElapsedTotal);
                Assert.True(value.ElapsedInner < value.ElapsedTotal);
                Assert.Equal(expectedTrace, trace.ToLines());
            }

            Assert.Equal(0, Trace.IndentLevel);
        }
Exemplo n.º 4
0
        public void WhenTraceSourceScopeCreatedWithUsableCreate_ThenItIsLazy()
        {
            using (var trace = new TraceListenerScope())
            {
                var usable =
                    from outer in Usable.Create(() => new TraceSourceScope("outer"), s => s.Dispose())
                    from inner in new TraceSourceScope($"{outer.Operation}/inner")
                    select $"{inner.Operation}/value".Trace();

                Assert.Equal(new string[] { }, trace.ToLines());

                var value = usable.Value();
            }
            Assert.Equal(0, Trace.IndentLevel);
        }
Exemplo n.º 5
0
        public void WhenTraceSourceScopeCreatedFromUsable_ThenItIsLazy()
        {
            using (var trace = new TraceListenerScope())
            {
                var usable =
                    from name in "outer".AsUsable()
                    from outer in new TraceSourceScope(name)
                    from inner in new TraceSourceScope($"{outer.Operation}/inner")
                    select $"{inner.Operation}/value".Trace();

                Assert.Equal(new string[] { }, trace.ToLines());

                var value = usable.Value();
            }
            Assert.Equal(0, Trace.IndentLevel);
        }
Exemplo n.º 6
0
        public void Select_WhenDisposableSelector_ThenUsableDoesNotcallDispose()
        {
            using (var trace = new TraceListenerScope())
            {
                var usable =
                    from name in "outer".AsUsable()
                    select new TraceSourceScope(name);
                var expectedTrace = new string[]
                {
                    "Enter: outer",
                };

                var value = usable.Value();

                Assert.Equal(expectedTrace, trace.ToLines());

                value.Dispose();
            }
            Assert.Equal(0, Trace.IndentLevel);
        }
Exemplo n.º 7
0
        public void SelectMany_WhenCompositingWithClassWhichImplementsIUsable_ThenCastIsNeeded()
        {
            using (var trace = new TraceListenerScope())
            {
                var usable =
                    from outer in new TraceSourceScopeUsable("outer")
                    from inner in new TraceSourceScopeUsable("inner") as IUsable <TraceSourceScope>
                    select string.Join('/', outer.Operation, inner.Operation, "value").Trace();

                var expectedTrace = new string[]
                {
                    "Enter: outer",
                    "    Enter: inner",
                    "        Value: outer/inner/value",
                    "    Leave: inner",
                    "Leave: outer"
                };

                var value = usable.Value();

                Assert.Equal(expectedTrace, trace.ToLines());
            }
            Assert.Equal(0, Trace.IndentLevel);
        }
Exemplo n.º 8
0
        public void SelectMany_WhenDisposableSelectorFrom_ThenUsableCallsDispose()
        {
            using (var trace = new TraceListenerScope())
            {
                var usable =
                    from name in "outer".AsUsable()
                    from outer in new TraceSourceScope(name)
                    from inner in new TraceSourceScope("inner")
                    select inner;
                var expectedTrace = new string[]
                {
                    "Enter: outer",
                    "    Enter: inner",
                    "    Leave: inner",
                    "Leave: outer",
                };

                var value = usable.Value();

                Assert.Equal(expectedTrace, trace.ToLines());
                Assert.Throws <ObjectDisposedException>(() => value.Dispose());
            }
            Assert.Equal(0, Trace.IndentLevel);
        }
Exemplo n.º 9
0
        public void WhenInnerCreatedFromOuter_ThenDisposedInTheRightOrder()
        {
            using (var trace = new TraceListenerScope())
            {
                var usable =
                    from outer in new TraceSourceScope("outer").AsUsableOnece()
                    from inner in new TraceSourceScope($"{outer.Operation}/inner")
                    select $"{inner.Operation}/value".Trace();
                var expectedTrace = new string[]
                {
                    "Enter: outer",
                    "    Enter: outer/inner",
                    "        Value: outer/inner/value",
                    "    Leave: outer/inner",
                    "Leave: outer"
                };

                var value = usable.Value();

                Assert.Equal("outer/inner/value", value);
                Assert.Equal(expectedTrace, trace.ToLines());
            }
            Assert.Equal(0, Trace.IndentLevel);
        }