-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathProgram.cs
222 lines (197 loc) · 9.43 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
//-----------------------------------------------------------------------
// <copyright file="Program.cs" company="Akka.NET Project">
// Copyright (C) 2009-2021 Lightbend Inc. <http://www.lightbend.com>
// Copyright (C) 2013-2021 .NET Foundation <https://github.com/akkadotnet/akka.net>
// </copyright>
//-----------------------------------------------------------------------
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Akka.Actor;
using Akka.IO;
using Akka.MultiNodeTestRunner.Shared.Sinks;
using Akka.Remote.TestKit;
using Xunit;
#if CORECLR
using System.Runtime.Loader;
using Microsoft.Extensions.DependencyModel;
#endif
namespace Akka.NodeTestRunner
{
class Program
{
/// <summary>
/// If it takes longer than this value for the <see cref="Sink"/> to get back to us
/// about a particular test passing or failing, throw loudly.
/// </summary>
private static readonly TimeSpan MaxProcessWaitTimeout = TimeSpan.FromMinutes(5);
private static IActorRef _logger;
static int Main(string[] args)
{
CommandLine.Initialize(args);
var nodeIndex = CommandLine.GetInt32("multinode.index");
var nodeRole = CommandLine.GetProperty("multinode.role");
var assemblyFileName = CommandLine.GetProperty("multinode.test-assembly");
var typeName = CommandLine.GetProperty("multinode.test-class");
var testName = CommandLine.GetProperty("multinode.test-method");
var displayName = testName;
var listenAddress = IPAddress.Parse(CommandLine.GetProperty("multinode.listen-address"));
var listenPort = CommandLine.GetInt32("multinode.listen-port");
var listenEndpoint = new IPEndPoint(listenAddress, listenPort);
var system = ActorSystem.Create("NoteTestRunner-" + nodeIndex);
var tcpClient = _logger = system.ActorOf<RunnerTcpClient>();
system.Tcp().Tell(new Tcp.Connect(listenEndpoint), tcpClient);
#if CORECLR
// In NetCore, if the assembly file hasn't been touched,
// XunitFrontController would fail loading external assemblies and its dependencies.
AssemblyLoadContext.Default.Resolving += (assemblyLoadContext, assemblyName) => DefaultOnResolving(assemblyLoadContext, assemblyName, assemblyFileName);
var assembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(assemblyFileName);
DependencyContext.Load(assembly)
.CompileLibraries
.Where(dep => dep.Name.ToLower()
.Contains(assembly.FullName.Split(new[] { ',' })[0].ToLower()))
.Select(dependency => AssemblyLoadContext.Default.LoadFromAssemblyName(new AssemblyName(dependency.Name)));
#endif
Thread.Sleep(TimeSpan.FromSeconds(10));
Environment.SetEnvironmentVariable(MultiNodeFactAttribute.MultiNodeTestEnvironmentName, "1");
using (var controller = new XunitFrontController(AppDomainSupport.IfAvailable, assemblyFileName))
{
/* need to pass in just the assembly name to Discovery, not the full path
* i.e. "Akka.Cluster.Tests.MultiNode.dll"
* not "bin/Release/Akka.Cluster.Tests.MultiNode.dll" - this will cause
* the Discovery class to actually not find any individual specs to run
*/
var assemblyName = Path.GetFileName(assemblyFileName);
Console.WriteLine("Running specs for {0} [{1}]", assemblyName, assemblyFileName);
using (var discovery = new Discovery(assemblyName, typeName))
{
using (var sink = new Sink(nodeIndex, nodeRole, tcpClient))
{
try
{
controller.Find(true, discovery, TestFrameworkOptions.ForDiscovery());
discovery.Finished.WaitOne();
controller.RunTests(discovery.TestCases, sink, TestFrameworkOptions.ForExecution());
}
catch (AggregateException ex)
{
var specFail = new SpecFail(nodeIndex, nodeRole, displayName);
specFail.FailureExceptionTypes.Add(ex.GetType().ToString());
specFail.FailureMessages.Add(ex.Message);
specFail.FailureStackTraces.Add(ex.StackTrace);
foreach (var innerEx in ex.Flatten().InnerExceptions)
{
specFail.FailureExceptionTypes.Add(innerEx.GetType().ToString());
specFail.FailureMessages.Add(innerEx.Message);
specFail.FailureStackTraces.Add(innerEx.StackTrace);
}
_logger.Tell(specFail.ToString());
Console.WriteLine(specFail);
//make sure message is send over the wire
FlushLogMessages();
Environment.Exit(1); //signal failure
return 1;
}
catch (Exception ex)
{
var specFail = new SpecFail(nodeIndex, nodeRole, displayName);
specFail.FailureExceptionTypes.Add(ex.GetType().ToString());
specFail.FailureMessages.Add(ex.Message);
specFail.FailureStackTraces.Add(ex.StackTrace);
var innerEx = ex.InnerException;
while (innerEx != null)
{
specFail.FailureExceptionTypes.Add(innerEx.GetType().ToString());
specFail.FailureMessages.Add(innerEx.Message);
specFail.FailureStackTraces.Add(innerEx.StackTrace);
innerEx = innerEx.InnerException;
}
_logger.Tell(specFail.ToString());
Console.WriteLine(specFail);
//make sure message is send over the wire
FlushLogMessages();
Environment.Exit(1); //signal failure
return 1;
}
var timedOut = false;
if (!sink.Finished.WaitOne(MaxProcessWaitTimeout)) //timed out
{
var line = string.Format("Timed out while waiting for test to complete after {0} ms",
MaxProcessWaitTimeout);
_logger.Tell(line);
Console.WriteLine(line);
timedOut = true;
}
FlushLogMessages();
system.Terminate().Wait();
Environment.Exit(sink.Passed && !timedOut ? 0 : 1);
return sink.Passed ? 0 : 1;
}
}
}
}
private static void FlushLogMessages()
{
try
{
_logger.GracefulStop(TimeSpan.FromSeconds(2)).Wait();
}
catch
{
Console.WriteLine("Exception thrown while waiting for TCP transport to flush - not all messages may have been logged.");
}
}
#if CORECLR
private static Assembly DefaultOnResolving(AssemblyLoadContext assemblyLoadContext, AssemblyName assemblyName, string assemblyPath)
{
string dllName = assemblyName.Name.Split(new[] { ',' })[0] + ".dll";
return assemblyLoadContext.LoadFromAssemblyPath(Path.Combine(Path.GetDirectoryName(assemblyPath), dllName));
}
#endif
}
class RunnerTcpClient : ReceiveActor, IWithUnboundedStash
{
private IActorRef _connection;
public RunnerTcpClient()
{
Become(WaitingForConnection);
}
/// <inheritdoc />
protected override void PostStop()
{
// Close connection property to avoid exception logged at TcpConnection actor once this actor is terminated
try
{
_connection.Ask<Tcp.Closed>(Tcp.Close.Instance, TimeSpan.FromSeconds(1)).Wait();
}
catch { /* well... at least we have tried */ }
base.PostStop();
}
private void WaitingForConnection()
{
Receive<Tcp.Connected>(connected =>
{
Sender.Tell(new Tcp.Register(Self));
_connection = Sender;
Become(Connected(Sender));
});
Receive<string>(_ => Stash.Stash());
}
private Receive Connected(IActorRef connection)
{
Stash.UnstashAll();
return message =>
{
var bytes = ByteString.FromString(message.ToString());
connection.Tell(Tcp.Write.Create(bytes));
return true;
};
}
public IStash Stash { get; set; }
}
}