-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathSmokeTester.cs
350 lines (308 loc) · 12.4 KB
/
SmokeTester.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using Sentry;
using UnityEngine;
using Debug = UnityEngine.Debug;
#if UNITY_WEBGL
using System.Web;
#endif
public class SmokeTester : MonoBehaviour
{
private void Awake()
{
Debug.Log("SmokeTester, awake!");
Application.quitting += () =>
{
// We're using this in the smoke-test-android.ps1 script to reliably detect when the tests have finished running.
Debug.Log("SmokeTester is quitting.");
};
}
public void Start()
{
Debug.Log("SmokeTester starting");
var arg = GetTestArg();
Debug.Log($"SmokeTester arg: '{arg}'");
if (arg == "smoke")
{
SmokeTest();
}
else if (arg == "hasnt-crashed")
{
HasntCrashedTest();
}
else if (arg == "crash")
{
CrashTest();
}
else if (arg == "has-crashed")
{
HasCrashedTest();
}
else if (arg != null)
{
Debug.Log($"Unknown command line argument: {arg}");
Application.Quit(-1);
}
}
#if UNITY_IOS && !UNITY_EDITOR
// .NET `Environment.GetCommandLineArgs()` doesn't seem to work on iOS so we get the test arg in Objective-C
[DllImport("__Internal", EntryPoint="getTestArgObjectiveC")]
private static extern string GetTestArg();
#else
private static string GetTestArg()
{
string arg = null;
#if UNITY_EDITOR
#elif UNITY_ANDROID
using (var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
using (var currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
using (var intent = currentActivity.Call<AndroidJavaObject>("getIntent"))
{
arg = intent.Call<String>("getStringExtra", "test");
}
#elif UNITY_WEBGL
var uri = new Uri(Application.absoluteURL);
arg = HttpUtility.ParseQueryString(uri.Query).Get("test");
#else
var args = Environment.GetCommandLineArgs();
if (args.Length > 2 && args[1] == "--test")
{
arg = args[2];
}
#endif
return arg;
}
#endif
internal static TestHandler t = new TestHandler();
internal static Func<int> CrashedLastRun = () => -1;
public static void SmokeTest()
{
t.Start("SMOKE");
try
{
#if !UNITY_EDITOR
var crashed = CrashedLastRun();
t.Expect($"options.CrashedLastRun ({crashed}) == false (0)", crashed == 0);
#endif
var currentMessage = 0;
t.ExpectMessage(currentMessage, "'type':'session'");
// Skip the session init requests (there may be multiple of them). We can't skip them by a "positive"
// because they're also repeated with standard events (in an envelope).
Debug.Log("SMOKE TEST: Skipping all session requests");
for (; currentMessage < 10; currentMessage++)
{
if (t.CheckMessage(currentMessage, "'type':'transaction'"))
{
break;
}
}
Debug.Log($"SMOKE TEST: Done skipping session requests. Last one was: #{currentMessage}");
t.ExpectMessage(currentMessage, "'type':'transaction");
t.ExpectMessage(currentMessage, "'op':'app.start'"); // startup transaction
#if !UNITY_EDITOR
t.ExpectMessage(currentMessage, "'op':'awake','description':'Main Camera.SmokeTester'"); // auto instrumentation
#endif
t.ExpectMessageNot(currentMessage, "'length':0");
var guid = Guid.NewGuid().ToString();
Debug.LogError($"LogError(GUID)={guid}");
currentMessage++;
t.ExpectMessage(currentMessage, "'type':'event'");
t.ExpectMessage(currentMessage, $"LogError(GUID)={guid}");
// Contexts
t.ExpectMessage(currentMessage, "'type':'app',");
t.ExpectMessage(currentMessage, "'type':'device',");
t.ExpectMessage(currentMessage, "'type':'gpu',");
t.ExpectMessage(currentMessage, "'type':'os',");
t.ExpectMessage(currentMessage, "'type':'runtime',");
t.ExpectMessage(currentMessage, "'type':'unity',");
// User
t.ExpectMessage(currentMessage, "'user':{'id':'"); // non-null automatic ID
// Attachment
t.ExpectMessage(currentMessage, "'filename':'screenshot.jpg','attachment_type':'event.attachment'");
t.ExpectMessageNot(currentMessage, "'length':0");
SentrySdk.CaptureMessage($"CaptureMessage(GUID)={guid}");
currentMessage++;
t.ExpectMessage(currentMessage, "'type':'event'");
t.ExpectMessage(currentMessage, $"CaptureMessage(GUID)={guid}");
t.ExpectMessage(currentMessage, "'filename':'screenshot.jpg','attachment_type':'event.attachment'");
t.ExpectMessageNot(currentMessage, "'length':0");
var ex = new Exception("Exception & context test");
AddContext();
SentrySdk.CaptureException(ex);
t.ExpectMessage(++currentMessage, "'type':'event'");
t.ExpectMessage(currentMessage, "'message':'crumb','type':'error','data':{'foo':'bar'},'category':'bread','level':'critical'}");
t.ExpectMessage(currentMessage, "'message':'scope-crumb'}");
t.ExpectMessage(currentMessage, "'extra':{'extra-key':42}");
t.ExpectMessage(currentMessage, "'tag-key':'tag-value'");
t.ExpectMessage(currentMessage, "'user':{'id':'user-id','username':'username','email':'[email protected]','ip_address':'::1','other':{'role':'admin'}}");
t.ExpectMessage(currentMessage, "'filename':'screenshot.jpg','attachment_type':'event.attachment'");
t.ExpectMessageNot(currentMessage, "'length':0");
Debug.Log("SMOKE TEST: Finished checking messages.");
t.Pass();
}
catch (Exception ex)
{
if (t.ExitCode == 0)
{
Debug.Log($"SMOKE TEST: FAILED with exception {ex}");
t.Exit(-1);
}
else
{
Debug.Log("SMOKE TEST: FAILED");
}
}
}
public static void CrashTest()
{
t.Start("CRASH");
AddContext();
Debug.Log("CRASH TEST: Issuing a native crash (c++ unhandled exception)");
throw_cpp();
// shouldn't execute because the previous call should have failed
Debug.Log("CRASH TEST: FAIL - unexpected code executed...");
Application.Quit(-1);
}
public static void HasntCrashedTest()
{
t.Start("HASNT-CRASHED");
var crashed = CrashedLastRun();
t.Expect($"options.CrashedLastRun ({crashed}) == false (0)", crashed == 0);
t.Pass();
}
public static void HasCrashedTest()
{
t.Start("HAS-CRASHED");
var crashed = CrashedLastRun();
t.Expect($"options.CrashedLastRun ({crashed}) == true (1)", crashed == 1);
t.Pass();
}
private static void AddContext()
{
SentrySdk.AddBreadcrumb("crumb", "bread", "error", new Dictionary<string, string>() { { "foo", "bar" } }, BreadcrumbLevel.Critical);
SentrySdk.ConfigureScope((Scope scope) =>
{
scope.SetExtra("extra-key", 42);
scope.AddBreadcrumb("scope-crumb");
scope.SetTag("tag-key", "tag-value");
scope.User = new SentryUser()
{
Username = "username",
Email = "[email protected]",
IpAddress = "::1",
Id = "user-id",
Other = new Dictionary<string, string>() { { "role", "admin" } }
};
});
}
// CppPlugin.cpp
[DllImport("__Internal")]
private static extern void throw_cpp();
internal class TestHandler : HttpClientHandler
{
private string _name;
private ConcurrentQueue<string> _requests = new ConcurrentQueue<string>();
private AutoResetEvent _requestReceived = new AutoResetEvent(false);
private readonly TimeSpan _receiveTimeout = TimeSpan.FromSeconds(10);
private int _testNumber = 0;
public int ExitCode = 0;
public void Start(string testName)
{
_name = testName;
Debug.Log($"{_name} TEST: start");
}
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
Receive(request);
return Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK));
}
private void Receive(HttpRequestMessage message)
{
var msgText = message.Content.ReadAsStringAsync().Result;
// Setting "Sentry" as tag to prevent the UnityLogHandlerIntegration from capturing this message and
// adding it as a breadcrumb, which in turn multiplies it on following (intercepted) HTTP requests...
// Note: remove the prefix once setting breadcrumb log level is possible - https://github.com/getsentry/sentry-unity/issues/60
Debug.unityLogger.Log(LogType.Log, "Sentry", $"{_name} TEST: Intercepted HTTP Request #{_requests.Count} = {msgText}");
_requests.Enqueue(msgText);
_requestReceived.Set();
}
public void Exit(int code)
{
if (ExitCode != 0)
{
Debug.Log($"{_name} TEST: Ignoring spurious Exit({code}). Application is already exiting with code {ExitCode}");
}
else
{
#if !UNITY_EDITOR
ExitCode = code;
Application.Quit(code);
// Application.Quit doesn't actually terminate immediately so exit the context at least...
throw new Exception($"Quitting with exit code {code}");
#endif
}
}
public void Pass()
{
if (ExitCode == 0)
{
// On Android we'll grep logcat for this string instead of relying on exit code:
Debug.Log($"{_name} TEST: PASS");
// Exit Code 200 to avoid false positive from a graceful exit unrelated to this test run
ExitCode = 200;
#if !UNITY_WEBGL // We don't quit on WebGL because outgoing HTTP requests (in coroutines) would be cancelled.
Application.Quit(ExitCode);
#endif
}
}
public void Expect(string message, bool result)
{
_testNumber++;
Debug.Log($"{_name} TEST | {_testNumber}. {message}: {(result ? "PASS" : "FAIL")}");
if (!result)
{
Debug.Log($"{_name} TEST: FAIL - quitting due to a failed test case #{_testNumber}: '{message}'");
Exit(_testNumber);
}
}
public string GetMessage(int index)
{
while (true)
{
if (_requests.Count > index)
{
break;
}
if (!_requestReceived.WaitOne(_receiveTimeout))
{
Debug.Log($"{_name} TEST: Failed while waiting for an HTTP request #{index} to come in.");
Exit(_testNumber);
}
}
return _requests.ElementAt(index);
}
public bool CheckMessage(int index, string substring, bool negate = false)
{
#if UNITY_WEBGL
// Note: we cannot use the standard checks on WebGL - it would get stuck here because of the lack of multi-threading.
// The verification is done in the python script used for WebGL smoke test - smoke-test-webgl.py
return true;
#else
var message = GetMessage(index);
var contains = message.Contains(substring) || message.Contains(substring.Replace("'", "\""));
return negate ? !contains : contains;
#endif
}
public void ExpectMessage(int index, string substring) =>
Expect($"HTTP Request #{index} contains \"{substring}\".", CheckMessage(index, substring));
public void ExpectMessageNot(int index, string substring) =>
Expect($"HTTP Request #{index} doesn't contain \"{substring}\".", CheckMessage(index, substring, negate: true));
}
}