Skip to content

Commit f38c140

Browse files
committed
fixed webbackgroundworker
1 parent be0c49a commit f38c140

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

src/Sentry.Unity/UnityWebRequestTransport.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ internal class WebBackgroundWorker : IBackgroundWorker
1515
{
1616
private readonly SentryMonoBehaviour _behaviour;
1717
private readonly UnityWebRequestTransport _transport;
18+
private int _pendingItems;
1819

1920
public WebBackgroundWorker(SentryUnityOptions options, SentryMonoBehaviour behaviour)
2021
{
@@ -24,13 +25,20 @@ public WebBackgroundWorker(SentryUnityOptions options, SentryMonoBehaviour behav
2425

2526
public bool EnqueueEnvelope(Envelope envelope)
2627
{
27-
_behaviour.QueueCoroutine(_transport.SendEnvelopeAsync(envelope));
28+
_pendingItems++;
29+
_behaviour.QueueCoroutine(SendAndTrack(envelope));
2830
return true;
2931
}
3032

33+
private IEnumerator SendAndTrack(Envelope envelope)
34+
{
35+
yield return _transport.SendEnvelopeAsync(envelope);
36+
_pendingItems--;
37+
}
38+
3139
public Task FlushAsync(TimeSpan timeout) => Task.CompletedTask;
3240

33-
public int QueuedItems { get; }
41+
public int QueuedItems => _pendingItems;
3442
}
3543

3644
internal class UnityWebRequestTransport : HttpTransportBase

test/Scripts.Integration.Test/Scripts/IntegrationTester.cs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ public void Start()
1717
switch (arg)
1818
{
1919
case "message-capture":
20-
MessageCapture();
20+
StartCoroutine(MessageCapture());
2121
break;
2222
case "exception-capture":
23-
ExceptionCapture();
23+
StartCoroutine(ExceptionCapture());
2424
break;
2525
case "crash-capture":
2626
StartCoroutine(CrashCapture());
@@ -56,21 +56,17 @@ private void AddIntegrationTestContext(string testType)
5656
SentrySdk.AddBreadcrumb("Context configuration finished");
5757
}
5858

59-
private void MessageCapture()
59+
private IEnumerator MessageCapture()
6060
{
6161
AddIntegrationTestContext("message-capture");
6262

6363
var eventId = SentrySdk.CaptureMessage("Integration test message");
6464
Debug.Log($"EVENT_CAPTURED: {eventId}");
6565

66-
SentrySdk.FlushAsync(TimeSpan.FromSeconds(5)).GetAwaiter().GetResult();
67-
Debug.Log("INTEGRATION_TEST_COMPLETE");
68-
#if !UNITY_WEBGL
69-
Application.Quit(0);
70-
#endif
66+
yield return CompleteAndQuit();
7167
}
7268

73-
private void ExceptionCapture()
69+
private IEnumerator ExceptionCapture()
7470
{
7571
AddIntegrationTestContext("exception-capture");
7672

@@ -84,10 +80,21 @@ private void ExceptionCapture()
8480
Debug.Log($"EVENT_CAPTURED: {eventId}");
8581
}
8682

87-
SentrySdk.FlushAsync(TimeSpan.FromSeconds(5)).GetAwaiter().GetResult();
83+
yield return CompleteAndQuit();
84+
}
85+
86+
private IEnumerator CompleteAndQuit()
87+
{
88+
#if UNITY_WEBGL
89+
// On WebGL, envelope sends are coroutine-based and need additional frames to
90+
// complete. Wait to avoid a race where the test harness shuts down the browser
91+
// before the send finishes.
92+
yield return new WaitForSeconds(3);
93+
Debug.Log("INTEGRATION_TEST_COMPLETE");
94+
#else
8895
Debug.Log("INTEGRATION_TEST_COMPLETE");
89-
#if !UNITY_WEBGL
9096
Application.Quit(0);
97+
yield break;
9198
#endif
9299
}
93100

0 commit comments

Comments
 (0)