~ubuntu-branches/debian/sid/nunit/sid

« back to all changes in this revision

Viewing changes to src/PNUnit/agent/agent.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2014-09-16 13:43:36 UTC
  • mfrom: (1.1.5)
  • Revision ID: package-import@ubuntu.com-20140916134336-kjxz48tty6lx2ja5
Tags: 2.6.3+dfsg-1
* [c7bd1b5] Imported Upstream version 2.6.3+dfsg
* [bcb4bf8] Move nunit-console-runner to GAC-installed libnunit2.6, 
  don't treat it as a private lib. This lib is signed, and treated 
  as a GAC lib by consumers such as MonoDevelop.
* [7f08e99] Bump version to 2.6.3 as required
* [84535eb] Refreshed patches
* [8479f61] Split package up into per-assembly packages. This makes 
  ABI tracking easier in the future, as we can meaningfully have GAC 
  policy for cases where ABI isn't truly bumped, and no policy for 
  cases where it is. For example, if nunit.framework bumps ABI but 
  nunit.core does not, previously we would need to rebuild everything 
  using NUnit, but under the new split packaging, that rebuild would 
  not be needed for apps only using nunit.core.
* [17a7dc7] Add missing nunit.mocks.dll to nunit.pc

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
using System;
2
 
using System.IO;
3
 
using System.Collections;
4
 
using System.Runtime.Remoting;
5
 
using System.Runtime.Remoting.Channels;
6
 
using System.Runtime.Remoting.Channels.Tcp;
7
 
using System.Runtime.Serialization.Formatters;
8
 
using System.Reflection;
9
 
using System.Threading;
10
 
 
11
 
using log4net;
12
 
using log4net.Config;
13
 
 
14
 
using PNUnit.Framework;
15
 
 
16
 
using NUnit.Util;
17
 
 
18
 
namespace PNUnit.Agent
19
 
{
20
 
    class Agent
21
 
    {
22
 
        [STAThread]
23
 
        static void Main(string[] args)
24
 
        {
25
 
            ConfigureLogging();
26
 
 
27
 
            AgentConfig config = new AgentConfig();
28
 
 
29
 
            // read --daemon
30
 
            bool bDaemonMode = ReadFlag(args, "--daemon");
31
 
 
32
 
            bool bDomainPool = ReadFlag(args, "--domainpool");
33
 
 
34
 
            bool bNoTimeout = ReadFlag(args, "--notimeout");
35
 
 
36
 
            string configfile = ReadArg(args);
37
 
 
38
 
            int port = -1;
39
 
 
40
 
            string pathtoassemblies = ReadArg(args);
41
 
 
42
 
            if (pathtoassemblies != null)
43
 
            {
44
 
                port = int.Parse(configfile);
45
 
                configfile = null;
46
 
            }
47
 
 
48
 
            // Load the test configuration file
49
 
            if (pathtoassemblies == null && configfile == null)
50
 
            {
51
 
                Console.WriteLine("Usage: agent [configfile | port path_to_assemblies] [--daemon] [--domainpool] [--noTimeout]");
52
 
                return;
53
 
            }
54
 
 
55
 
            if (configfile != null)
56
 
            {
57
 
                config = AgentConfigLoader.LoadFromFile(configfile);
58
 
 
59
 
                if (config == null)
60
 
                {
61
 
                    Console.WriteLine("No agent.conf file found");
62
 
                }
63
 
            }
64
 
            else
65
 
            {
66
 
                config.Port = port;
67
 
                config.PathToAssemblies = pathtoassemblies;
68
 
            }
69
 
 
70
 
            // only override if set
71
 
            if( bDomainPool )
72
 
            {
73
 
                config.UseDomainPool = true;
74
 
            }
75
 
 
76
 
            if( bNoTimeout )
77
 
            {
78
 
                config.NoTimeout = true;
79
 
            }
80
 
 
81
 
            // initialize NUnit services
82
 
            // Add Standard Services to ServiceManager
83
 
            ServiceManager.Services.AddService(new SettingsService());
84
 
            ServiceManager.Services.AddService(new DomainManager());
85
 
            ServiceManager.Services.AddService(new ProjectService());
86
 
 
87
 
            // initialize NUnit services
88
 
            // Add Standard Services to ServiceManager
89
 
            ServiceManager.Services.AddService(new SettingsService());
90
 
            ServiceManager.Services.AddService(new DomainManager());
91
 
            ServiceManager.Services.AddService(new ProjectService());
92
 
 
93
 
            // Initialize Services
94
 
            ServiceManager.Services.InitializeServices();
95
 
 
96
 
 
97
 
            PNUnitAgent agent = new PNUnitAgent();
98
 
            agent.Run(config, bDaemonMode);
99
 
        }
100
 
 
101
 
        private static bool ReadFlag(string[] args, string flag)
102
 
        {
103
 
            for (int i = args.Length - 1; i >= 0; --i)
104
 
                if (args[i] == flag)
105
 
                {
106
 
                    args[i] = null;
107
 
                    return true;
108
 
                }
109
 
 
110
 
            return false;
111
 
        }
112
 
 
113
 
        private static string ReadArg(string[] args)
114
 
        {
115
 
            for (int i = 0; i < args.Length; ++i)
116
 
                if (args[i] != null)
117
 
                {
118
 
                    string result = args[i];
119
 
                    args[i] = null;
120
 
                    return result;
121
 
                }
122
 
            return null;
123
 
        }
124
 
 
125
 
        private static void ConfigureLogging()
126
 
        {
127
 
            string log4netpath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "agent.log.conf");
128
 
            XmlConfigurator.Configure(new FileInfo(log4netpath));
129
 
        }
130
 
    }
131
 
 
132
 
    public class PNUnitAgent : MarshalByRefObject, IPNUnitAgent
133
 
    {
134
 
        private AgentConfig mConfig;
135
 
        private static readonly ILog log = LogManager.GetLogger(typeof(PNUnitAgent));
136
 
 
137
 
        #region IPNUnitAgent
138
 
 
139
 
        public void RunTest(PNUnitTestInfo info)
140
 
        {
141
 
            log.InfoFormat("RunTest called for Test {0}, AssemblyName {1}, TestToRun {2}",
142
 
                info.TestName, info.AssemblyName, info.TestToRun);
143
 
 
144
 
            new PNUnitTestRunner(info, mConfig).Run();
145
 
        }
146
 
 
147
 
        #endregion
148
 
 
149
 
        #region MarshallByRefObject
150
 
        // Lives forever
151
 
        public override object InitializeLifetimeService()
152
 
        {
153
 
            return null;
154
 
        }
155
 
        #endregion
156
 
 
157
 
 
158
 
        private void ConfigureRemoting(int port)
159
 
        {
160
 
            if( File.Exists("agent.remoting.conf") )
161
 
            {
162
 
                log.Info("Using agent.remoting.conf");
163
 
#if CLR_2_0 || CLR_4_0
164
 
                RemotingConfiguration.Configure("agent.remoting.conf", false);
165
 
#else
166
 
                RemotingConfiguration.Configure("agent.remoting.conf");
167
 
#endif
168
 
                return;
169
 
            }
170
 
 
171
 
            // init remoting
172
 
            BinaryClientFormatterSinkProvider clientProvider =
173
 
                new BinaryClientFormatterSinkProvider();
174
 
            BinaryServerFormatterSinkProvider serverProvider =
175
 
                new BinaryServerFormatterSinkProvider();
176
 
            serverProvider.TypeFilterLevel =
177
 
                System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;
178
 
 
179
 
            IDictionary props = new Hashtable();
180
 
            props["port"] = port;
181
 
            string s = System.Guid.NewGuid().ToString();
182
 
            props["name"] = s;
183
 
            props["typeFilterLevel"] = TypeFilterLevel.Full;
184
 
            try
185
 
            {
186
 
                TcpChannel chan = new TcpChannel(
187
 
                    props, clientProvider, serverProvider);
188
 
 
189
 
                log.InfoFormat("Registering channel on port {0}", port);
190
 
#if CLR_2_0 || CLR_4_0
191
 
                ChannelServices.RegisterChannel(chan, false);
192
 
#else
193
 
                ChannelServices.RegisterChannel(chan);
194
 
#endif
195
 
            }
196
 
            catch (Exception e)
197
 
            {
198
 
                log.InfoFormat("Can't register channel.\n{0}", e.Message);
199
 
                return;
200
 
            }
201
 
        }
202
 
 
203
 
        public void Run(AgentConfig config, bool bDaemonMode)
204
 
        {
205
 
            if( config.UseDomainPool )
206
 
                log.Info("Agent using a domain pool to launch tests");
207
 
            mConfig = config;
208
 
 
209
 
            ConfigureRemoting(mConfig.Port);
210
 
 
211
 
            // publish
212
 
            RemotingServices.Marshal(this, PNUnit.Framework.Names.PNUnitAgentServiceName);
213
 
 
214
 
            // otherwise in .NET 2.0 memory grows continuosly
215
 
            FreeMemory();
216
 
 
217
 
            if( bDaemonMode )
218
 
            {
219
 
                // wait continously
220
 
                while (true)
221
 
                {
222
 
                    Thread.Sleep(10000);
223
 
                }
224
 
            }
225
 
            else
226
 
            {
227
 
                string line;
228
 
                while( (line = Console.ReadLine()) != "" )
229
 
                {
230
 
                    switch( line )
231
 
                    {
232
 
                        case "gc":
233
 
                            Console.WriteLine("Cleaning up memory {0} Mb",
234
 
                                GC.GetTotalMemory(true)/1024/1024);
235
 
                            break;
236
 
                        case "collect":
237
 
                            Console.WriteLine("Collecting memory {0} Mb",
238
 
                                GC.GetTotalMemory(false)/1024/1024);
239
 
                            GC.Collect();
240
 
                            Console.WriteLine("Memory collected {0} Mb",
241
 
                                GC.GetTotalMemory(false)/1024/1024);
242
 
                            break;
243
 
                    }
244
 
                }
245
 
            }
246
 
 
247
 
            //RemotingServices.Disconnect(this);
248
 
        }
249
 
 
250
 
        private void FreeMemory()
251
 
        {
252
 
            GC.GetTotalMemory(true);
253
 
        }
254
 
    }
255
 
 
256
 
}
 
1
using System;
 
2
using System.IO;
 
3
using System.Collections;
 
4
using System.Runtime.Remoting;
 
5
using System.Runtime.Remoting.Channels;
 
6
using System.Runtime.Remoting.Channels.Tcp;
 
7
using System.Runtime.Serialization.Formatters;
 
8
using System.Reflection;
 
9
using System.Threading;
 
10
 
 
11
using log4net;
 
12
using log4net.Config;
 
13
 
 
14
using PNUnit.Framework;
 
15
 
 
16
using NUnit.Util;
 
17
 
 
18
namespace PNUnit.Agent
 
19
{
 
20
    class Agent
 
21
    {
 
22
        [STAThread]
 
23
        static void Main(string[] args)
 
24
        {
 
25
            ConfigureLogging();
 
26
 
 
27
            AgentConfig config = new AgentConfig();
 
28
 
 
29
            // read --daemon
 
30
            bool bDaemonMode = ReadFlag(args, "--daemon");
 
31
 
 
32
            bool bDomainPool = ReadFlag(args, "--domainpool");
 
33
 
 
34
            bool bNoTimeout = ReadFlag(args, "--notimeout");
 
35
 
 
36
            string configfile = ReadArg(args);
 
37
 
 
38
            int port = -1;
 
39
 
 
40
            string pathtoassemblies = ReadArg(args);
 
41
 
 
42
            if (pathtoassemblies != null)
 
43
            {
 
44
                port = int.Parse(configfile);
 
45
                configfile = null;
 
46
            }
 
47
 
 
48
            // Load the test configuration file
 
49
            if (pathtoassemblies == null && configfile == null)
 
50
            {
 
51
                Console.WriteLine("Usage: agent [configfile | port path_to_assemblies] [--daemon] [--domainpool] [--noTimeout]");
 
52
                return;
 
53
            }
 
54
 
 
55
            if (configfile != null)
 
56
            {
 
57
                config = AgentConfigLoader.LoadFromFile(configfile);
 
58
 
 
59
                if (config == null)
 
60
                {
 
61
                    Console.WriteLine("No agent.conf file found");
 
62
                }
 
63
            }
 
64
            else
 
65
            {
 
66
                config.Port = port;
 
67
                config.PathToAssemblies = pathtoassemblies;
 
68
            }
 
69
 
 
70
            // only override if set
 
71
            if( bDomainPool )
 
72
            {
 
73
                config.UseDomainPool = true;
 
74
            }
 
75
 
 
76
            if( bNoTimeout )
 
77
            {
 
78
                config.NoTimeout = true;
 
79
            }
 
80
 
 
81
            // initialize NUnit services
 
82
            // Add Standard Services to ServiceManager
 
83
            ServiceManager.Services.AddService(new SettingsService());
 
84
            ServiceManager.Services.AddService(new DomainManager());
 
85
            ServiceManager.Services.AddService(new ProjectService());
 
86
 
 
87
            // initialize NUnit services
 
88
            // Add Standard Services to ServiceManager
 
89
            ServiceManager.Services.AddService(new SettingsService());
 
90
            ServiceManager.Services.AddService(new DomainManager());
 
91
            ServiceManager.Services.AddService(new ProjectService());
 
92
 
 
93
            // Initialize Services
 
94
            ServiceManager.Services.InitializeServices();
 
95
 
 
96
 
 
97
            PNUnitAgent agent = new PNUnitAgent();
 
98
            agent.Run(config, bDaemonMode);
 
99
        }
 
100
 
 
101
        private static bool ReadFlag(string[] args, string flag)
 
102
        {
 
103
            for (int i = args.Length - 1; i >= 0; --i)
 
104
                if (args[i] == flag)
 
105
                {
 
106
                    args[i] = null;
 
107
                    return true;
 
108
                }
 
109
 
 
110
            return false;
 
111
        }
 
112
 
 
113
        private static string ReadArg(string[] args)
 
114
        {
 
115
            for (int i = 0; i < args.Length; ++i)
 
116
                if (args[i] != null)
 
117
                {
 
118
                    string result = args[i];
 
119
                    args[i] = null;
 
120
                    return result;
 
121
                }
 
122
            return null;
 
123
        }
 
124
 
 
125
        private static void ConfigureLogging()
 
126
        {
 
127
            string log4netpath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "agent.log.conf");
 
128
            XmlConfigurator.Configure(new FileInfo(log4netpath));
 
129
        }
 
130
    }
 
131
 
 
132
    public class PNUnitAgent : MarshalByRefObject, IPNUnitAgent
 
133
    {
 
134
        private AgentConfig mConfig;
 
135
        private static readonly ILog log = LogManager.GetLogger(typeof(PNUnitAgent));
 
136
 
 
137
        #region IPNUnitAgent
 
138
 
 
139
        public void RunTest(PNUnitTestInfo info)
 
140
        {
 
141
            log.InfoFormat("RunTest called for Test {0}, AssemblyName {1}, TestToRun {2}",
 
142
                info.TestName, info.AssemblyName, info.TestToRun);
 
143
 
 
144
            new PNUnitTestRunner(info, mConfig).Run();
 
145
        }
 
146
 
 
147
        #endregion
 
148
 
 
149
        #region MarshallByRefObject
 
150
        // Lives forever
 
151
        public override object InitializeLifetimeService()
 
152
        {
 
153
            return null;
 
154
        }
 
155
        #endregion
 
156
 
 
157
 
 
158
        private void ConfigureRemoting(int port)
 
159
        {
 
160
            if( File.Exists("agent.remoting.conf") )
 
161
            {
 
162
                log.Info("Using agent.remoting.conf");
 
163
#if CLR_2_0 || CLR_4_0
 
164
                RemotingConfiguration.Configure("agent.remoting.conf", false);
 
165
#else
 
166
                RemotingConfiguration.Configure("agent.remoting.conf");
 
167
#endif
 
168
                return;
 
169
            }
 
170
 
 
171
            // init remoting
 
172
            BinaryClientFormatterSinkProvider clientProvider =
 
173
                new BinaryClientFormatterSinkProvider();
 
174
            BinaryServerFormatterSinkProvider serverProvider =
 
175
                new BinaryServerFormatterSinkProvider();
 
176
            serverProvider.TypeFilterLevel =
 
177
                System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;
 
178
 
 
179
            IDictionary props = new Hashtable();
 
180
            props["port"] = port;
 
181
            string s = System.Guid.NewGuid().ToString();
 
182
            props["name"] = s;
 
183
            props["typeFilterLevel"] = TypeFilterLevel.Full;
 
184
            try
 
185
            {
 
186
                TcpChannel chan = new TcpChannel(
 
187
                    props, clientProvider, serverProvider);
 
188
 
 
189
                log.InfoFormat("Registering channel on port {0}", port);
 
190
#if CLR_2_0 || CLR_4_0
 
191
                ChannelServices.RegisterChannel(chan, false);
 
192
#else
 
193
                ChannelServices.RegisterChannel(chan);
 
194
#endif
 
195
            }
 
196
            catch (Exception e)
 
197
            {
 
198
                log.InfoFormat("Can't register channel.\n{0}", e.Message);
 
199
                return;
 
200
            }
 
201
        }
 
202
 
 
203
        public void Run(AgentConfig config, bool bDaemonMode)
 
204
        {
 
205
            if( config.UseDomainPool )
 
206
                log.Info("Agent using a domain pool to launch tests");
 
207
            mConfig = config;
 
208
 
 
209
            ConfigureRemoting(mConfig.Port);
 
210
 
 
211
            // publish
 
212
            RemotingServices.Marshal(this, PNUnit.Framework.Names.PNUnitAgentServiceName);
 
213
 
 
214
            // otherwise in .NET 2.0 memory grows continuosly
 
215
            FreeMemory();
 
216
 
 
217
            if( bDaemonMode )
 
218
            {
 
219
                // wait continously
 
220
                while (true)
 
221
                {
 
222
                    Thread.Sleep(10000);
 
223
                }
 
224
            }
 
225
            else
 
226
            {
 
227
                string line;
 
228
                while( (line = Console.ReadLine()) != "" )
 
229
                {
 
230
                    switch( line )
 
231
                    {
 
232
                        case "gc":
 
233
                            Console.WriteLine("Cleaning up memory {0} Mb",
 
234
                                GC.GetTotalMemory(true)/1024/1024);
 
235
                            break;
 
236
                        case "collect":
 
237
                            Console.WriteLine("Collecting memory {0} Mb",
 
238
                                GC.GetTotalMemory(false)/1024/1024);
 
239
                            GC.Collect();
 
240
                            Console.WriteLine("Memory collected {0} Mb",
 
241
                                GC.GetTotalMemory(false)/1024/1024);
 
242
                            break;
 
243
                    }
 
244
                }
 
245
            }
 
246
 
 
247
            //RemotingServices.Disconnect(this);
 
248
        }
 
249
 
 
250
        private void FreeMemory()
 
251
        {
 
252
            GC.GetTotalMemory(true);
 
253
        }
 
254
    }
 
255
 
 
256
}