Welcome to mirror list, hosted at ThFree Co, Russian Federation.

TraceTest.cs « System.Diagnostics « Test « System « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 25354a19ee5571ce6aca381f0c679fb580aec7cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
//
// TraceTest.cs - NUnit Test Cases for System.Diagnostics.Trace
//
// Authors:
//   Jonathan Pryor (jonpryor@vt.edu)
//   Martin Willemoes Hansen (mwh@sysrq.dk)
//
// (C) Jonathan Pryor
// (C) 2003 Martin Willemoes Hansen
// 

// We want tracing enabled, so...
#define TRACE

using NUnit.Framework;
using System;
using System.IO;
using System.Diagnostics;
using System.Threading;

namespace MonoTests.System.Diagnostics {

	[TestFixture]
	public class TraceTest {
    
		private StringWriter buffer;
		private TraceListener listener;

		[SetUp]
		public void GetReady ()
		{
			// We don't want to deal with the default listener, which can send the
			// output to various places (Debug stream, Console.Out, ...)
			// Trace.Listeners.Remove ("Default");

			buffer = new StringWriter ();
			listener = new TextWriterTraceListener (buffer, "TestOutput");
			Trace.Listeners.Clear ();
			Trace.Listeners.Add (listener);
			Trace.AutoFlush = true;
		}

		[TearDown]
		public void Clear ()
		{
			// Trace.Listeners.Add (new DefaultTraceListener ());
			Trace.Listeners.Remove (listener);
		}

		// Make sure that when we get the output we expect....
		[Test]
		public void Tracing ()
		{
			Trace.IndentLevel = 0;
			Trace.IndentSize = 4;

			string value =  
				"Entering Main" + Environment.NewLine +
				"Exiting Main" + Environment.NewLine;

			Trace.WriteLine ("Entering Main");
			Trace.WriteLine ("Exiting Main");

			Assert.AreEqual (value, buffer.ToString (), "#Tr01");
		}

		// Make sure we get the output we expect in the presence of indenting...
		[Test]
		public void Indent ()
		{
			Trace.IndentLevel = 0;
			Trace.IndentSize = 4;

			string value =  
				"List of errors:" + Environment.NewLine +
				"    Error 1: File not found" + Environment.NewLine +
				"    Error 2: Directory not found" + Environment.NewLine +
				"End of list of errors" + Environment.NewLine;

			Trace.WriteLine ("List of errors:");
			Trace.Indent ();
			Assert.AreEqual (1, Trace.IndentLevel);
			Trace.WriteLine ("Error 1: File not found");
			Trace.WriteLine ("Error 2: Directory not found");
			Trace.Unindent ();
			Assert.AreEqual (0, Trace.IndentLevel);
			Trace.WriteLine ("End of list of errors");

			Assert.AreEqual (value, buffer.ToString(), "#In01");
		}

		// Make sure that TraceListener properties (IndentLevel, IndentSize) are
		// modified when the corresponding Trace properties are changed.
		[Test]
		public void AddedTraceListenerProperties ()
		{
			TraceListener t1 = new TextWriterTraceListener (Console.Out);
			TraceListener t2 = new TextWriterTraceListener (Console.Error);
			Trace.Listeners.Add(t1);
			Trace.Listeners.Add(t2);

			const int ExpectedSize = 5;
			const int ExpectedLevel = 2;

			Trace.IndentSize = ExpectedSize;
			Trace.IndentLevel = ExpectedLevel;

			foreach (TraceListener t in Trace.Listeners) {
				string ids = "#TATLP-S-" + t.Name;
				string idl = "#TATLP-L-" + t.Name;
				Assert.AreEqual (ExpectedSize, t.IndentSize, ids);
				Assert.AreEqual (ExpectedLevel, t.IndentLevel, idl);
			}

			Trace.Listeners.Remove(t1);
			Trace.Listeners.Remove(t2);
		}

		// Make sure that the TraceListener properties (IndentLevel, IndentSize)
		// are properly modified when the TraceListener is added to the
		// collection.
		[Test]
		public void Listeners_Add_Values()
		{
			const int ExpectedLevel = 0;
			const int ExpectedSize = 4;
			Trace.IndentLevel = ExpectedLevel;
			Trace.IndentSize = ExpectedSize;
			TraceListener tl = new TextWriterTraceListener(Console.Out);

			tl.IndentLevel = 2*ExpectedLevel;
			tl.IndentSize = 2*ExpectedSize;

			Trace.Listeners.Add(tl);

			// Assertion.Assert that the listener we added has been set to the correct indent
			// level.
			Assert.AreEqual (ExpectedLevel, tl.IndentLevel, "#LATL-L");
			Assert.AreEqual (ExpectedSize, tl.IndentSize, "#LATL-S");

			// Assertion.Assert that all listeners in the collection have the same level.
			foreach (TraceListener t in Trace.Listeners)
			{
				string idl = "#LATL-L:" + t.Name;
				string ids = "#LATL-S:" + t.Name;
				Assert.AreEqual (ExpectedLevel, t.IndentLevel, idl);
				Assert.AreEqual (ExpectedSize, t.IndentSize, ids);
			}
		}

		// IndentSize, IndentLevel are thread-static

		class MyTraceListener : TraceListener
		{
			public int Writes;
			public int WriteLines;

			public MyTraceListener ()
				: base ("mt-test")
			{
			}

			public override void Write (string msg)
			{
				++Writes;
			}

			public override void WriteLine (string msg)
			{
				++WriteLines;
			}
		}

		class MultiThreadModify
		{
			public MyTraceListener listener = new MyTraceListener ();

			public const int MaxIterations = 10000;

			public String Exception = null;

			public MultiThreadModify ()
			{
				Trace.Listeners.Add (listener);
			}

			public void Write ()
			{
				try {
					for (int i = 0; i < MaxIterations; ++i)
						Trace.WriteLine ("message " + i + "... ");
				}
				catch (Exception e) {
					Exception = string.Format (
							"#MTMW: Exception emitted from Trace.WriteLine: {0}", e);
				}
			}

			public void Remove ()
			{
				try {
					Trace.Listeners.Remove (listener);
				}
				catch (Exception e) {
					Exception = string.Format (
							"#MTMR: Exception emitted from Trace.Listeners.Remove: {0}", e);
				}
			}
		}

		[Test]
		[Category ("NotWorking")]
		// Is this even valid !?!?!?!
		public void TestMultiThreadModify ()
		{
			MultiThreadModify m = new MultiThreadModify ();

			Thread t1 = new Thread (new ThreadStart (m.Write));
			Thread t2 = new Thread (new ThreadStart (m.Remove));

			t1.Start ();
			t2.Start ();

			t1.Join ();
			t2.Join ();

			Assert.IsTrue (m.Exception == null, m.Exception);
			Assert.AreEqual (MultiThreadModify.MaxIterations, m.listener.WriteLines,
					"#tmtm: listener was removed before iterations were completed");
		}
	}
}