#region Copyright
// Diagnostic Explorer, a .Net diagnostic toolset
// Copyright (C) 2010 Cameron Elliot
//
// This file is part of Diagnostic Explorer.
//
// Diagnostic Explorer is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Diagnostic Explorer is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Diagnostic Explorer. If not, see <http://www.gnu.org/licenses/>.
//
// http://diagexplorer.sourceforge.net/
#endregion
#if TESTS
using System;
using System.Collections.Generic;
using System.Threading;
using DiagnosticExplorer;
using log4net;
using NUnit.Framework;
namespace DiagnosticExplorer.Test
{
[TestFixture]
public class TraceScopeTest
{
private static ILog _logger = LogManager.GetLogger(typeof (TraceScopeTest));
[Test]
public void TestFirstTraceAction()
{
bool action1 = false;
bool action2 = false;
bool action3 = false;
using (TraceScope scope = new TraceScope(str => action1 = true))
{
scope.SetTraceAction(100, str => action2 = true);
scope.SetTraceAction(200, str => action3 = true);
Thread.Sleep(50);
}
Assert.IsTrue(action1);
Assert.IsFalse(action2);
Assert.IsFalse(action3);
}
[Test]
public void TestSecondTraceAction()
{
bool action1 = false;
bool action2 = false;
bool action3 = false;
using (TraceScope scope = new TraceScope(str => action1 = true))
{
scope.SetTraceAction(100, str => action2 = true);
scope.SetTraceAction(200, str => action3 = true);
Thread.Sleep(150);
}
Assert.IsFalse(action1);
Assert.IsTrue(action2);
Assert.IsFalse(action3);
}
[Test]
public void TestThirdTraceAction()
{
bool action1 = false;
bool action2 = false;
bool action3 = false;
using (TraceScope scope = new TraceScope(str => action1 = true))
{
scope.SetTraceAction(100, str => action2 = true);
scope.SetTraceAction(200, str => action3 = true);
Thread.Sleep(250);
}
Assert.IsFalse(action1);
Assert.IsFalse(action2);
Assert.IsTrue(action3);
}
[Test]
public void TestLastTraceAction()
{
bool action1 = false;
bool action2 = false;
bool action3 = false;
using (TraceScope scope = new TraceScope(str => action1 = true))
{
scope.SetTraceAction(100, str => action2 = true);
scope.SetTraceAction(200, str => action3 = true);
Thread.Sleep(400);
}
Assert.IsFalse(action1);
Assert.IsFalse(action2);
Assert.IsTrue(action3);
}
[Test]
public void Test1()
{
using (new TraceScope("Fred", _logger.Error))
{
TraceScope.Trace("One");
TraceScope.Trace("Two");
TraceScope.Trace("Three");
}
}
[Test]
public void Test2()
{
One();
One();
One();
One();
One();
}
private void One()
{
using (new TraceScope(Console.WriteLine))
{
TraceScope.Trace("One");
Thread.Sleep(100);
Two<int, string>(null);
}
}
private void Two<T1, T2>(List<T1> someList)
{
using (new TraceScope(Console.WriteLine))
{
TraceScope.Trace("Two");
Thread.Sleep(100);
Three<int, long, double>(null, null, null);
}
}
private void Three<T1, T2, T3>(List<T1> one, List<T2> two, List<T3> three)
{
using (new TraceScope())
{
TraceScope.Trace("Three");
Thread.Sleep(100);
Four();
}
}
private void Four()
{
using (new TraceScope())
{
TraceScope.Trace("Four");
Thread.Sleep(100);
Five();
}
}
private void Five()
{
using (new TraceScope())
{
TraceScope.Trace("Five");
}
}
}
}
#endif //TESTS