-
-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathIntegrationTester.cs
More file actions
236 lines (204 loc) · 7.16 KB
/
Copy pathIntegrationTester.cs
File metadata and controls
236 lines (204 loc) · 7.16 KB
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using DependencyConflictPackage;
using Sentry;
using Sentry.Unity;
using UnityEngine;
using UnityEngine.Diagnostics;
#if UNITY_WEBGL
using System.Web;
#endif
public class IntegrationTester : MonoBehaviour
{
private void Awake()
{
Logger.Log("IntegrationTester, awake!");
Application.quitting += () =>
{
Logger.Log("IntegrationTester is quitting.");
};
ExerciseConflictingDependencies();
}
// Invokes the DependencyConflict package, which ships plain, UNALIASED
// System.*/Microsoft.* assemblies at versions that differ from the ones the
// Sentry SDK ships aliased. Calling into it forces those assemblies to be
// linked into the build right next to Sentry's aliased copies - so if the
// assembly aliasing ever regresses, this build fails to compile/link rather
// than the conflict going unnoticed.
//
// The "Dependencies say hi" / "FAILED" markers below are asserted by the
// integration test harness (CommonTestCases.ps1), so a runtime conflict turns
// the build red too instead of being swallowed into a log line.
private void ExerciseConflictingDependencies()
{
try
{
var greeting = DependencyConflictPackageClient.SayHiAsync().GetAwaiter().GetResult();
Logger.Log($"DependencyConflict: {greeting}");
}
catch (Exception ex)
{
Logger.LogError($"DependencyConflict: FAILED - {ex}");
}
}
public void Start()
{
var arg = GetTestArg();
Logger.Log($"IntegrationTester arg: '{arg}'");
switch (arg)
{
case "message-capture":
StartCoroutine(MessageCapture());
break;
case "exception-capture":
StartCoroutine(ExceptionCapture());
break;
case "crash-capture":
StartCoroutine(CrashCapture());
break;
case "crash-send":
CrashSend();
break;
default:
Logger.LogError($"IntegrationTester: Unknown command: {arg}");
#if !UNITY_WEBGL
Application.Quit(1);
#endif
break;
}
}
#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
private void AddIntegrationTestContext(string testType)
{
SentrySdk.AddBreadcrumb("Integration test started");
SentrySdk.ConfigureScope(scope =>
{
scope.SetTag("test.suite", "integration");
scope.SetTag("test.type", testType);
scope.User = new SentryUser
{
Id = "12345",
Username = "TestUser",
Email = "user-mail@test.abc"
};
});
SentrySdk.AddBreadcrumb("Context configuration finished");
}
private IEnumerator MessageCapture()
{
AddIntegrationTestContext("message-capture");
var eventId = SentrySdk.CaptureMessage("Integration test message");
Logger.Log($"EVENT_CAPTURED: {eventId}");
yield return CompleteAndQuit();
}
private IEnumerator ExceptionCapture()
{
AddIntegrationTestContext("exception-capture");
try
{
DoSomeWork();
}
catch (Exception ex)
{
var eventId = SentrySdk.CaptureException(ex);
Logger.Log($"EVENT_CAPTURED: {eventId}");
}
yield return CompleteAndQuit();
}
private IEnumerator CompleteAndQuit()
{
#if UNITY_WEBGL
// On WebGL, envelope sends are coroutine-based and need additional frames to
// complete. Wait to avoid a race where the test harness shuts down the browser
// before the send finishes.
yield return new WaitForSeconds(3);
Logger.Log("INTEGRATION_TEST_COMPLETE");
#else
Logger.Log("INTEGRATION_TEST_COMPLETE");
Application.Quit(0);
yield break;
#endif
}
// Use a deeper call stack with NoInlining to ensure Unity 2022's IL2CPP
// produces a non-empty managed stack trace (single-method throw/catch can
// result in an empty stack trace with OptimizeSize + High stripping).
[MethodImpl(MethodImplOptions.NoInlining)]
private static void DoSomeWork()
{
if (DateTime.Now.Ticks > 0) // Always true but not optimizable
{
ThrowException();
}
}
[MethodImpl(MethodImplOptions.NoInlining)]
private static void ThrowException()
{
throw new InvalidOperationException("Integration test exception");
}
private IEnumerator CrashCapture()
{
var crashId = Guid.NewGuid().ToString();
AddIntegrationTestContext("crash-capture");
SentrySdk.ConfigureScope(scope =>
{
scope.SetTag("test.crash_id", crashId);
});
// Wait for the scope sync to complete on platforms that use a background thread (e.g. Android JNI)
yield return new WaitForSeconds(0.5f);
Logger.Log($"EVENT_CAPTURED: {crashId}");
Logger.Log("CRASH TEST: Issuing a native crash (AccessViolation)");
Utils.ForceCrash(ForcedCrashCategory.AccessViolation);
// Should not reach here
Logger.LogError("CRASH TEST: FAIL - unexpected code executed after crash");
Application.Quit(1);
}
private void CrashSend()
{
Logger.Log("CrashSend: Initializing Sentry to flush cached crash report...");
var lastRunState = SentrySdk.GetLastRunState();
Logger.Log($"CrashSend: crashedLastRun={lastRunState}");
// Sentry is already initialized by IntegrationOptionsConfiguration.
// Just wait a bit for the queued crash report to be sent, then quit.
StartCoroutine(WaitAndQuit());
}
private IEnumerator WaitAndQuit()
{
// Wait for the crash report to be sent
yield return new WaitForSeconds(10);
SentrySdk.FlushAsync(TimeSpan.FromSeconds(5)).GetAwaiter().GetResult();
Logger.Log("CrashSend: Flush complete, quitting.");
Application.Quit(0);
}
}