~ubuntu-branches/ubuntu/trusty/gnome-do/trusty

« back to all changes in this revision

Viewing changes to Tests/TestLogging.cs

  • Committer: Package Import Robot
  • Author(s): Christopher James Halse Rogers
  • Date: 2012-03-26 11:12:21 UTC
  • mfrom: (0.1.12 sid)
  • Revision ID: package-import@ubuntu.com-20120326111221-1jk143fy37zxi3e4
Tags: 0.9-1
* New upstream version no longer uses deprecated internal glib headers.
  (Closes: #665537)
* [59fa37b9] Fix watch file
* [63486516] Imported Upstream version 0.9
* [8c636d84] Disable testsuite for now; requires running dbus and gconf daemons
* [e46de4b9] Remove inaccurate README.Source
* [4591d677] Add git-buildpackage configuration to default to pristine-tar

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
//  TestLogging.cs
 
3
//  
 
4
//  Author:
 
5
//       Christopher James Halse Rogers <raof@ubuntu.com>
 
6
// 
 
7
//  Copyright © 2011 Christopher James Halse Rogers <raof@ubuntu.com>
 
8
// 
 
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.
 
13
// 
 
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.
 
18
// 
 
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
 
22
using System;
 
23
using System.Linq;
 
24
using System.Threading;
 
25
using System.Collections.Generic;
 
26
 
 
27
using NUnit.Framework;
 
28
 
 
29
using Mono.Addins;
 
30
using Do.Platform;
 
31
using Do.Platform.Common;
 
32
using Do.Platform.ServiceStack;
 
33
 
 
34
namespace Do
 
35
{
 
36
        public class MockLogger : AbstractLogService
 
37
        {
 
38
                List<Tuple<LogLevel, string>> log;
 
39
                public bool recursiveLog = false;
 
40
 
 
41
                public override void Log (LogLevel level, string msg)
 
42
                {
 
43
                        if (log != null) {
 
44
                                log.Add (new Tuple<LogLevel, string> (level, msg));
 
45
                                if (recursiveLog) {
 
46
                                        Platform.Log.Debug ("Recursive log: {0}", msg);
 
47
                                }
 
48
                        }
 
49
                }
 
50
 
 
51
                public void StartLog ()
 
52
                {
 
53
                        log = new List<Tuple<LogLevel, string>> ();
 
54
                }
 
55
 
 
56
                public IEnumerable<Tuple<LogLevel, string>> EndLog ()
 
57
                {
 
58
                        return Interlocked.Exchange (ref log, null);
 
59
                }
 
60
        }
 
61
 
 
62
        [TestFixture()]
 
63
        public class TestLogging
 
64
        {
 
65
                MockLogger logger;
 
66
                [SetUp()]
 
67
                public void SetUp ()
 
68
                {
 
69
                        Gtk.Application.Init ();
 
70
                        Gdk.Threads.Init ();
 
71
                        Core.PluginManager.Initialize ();
 
72
                        AddinManager.Registry.Update ();
 
73
                        logger = Services.Logs.OfType<MockLogger> ().First ();
 
74
                }
 
75
 
 
76
                [Test()]
 
77
                public void TestSimpleDebugLogIsLogged ()
 
78
                {
 
79
                        logger.StartLog ();
 
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 ());
 
83
                }
 
84
 
 
85
                [Test, Timeout (2000)]
 
86
                public void TestMultiThreadedLogging ()
 
87
                {
 
88
                        var logMessages = new List<string> ();
 
89
                        for (int i = 0; i < 100; i++) {
 
90
                                logMessages.Add (string.Format ("Log message {0}", i));
 
91
                        }
 
92
 
 
93
                        logger.StartLog ();
 
94
 
 
95
                        ManualResetEvent[] waitHandles = new ManualResetEvent[logMessages.Count];
 
96
                        for (int i = 0; i < waitHandles.Length; ++i) {
 
97
                                waitHandles [i] = new ManualResetEvent (false);
 
98
                        }
 
99
 
 
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 ();
 
106
                                });
 
107
                                thread.Start ();
 
108
                        }
 
109
 
 
110
                        foreach (var handle in waitHandles)
 
111
                                handle.WaitOne ();
 
112
 
 
113
                        var logs = logger.EndLog ().ToArray ();
 
114
                        foreach (var msg in logMessages) {
 
115
                                Assert.Contains (new Tuple<LogLevel, string> (LogLevel.Debug, msg), logs);
 
116
                        }
 
117
                }
 
118
 
 
119
                [Test]
 
120
                public void TestRecursiveLoggingThrowsException ()
 
121
                {
 
122
                        string msg = "Hello";
 
123
                        logger.StartLog ();
 
124
                        logger.recursiveLog = true;
 
125
                        Assert.Throws<InvalidOperationException> (delegate {
 
126
                                Log.Debug (msg); });
 
127
                        logger.recursiveLog = false;
 
128
                }
 
129
        }
 
130
}
 
131