|
| 1 | +using Backtrace.Unity.Model; |
| 2 | +using Backtrace.Unity.Types; |
| 3 | +using NUnit.Framework; |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Linq; |
| 7 | +using System.Threading; |
| 8 | + |
| 9 | +namespace Backtrace.Unity.Tests.Runtime |
| 10 | +{ |
| 11 | + public class BackgroundThreadSupport : BacktraceBaseTest |
| 12 | + { |
| 13 | + [SetUp] |
| 14 | + public void Setup() |
| 15 | + { |
| 16 | + BeforeSetup(); |
| 17 | + BacktraceClient.Configuration = GetBasicConfiguration(); |
| 18 | + AfterSetup(); |
| 19 | + } |
| 20 | + |
| 21 | + [Test] |
| 22 | + public void TestBackgroundThreadSupport_BackgroundExceptionShouldntThrow_ExceptionIsSavedInMainThreadLoop() |
| 23 | + { |
| 24 | + var client = BacktraceClient; |
| 25 | + string exceptionMessage = "foo"; |
| 26 | + var mainThreadId = Thread.CurrentThread.ManagedThreadId; |
| 27 | + var thread = new Thread(() => |
| 28 | + { |
| 29 | + Assert.IsTrue(Thread.CurrentThread.ManagedThreadId != mainThreadId); |
| 30 | + var exception = new InvalidOperationException(exceptionMessage); |
| 31 | + client.Send(exception); |
| 32 | + }); |
| 33 | + thread.Start(); |
| 34 | + thread.Join(); |
| 35 | + |
| 36 | + Assert.IsNotEmpty(BacktraceClient.BackgroundExceptions); |
| 37 | + Assert.AreEqual(exceptionMessage, BacktraceClient.BackgroundExceptions.First().Message); |
| 38 | + Assert.IsTrue(BacktraceClient.BackgroundExceptions.First().ExceptionTypeReport); |
| 39 | + } |
| 40 | + |
| 41 | + |
| 42 | + [Test] |
| 43 | + public void TestBackgroundThreadSupport_BackgroundReportShouldntThrow_ExceptionIsSavedInMainThreadLoop() |
| 44 | + { |
| 45 | + var client = BacktraceClient; |
| 46 | + string exceptionMessage = "foo"; |
| 47 | + var mainThreadId = Thread.CurrentThread.ManagedThreadId; |
| 48 | + var thread = new Thread(() => |
| 49 | + { |
| 50 | + Assert.IsTrue(Thread.CurrentThread.ManagedThreadId != mainThreadId); |
| 51 | + var exception = new InvalidOperationException(exceptionMessage); |
| 52 | + client.Send(new BacktraceReport(exception)); |
| 53 | + }); |
| 54 | + thread.Start(); |
| 55 | + thread.Join(); |
| 56 | + |
| 57 | + Assert.IsNotEmpty(BacktraceClient.BackgroundExceptions); |
| 58 | + Assert.AreEqual(exceptionMessage, BacktraceClient.BackgroundExceptions.First().Message); |
| 59 | + Assert.IsTrue(BacktraceClient.BackgroundExceptions.First().ExceptionTypeReport); |
| 60 | + } |
| 61 | + |
| 62 | + [Test] |
| 63 | + public void TestBackgroundThreadSupport_BackgroundReportWithAttributesShouldntThrow_ExceptionIsSavedInMainThreadLoop() |
| 64 | + { |
| 65 | + var client = BacktraceClient; |
| 66 | + string exceptionMessage = "foo"; |
| 67 | + var attributeKey = "attribute-key"; |
| 68 | + var attributeValue = exceptionMessage; |
| 69 | + var mainThreadId = Thread.CurrentThread.ManagedThreadId; |
| 70 | + var thread = new Thread(() => |
| 71 | + { |
| 72 | + Assert.IsTrue(Thread.CurrentThread.ManagedThreadId != mainThreadId); |
| 73 | + var exception = new InvalidOperationException(exceptionMessage); |
| 74 | + client.Send(new BacktraceReport(exception, new Dictionary<string, string> { { attributeKey, attributeValue } })); |
| 75 | + }); |
| 76 | + thread.Start(); |
| 77 | + thread.Join(); |
| 78 | + |
| 79 | + Assert.IsNotEmpty(BacktraceClient.BackgroundExceptions); |
| 80 | + var storedReport = BacktraceClient.BackgroundExceptions.First(); |
| 81 | + Assert.AreEqual(exceptionMessage, storedReport.Message); |
| 82 | + Assert.IsTrue(storedReport.ExceptionTypeReport); |
| 83 | + Assert.IsNotNull(storedReport.Attributes[attributeKey]); |
| 84 | + Assert.AreEqual(storedReport.Attributes[attributeKey], attributeValue); |
| 85 | + } |
| 86 | + |
| 87 | + |
| 88 | + [Test] |
| 89 | + public void TestBackgroundThreadSupport_BackgroundMessageShouldntThrow_ExceptionIsSavedInMainThreadLoop() |
| 90 | + { |
| 91 | + var client = BacktraceClient; |
| 92 | + string exceptionMessage = "foo"; |
| 93 | + var mainThreadId = Thread.CurrentThread.ManagedThreadId; |
| 94 | + var thread = new Thread(() => |
| 95 | + { |
| 96 | + Assert.IsTrue(Thread.CurrentThread.ManagedThreadId != mainThreadId); |
| 97 | + client.Send(exceptionMessage); |
| 98 | + }); |
| 99 | + thread.Start(); |
| 100 | + thread.Join(); |
| 101 | + |
| 102 | + Assert.IsNotEmpty(BacktraceClient.BackgroundExceptions); |
| 103 | + Assert.AreEqual(exceptionMessage, BacktraceClient.BackgroundExceptions.First().Message); |
| 104 | + Assert.IsFalse(BacktraceClient.BackgroundExceptions.First().ExceptionTypeReport); |
| 105 | + } |
| 106 | + |
| 107 | + [Test] |
| 108 | + public void TestBackgroundThreadSupport_BackgroundUnhandledExceptionShouldntThrow_ExceptionIsSavedInMainThreadLoop() |
| 109 | + { |
| 110 | + var client = BacktraceClient; |
| 111 | + string exceptionMessage = "foo"; |
| 112 | + var mainThreadId = Thread.CurrentThread.ManagedThreadId; |
| 113 | + |
| 114 | + var thread = new Thread(() => |
| 115 | + { |
| 116 | + Assert.IsTrue(Thread.CurrentThread.ManagedThreadId != mainThreadId); |
| 117 | + client.HandleUnityBackgroundException(exceptionMessage, string.Empty, UnityEngine.LogType.Exception); |
| 118 | + }); |
| 119 | + thread.Start(); |
| 120 | + thread.Join(); |
| 121 | + |
| 122 | + Assert.IsNotEmpty(BacktraceClient.BackgroundExceptions); |
| 123 | + Assert.AreEqual(exceptionMessage, BacktraceClient.BackgroundExceptions.First().Message); |
| 124 | + Assert.IsTrue(BacktraceClient.BackgroundExceptions.First().ExceptionTypeReport); |
| 125 | + } |
| 126 | + |
| 127 | + [Test] |
| 128 | + public void TestBackgroundThreadSupport_UserShouldBeAbleToFilterUnhandledExceptions_ReportShouldntBeAvailableInMainThreadLoop() |
| 129 | + { |
| 130 | + var client = BacktraceClient; |
| 131 | + string exceptionMessage = "foo"; |
| 132 | + |
| 133 | + BacktraceClient.SkipReport = (ReportFilterType type, Exception e, string message) => |
| 134 | + { |
| 135 | + return true; |
| 136 | + }; |
| 137 | + var mainThreadId = Thread.CurrentThread.ManagedThreadId; |
| 138 | + var thread = new Thread(() => |
| 139 | + { |
| 140 | + Assert.IsTrue(Thread.CurrentThread.ManagedThreadId != mainThreadId); |
| 141 | + client.HandleUnityBackgroundException(exceptionMessage, string.Empty, UnityEngine.LogType.Exception); |
| 142 | + }); |
| 143 | + thread.Start(); |
| 144 | + thread.Join(); |
| 145 | + |
| 146 | + Assert.IsEmpty(BacktraceClient.BackgroundExceptions); |
| 147 | + } |
| 148 | + |
| 149 | + [Test] |
| 150 | + public void TestBackgroundThreadSupport_UserShouldBeAbleToFilterReports_ReportShouldntBeAvailableInMainThreadLoop() |
| 151 | + { |
| 152 | + var client = BacktraceClient; |
| 153 | + string exceptionMessage = "foo"; |
| 154 | + |
| 155 | + BacktraceClient.SkipReport = (ReportFilterType type, Exception e, string message) => |
| 156 | + { |
| 157 | + return true; |
| 158 | + }; |
| 159 | + var thread = new Thread(() => |
| 160 | + { |
| 161 | + client.Send(exceptionMessage); |
| 162 | + var exception = new InvalidOperationException(exceptionMessage); |
| 163 | + client.Send(exception); |
| 164 | + client.Send(new BacktraceReport(exception)); |
| 165 | + }); |
| 166 | + |
| 167 | + thread.Start(); |
| 168 | + thread.Join(); |
| 169 | + |
| 170 | + Assert.IsEmpty(BacktraceClient.BackgroundExceptions); |
| 171 | + } |
| 172 | + |
| 173 | + |
| 174 | + [Test] |
| 175 | + public void TestBackgroundThreadSupport_RateLimitSkipReports_ReportShouldntBeAvailableInMainThreadLoop() |
| 176 | + { |
| 177 | + var client = BacktraceClient; |
| 178 | + string exceptionMessage = "foo"; |
| 179 | + |
| 180 | + uint rateLimit = 5; |
| 181 | + var expectedNumberOfSkippedReports = 5; |
| 182 | + int actualNumberOfSkippedReports = 0; |
| 183 | + |
| 184 | + client.SetClientReportLimit(rateLimit); |
| 185 | + client.OnClientReportLimitReached = (BacktraceReport report) => |
| 186 | + { |
| 187 | + actualNumberOfSkippedReports++; |
| 188 | + }; |
| 189 | + |
| 190 | + var thread = new Thread(() => |
| 191 | + { |
| 192 | + for (int i = 0; i < rateLimit + expectedNumberOfSkippedReports; i++) |
| 193 | + { |
| 194 | + client.Send(new InvalidOperationException(exceptionMessage)); |
| 195 | + } |
| 196 | + |
| 197 | + }); |
| 198 | + thread.Start(); |
| 199 | + thread.Join(); |
| 200 | + Assert.IsNotEmpty(BacktraceClient.BackgroundExceptions); |
| 201 | + Assert.AreEqual(rateLimit, BacktraceClient.BackgroundExceptions.Count); |
| 202 | + Assert.AreEqual(expectedNumberOfSkippedReports, actualNumberOfSkippedReports); |
| 203 | + } |
| 204 | + } |
| 205 | +} |
0 commit comments