public void Trace_From_ManyThreads() { TracerConfig.Reset("null;* *"); StringListTraceListener stringTracer = new StringListTraceListener(); TracerConfig.Listeners.Add(stringTracer); List <Thread> threads = new List <Thread>(); int Threads = 5; int GenerateCount = 10 * 1000; int LinesPerGenerate = 5; for (int i = 0; i < Threads; i++) { Thread t = new Thread(new ParameterizedThreadStart(GenerateTraces)); t.IsBackground = true; t.Start(GenerateCount); threads.Add(t); } threads.ForEach((t) => t.Join()); Assert.AreEqual(Threads * GenerateCount * LinesPerGenerate, stringTracer.Messages.Count); HashSet <int> threadIds = new HashSet <int>(); foreach (var line in stringTracer.Messages) { threadIds.Add(int.Parse(line.Substring(20, 4))); } Assert.AreEqual(Threads, threadIds.Count); }
public void Trace_Only_Exceptions() { TracerConfig.Reset("null;* Exception"); StringListTraceListener stringTracer = new StringListTraceListener(); TracerConfig.Listeners.Add(stringTracer); const int Runs = 20; for (int i = 0; i < Runs; i++) { string exStr = "Ex Nr" + i; try { using (Tracer tr1 = new Tracer(myType, "Enclosing Method")) { using (Tracer tracer = new Tracer(myType, "Thread Method")) { throw new NotImplementedException(exStr); } } } catch (Exception) { } Assert.AreEqual(i + 1, stringTracer.Messages.Count); Assert.IsTrue(stringTracer.Messages[i].Contains(exStr), String.Format("Got {0} but did not find substring {1}", stringTracer.Messages[i], exStr)); } }
public void Every_Thread_Prints_His_Exception_Only_Once() { TracerConfig.Reset("null"); StringListTraceListener stringTracer = new StringListTraceListener(); TracerConfig.Listeners.Add(stringTracer); ThreadStart method = () => { try { using (Tracer tr1 = new Tracer(myType, "Enclosing Method")) { using (Tracer tracer = new Tracer(myType, "Thread Method")) { throw new NotImplementedException(Thread.CurrentThread.Name); } } } catch (Exception) { } }; List <Thread> threads = new List <Thread>(); const int ThreadCount = 3; List <string> threadNames = new List <string>(); for (int i = 0; i < ThreadCount; i++) { Thread t = new Thread(method); string threadName = "Tracer Thread " + i; t.Name = threadName; threadNames.Add(threadName); t.Start(); threads.Add(t); } threads.ForEach(t => t.Join()); var exLines = stringTracer.GetMessages(line => line.Contains("Exception")); Assert.AreEqual(ThreadCount, exLines.Count); for (int i = 0; i < threadNames.Count; i++) { Assert.IsTrue(exLines.Any(traceLine => traceLine.Contains(threadNames[i])), String.Format("Thread with name {0} not found in output", exLines[i])); } Assert.AreEqual(ThreadCount * 5, stringTracer.Messages.Count); }
public void When_Level1_And_Error_IsEnabled_NothingElse_Must_Pass() { TracerConfig.Reset("null;* l1+error"); StringListTraceListener stringTracer = new StringListTraceListener(); TracerConfig.Listeners.Add(stringTracer); GenerateLevelTraces(Level.L1); try { Assert.AreEqual(4, stringTracer.Messages.Count); } finally { ExceptionHelper.WhenException(() => stringTracer.Messages.ForEach((str) => Console.Write(str))); } }
public void When_Level2_IsEnabled_NoOtherTracesArrive() { TracerConfig.Reset("null;* level2"); StringListTraceListener stringTracer = new StringListTraceListener(); TracerConfig.Listeners.Add(stringTracer); GenerateLevelTraces(Level.L1); try { Assert.AreEqual(0, stringTracer.Messages.Count); } finally { ExceptionHelper.WhenException(() => stringTracer.Messages.ForEach((str) => Console.Write(str))); } }