1 // Copyright (c) Microsoft. All rights reserved.
2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 
4 using System;
5 using System.Collections.Generic;
6 using System.Collections;
7 using System.Text;
8 using System.Threading;
9 using NUnit.Framework;
10 using Microsoft.Build.BuildEngine;
11 using Microsoft.Build.Framework;
12 using Microsoft.Build.BuildEngine.Shared;
13 using System.Diagnostics;
14 using System.Reflection;
15 
16 // All of these tests were created by Chris Mann and Jeff Callahan
17 namespace Microsoft.Build.UnitTests
18 {
19     #region EngineLoggingHelper
20     /// <summary>
21     /// This class is a basic implementation of EngineLoggingServices so that we can try and test the abstract EngineLoggingServices
22     /// class without a lot of other methods being added
23     /// </summary>
24     internal class EngineLoggingServicesHelper : EngineLoggingServices
25     {
26         /// <summary>
27         /// Reading queue
28         /// </summary>
29         DualQueue<BuildEventArgs> currentQueueBuildEvent;
30         /// <summary>
31         /// Reading queue
32         /// </summary>
33         DualQueue<NodeLoggingEvent> currentQueueNodeEvent;
34 
EngineLoggingServicesHelper()35         internal EngineLoggingServicesHelper()
36         {
37             base.Initialize(new ManualResetEvent(false));
38         }
39 
40         /// <summary>
41         /// We dont need to do anything to process events, we just want to get what events are in the queue
42         /// </summary>
ProcessPostedLoggingEvents()43         internal override bool ProcessPostedLoggingEvents()
44         {
45             currentQueueBuildEvent = loggingQueueOfBuildEvents;
46             currentQueueNodeEvent  = loggingQueueOfNodeEvents;
47 
48             return true;
49         }
50 
51         /// <summary>
52         /// Note that this "get" indirectly calls GetCurrentReadingQueue() which
53         /// returns references to distinct queues on sequential calls; in other
54         /// words this property has side effects on the internal data structures.
55         /// </summary>
GetCurrentQueueBuildEvents()56         internal DualQueue<BuildEventArgs> GetCurrentQueueBuildEvents()
57         {
58             ProcessPostedLoggingEvents();
59             return currentQueueBuildEvent;
60         }
61 
62         /// <summary>
63         /// Note that this "get" indirectly calls GetCurrentReadingQueue() which
64         /// returns references to distinct queues on sequential calls; in other
65         /// words this property has side effects on the internal data structures.
66         /// </summary>
GetCurrentQueueNodeEvents()67         internal DualQueue<NodeLoggingEvent> GetCurrentQueueNodeEvents()
68         {
69             ProcessPostedLoggingEvents();
70             return loggingQueueOfNodeEvents;
71         }
72 
73     }
74     #endregion
75 
76     [TestFixture]
77     public class EngineLoggingServices_Tests
78     {
79         // A simple implementation of the abstract class EngineLoggingServices
80         EngineLoggingServicesHelper engineLoggingServicesHelper;
81 
82         /// <summary>
83         /// Generate a generic BuildErrorEventArgs
84         /// </summary>
85         /// <param name="message">message to put in the event</param>
86         /// <returns>Event</returns>
GenerateBuildErrorEventArgs(string message)87         private BuildErrorEventArgs GenerateBuildErrorEventArgs(string message)
88         {
89             return new BuildErrorEventArgs("SubCategory", "code", null, 0, 1, 2, 3, message, "Help", "EngineLoggingServicesTest");
90         }
91 
92         /// <summary>
93         /// Generate a generic BuildWarningEventArgs
94         /// </summary>
95         /// <param name="message">message to put in the event</param>
96         /// <returns>Event</returns>
GenerateBuildWarningEventArgs(string message)97         private BuildWarningEventArgs GenerateBuildWarningEventArgs(string message)
98         {
99             return new BuildWarningEventArgs("SubCategory", "code", null, 0, 1, 2, 3, message, "warning", "EngineLoggingServicesTest");
100         }
101 
102         /// <summary>
103         /// Generate a generic BuildMessageEventArgs
104         /// </summary>
105         /// <param name="message">message to put in the event</param>
106         /// <param name="importance">importance for the message</param>
107         /// <returns>Event</returns>
GenerateBuildMessageEventArgs(string message, MessageImportance importance)108         private BuildMessageEventArgs GenerateBuildMessageEventArgs(string message, MessageImportance importance)
109         {
110             return new BuildMessageEventArgs(message, "HelpKeyword", "senderName", importance);
111         }
112 
113         /// <summary>
114         /// A basic event derived from the abstract class CustomBuildEventArgs
115         /// </summary>
116         class MyCustomBuildEventArgs : CustomBuildEventArgs
117         {
MyCustomBuildEventArgs()118             public MyCustomBuildEventArgs() : base() { }
MyCustomBuildEventArgs(string message)119             public MyCustomBuildEventArgs(string message) : base(message, "HelpKeyword", "SenderName") { }
120         }
121 
122         /// <summary>
123         /// A custom BuildEventArgs derived from a CustomBuildEventArgs
124         /// </summary>
125         /// <param name="message">message to put in the event</param>
126         /// <returns></returns>
GenerateBuildCustomEventArgs(string message)127         private MyCustomBuildEventArgs GenerateBuildCustomEventArgs(string message)
128         {
129             return new MyCustomBuildEventArgs("testCustomBuildEvent");
130         }
131 
132         [SetUp]
SetUp()133         public void SetUp()
134         {
135             engineLoggingServicesHelper = new EngineLoggingServicesHelper();
136         }
137 
138         [TearDown]
TearDown()139         public void TearDown()
140         {
141             engineLoggingServicesHelper = null;
142         }
143 
144         #region TestEventBasedLoggingMethods
145         /// <summary>
146         /// Test the logging of and ErrorEvent
147         /// </summary>
148         [Test]
LogErrorEvent()149         public void LogErrorEvent()
150         {
151             List<BuildEventArgs> eventList = new List<BuildEventArgs>();
152 
153             // Log a number of events and then make sure that queue at the end contains all of those events
154             for (int i = 0; i < 10; i++)
155             {
156                 BuildErrorEventArgs eventToAdd = GenerateBuildErrorEventArgs("ErrorMessage" + i);
157                 eventList.Add(eventToAdd);
158                 engineLoggingServicesHelper.LogErrorEvent(eventToAdd);
159             }
160 
161             // Get the logging queue after we have logged a number of messages
162            DualQueue<BuildEventArgs> currentQueue = engineLoggingServicesHelper.GetCurrentQueueBuildEvents();
163 
164             // Assert that every event we sent to the logger exists in the queue
165             Assert.IsTrue(eventList.TrueForAll(delegate(BuildEventArgs args)
166                                                {
167                                                    return currentQueue.Contains(args);
168                                                }), "Expected to find all events sent to LogErrorEvent");
169 
170             // Assert that every event in the queue is of the correct type
171             AssertForEachEventInQueue(currentQueue, IsInstanceOfType<BuildErrorEventArgs>);
172         }
173 
174         /// <summary>
175         /// Test the case where null events are attempted to be logged
176         /// </summary>
177         [Test]
178         [ExpectedException(typeof(ArgumentNullException))]
LogErrorEventNullEvent()179         public void LogErrorEventNullEvent()
180         {
181             engineLoggingServicesHelper.LogErrorEvent(null);
182         }
183 
184         /// <summary>
185         /// Test warning events
186         /// </summary>
187         [Test]
LogWarningEvent()188         public void LogWarningEvent()
189         {
190             List<BuildEventArgs> eventList = new List<BuildEventArgs>();
191 
192             // Log a number of events
193             for (int i = 0; i < 10; i++)
194             {
195                 BuildWarningEventArgs eventToAdd = GenerateBuildWarningEventArgs("WarningMessage" + i);
196                 eventList.Add(eventToAdd);
197                 engineLoggingServicesHelper.LogWarningEvent(eventToAdd);
198             }
199 
200             // Get the logged event queue from the "logger"
201            DualQueue<BuildEventArgs> currentQueue = engineLoggingServicesHelper.GetCurrentQueueBuildEvents();
202 
203             // Assert that every event we sent to the logger now exists in the queue
204             Assert.IsTrue(eventList.TrueForAll(delegate(BuildEventArgs args)
205                                                {
206                                                    return currentQueue.Contains(args);
207                                                }), "Expected to find all events sent to LogWarningEvent");
208 
209             // Assert that every event in the queue is of the correct type
210             AssertForEachEventInQueue(currentQueue, IsInstanceOfType<BuildWarningEventArgs>);
211         }
212 
213         /// <summary>
214         ///  Test the case where we attempt to log a null warning event
215         /// </summary>
216         [Test]
217         [ExpectedException(typeof(ArgumentNullException))]
LogWarningEventNullEvent()218         public void LogWarningEventNullEvent()
219         {
220             engineLoggingServicesHelper.LogWarningEvent(null);
221         }
222 
223         /// <summary>
224         /// Test the case where we try and log a null message event
225         /// </summary>
226         [Test]
227         [ExpectedException(typeof(ArgumentNullException))]
LogMessageEventNullEvent()228         public void LogMessageEventNullEvent()
229         {
230             // Try and log a null when we are only going to log critical events
231             try
232             {
233                 engineLoggingServicesHelper.OnlyLogCriticalEvents = true;
234                 engineLoggingServicesHelper.LogMessageEvent(null);
235                 engineLoggingServicesHelper.OnlyLogCriticalEvents = false;
236             }
237             catch (Exception e)
238             {
239                 Assert.Fail(e.Message + " Should not throw exception if OnlyLogCriticalEvents is true");
240             }
241 
242             // Should throw an exception as OnlyLogCriticalEvents is false and the null check is performed
243             engineLoggingServicesHelper.LogMessageEvent(null);
244         }
245 
246         /// <summary>
247         /// Test that we can log message events
248         /// </summary>
249         [Test]
LogMessageEvent()250         public void LogMessageEvent()
251         {
252             List<BuildEventArgs> eventList = new List<BuildEventArgs>();
253 
254             // Log a number of message events and keep track of the events we tried to log
255             for (int i = 0; i < 10; i++)
256             {
257                 BuildMessageEventArgs eventToAdd = GenerateBuildMessageEventArgs("MessageMessage" + i, MessageImportance.Normal);
258                 eventList.Add(eventToAdd);
259                 engineLoggingServicesHelper.LogMessageEvent(eventToAdd);
260             }
261 
262             // Get the queue of the events logged by the logger
263            DualQueue<BuildEventArgs> currentQueue = engineLoggingServicesHelper.GetCurrentQueueBuildEvents();
264 
265             // Assert that every event we sent to the logger exists in the logging queue
266             Assert.IsTrue(eventList.TrueForAll(delegate(BuildEventArgs args)
267                                                {
268                                                    return currentQueue.Contains(args);
269                                                }), "Expected to find all events sent to LogMessageEvent");
270 
271             // Assert that every event in the queue is of the correct type
272             AssertForEachEventInQueue(currentQueue, IsInstanceOfType<BuildMessageEventArgs>);
273         }
274 
275         /// <summary>
276         ///  Test the case where we try and log a null event
277         /// </summary>
278         [Test]
279         [ExpectedException(typeof(ArgumentNullException))]
PostLoggingEventNullEvent()280         public void PostLoggingEventNullEvent()
281         {
282             BuildEventArgs nullEvent = null;
283             engineLoggingServicesHelper.PostLoggingEvent(nullEvent);
284         }
285 
286         /// <summary>
287         /// Test the case where we try and log a null CustomEvent
288         /// </summary>
289         [Test]
290         [ExpectedException(typeof(ArgumentNullException))]
LogCustomEventNullEvent()291         public void LogCustomEventNullEvent()
292         {
293             engineLoggingServicesHelper.LogCustomEvent(null);
294         }
295 
296         /// <summary>
297         /// Test that we can log CustomEvents
298         /// </summary>
299         [Test]
LogCustomEvent()300         public void LogCustomEvent()
301         {
302             List<BuildEventArgs> eventList = new List<BuildEventArgs>();
303 
304             // Log a number of events and keep track of which events we sent to the logger
305             for (int i = 0; i < 10; i++)
306             {
307                 MyCustomBuildEventArgs eventToAdd = GenerateBuildCustomEventArgs("CustomMessage" + i);
308                 eventList.Add(eventToAdd);
309                 engineLoggingServicesHelper.LogCustomEvent(eventToAdd);
310             }
311 
312             // Get the current queue of the logger
313            DualQueue<BuildEventArgs> currentQueue = engineLoggingServicesHelper.GetCurrentQueueBuildEvents();
314 
315             // Assert that every event we sent to the logger exists in the logging queue
316             Assert.IsTrue(eventList.TrueForAll(delegate(BuildEventArgs args)
317                                                {
318                                                    return currentQueue.Contains(args);
319                                                }), "Expected to find all events sent to logcustomevent");
320 
321             // Assert that every event in the queue is of the correct type
322             AssertForEachEventInQueue(currentQueue, IsInstanceOfType<MyCustomBuildEventArgs>);
323         }
324 
325         /// <summary>
326         /// Test that when we send an event to the logger we should see that same event in the queue
327         /// </summary>
328         [Test]
PostLoggingEventCustomEvent()329         public void PostLoggingEventCustomEvent()
330         {
331             BuildEventArgs testBuildEventArgs = GenerateBuildCustomEventArgs("CustomMessage");
332             engineLoggingServicesHelper.PostLoggingEvent(testBuildEventArgs);
333             Assert.IsTrue(engineLoggingServicesHelper.GetCurrentQueueBuildEvents().Contains(testBuildEventArgs), "Expected to find event sent to postloggingevent");
334         }
335 
336         /// <summary>
337         /// Test that when we send an event to the logger we should see that same event in the queue
338         /// </summary>
339         [Test]
PostLoggingEventErrorEvent()340         public void PostLoggingEventErrorEvent()
341         {
342             BuildEventArgs testBuildEventArgs = GenerateBuildErrorEventArgs("testErrorBuildEvent");
343             engineLoggingServicesHelper.PostLoggingEvent(testBuildEventArgs);
344             Assert.IsTrue(engineLoggingServicesHelper.GetCurrentQueueBuildEvents().Contains(testBuildEventArgs), "Expected to find event we sent to postloggingevent");
345 
346         }
347 
348         /// <summary>
349         /// Test that when we send an event to the logger we should see that same event in the queue
350         /// </summary>
351         [Test]
PostLoggingEventWarningEvent()352         public void PostLoggingEventWarningEvent()
353         {
354             BuildEventArgs testBuildEventArgs = GenerateBuildWarningEventArgs("testWarningBuildEvent");
355             engineLoggingServicesHelper.PostLoggingEvent(testBuildEventArgs);
356             Assert.IsTrue(engineLoggingServicesHelper.GetCurrentQueueBuildEvents().Contains(testBuildEventArgs), "Expected to find event sent to postloggingevent");
357         }
358 
359         /// <summary>
360         /// Test that when we send an event to the logger we should see that same event in the queue
361         /// </summary>
362         [Test]
PostLoggingEventMessageEvent()363         public void PostLoggingEventMessageEvent()
364         {
365             BuildEventArgs testBuildEventArgs = GenerateBuildMessageEventArgs("testMessageBuildEvent", MessageImportance.Normal);
366             engineLoggingServicesHelper.PostLoggingEvent(testBuildEventArgs);
367             Assert.IsTrue(engineLoggingServicesHelper.GetCurrentQueueBuildEvents().Contains(testBuildEventArgs), "Expected to find event sent to postloggingevent");
368         }
369 
370         /// <summary>
371         /// Test that when we send an multiple event to the logger  on multiple threads, we should everyone one of those event in the queue
372         /// </summary>
373         [Test]
PostLoggingEventMultiThreaded()374         public void PostLoggingEventMultiThreaded()
375         {
376             List<BuildEventArgs> eventsAdded = new List<BuildEventArgs>();
377 
378             // Add a number of events on multiple threads
379             ManualResetEvent[] waitHandles = new ManualResetEvent[10];
380             for (int i = 0; i < waitHandles.Length; i++)
381             {
382                 waitHandles[i] = new ManualResetEvent(false);
383                 ThreadPool.QueueUserWorkItem(
384                               delegate(object state)
385                               {
386                                   for (int j = 0; j < 4; j++)
387                                   {
388                                       BuildEventArgs testBuildEventArgs = GenerateBuildMessageEventArgs("testMessageBuildEvent" + i + "_" + j, MessageImportance.Normal);
389                                       lock (eventsAdded)
390                                       {
391                                           eventsAdded.Add(testBuildEventArgs);
392                                       }
393                                       engineLoggingServicesHelper.PostLoggingEvent(testBuildEventArgs);
394                                   }
395                                   ((ManualResetEvent)state).Set();
396                               }, waitHandles[i]);
397             }
398 
399             // Wait for the threads to finish
400             foreach (ManualResetEvent resetEvent in waitHandles)
401             {
402                 resetEvent.WaitOne();
403             }
404 
405             // Get the current queue
406             DualQueue<BuildEventArgs> currentQueue = engineLoggingServicesHelper.GetCurrentQueueBuildEvents();
407 
408             // Assert that every event we sent to the logger on the multiple threads is in the queue at the end
409             Assert.IsTrue(eventsAdded.TrueForAll(delegate(BuildEventArgs args)
410                                                {
411                                                    return currentQueue.Contains(args);
412                                                }), "Expected to find all events added to queue on multiple threads to be in the queue at the end");
413         }
414      #endregion
415 
416         #region TestLogCommentMethods
417         /// <summary>
418         ///  Test logging a null comment to the logger
419         /// </summary>
420         [Test]
LogCommentFromTextNullMessage()421         public void LogCommentFromTextNullMessage()
422         {
423             // Test the case where we are trying to log a null and we are also trying to log only critical events
424             try
425             {
426                 engineLoggingServicesHelper.OnlyLogCriticalEvents = true;
427                 engineLoggingServicesHelper.LogCommentFromText(null,MessageImportance.Low, null);
428                 engineLoggingServicesHelper.OnlyLogCriticalEvents = false;
429             }
430             catch (Exception e)
431             {
432                 Assert.Fail(e.Message + " Should not throw exception if OnlyLogCriticalEvents is true");
433             }
434 
435             // Would have tested the case where null was passed and critical events is false, but this would cause an assertion window
436             // to popup thereby failing the test
437         }
438 
439         /// <summary>
440         /// Test logging messages to the logger
441         /// </summary>
442         [Test]
LogCommentFromTextGoodMessages()443         public void LogCommentFromTextGoodMessages()
444         {
445             // Send a message, this message should be posted to the queue
446             engineLoggingServicesHelper.LogCommentFromText(null, MessageImportance.Low, "Message");
447             engineLoggingServicesHelper.LogCommentFromText(null, MessageImportance.Low, string.Empty);
448 
449             // Make sure that the one message got posted to the queue
450             DualQueue<BuildEventArgs> currentQueue = engineLoggingServicesHelper.GetCurrentQueueBuildEvents();
451             Assert.IsTrue(currentQueue.Count == 2, "Expected to find two events on the queue");
452             AssertForEachEventInQueue(currentQueue, IsInstanceOfType<BuildMessageEventArgs>);
453 
454         }
455 
456         /// <summary>
457         /// Test logging message comments to the logger
458         /// </summary>
459         [Test]
LogCommentGoodMessages()460         public void LogCommentGoodMessages()
461         {
462             // Send a message while not logging critical events, since comments are not considered critical they should
463             // not show up in the queue
464             engineLoggingServicesHelper.OnlyLogCriticalEvents = true;
465             engineLoggingServicesHelper.LogComment((BuildEventContext)null, MessageImportance.Normal, "ErrorConvertedIntoWarning");
466             engineLoggingServicesHelper.LogComment((BuildEventContext)null, MessageImportance.Normal, "ErrorConvertedIntoWarning", 3);
467             engineLoggingServicesHelper.LogComment((BuildEventContext)null, "ErrorConvertedIntoWarning");
468             engineLoggingServicesHelper.LogComment((BuildEventContext)null, "ErrorCount", 3);
469             Assert.IsTrue(engineLoggingServicesHelper.GetCurrentQueueBuildEvents().Count == 0, "Expected to find no events on the queue");
470 
471             // Sent the message while we are logging events, even non critical ones.
472             engineLoggingServicesHelper.OnlyLogCriticalEvents = false;
473             engineLoggingServicesHelper.LogComment((BuildEventContext)null, MessageImportance.Normal, "ErrorConvertedIntoWarning");
474             engineLoggingServicesHelper.LogComment((BuildEventContext)null, MessageImportance.Normal, "ErrorConvertedIntoWarning", 3);
475             engineLoggingServicesHelper.LogComment((BuildEventContext)null, "ErrorConvertedIntoWarning");
476             engineLoggingServicesHelper.LogComment((BuildEventContext)null, "ErrorCount", 3);
477 
478             // Get the queue from the logger
479            DualQueue<BuildEventArgs> currentQueue = engineLoggingServicesHelper.GetCurrentQueueBuildEvents();
480 
481             // Make sure we got all the events we sent to the logger
482             Assert.IsTrue(currentQueue.Count == 4, "Expected to find four events on the queue");
483 
484             // Make sure that every event in the queue is of the correct type
485             AssertForEachEventInQueue(currentQueue, IsInstanceOfType<BuildMessageEventArgs>);
486         }
487         #endregion
488 
489         #region TestStartedFinished
490         /// <summary>
491         /// Test logging the build started event
492         /// </summary>
493         [Test]
LogBuildStarted()494         public void LogBuildStarted()
495         {
496             engineLoggingServicesHelper.LogBuildStarted();
497             DualQueue<BuildEventArgs> currentQueue = engineLoggingServicesHelper.GetCurrentQueueBuildEvents();
498             Assert.IsTrue(currentQueue.Count == 1, "Expected to find one event in the queue");
499             AssertForEachEventInQueue(currentQueue, IsInstanceOfType<BuildStartedEventArgs>);
500 
501         }
502 
503         /// <summary>
504         ///  Test logging the build finished event
505         /// </summary>
506         [Test]
LogBuildFinished()507         public void LogBuildFinished()
508         {
509             engineLoggingServicesHelper.LogBuildFinished(true);
510             engineLoggingServicesHelper.LogBuildFinished(false);
511             DualQueue<BuildEventArgs> currentQueue = engineLoggingServicesHelper.GetCurrentQueueBuildEvents();
512             Assert.IsTrue(currentQueue.Count == 2, "Expected to find two events in the queue");
513             AssertForEachEventInQueue(currentQueue, IsInstanceOfType<BuildFinishedEventArgs>);
514         }
515 
516         /// <summary>
517         /// Checks to make sure that the event passed in is an instance of TType
518         /// </summary>
519         /// <typeparam name="TType">Type of event which should be found</typeparam>
520         /// <param name="e">Event to check against TType</param>
IsInstanceOfType(BuildEventArgs e)521         public void IsInstanceOfType<TType>(BuildEventArgs e)
522         {
523             Assert.IsTrue(typeof(TType).IsInstanceOfType(e), "Expected event to be a " + typeof(TType).Name);
524         }
525 
526         /// <summary>
527         /// Check that every event in the queue is of the correct event type
528         /// </summary>
529         /// <param name="queue">queue to check</param>
530         /// <param name="action">An action which determines wheather or not a queue item is correct</param>
AssertForEachEventInQueue(DualQueue<BuildEventArgs> queue, Action<BuildEventArgs> action)531         private void AssertForEachEventInQueue(DualQueue<BuildEventArgs> queue, Action<BuildEventArgs> action)
532         {
533             BuildEventArgs eventArgs;
534 
535             while((eventArgs = queue.Dequeue())!=null)
536             {
537                 action(eventArgs);
538             }
539         }
540 
541         /// <summary>
542         /// Test logging of the task started event
543         /// </summary>
544         [Test]
LogTaskStarted()545         public void LogTaskStarted()
546         {
547             // Test the logging while only logging critical events
548             engineLoggingServicesHelper.OnlyLogCriticalEvents = true;
549             engineLoggingServicesHelper.LogTaskStarted(null, "taskName", "projectFile", "projectFileOfTaskNode");
550             Assert.IsTrue(engineLoggingServicesHelper.GetCurrentQueueBuildEvents().Count == 0, "Expected to find no events in the queue");
551 
552             // Test logging while logging all events
553             engineLoggingServicesHelper.OnlyLogCriticalEvents = false;
554             engineLoggingServicesHelper.LogTaskStarted(null, "taskName", "projectFile", "projectFileOfTaskNode");
555            DualQueue<BuildEventArgs> currentQueue = engineLoggingServicesHelper.GetCurrentQueueBuildEvents();
556             Assert.IsTrue(currentQueue.Count == 1, "Expected to find one event in the queue");
557             AssertForEachEventInQueue(currentQueue, IsInstanceOfType<TaskStartedEventArgs>);
558         }
559 
560         /// <summary>
561         ///  Test that the TaskFinished event logs correctly
562         /// </summary>
563         [Test]
LogTaskFinished()564         public void LogTaskFinished()
565         {
566             // Test the logging while only logging critical events
567             engineLoggingServicesHelper.OnlyLogCriticalEvents = true;
568             engineLoggingServicesHelper.LogTaskFinished(null, "taskName", "projectFile", "projectFileOfTaskNode", true);
569             Assert.IsTrue(engineLoggingServicesHelper.GetCurrentQueueBuildEvents().Count == 0, "Expected to find no events in the queue");
570 
571             // Test logging while logging all events
572             engineLoggingServicesHelper.OnlyLogCriticalEvents = false;
573             engineLoggingServicesHelper.LogTaskFinished(null, "taskName", "projectFile", "projectFileOfTaskNode", true);
574            DualQueue<BuildEventArgs> currentQueue = engineLoggingServicesHelper.GetCurrentQueueBuildEvents();
575             Assert.IsTrue(currentQueue.Count == 1, "Expected to find one event in the queue");
576             AssertForEachEventInQueue(currentQueue, IsInstanceOfType<TaskFinishedEventArgs>);
577         }
578 
579         /// <summary>
580         /// Test that the TargetStarted event logs correctly
581         /// </summary>
582         [Test]
LogTargetStarted()583         public void LogTargetStarted()
584         {
585             // Test logging while only logging critical events
586             engineLoggingServicesHelper.OnlyLogCriticalEvents = true;
587             engineLoggingServicesHelper.LogTargetStarted(null, "TargetName", "projectFile", "projectFileOfTargetNode");
588             Assert.IsTrue(engineLoggingServicesHelper.GetCurrentQueueBuildEvents().Count == 0, "Expected to find no events in the queue");
589 
590             // Test logging while logging all events
591             engineLoggingServicesHelper.OnlyLogCriticalEvents = false;
592             engineLoggingServicesHelper.LogTargetStarted(null, "targetName", "projectFile", "projectFileOfTargetNode");
593            DualQueue<BuildEventArgs> currentQueue = engineLoggingServicesHelper.GetCurrentQueueBuildEvents();
594             Assert.IsTrue(currentQueue.Count == 1, "Expected to find one event in the queue");
595             AssertForEachEventInQueue(currentQueue, IsInstanceOfType<TargetStartedEventArgs>);
596         }
597 
598         /// <summary>
599         /// Test that TargetFinished logs correctly
600         /// </summary>
601         [Test]
LogTargetFinished()602         public void LogTargetFinished()
603         {
604             // Test logging while only logging critical events
605             engineLoggingServicesHelper.OnlyLogCriticalEvents = true;
606             engineLoggingServicesHelper.LogTargetFinished(null, "TargetName", "projectFile", "projectFileOfTargetNode", true);
607             Assert.IsTrue(engineLoggingServicesHelper.GetCurrentQueueBuildEvents().Count == 0, "Expected to find no events in the queue");
608 
609             // Test logging while logging all events, even non critical ones
610             engineLoggingServicesHelper.OnlyLogCriticalEvents = false;
611             engineLoggingServicesHelper.LogTargetFinished(null, "TargetName", "projectFile", "projectFileOfTargetNode", true);
612            DualQueue<BuildEventArgs> currentQueue = engineLoggingServicesHelper.GetCurrentQueueBuildEvents();
613             Assert.IsTrue(currentQueue.Count == 1, "Expected to find one event in the queue");
614             AssertForEachEventInQueue(currentQueue, IsInstanceOfType<TargetFinishedEventArgs>);
615         }
616 
617         /// <summary>
618         /// Test logging the ProjectStarted event
619         /// </summary>
620         [Test]
LogProjectStarted()621         public void LogProjectStarted()
622         {
623             // Test logging while only logging critical events
624             engineLoggingServicesHelper.OnlyLogCriticalEvents = true;
625             engineLoggingServicesHelper.LogProjectStarted(-1, null, null, "projectFile", "targetNames", null, null);
626             Assert.IsTrue(engineLoggingServicesHelper.GetCurrentQueueBuildEvents().Count == 0, "Expected to find no events in the queue");
627 
628             // Test logging while logging all events, even non critical ones
629             engineLoggingServicesHelper.OnlyLogCriticalEvents = false;
630             engineLoggingServicesHelper.LogProjectStarted(-1, null, null, "projectFile", "targetNames", null, null);
631            DualQueue<BuildEventArgs> currentQueue = engineLoggingServicesHelper.GetCurrentQueueBuildEvents();
632             Assert.IsTrue(currentQueue.Count == 1, "Expected to find one event in the queue");
633             AssertForEachEventInQueue(currentQueue, IsInstanceOfType<ProjectStartedEventArgs>);
634         }
635 
636         /// <summary>
637         /// Test logging the ProjectFinished event
638         /// </summary>
639         [Test]
LogProjectFinished()640         public void LogProjectFinished()
641         {
642             // Test logging while only logging critical events
643             engineLoggingServicesHelper.OnlyLogCriticalEvents = true;
644             engineLoggingServicesHelper.LogProjectFinished(null, "projectFile", true);
645             Assert.IsTrue(engineLoggingServicesHelper.GetCurrentQueueBuildEvents().Count == 0, "Expected no events in queue but found some");
646 
647             //Test logging while logging all events, even non critical ones
648             engineLoggingServicesHelper.OnlyLogCriticalEvents = false;
649             engineLoggingServicesHelper.LogProjectFinished(null, "projectFile", true);
650            DualQueue<BuildEventArgs> currentQueue = engineLoggingServicesHelper.GetCurrentQueueBuildEvents();
651             Assert.IsTrue(currentQueue.Count == 1, "Expected find one item in the queue");
652             AssertForEachEventInQueue(currentQueue, IsInstanceOfType<ProjectFinishedEventArgs>);
653         }
654         #endregion
655 
656         #region LoggingMethodTests
657 
658         [Test]
LogTaskWarningFromException()659         public void LogTaskWarningFromException()
660         {
661             engineLoggingServicesHelper.LogTaskWarningFromException(null, new Exception("testException"), new BuildEventFileInfo("noFile"), "taskName");
662             engineLoggingServicesHelper.LogTaskWarningFromException(null, null, new BuildEventFileInfo("noFile"), "taskName");
663            DualQueue<BuildEventArgs> currentQueue = engineLoggingServicesHelper.GetCurrentQueueBuildEvents();
664             Assert.IsTrue(currentQueue.Count == 2, "Expected two warnings in queue items");
665             AssertForEachEventInQueue(currentQueue, IsInstanceOfType<BuildWarningEventArgs>);
666         }
667 
668         [Test]
LogErrorWithoutSubcategoryResourceName()669         public void LogErrorWithoutSubcategoryResourceName()
670         {
671             engineLoggingServicesHelper.OnlyLogCriticalEvents = true;
672             engineLoggingServicesHelper.LogError(null, new BuildEventFileInfo("file"), "BuildTargetCompletely", "target");
673            DualQueue<BuildEventArgs> currentQueue = engineLoggingServicesHelper.GetCurrentQueueBuildEvents();
674             Assert.IsTrue(currentQueue.Count == 1, "Expected one event in queue!");
675             AssertForEachEventInQueue(currentQueue, IsInstanceOfType<BuildErrorEventArgs>);
676         }
677 
678         [Test]
LogErrorWithSubcategoryResourceName()679         public void LogErrorWithSubcategoryResourceName()
680         {
681             engineLoggingServicesHelper.OnlyLogCriticalEvents = true;
682             engineLoggingServicesHelper.LogError(null, "SubCategoryForSchemaValidationErrors", new BuildEventFileInfo("file"), "BuildTargetCompletely", "target");
683            DualQueue<BuildEventArgs> currentQueue = engineLoggingServicesHelper.GetCurrentQueueBuildEvents();
684             Assert.IsTrue(currentQueue.Count == 1, "Expected one event in queue!");
685             AssertForEachEventInQueue(currentQueue, IsInstanceOfType<BuildErrorEventArgs>);
686         }
687 
688         [Test]
LogErrorFromText()689         public void LogErrorFromText()
690         {
691             engineLoggingServicesHelper.OnlyLogCriticalEvents = true;
692             engineLoggingServicesHelper.LogErrorFromText(null, "SubCategoryForSchemaValidationErrors", "MSB4000", "helpKeyword", new BuildEventFileInfo("file"), "error");
693            DualQueue<BuildEventArgs> currentQueue = engineLoggingServicesHelper.GetCurrentQueueBuildEvents();
694             Assert.IsTrue(currentQueue.Count == 1, "Expected one event in queue!");
695             AssertForEachEventInQueue(currentQueue, IsInstanceOfType<BuildErrorEventArgs>);
696         }
697 
698         [Test]
LogWarningWithoutSubcategoryResourceName()699         public void LogWarningWithoutSubcategoryResourceName()
700         {
701             engineLoggingServicesHelper.OnlyLogCriticalEvents = true;
702             engineLoggingServicesHelper.LogWarning(null, new BuildEventFileInfo("file"), "BuildTargetCompletely", "target");
703            DualQueue<BuildEventArgs> currentQueue = engineLoggingServicesHelper.GetCurrentQueueBuildEvents();
704             Assert.IsTrue(currentQueue.Count == 1, "Expected one event in queue!");
705             AssertForEachEventInQueue(currentQueue, IsInstanceOfType<BuildWarningEventArgs>);
706         }
707 
708         [Test]
LogWarningWithSubcategoryResourceName()709         public void LogWarningWithSubcategoryResourceName()
710         {
711             engineLoggingServicesHelper.OnlyLogCriticalEvents = true;
712             engineLoggingServicesHelper.LogWarning(null, "SubCategoryForSchemaValidationErrors", new BuildEventFileInfo("file"), "BuildTargetCompletely", "target");
713            DualQueue<BuildEventArgs> currentQueue = engineLoggingServicesHelper.GetCurrentQueueBuildEvents();
714             Assert.IsTrue(currentQueue.Count == 1, "Expected one event in queue!");
715             AssertForEachEventInQueue(currentQueue, IsInstanceOfType<BuildWarningEventArgs>);
716         }
717 
718         [Test]
LogWarningFromText()719         public void LogWarningFromText()
720         {
721             engineLoggingServicesHelper.OnlyLogCriticalEvents = true;
722             engineLoggingServicesHelper.LogWarningFromText(null, "SubCategoryForSchemaValidationErrors", "MSB4000", "helpKeyword", new BuildEventFileInfo("file"), "Warning");
723            DualQueue<BuildEventArgs> currentQueue = engineLoggingServicesHelper.GetCurrentQueueBuildEvents();
724             Assert.IsTrue(currentQueue.Count == 1, "Expected one event in queue!");
725             AssertForEachEventInQueue(currentQueue, IsInstanceOfType<BuildWarningEventArgs>);
726         }
727 
728         [Test]
LogInvalidProjectFileError()729         public void LogInvalidProjectFileError()
730         {
731             engineLoggingServicesHelper.OnlyLogCriticalEvents = true;
732             engineLoggingServicesHelper.LogInvalidProjectFileError(null, new InvalidProjectFileException());
733             engineLoggingServicesHelper.LogInvalidProjectFileError(null, new InvalidProjectFileException("invalidProjectFile"));
734             InvalidProjectFileException invalidProjectFileException = new InvalidProjectFileException("anotherInvalidProjectFile");
735             invalidProjectFileException.HasBeenLogged = true;
736             engineLoggingServicesHelper.LogInvalidProjectFileError(null, invalidProjectFileException);
737            DualQueue<BuildEventArgs> currentQueue = engineLoggingServicesHelper.GetCurrentQueueBuildEvents();
738             Assert.IsTrue(currentQueue.Count == 2, "Expected two errors in Queue");
739             AssertForEachEventInQueue(currentQueue, IsInstanceOfType<BuildErrorEventArgs>);
740         }
741 
742         [Test]
LogFatalBuildError()743         public void LogFatalBuildError()
744         {
745             engineLoggingServicesHelper.OnlyLogCriticalEvents = true;
746             engineLoggingServicesHelper.LogFatalBuildError(null, new Exception("exception1!"), new BuildEventFileInfo("file1"));
747             engineLoggingServicesHelper.LogFatalBuildError(null, new Exception("exception2!"), new BuildEventFileInfo("file2"));
748             engineLoggingServicesHelper.LogFatalBuildError(null, new Exception("exception3!"), new BuildEventFileInfo("file3"));
749            DualQueue<BuildEventArgs> currentQueue = engineLoggingServicesHelper.GetCurrentQueueBuildEvents();
750             Assert.IsTrue(currentQueue.Count == 3, "Expected three errors in Queue");
751             AssertForEachEventInQueue(currentQueue, IsInstanceOfType<BuildErrorEventArgs>);
752         }
753 
754         [Test]
LogFatalError()755         public void LogFatalError()
756         {
757             engineLoggingServicesHelper.OnlyLogCriticalEvents = true;
758             engineLoggingServicesHelper.LogFatalError(null, new Exception("exception1"), new BuildEventFileInfo("file1"), "BuildTargetCompletely", "target1");
759             engineLoggingServicesHelper.LogFatalError(null, new Exception("exception2"), new BuildEventFileInfo("file2"), "BuildTargetCompletely", "target2");
760             engineLoggingServicesHelper.LogFatalError(null, new Exception("exception3"), new BuildEventFileInfo("file3"), "BuildTargetCompletely", "target3");
761            DualQueue<BuildEventArgs> currentQueue = engineLoggingServicesHelper.GetCurrentQueueBuildEvents();
762             Assert.IsTrue(currentQueue.Count == 3, "Expected three errors in Queue");
763             AssertForEachEventInQueue(currentQueue, IsInstanceOfType<BuildErrorEventArgs>);
764 
765         }
766 
767         [Test]
LogFatalTaskError()768         public void LogFatalTaskError()
769         {
770             engineLoggingServicesHelper.OnlyLogCriticalEvents = true;
771             engineLoggingServicesHelper.LogFatalTaskError(null, new Exception("exception1"), new BuildEventFileInfo("file1"), "task1");
772             engineLoggingServicesHelper.LogFatalTaskError(null, new Exception("exception2"), new BuildEventFileInfo("file2"), "task2");
773             engineLoggingServicesHelper.LogFatalTaskError(null, new Exception("exception3"), new BuildEventFileInfo("file3"), "task3");
774            DualQueue<BuildEventArgs> currentQueue = engineLoggingServicesHelper.GetCurrentQueueBuildEvents();
775             Assert.IsTrue(currentQueue.Count == 3, "Expected three errors in Queue");
776             AssertForEachEventInQueue(currentQueue, IsInstanceOfType<BuildErrorEventArgs>);
777         }
778         #endregion
779 
780         #region InProcLoggingTests
781 
782         internal class VerifyEventSourceHelper
783         {
VerifyEventSourceHelper()784             public VerifyEventSourceHelper()
785             {
786                 sourceForEvents = new EventSource();
787                 sourceForEvents.AnyEventRaised += new AnyEventHandler(this.AnyEventRaised);
788                 sourceForEvents.BuildFinished += new BuildFinishedEventHandler(this.BuildFinished);
789                 sourceForEvents.BuildStarted += new BuildStartedEventHandler(this.BuildStarted);
790                 sourceForEvents.CustomEventRaised += new CustomBuildEventHandler(this.CustomEventRaised);
791                 sourceForEvents.ErrorRaised += new BuildErrorEventHandler(this.ErrorRaised);
792                 sourceForEvents.MessageRaised += new BuildMessageEventHandler(this.MessageRaised);
793                 sourceForEvents.ProjectFinished += new ProjectFinishedEventHandler(this.ProjectFinished);
794                 sourceForEvents.ProjectStarted += new ProjectStartedEventHandler(this.ProjectStarted);
795                 sourceForEvents.TargetFinished += new TargetFinishedEventHandler(this.TargetFinished);
796                 sourceForEvents.TargetStarted += new TargetStartedEventHandler(this.TargetStarted);
797                 sourceForEvents.TaskFinished += new TaskFinishedEventHandler(this.TaskFinished);
798                 sourceForEvents.TaskStarted += new TaskStartedEventHandler(this.TaskStarted);
799                 sourceForEvents.WarningRaised += new BuildWarningEventHandler(this.WarningRaised);
800                 sourceForEvents.StatusEventRaised += new BuildStatusEventHandler(this.StatusRaised);
801                 ClearEvents();
802             }
803 
ClearEvents()804             public void ClearEvents()
805             {
806                 eventsRaisedHash = new Hashtable();
807                 eventsRaisedHash.Add("messageRaised", false);
808                 eventsRaisedHash.Add("errorRaised", false);
809                 eventsRaisedHash.Add("warningRaised", false);
810                 eventsRaisedHash.Add("buildStarted", false);
811                 eventsRaisedHash.Add("buildStatus", false);
812                 eventsRaisedHash.Add("buildFinished", false);
813                 eventsRaisedHash.Add("projectStarted", false);
814                 eventsRaisedHash.Add("projectFinished", false);
815                 eventsRaisedHash.Add("targetStarted", false);
816                 eventsRaisedHash.Add("targetFinished", false);
817                 eventsRaisedHash.Add("taskStarted", false);
818                 eventsRaisedHash.Add("taskFinished", false);
819                 eventsRaisedHash.Add("customEventRaised", false);
820                 eventsRaisedHash.Add("anyEventRaised", false);
821                 eventsRaisedHash.Add("statusRaised", false);
822 
823             }
824             #region Fields
825             Hashtable eventsRaisedHash;
826             public EventSource sourceForEvents;
827             #endregion
828             #region EventHandlers
MessageRaised(object sender, BuildMessageEventArgs arg)829             public void MessageRaised(object sender, BuildMessageEventArgs arg)
830             {
831                 eventsRaisedHash["messageRaised"] = true;
832             }
833 
StatusRaised(object sender, BuildStatusEventArgs arg)834             public void StatusRaised(object sender, BuildStatusEventArgs arg)
835             {
836                 eventsRaisedHash["statusRaised"] = true;
837             }
ErrorRaised(object sender, BuildErrorEventArgs arg)838             public void ErrorRaised(object sender, BuildErrorEventArgs arg)
839             {
840                 eventsRaisedHash["errorRaised"] = true;
841             }
WarningRaised(object sender, BuildWarningEventArgs arg)842             public void WarningRaised(object sender, BuildWarningEventArgs arg)
843             {
844                 eventsRaisedHash["warningRaised"] = true;
845             }
BuildStarted(object sender, BuildStartedEventArgs arg)846             public void BuildStarted(object sender, BuildStartedEventArgs arg)
847             {
848                 eventsRaisedHash["buildStarted"] = true;
849             }
BuildFinished(object sender, BuildFinishedEventArgs arg)850             public void BuildFinished(object sender, BuildFinishedEventArgs arg)
851             {
852                 eventsRaisedHash["buildFinished"] = true;
853             }
ProjectStarted(object sender, ProjectStartedEventArgs arg)854             public void ProjectStarted(object sender, ProjectStartedEventArgs arg)
855             {
856                 eventsRaisedHash["projectStarted"] = true;
857             }
ProjectFinished(object sender, ProjectFinishedEventArgs arg)858             public void ProjectFinished(object sender, ProjectFinishedEventArgs arg)
859             {
860                 eventsRaisedHash["projectFinished"] = true;
861             }
TargetStarted(object sender, TargetStartedEventArgs arg)862             public void TargetStarted(object sender, TargetStartedEventArgs arg)
863             {
864                 eventsRaisedHash["targetStarted"] = true;
865             }
TargetFinished(object sender, TargetFinishedEventArgs arg)866             public void TargetFinished(object sender, TargetFinishedEventArgs arg)
867             {
868                 eventsRaisedHash["targetFinished"] = true;
869             }
TaskStarted(object sender, TaskStartedEventArgs arg)870             public void TaskStarted(object sender, TaskStartedEventArgs arg)
871             {
872                 eventsRaisedHash["taskStarted"] = true;
873             }
TaskFinished(object sender, TaskFinishedEventArgs arg)874             public void TaskFinished(object sender, TaskFinishedEventArgs arg)
875             {
876                 eventsRaisedHash["taskFinished"] = true;
877             }
CustomEventRaised(object sender, CustomBuildEventArgs arg)878             public void CustomEventRaised(object sender, CustomBuildEventArgs arg)
879             {
880                 eventsRaisedHash["customEventRaised"] = true;
881             }
AnyEventRaised(object sender, BuildEventArgs arg)882             public void AnyEventRaised(object sender, BuildEventArgs arg)
883             {
884                 eventsRaisedHash["anyEventRaised"] = true;
885             }
886             #endregion
887 
888             #region Assertions
AssertEventsAndNoOthers(params string[] eventList)889             public void AssertEventsAndNoOthers(params string[] eventList)
890             {
891                 List<string> events = new List<string>(eventList);
892                 foreach (string eventKey in eventList)
893                 {
894                     Assert.IsTrue(eventsRaisedHash[eventKey] != null, string.Format("Key {0} was not found in events list", eventKey));
895                 }
896 
897                 foreach (string key in eventsRaisedHash.Keys)
898                 {
899 
900                     if (events.Contains(key))
901                     {
902                         Assert.IsTrue((bool)(eventsRaisedHash[key]) == true, string.Format("Key {0} Should have been true", key));
903                         continue;
904                     }
905                     Assert.IsFalse((bool)(eventsRaisedHash[key]) == true, string.Format("Key {0} Should not have been true", key));
906                 }
907             }
908             #endregion
909         }
910         internal class MyCustomBuildErrorEventArgs : BuildErrorEventArgs
911         {
MyCustomBuildErrorEventArgs()912             public MyCustomBuildErrorEventArgs()
913                 : base()
914             {
915             }
916         }
917 
918         internal class MyCustomBuildWarningEventArgs : BuildWarningEventArgs
919         {
MyCustomBuildWarningEventArgs()920             public MyCustomBuildWarningEventArgs()
921                 : base()
922             {
923             }
924         }
925 
926         internal class MyCustomBuildMessageEventArgs : BuildMessageEventArgs
927         {
MyCustomBuildMessageEventArgs()928             public MyCustomBuildMessageEventArgs()
929                 : base()
930             {
931             }
932         }
933 
934         internal class MyCustomBuildEventArg : BuildEventArgs
935         {
MyCustomBuildEventArg()936             public MyCustomBuildEventArg()
937                 : base()
938             {
939             }
940         }
941 
942         internal class MyCustomStatusEventArg : BuildStatusEventArgs
943         {
MyCustomStatusEventArg()944             public MyCustomStatusEventArg()
945                 : base()
946             {
947             }
948         }
949 
950         [Test]
InProcProcessPostedLoggingEvents()951         public void InProcProcessPostedLoggingEvents()
952         {
953 
954             VerifyEventSourceHelper eventSourceHelper = new VerifyEventSourceHelper();
955 
956             List<EngineLoggingServicesInProc> engines = new List<EngineLoggingServicesInProc>();
957             EngineLoggingServicesInProc inProcLoggingServicesEventsAllEvents = new EngineLoggingServicesInProc(eventSourceHelper.sourceForEvents, false, new ManualResetEvent(false));
958             EngineLoggingServicesInProc inProcLoggingServicesEventsOnlyCriticalEvents = new EngineLoggingServicesInProc(eventSourceHelper.sourceForEvents, true, new ManualResetEvent(false));
959             engines.Add(inProcLoggingServicesEventsAllEvents);
960             engines.Add(inProcLoggingServicesEventsOnlyCriticalEvents);
961 
962             foreach (EngineLoggingServicesInProc inProcLoggingServicesEvents in engines)
963             {
964                 inProcLoggingServicesEvents.PostLoggingEvent(new BuildMessageEventArgs("Message", "help", "sender", MessageImportance.Low));
965                 inProcLoggingServicesEvents.ProcessPostedLoggingEvents();
966                 eventSourceHelper.AssertEventsAndNoOthers("anyEventRaised", "messageRaised");
967                 eventSourceHelper.ClearEvents();
968 
969                 inProcLoggingServicesEvents.PostLoggingEvent(new TaskStartedEventArgs("message", "help", "projectFile", "taskFile", "taskName"));
970                 inProcLoggingServicesEvents.ProcessPostedLoggingEvents();
971                 eventSourceHelper.AssertEventsAndNoOthers("anyEventRaised", "taskStarted", "statusRaised");
972                 eventSourceHelper.ClearEvents();
973 
974                 inProcLoggingServicesEvents.PostLoggingEvent(new TaskFinishedEventArgs("message", "help", "projectFile", "taskFile", "taskName", true));
975                 inProcLoggingServicesEvents.ProcessPostedLoggingEvents();
976                 eventSourceHelper.AssertEventsAndNoOthers("anyEventRaised", "taskFinished", "statusRaised");
977                 eventSourceHelper.ClearEvents();
978 
979                 inProcLoggingServicesEvents.PostLoggingEvent(new TaskCommandLineEventArgs("commandLine", "taskName", MessageImportance.Low));
980                 inProcLoggingServicesEvents.ProcessPostedLoggingEvents();
981                 eventSourceHelper.AssertEventsAndNoOthers("anyEventRaised", "messageRaised");
982                 eventSourceHelper.ClearEvents();
983 
984                 inProcLoggingServicesEvents.PostLoggingEvent(new BuildWarningEventArgs("SubCategoryForSchemaValidationErrors", "MSB4000", "file", 1, 2, 3, 4, "message", "help", "sender"));
985                 inProcLoggingServicesEvents.ProcessPostedLoggingEvents();
986                 eventSourceHelper.AssertEventsAndNoOthers("anyEventRaised", "warningRaised");
987                 eventSourceHelper.ClearEvents();
988 
989                 inProcLoggingServicesEvents.PostLoggingEvent(new BuildErrorEventArgs("SubCategoryForSchemaValidationErrors", "MSB4000", "file", 1, 2, 3, 4, "message", "help", "sender"));
990                 inProcLoggingServicesEvents.ProcessPostedLoggingEvents();
991                 eventSourceHelper.AssertEventsAndNoOthers("anyEventRaised", "errorRaised");
992                 eventSourceHelper.ClearEvents();
993 
994                 inProcLoggingServicesEvents.PostLoggingEvent(new TargetStartedEventArgs("message", "help", "targetName", "ProjectFile", "targetFile"));
995                 inProcLoggingServicesEvents.ProcessPostedLoggingEvents();
996                 eventSourceHelper.AssertEventsAndNoOthers("anyEventRaised", "targetStarted", "statusRaised");
997                 eventSourceHelper.ClearEvents();
998 
999                 inProcLoggingServicesEvents.PostLoggingEvent(new TargetFinishedEventArgs("message", "help", "targetName", "ProjectFile", "targetFile", true));
1000                 inProcLoggingServicesEvents.ProcessPostedLoggingEvents();
1001                 eventSourceHelper.AssertEventsAndNoOthers("anyEventRaised", "targetFinished", "statusRaised");
1002                 eventSourceHelper.ClearEvents();
1003 
1004                 inProcLoggingServicesEvents.PostLoggingEvent(new ProjectStartedEventArgs(-1, "message", "help", "ProjectFile", "targetNames", null, null, null));
1005                 inProcLoggingServicesEvents.ProcessPostedLoggingEvents();
1006                 eventSourceHelper.AssertEventsAndNoOthers("anyEventRaised", "projectStarted", "statusRaised");
1007                 eventSourceHelper.ClearEvents();
1008 
1009                 inProcLoggingServicesEvents.PostLoggingEvent(new ProjectFinishedEventArgs("message", "help", "ProjectFile", true));
1010                 inProcLoggingServicesEvents.ProcessPostedLoggingEvents();
1011                 eventSourceHelper.AssertEventsAndNoOthers("anyEventRaised", "projectFinished", "statusRaised");
1012                 eventSourceHelper.ClearEvents();
1013 
1014                 inProcLoggingServicesEvents.PostLoggingEvent(new BuildStartedEventArgs("message", "help"));
1015                 inProcLoggingServicesEvents.ProcessPostedLoggingEvents();
1016                 eventSourceHelper.AssertEventsAndNoOthers("anyEventRaised", "buildStarted", "statusRaised");
1017                 eventSourceHelper.ClearEvents();
1018 
1019                 inProcLoggingServicesEvents.PostLoggingEvent(new BuildFinishedEventArgs("message", "help", true));
1020                 inProcLoggingServicesEvents.ProcessPostedLoggingEvents();
1021                 eventSourceHelper.AssertEventsAndNoOthers("anyEventRaised", "buildFinished", "statusRaised");
1022                 eventSourceHelper.ClearEvents();
1023 
1024                 inProcLoggingServicesEvents.PostLoggingEvent(new ExternalProjectStartedEventArgs("message", "help", "senderName", "projectFile", "targetNames"));
1025                 inProcLoggingServicesEvents.ProcessPostedLoggingEvents();
1026                 eventSourceHelper.AssertEventsAndNoOthers("anyEventRaised", "customEventRaised");
1027                 eventSourceHelper.ClearEvents();
1028 
1029                 inProcLoggingServicesEvents.PostLoggingEvent(new ExternalProjectFinishedEventArgs("message", "help", "senderName", "projectFile", true));
1030                 inProcLoggingServicesEvents.ProcessPostedLoggingEvents();
1031                 eventSourceHelper.AssertEventsAndNoOthers("anyEventRaised", "customEventRaised");
1032                 eventSourceHelper.ClearEvents();
1033 
1034                 inProcLoggingServicesEvents.PostLoggingEvent(new MyCustomBuildEventArgs());
1035                 inProcLoggingServicesEvents.ProcessPostedLoggingEvents();
1036                 eventSourceHelper.AssertEventsAndNoOthers("anyEventRaised", "customEventRaised");
1037                 eventSourceHelper.ClearEvents();
1038 
1039                 inProcLoggingServicesEvents.PostLoggingEvent(new MyCustomBuildErrorEventArgs());
1040                 inProcLoggingServicesEvents.ProcessPostedLoggingEvents();
1041                 eventSourceHelper.AssertEventsAndNoOthers("anyEventRaised", "errorRaised");
1042                 eventSourceHelper.ClearEvents();
1043 
1044                 inProcLoggingServicesEvents.PostLoggingEvent(new MyCustomBuildWarningEventArgs());
1045                 inProcLoggingServicesEvents.ProcessPostedLoggingEvents();
1046                 eventSourceHelper.AssertEventsAndNoOthers("anyEventRaised", "warningRaised");
1047                 eventSourceHelper.ClearEvents();
1048 
1049                 inProcLoggingServicesEvents.PostLoggingEvent(new MyCustomBuildMessageEventArgs());
1050                 inProcLoggingServicesEvents.ProcessPostedLoggingEvents();
1051                 eventSourceHelper.AssertEventsAndNoOthers("anyEventRaised", "messageRaised");
1052                 eventSourceHelper.ClearEvents();
1053 
1054                 inProcLoggingServicesEvents.PostLoggingEvent(new MyCustomStatusEventArg());
1055                 inProcLoggingServicesEvents.ProcessPostedLoggingEvents();
1056                 eventSourceHelper.AssertEventsAndNoOthers("anyEventRaised", "statusRaised");
1057                 eventSourceHelper.ClearEvents();
1058             }
1059         }
1060 
1061 
1062 
1063         [Test]
TestCheckForFlushing()1064         public void TestCheckForFlushing()
1065         {
1066 
1067             VerifyEventSourceHelper eventSourceHelper = new VerifyEventSourceHelper();
1068             ManualResetEvent flushEvent = new ManualResetEvent(false);
1069 
1070             EngineLoggingServicesInProc inProcLoggingServicesEventsAllEvents = new EngineLoggingServicesInProc(eventSourceHelper.sourceForEvents, false, flushEvent);
1071 
1072             Assert.IsFalse(inProcLoggingServicesEventsAllEvents.NeedsFlush(DateTime.Now.Ticks), "Didn't expect to need a flush because of time passed");
1073 
1074             Assert.IsTrue(inProcLoggingServicesEventsAllEvents.NeedsFlush(DateTime.Now.Ticks + EngineLoggingServices.flushTimeoutInTicks + 1), "Expect to need a flush because of time passed");
1075 
1076             for (int i = 0; i < 1001; i++)
1077             {
1078                 inProcLoggingServicesEventsAllEvents.PostLoggingEvent(new BuildMessageEventArgs("Message", "help", "sender", MessageImportance.Low));
1079             }
1080 
1081             Assert.IsTrue(inProcLoggingServicesEventsAllEvents.NeedsFlush(0), "Expect to need a flush because of number of events");
1082 
1083             // Expect the handle to be signaled
1084             long currentTicks = DateTime.Now.Ticks;
1085             flushEvent.WaitOne(5000, false);
1086             Assert.IsTrue((DateTime.Now.Ticks - currentTicks) / TimeSpan.TicksPerMillisecond < 4900, "Expected the handle to be signaled");
1087         }
1088 
1089         [Test]
LocalForwardingOfLoggingEvents()1090         public void LocalForwardingOfLoggingEvents()
1091         {
1092             VerifyEventSourceHelper eventSourceHelper = new VerifyEventSourceHelper();
1093             EngineLoggingServicesInProc inProcLoggingServicesEventsAllEvents = new EngineLoggingServicesInProc(eventSourceHelper.sourceForEvents, false, new ManualResetEvent(false));
1094 
1095             VerifyEventSourceHelper localForwardingSourceHelper = new VerifyEventSourceHelper();
1096             VerifyEventSourceHelper centralLoggerEventSource   = new VerifyEventSourceHelper();
1097             inProcLoggingServicesEventsAllEvents.RegisterEventSource(EngineLoggingServicesInProc.LOCAL_FORWARDING_EVENTSOURCE, localForwardingSourceHelper.sourceForEvents);
1098             inProcLoggingServicesEventsAllEvents.RegisterEventSource(EngineLoggingServicesInProc.FIRST_AVAILABLE_LOGGERID, centralLoggerEventSource.sourceForEvents);
1099 
1100             // Create a local forwarding logger and initialize it
1101             ConfigurableForwardingLogger localForwardingLogger = new ConfigurableForwardingLogger();
1102             EventRedirector newRedirector = new EventRedirector(EngineLoggingServicesInProc.FIRST_AVAILABLE_LOGGERID, inProcLoggingServicesEventsAllEvents);
1103             localForwardingLogger.BuildEventRedirector = newRedirector;
1104             localForwardingLogger.Parameters = "TARGETSTARTEDEVENT;TARGETFINISHEDEVENT";
1105             localForwardingLogger.Initialize(localForwardingSourceHelper.sourceForEvents);
1106 
1107             // Verify that BuildStarted event is delivered both to the forwarding logger and ILoggers and
1108             // that the forwarding logger forwards it to the central logger
1109             inProcLoggingServicesEventsAllEvents.PostLoggingEvent(new TargetStartedEventArgs("message", "help", "targetName", "ProjectFile", "targetFile"));
1110             inProcLoggingServicesEventsAllEvents.ProcessPostedLoggingEvents();
1111             eventSourceHelper.AssertEventsAndNoOthers("anyEventRaised", "targetStarted", "statusRaised");
1112             localForwardingSourceHelper.AssertEventsAndNoOthers("anyEventRaised", "targetStarted", "statusRaised");
1113             inProcLoggingServicesEventsAllEvents.ProcessPostedLoggingEvents();
1114             centralLoggerEventSource.AssertEventsAndNoOthers("anyEventRaised", "targetStarted", "statusRaised");
1115             localForwardingSourceHelper.ClearEvents();
1116             centralLoggerEventSource.ClearEvents();
1117             eventSourceHelper.ClearEvents();
1118 
1119             // Verify that BuildFinished event is delivered both to the forwarding logger and ILoggers and
1120             // that the forwarding logger forwards it to the central logger
1121             inProcLoggingServicesEventsAllEvents.PostLoggingEvent(new TargetFinishedEventArgs("message", "help", "targetName", "ProjectFile", "targetFile", true));
1122             inProcLoggingServicesEventsAllEvents.ProcessPostedLoggingEvents();
1123             eventSourceHelper.AssertEventsAndNoOthers("anyEventRaised", "targetFinished", "statusRaised");
1124             localForwardingSourceHelper.AssertEventsAndNoOthers("anyEventRaised", "targetFinished", "statusRaised");
1125             inProcLoggingServicesEventsAllEvents.ProcessPostedLoggingEvents();
1126             centralLoggerEventSource.AssertEventsAndNoOthers("anyEventRaised", "targetFinished", "statusRaised");
1127             localForwardingSourceHelper.ClearEvents();
1128             centralLoggerEventSource.ClearEvents();
1129             eventSourceHelper.ClearEvents();
1130 
1131             // Verify that events that are not forwarded are not delivered to the central logger
1132             inProcLoggingServicesEventsAllEvents.PostLoggingEvent(new BuildMessageEventArgs("Message", "help", "sender", MessageImportance.Low));
1133             inProcLoggingServicesEventsAllEvents.ProcessPostedLoggingEvents();
1134             eventSourceHelper.AssertEventsAndNoOthers("anyEventRaised", "messageRaised");
1135             localForwardingSourceHelper.AssertEventsAndNoOthers("anyEventRaised", "messageRaised");
1136             inProcLoggingServicesEventsAllEvents.ProcessPostedLoggingEvents();
1137             centralLoggerEventSource.AssertEventsAndNoOthers();
1138             localForwardingSourceHelper.ClearEvents();
1139             centralLoggerEventSource.ClearEvents();
1140             eventSourceHelper.ClearEvents();
1141 
1142             // Verify that external events with no logger id are not delivered to the forwarding or central loggers
1143             inProcLoggingServicesEventsAllEvents.PostLoggingEvent(new NodeLoggingEvent(new BuildMessageEventArgs("Message", "help", "sender", MessageImportance.Low)));
1144             inProcLoggingServicesEventsAllEvents.ProcessPostedLoggingEvents();
1145             eventSourceHelper.AssertEventsAndNoOthers("anyEventRaised", "messageRaised");
1146             localForwardingSourceHelper.AssertEventsAndNoOthers();
1147             centralLoggerEventSource.AssertEventsAndNoOthers();
1148             localForwardingSourceHelper.ClearEvents();
1149             centralLoggerEventSource.ClearEvents();
1150             eventSourceHelper.ClearEvents();
1151 
1152             // Verify that external events with logger id are only delivered to central logger
1153             inProcLoggingServicesEventsAllEvents.PostLoggingEvent
1154                 (new NodeLoggingEventWithLoggerId(new BuildMessageEventArgs("Message", "help", "sender", MessageImportance.Low), 2));
1155             inProcLoggingServicesEventsAllEvents.ProcessPostedLoggingEvents();
1156             eventSourceHelper.AssertEventsAndNoOthers();
1157             localForwardingSourceHelper.AssertEventsAndNoOthers();
1158             centralLoggerEventSource.AssertEventsAndNoOthers("anyEventRaised", "messageRaised");
1159             localForwardingSourceHelper.ClearEvents();
1160             centralLoggerEventSource.ClearEvents();
1161             eventSourceHelper.ClearEvents();
1162         }
1163 
1164         [Test]
ConfigurationByEngine()1165         public void ConfigurationByEngine()
1166         {
1167             VerifyEventSourceHelper eventSourceHelper = new VerifyEventSourceHelper();
1168             EngineLoggingServicesInProc inProcLoggingServicesEventsAllEvents = new EngineLoggingServicesInProc(eventSourceHelper.sourceForEvents, false, new ManualResetEvent(false));
1169             VerifyEventSourceHelper eventSourcePrivateHelper = new VerifyEventSourceHelper();
1170             EngineLoggingServicesInProc inProcLoggingServicesEventsAllEventsPrivate = new EngineLoggingServicesInProc(eventSourcePrivateHelper.sourceForEvents, false, new ManualResetEvent(false));
1171 
1172             Engine buildEngine = new Engine();
1173             buildEngine.LoggingServices = inProcLoggingServicesEventsAllEvents;
1174 
1175             // Create a logger that points at the private engine service (we'll use that logger as the central logger)
1176             ConfigurableForwardingLogger localLogger = new ConfigurableForwardingLogger();
1177             EventRedirector newRedirector = new EventRedirector(EngineLoggingServicesInProc.FIRST_AVAILABLE_LOGGERID, inProcLoggingServicesEventsAllEventsPrivate);
1178             localLogger.BuildEventRedirector = newRedirector;
1179             localLogger.Parameters = "TARGETSTARTEDEVENT;TARGETFINISHEDEVENT";
1180             inProcLoggingServicesEventsAllEventsPrivate.RegisterEventSource(EngineLoggingServicesInProc.FIRST_AVAILABLE_LOGGERID, eventSourcePrivateHelper.sourceForEvents);
1181 
1182             string filename = null;
1183             Assembly assembly = Assembly.Load("MICROSOFT.BUILD.ENGINE");
1184             filename = assembly.Location;
1185             Console.WriteLine("Using the following engine assembly: " + filename);
1186 
1187             LoggerDescription description = new LoggerDescription("ConfigurableForwardingLogger", null, filename, "TARGETSTARTEDEVENT;TARGETFINISHEDEVENT", LoggerVerbosity.Normal);
1188 
1189             buildEngine.RegisterDistributedLogger(localLogger, description);
1190 
1191             // Verify that BuildStarted event is delivered both to the forwarding logger and ILoggers and
1192             // that the forwarding logger forwards it to the central logger
1193             inProcLoggingServicesEventsAllEvents.PostLoggingEvent(new TargetStartedEventArgs("message", "help", "targetName", "ProjectFile", "targetFile"));
1194             inProcLoggingServicesEventsAllEvents.ProcessPostedLoggingEvents();
1195             eventSourceHelper.AssertEventsAndNoOthers("anyEventRaised", "targetStarted", "statusRaised");
1196             inProcLoggingServicesEventsAllEvents.ProcessPostedLoggingEvents();
1197             inProcLoggingServicesEventsAllEventsPrivate.ProcessPostedLoggingEvents();
1198             eventSourcePrivateHelper.AssertEventsAndNoOthers("anyEventRaised", "targetStarted", "statusRaised");
1199             eventSourcePrivateHelper.ClearEvents();
1200             eventSourceHelper.ClearEvents();
1201 
1202 
1203             // Verify that BuildFinished event is delivered both to the forwarding logger and ILoggers and
1204             // that the forwarding logger forwards it to the central logger
1205             inProcLoggingServicesEventsAllEvents.PostLoggingEvent(
1206                     new TargetFinishedEventArgs("message", "help", "targetName", "ProjectFile", "targetFile", true));
1207             inProcLoggingServicesEventsAllEvents.ProcessPostedLoggingEvents();
1208             eventSourceHelper.AssertEventsAndNoOthers("anyEventRaised", "targetFinished", "statusRaised");
1209             inProcLoggingServicesEventsAllEvents.ProcessPostedLoggingEvents();
1210             inProcLoggingServicesEventsAllEventsPrivate.ProcessPostedLoggingEvents();
1211             eventSourcePrivateHelper.AssertEventsAndNoOthers("anyEventRaised", "targetFinished", "statusRaised");
1212             eventSourcePrivateHelper.ClearEvents();
1213             eventSourceHelper.ClearEvents();
1214 
1215             // Verify that events that are not forwarded are not delivered to the central logger
1216             inProcLoggingServicesEventsAllEvents.PostLoggingEvent(new BuildMessageEventArgs("Message", "help", "sender", MessageImportance.Low));
1217             inProcLoggingServicesEventsAllEvents.ProcessPostedLoggingEvents();
1218             eventSourceHelper.AssertEventsAndNoOthers("anyEventRaised", "messageRaised");
1219             inProcLoggingServicesEventsAllEvents.ProcessPostedLoggingEvents();
1220             inProcLoggingServicesEventsAllEventsPrivate.ProcessPostedLoggingEvents();
1221             eventSourcePrivateHelper.AssertEventsAndNoOthers();
1222             eventSourcePrivateHelper.ClearEvents();
1223             eventSourceHelper.ClearEvents();
1224 
1225             // Verify that external events with no logger id are not delivered to the forwarding or central loggers
1226             inProcLoggingServicesEventsAllEvents.PostLoggingEvent(new NodeLoggingEvent(new BuildMessageEventArgs("Message", "help", "sender", MessageImportance.Low)));
1227             inProcLoggingServicesEventsAllEvents.ProcessPostedLoggingEvents();
1228             eventSourceHelper.AssertEventsAndNoOthers("anyEventRaised", "messageRaised");
1229             inProcLoggingServicesEventsAllEvents.ProcessPostedLoggingEvents();
1230             inProcLoggingServicesEventsAllEventsPrivate.ProcessPostedLoggingEvents();
1231             eventSourcePrivateHelper.AssertEventsAndNoOthers();
1232             eventSourcePrivateHelper.ClearEvents();
1233             eventSourceHelper.ClearEvents();
1234 
1235             // Verify that external events with logger id are only delivered to central logger
1236             inProcLoggingServicesEventsAllEvents.PostLoggingEvent
1237                 (new NodeLoggingEventWithLoggerId(
1238                         new TargetFinishedEventArgs("message", "help", "targetName", "ProjectFile", "targetFile", true), 2));
1239             inProcLoggingServicesEventsAllEvents.ProcessPostedLoggingEvents();
1240             eventSourceHelper.AssertEventsAndNoOthers();
1241             inProcLoggingServicesEventsAllEvents.ProcessPostedLoggingEvents();
1242             inProcLoggingServicesEventsAllEventsPrivate.ProcessPostedLoggingEvents();
1243             eventSourcePrivateHelper.AssertEventsAndNoOthers("anyEventRaised", "targetFinished", "statusRaised");
1244             eventSourcePrivateHelper.ClearEvents();
1245             eventSourceHelper.ClearEvents();
1246         }
1247 
1248         #endregion
1249 
1250         #region OutProcLoggingTest
1251 
1252         /// <summary>
1253         /// Test logging the out of proc logger by sending events to the logger and check
1254         /// the inproc logger queue which is the eventual handler of the events
1255         /// </summary>
1256         [Test]
OutProcLoggingTest()1257         public void OutProcLoggingTest()
1258         {
1259 
1260             VerifyEventSourceHelper eventSourceHelper = new VerifyEventSourceHelper();
1261             EngineLoggingServicesInProc inProcLoggingServicesEventsAllEvents = new EngineLoggingServicesInProc(eventSourceHelper.sourceForEvents, false, new ManualResetEvent(false));
1262 
1263 
1264             Engine buildEngine = new Engine();
1265             buildEngine.LoggingServices = inProcLoggingServicesEventsAllEvents;
1266 
1267             EngineCallback outProcessorProxy = new EngineCallback(buildEngine);
1268             int nodeId = buildEngine.GetNextNodeId();
1269             Node parentNode = new Node(nodeId, new LoggerDescription[0], outProcessorProxy, null,
1270                 ToolsetDefinitionLocations.ConfigurationFile | ToolsetDefinitionLocations.Registry, String.Empty);
1271 
1272 
1273             EngineLoggingServicesOutProc loggingServicesOutProc = new EngineLoggingServicesOutProc(parentNode, new ManualResetEvent(false));
1274 
1275             loggingServicesOutProc.PostLoggingEvent(new BuildMessageEventArgs("Message", "help", "sender", MessageImportance.Low));
1276             loggingServicesOutProc.ProcessPostedLoggingEvents();
1277             inProcLoggingServicesEventsAllEvents.ProcessPostedLoggingEvents();
1278             eventSourceHelper.AssertEventsAndNoOthers("anyEventRaised", "messageRaised");
1279             eventSourceHelper.ClearEvents();
1280 
1281             loggingServicesOutProc.PostLoggingEvent(new TaskStartedEventArgs("message", "help", "projectFile", "taskFile", "taskName"));
1282             loggingServicesOutProc.ProcessPostedLoggingEvents();
1283             inProcLoggingServicesEventsAllEvents.ProcessPostedLoggingEvents();
1284             eventSourceHelper.AssertEventsAndNoOthers("anyEventRaised", "taskStarted", "statusRaised");
1285             eventSourceHelper.ClearEvents();
1286 
1287             loggingServicesOutProc.PostLoggingEvent(new TaskFinishedEventArgs("message", "help", "projectFile", "taskFile", "taskName", true));
1288             loggingServicesOutProc.ProcessPostedLoggingEvents();
1289             inProcLoggingServicesEventsAllEvents.ProcessPostedLoggingEvents();
1290             eventSourceHelper.AssertEventsAndNoOthers("anyEventRaised", "taskFinished", "statusRaised");
1291             eventSourceHelper.ClearEvents();
1292 
1293             loggingServicesOutProc.PostLoggingEvent(new TaskCommandLineEventArgs("commandLine", "taskName", MessageImportance.Low));
1294             loggingServicesOutProc.ProcessPostedLoggingEvents();
1295             inProcLoggingServicesEventsAllEvents.ProcessPostedLoggingEvents();
1296             eventSourceHelper.AssertEventsAndNoOthers("anyEventRaised", "messageRaised");
1297             eventSourceHelper.ClearEvents();
1298 
1299             loggingServicesOutProc.PostLoggingEvent(new BuildWarningEventArgs("SubCategoryForSchemaValidationErrors", "MSB4000", "file", 1, 2, 3, 4, "message", "help", "sender"));
1300             loggingServicesOutProc.ProcessPostedLoggingEvents();
1301             inProcLoggingServicesEventsAllEvents.ProcessPostedLoggingEvents();
1302             eventSourceHelper.AssertEventsAndNoOthers("anyEventRaised", "warningRaised");
1303             eventSourceHelper.ClearEvents();
1304 
1305             loggingServicesOutProc.PostLoggingEvent(new BuildErrorEventArgs("SubCategoryForSchemaValidationErrors", "MSB4000", "file", 1, 2, 3, 4, "message", "help", "sender"));
1306             loggingServicesOutProc.ProcessPostedLoggingEvents();
1307             inProcLoggingServicesEventsAllEvents.ProcessPostedLoggingEvents();
1308             eventSourceHelper.AssertEventsAndNoOthers("anyEventRaised", "errorRaised");
1309             eventSourceHelper.ClearEvents();
1310 
1311             loggingServicesOutProc.PostLoggingEvent(new TargetStartedEventArgs("message", "help", "targetName", "ProjectFile", "targetFile"));
1312             loggingServicesOutProc.ProcessPostedLoggingEvents();
1313             inProcLoggingServicesEventsAllEvents.ProcessPostedLoggingEvents();
1314             eventSourceHelper.AssertEventsAndNoOthers("anyEventRaised", "targetStarted", "statusRaised");
1315             eventSourceHelper.ClearEvents();
1316 
1317             loggingServicesOutProc.PostLoggingEvent(new TargetFinishedEventArgs("message", "help", "targetName", "ProjectFile", "targetFile", true));
1318             loggingServicesOutProc.ProcessPostedLoggingEvents();
1319             inProcLoggingServicesEventsAllEvents.ProcessPostedLoggingEvents();
1320             eventSourceHelper.AssertEventsAndNoOthers("anyEventRaised", "targetFinished", "statusRaised");
1321             eventSourceHelper.ClearEvents();
1322 
1323             loggingServicesOutProc.PostLoggingEvent(new ProjectStartedEventArgs(-1, "message", "help", "ProjectFile", "targetNames", null, null, null));
1324             loggingServicesOutProc.ProcessPostedLoggingEvents();
1325             inProcLoggingServicesEventsAllEvents.ProcessPostedLoggingEvents();
1326             eventSourceHelper.AssertEventsAndNoOthers("anyEventRaised", "projectStarted", "statusRaised");
1327             eventSourceHelper.ClearEvents();
1328 
1329             loggingServicesOutProc.PostLoggingEvent(new ProjectFinishedEventArgs("message", "help", "ProjectFile", true));
1330             loggingServicesOutProc.ProcessPostedLoggingEvents();
1331             inProcLoggingServicesEventsAllEvents.ProcessPostedLoggingEvents();
1332             eventSourceHelper.AssertEventsAndNoOthers("anyEventRaised", "projectFinished", "statusRaised");
1333             eventSourceHelper.ClearEvents();
1334 
1335             loggingServicesOutProc.PostLoggingEvent(new BuildStartedEventArgs("message", "help"));
1336             loggingServicesOutProc.ProcessPostedLoggingEvents();
1337             inProcLoggingServicesEventsAllEvents.ProcessPostedLoggingEvents();
1338             eventSourceHelper.AssertEventsAndNoOthers("anyEventRaised", "buildStarted", "statusRaised");
1339             eventSourceHelper.ClearEvents();
1340 
1341             loggingServicesOutProc.PostLoggingEvent(new BuildFinishedEventArgs("message", "help", true));
1342             loggingServicesOutProc.ProcessPostedLoggingEvents();
1343             inProcLoggingServicesEventsAllEvents.ProcessPostedLoggingEvents();
1344             eventSourceHelper.AssertEventsAndNoOthers("anyEventRaised", "buildFinished", "statusRaised");
1345             eventSourceHelper.ClearEvents();
1346 
1347             loggingServicesOutProc.PostLoggingEvent(new ExternalProjectStartedEventArgs("message", "help", "senderName", "projectFile", "targetNames"));
1348             loggingServicesOutProc.ProcessPostedLoggingEvents();
1349             inProcLoggingServicesEventsAllEvents.ProcessPostedLoggingEvents();
1350             eventSourceHelper.AssertEventsAndNoOthers("anyEventRaised", "customEventRaised");
1351             eventSourceHelper.ClearEvents();
1352 
1353             loggingServicesOutProc.PostLoggingEvent(new ExternalProjectFinishedEventArgs("message", "help", "senderName", "projectFile", true));
1354             loggingServicesOutProc.ProcessPostedLoggingEvents();
1355             inProcLoggingServicesEventsAllEvents.ProcessPostedLoggingEvents();
1356             eventSourceHelper.AssertEventsAndNoOthers("anyEventRaised", "customEventRaised");
1357             eventSourceHelper.ClearEvents();
1358 
1359             loggingServicesOutProc.PostLoggingEvent(new MyCustomBuildEventArgs());
1360             loggingServicesOutProc.ProcessPostedLoggingEvents();
1361             inProcLoggingServicesEventsAllEvents.ProcessPostedLoggingEvents();
1362             eventSourceHelper.AssertEventsAndNoOthers("anyEventRaised", "customEventRaised");
1363             eventSourceHelper.ClearEvents();
1364 
1365             loggingServicesOutProc.PostLoggingEvent(new MyCustomBuildErrorEventArgs());
1366             loggingServicesOutProc.ProcessPostedLoggingEvents();
1367             inProcLoggingServicesEventsAllEvents.ProcessPostedLoggingEvents();
1368             eventSourceHelper.AssertEventsAndNoOthers("anyEventRaised", "errorRaised");
1369             eventSourceHelper.ClearEvents();
1370 
1371             loggingServicesOutProc.PostLoggingEvent(new MyCustomBuildWarningEventArgs());
1372             loggingServicesOutProc.ProcessPostedLoggingEvents();
1373             inProcLoggingServicesEventsAllEvents.ProcessPostedLoggingEvents();
1374             eventSourceHelper.AssertEventsAndNoOthers("anyEventRaised", "warningRaised");
1375             eventSourceHelper.ClearEvents();
1376 
1377             loggingServicesOutProc.PostLoggingEvent(new MyCustomBuildMessageEventArgs());
1378             loggingServicesOutProc.ProcessPostedLoggingEvents();
1379             inProcLoggingServicesEventsAllEvents.ProcessPostedLoggingEvents();
1380             eventSourceHelper.AssertEventsAndNoOthers("anyEventRaised", "messageRaised");
1381             eventSourceHelper.ClearEvents();
1382 
1383             loggingServicesOutProc.PostLoggingEvent(new MyCustomBuildMessageEventArgs());
1384             loggingServicesOutProc.PostLoggingEvent(new MyCustomBuildWarningEventArgs());
1385             loggingServicesOutProc.PostLoggingEvent(new MyCustomBuildErrorEventArgs());
1386             loggingServicesOutProc.ProcessPostedLoggingEvents();
1387             inProcLoggingServicesEventsAllEvents.ProcessPostedLoggingEvents();
1388             eventSourceHelper.AssertEventsAndNoOthers("anyEventRaised", "messageRaised", "warningRaised", "errorRaised");
1389             eventSourceHelper.ClearEvents();
1390 
1391             // Check that node logging events are forwarded correctly with Id
1392             loggingServicesOutProc.PostLoggingEvent(new NodeLoggingEventWithLoggerId(new BuildMessageEventArgs("Message", "help", "sender", MessageImportance.Low), 0));
1393             loggingServicesOutProc.ProcessPostedLoggingEvents();
1394             inProcLoggingServicesEventsAllEvents.ProcessPostedLoggingEvents();
1395             eventSourceHelper.AssertEventsAndNoOthers("anyEventRaised", "messageRaised");
1396             eventSourceHelper.ClearEvents();
1397 
1398             // Check that node logging events are forwarded correctly with no Id
1399             loggingServicesOutProc.PostLoggingEvent(new NodeLoggingEvent(new BuildMessageEventArgs("Message", "help", "sender", MessageImportance.Low)));
1400             loggingServicesOutProc.ProcessPostedLoggingEvents();
1401             inProcLoggingServicesEventsAllEvents.ProcessPostedLoggingEvents();
1402             eventSourceHelper.AssertEventsAndNoOthers("anyEventRaised", "messageRaised");
1403             eventSourceHelper.ClearEvents();
1404 
1405             // Register another event source and test that events are delivered correctly
1406             VerifyEventSourceHelper privateEventSourceHelper1 = new VerifyEventSourceHelper();
1407             VerifyEventSourceHelper privateEventSourceHelper2 = new VerifyEventSourceHelper();
1408             inProcLoggingServicesEventsAllEvents.RegisterEventSource(EngineLoggingServicesInProc.FIRST_AVAILABLE_LOGGERID, privateEventSourceHelper1.sourceForEvents);
1409             inProcLoggingServicesEventsAllEvents.RegisterEventSource(EngineLoggingServicesInProc.FIRST_AVAILABLE_LOGGERID + 1, privateEventSourceHelper2.sourceForEvents);
1410 
1411             // Check that node logging events are forwarded correctly with Id
1412             loggingServicesOutProc.PostLoggingEvent(new NodeLoggingEventWithLoggerId(new BuildMessageEventArgs("Message", "help", "sender", MessageImportance.Low), 0));
1413             loggingServicesOutProc.ProcessPostedLoggingEvents();
1414             inProcLoggingServicesEventsAllEvents.ProcessPostedLoggingEvents();
1415             eventSourceHelper.AssertEventsAndNoOthers("anyEventRaised", "messageRaised");
1416             eventSourceHelper.ClearEvents();
1417 
1418             //send a lot of events to test the event batching
1419             for (int i = 0; i < 600; i++)
1420             {
1421                 loggingServicesOutProc.PostLoggingEvent(new NodeLoggingEventWithLoggerId(new BuildMessageEventArgs("Message", "help", "sender", MessageImportance.Low), 0));
1422             }
1423             loggingServicesOutProc.ProcessPostedLoggingEvents();
1424             inProcLoggingServicesEventsAllEvents.ProcessPostedLoggingEvents();
1425             eventSourceHelper.AssertEventsAndNoOthers("anyEventRaised", "messageRaised");
1426             eventSourceHelper.ClearEvents();
1427 
1428             // Check that the events are correctly sorted when posted with different logger ids
1429             loggingServicesOutProc.PostLoggingEvent(new BuildMessageEventArgs("Message", "help", "sender", MessageImportance.Low));
1430             loggingServicesOutProc.PostLoggingEvent(new NodeLoggingEventWithLoggerId(new BuildStartedEventArgs("message", "help"), EngineLoggingServicesInProc.FIRST_AVAILABLE_LOGGERID));
1431             loggingServicesOutProc.PostLoggingEvent(new NodeLoggingEventWithLoggerId(new TargetFinishedEventArgs("message", "help", "targetName", "ProjectFile", "targetFile", true),
1432                                                                                      EngineLoggingServicesInProc.FIRST_AVAILABLE_LOGGERID +1));
1433             loggingServicesOutProc.ProcessPostedLoggingEvents();
1434             inProcLoggingServicesEventsAllEvents.ProcessPostedLoggingEvents();
1435             privateEventSourceHelper1.AssertEventsAndNoOthers("anyEventRaised", "statusRaised", "buildStarted");
1436             privateEventSourceHelper2.AssertEventsAndNoOthers("anyEventRaised", "statusRaised", "targetFinished");
1437             eventSourceHelper.AssertEventsAndNoOthers("anyEventRaised", "messageRaised" );
1438             eventSourceHelper.ClearEvents();
1439 
1440         }
1441 
1442         #endregion
1443     }
1444 }
1445