5
// Christopher James Halse Rogers <raof@ubuntu.com>
7
// Copyright © 2011 Christopher James Halse Rogers <raof@ubuntu.com>
9
// This library is free software; you can redistribute it and/or modify
10
// it under the terms of the GNU Lesser General Public License as
11
// published by the Free Software Foundation; either version 2.1 of the
12
// License, or (at your option) any later version.
14
// This library is distributed in the hope that it will be useful, but
15
// WITHOUT ANY WARRANTY; without even the implied warranty of
16
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17
// Lesser General Public License for more details.
19
// You should have received a copy of the GNU Lesser General Public
20
// License along with this library; if not, write to the Free Software
21
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24
using System.Threading;
25
using System.Collections.Generic;
27
using NUnit.Framework;
31
using Do.Platform.Common;
32
using Do.Platform.ServiceStack;
36
public class MockLogger : AbstractLogService
38
List<Tuple<LogLevel, string>> log;
39
public bool recursiveLog = false;
41
public override void Log (LogLevel level, string msg)
44
log.Add (new Tuple<LogLevel, string> (level, msg));
46
Platform.Log.Debug ("Recursive log: {0}", msg);
51
public void StartLog ()
53
log = new List<Tuple<LogLevel, string>> ();
56
public IEnumerable<Tuple<LogLevel, string>> EndLog ()
58
return Interlocked.Exchange (ref log, null);
63
public class TestLogging
69
Gtk.Application.Init ();
71
Core.PluginManager.Initialize ();
72
AddinManager.Registry.Update ();
73
logger = Services.Logs.OfType<MockLogger> ().First ();
77
public void TestSimpleDebugLogIsLogged ()
80
Log.Debug ("This is a log message");
81
var logs = logger.EndLog ();
82
Assert.Contains (new Tuple<LogLevel, string> (LogLevel.Debug, "This is a log message"), logs.ToArray ());
85
[Test, Timeout (2000)]
86
public void TestMultiThreadedLogging ()
88
var logMessages = new List<string> ();
89
for (int i = 0; i < 100; i++) {
90
logMessages.Add (string.Format ("Log message {0}", i));
95
ManualResetEvent[] waitHandles = new ManualResetEvent[logMessages.Count];
96
for (int i = 0; i < waitHandles.Length; ++i) {
97
waitHandles [i] = new ManualResetEvent (false);
100
int threadCounter = -1;
101
foreach (var message in logMessages) {
102
var thread = new Thread (() => {
103
int i = Interlocked.Increment (ref threadCounter);
104
Log.Debug (logMessages [i]);
105
waitHandles [i].Set ();
110
foreach (var handle in waitHandles)
113
var logs = logger.EndLog ().ToArray ();
114
foreach (var msg in logMessages) {
115
Assert.Contains (new Tuple<LogLevel, string> (LogLevel.Debug, msg), logs);
120
public void TestRecursiveLoggingThrowsException ()
122
string msg = "Hello";
124
logger.recursiveLog = true;
125
Assert.Throws<InvalidOperationException> (delegate {
127
logger.recursiveLog = false;