@bugsee/react-native 0.0.0-stage → 7.0.0-beta2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (250) hide show
  1. package/BugseeReactNative.podspec +95 -0
  2. package/LICENSE +21 -0
  3. package/README.md +557 -2
  4. package/android/build.gradle +162 -0
  5. package/android/consumer-rules.pro +2 -0
  6. package/android/src/main/AndroidManifest.xml +33 -0
  7. package/android/src/main/java/com/bugsee/reactnative/AppearanceBridge.java +82 -0
  8. package/android/src/main/java/com/bugsee/reactnative/AttributeBridge.java +209 -0
  9. package/android/src/main/java/com/bugsee/reactnative/BreadcrumbFilterBridge.java +443 -0
  10. package/android/src/main/java/com/bugsee/reactnative/BridgeJson.java +168 -0
  11. package/android/src/main/java/com/bugsee/reactnative/BugseeModule.java +1869 -0
  12. package/android/src/main/java/com/bugsee/reactnative/BugseeOptionEnums.java +136 -0
  13. package/android/src/main/java/com/bugsee/reactnative/BugseePackage.java +42 -0
  14. package/android/src/main/java/com/bugsee/reactnative/BugseeReactNativeWrapper.java +200 -0
  15. package/android/src/main/java/com/bugsee/reactnative/BugseeStatusMapper.java +66 -0
  16. package/android/src/main/java/com/bugsee/reactnative/BugseeTokens.java +22 -0
  17. package/android/src/main/java/com/bugsee/reactnative/ConsoleEchoDedup.java +202 -0
  18. package/android/src/main/java/com/bugsee/reactnative/CreatedReports.java +127 -0
  19. package/android/src/main/java/com/bugsee/reactnative/DataRequestBridge.java +375 -0
  20. package/android/src/main/java/com/bugsee/reactnative/ExceptionBridge.java +88 -0
  21. package/android/src/main/java/com/bugsee/reactnative/ExceptionExecutors.java +33 -0
  22. package/android/src/main/java/com/bugsee/reactnative/LogFilterBridge.java +375 -0
  23. package/android/src/main/java/com/bugsee/reactnative/NetworkEvents.java +227 -0
  24. package/android/src/main/java/com/bugsee/reactnative/NetworkFilterBridge.java +498 -0
  25. package/android/src/main/java/com/bugsee/reactnative/PluginEndpoint.java +48 -0
  26. package/android/src/main/java/com/bugsee/reactnative/ReactNativeWebException.java +19 -0
  27. package/android/src/main/java/com/bugsee/reactnative/ReactNativeWrapperInitProvider.java +61 -0
  28. package/android/src/main/java/com/bugsee/reactnative/ReactRootOriginTracker.java +867 -0
  29. package/android/src/main/java/com/bugsee/reactnative/ReportArgs.java +58 -0
  30. package/android/src/main/java/com/bugsee/reactnative/ReportHandlerBridge.java +336 -0
  31. package/android/src/main/java/com/bugsee/reactnative/ReportHandlerDeadlines.java +57 -0
  32. package/android/src/main/java/com/bugsee/reactnative/ReportOps.java +287 -0
  33. package/android/src/main/java/com/bugsee/reactnative/SecureRectanglePulls.java +113 -0
  34. package/android/src/main/java/com/bugsee/reactnative/SecureRectangleStore.java +692 -0
  35. package/android/src/main/java/com/bugsee/reactnative/SpanHandles.java +143 -0
  36. package/android/src/main/java/com/bugsee/reactnative/WrapperChannelHolder.java +137 -0
  37. package/android/src/main/java/com/bugsee/reactnative/WrapperEventBus.java +110 -0
  38. package/android/src/main/java/com/bugsee/reactnative/WrapperRegistrar.java +54 -0
  39. package/app.plugin.js +4 -0
  40. package/ios/BGSRNConsoleCapture.h +52 -0
  41. package/ios/BGSRNConsoleCapture.mm +344 -0
  42. package/ios/BugseeModule.h +25 -0
  43. package/ios/BugseeModule.mm +2728 -0
  44. package/ios/Package.swift +71 -0
  45. package/ios/Support/Package.resolved +14 -0
  46. package/ios/Support/Package.swift +44 -0
  47. package/ios/Support/Sources/BugseeRNSupport/BGSRNAttributes.m +52 -0
  48. package/ios/Support/Sources/BugseeRNSupport/BGSRNCreatedReportOps.m +236 -0
  49. package/ios/Support/Sources/BugseeRNSupport/BGSRNCreatedReports.m +184 -0
  50. package/ios/Support/Sources/BugseeRNSupport/BGSRNDataRequestBridge.m +431 -0
  51. package/ios/Support/Sources/BugseeRNSupport/BGSRNErrorMessage.m +40 -0
  52. package/ios/Support/Sources/BugseeRNSupport/BGSRNEventBus.m +70 -0
  53. package/ios/Support/Sources/BugseeRNSupport/BGSRNExceptions.m +63 -0
  54. package/ios/Support/Sources/BugseeRNSupport/BGSRNGuardedEmit.mm +32 -0
  55. package/ios/Support/Sources/BugseeRNSupport/BGSRNJSON.m +40 -0
  56. package/ios/Support/Sources/BugseeRNSupport/BGSRNLaunchHold.m +147 -0
  57. package/ios/Support/Sources/BugseeRNSupport/BGSRNMainThread.m +23 -0
  58. package/ios/Support/Sources/BugseeRNSupport/BGSRNNetworkEvents.m +159 -0
  59. package/ios/Support/Sources/BugseeRNSupport/BGSRNNetworkFilter.m +155 -0
  60. package/ios/Support/Sources/BugseeRNSupport/BGSRNPluginEndpoint.m +21 -0
  61. package/ios/Support/Sources/BugseeRNSupport/BGSRNReactRootOriginTracker.m +63 -0
  62. package/ios/Support/Sources/BugseeRNSupport/BGSRNReactWindow.m +251 -0
  63. package/ios/Support/Sources/BugseeRNSupport/BGSRNReportArgs.m +47 -0
  64. package/ios/Support/Sources/BugseeRNSupport/BGSRNReportDeadlines.m +8 -0
  65. package/ios/Support/Sources/BugseeRNSupport/BGSRNReportHandlerBridge.m +308 -0
  66. package/ios/Support/Sources/BugseeRNSupport/BGSRNReportOps.m +345 -0
  67. package/ios/Support/Sources/BugseeRNSupport/BGSRNSecureRectangles.m +508 -0
  68. package/ios/Support/Sources/BugseeRNSupport/BGSRNSettleOnce.m +62 -0
  69. package/ios/Support/Sources/BugseeRNSupport/BGSRNSpanHandles.m +136 -0
  70. package/ios/Support/Sources/BugseeRNSupport/BGSRNStatusMapper.m +18 -0
  71. package/ios/Support/Sources/BugseeRNSupport/BGSRNTokens.m +9 -0
  72. package/ios/Support/Sources/BugseeRNSupport/BGSRNValues.m +5 -0
  73. package/ios/Support/Sources/BugseeRNSupport/BGSRNWrapper.m +54 -0
  74. package/ios/Support/Sources/BugseeRNSupport/BGSRNWrapperChannelHolder.m +59 -0
  75. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNAttributes.h +63 -0
  76. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNCreatedReportOps.h +51 -0
  77. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNCreatedReports.h +81 -0
  78. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNDataRequestBridge.h +118 -0
  79. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNErrorMessage.h +30 -0
  80. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNEventBus.h +45 -0
  81. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNExceptions.h +31 -0
  82. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNGuardedEmit.h +31 -0
  83. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNJSON.h +25 -0
  84. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNLaunchHold.h +86 -0
  85. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNMainThread.h +32 -0
  86. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNNetworkEvents.h +28 -0
  87. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNNetworkFilter.h +28 -0
  88. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNPluginEndpoint.h +17 -0
  89. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNReactRootOriginTracker.h +48 -0
  90. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNReactWindow.h +135 -0
  91. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNReportArgs.h +17 -0
  92. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNReportDeadlines.h +37 -0
  93. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNReportHandlerBridge.h +94 -0
  94. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNReportOps.h +86 -0
  95. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNSecureRectangles.h +198 -0
  96. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNSettleOnce.h +25 -0
  97. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNSpanHandles.h +49 -0
  98. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNStatusMapper.h +26 -0
  99. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNTokens.h +14 -0
  100. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNValues.h +18 -0
  101. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNWrapper.h +40 -0
  102. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNWrapperChannelHolder.h +74 -0
  103. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNAttributesTests.m +194 -0
  104. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNCreatedReportOpsTests.m +274 -0
  105. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNCreatedReportsTests.m +240 -0
  106. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNDataRequestBridgeTests.m +685 -0
  107. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNErrorMessageTests.m +102 -0
  108. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNEventBusTests.m +136 -0
  109. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNExceptionsTests.m +68 -0
  110. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNFakeReport.h +41 -0
  111. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNFakeReport.m +166 -0
  112. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNGuardedEmitTests.mm +85 -0
  113. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNJSONTests.m +281 -0
  114. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNLaunchHoldTests.m +230 -0
  115. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNMainThreadTests.m +97 -0
  116. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNNetworkEventsTests.m +99 -0
  117. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNNetworkFilterTests.m +134 -0
  118. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNPluginEndpointTests.m +38 -0
  119. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNReactRootOriginTrackerTests.m +210 -0
  120. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNReactWindowTests.m +548 -0
  121. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNReportArgsTests.m +51 -0
  122. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNReportDeadlinesTests.m +22 -0
  123. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNReportHandlerBridgeTests.m +382 -0
  124. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNReportOpsTests.m +295 -0
  125. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNSecureRectanglesTests.m +568 -0
  126. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNSettleOnceTests.m +134 -0
  127. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNSpanHandlesTests.m +153 -0
  128. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNStatusMapperTests.m +81 -0
  129. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNValuesTests.m +32 -0
  130. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNWrapperChannelHolderTests.m +205 -0
  131. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNWrapperTests.m +66 -0
  132. package/ios/react-native-spm-prefix.h +10 -0
  133. package/native-versions.json +10 -0
  134. package/package.json +62 -3
  135. package/plugin/build/bundle-phase.d.ts +41 -0
  136. package/plugin/build/bundle-phase.js +116 -0
  137. package/plugin/build/bundle-phase.js.map +1 -0
  138. package/plugin/build/dsym-script.d.ts +32 -0
  139. package/plugin/build/dsym-script.js +159 -0
  140. package/plugin/build/dsym-script.js.map +1 -0
  141. package/plugin/build/endpoint.d.ts +30 -0
  142. package/plugin/build/endpoint.js +69 -0
  143. package/plugin/build/endpoint.js.map +1 -0
  144. package/plugin/build/gradle.d.ts +65 -0
  145. package/plugin/build/gradle.js +1066 -0
  146. package/plugin/build/gradle.js.map +1 -0
  147. package/plugin/build/index.d.ts +85 -0
  148. package/plugin/build/index.js +217 -0
  149. package/plugin/build/index.js.map +1 -0
  150. package/plugin/build/manifest.d.ts +48 -0
  151. package/plugin/build/manifest.js +69 -0
  152. package/plugin/build/manifest.js.map +1 -0
  153. package/plugin/build/native-versions.baked.json +4 -0
  154. package/plugin/build/native-versions.d.ts +10 -0
  155. package/plugin/build/native-versions.js +27 -0
  156. package/plugin/build/native-versions.js.map +1 -0
  157. package/plugin/build/pbx-string.d.ts +8 -0
  158. package/plugin/build/pbx-string.js +31 -0
  159. package/plugin/build/pbx-string.js.map +1 -0
  160. package/plugin/build/podfile.d.ts +3 -0
  161. package/plugin/build/podfile.js +28 -0
  162. package/plugin/build/podfile.js.map +1 -0
  163. package/plugin/build/properties.d.ts +19 -0
  164. package/plugin/build/properties.js +41 -0
  165. package/plugin/build/properties.js.map +1 -0
  166. package/plugin/build/scheme.d.ts +20 -0
  167. package/plugin/build/scheme.js +107 -0
  168. package/plugin/build/scheme.js.map +1 -0
  169. package/react-native.config.js +26 -0
  170. package/scripts/bugsee-sourcemaps.gradle +201 -0
  171. package/scripts/bugsee-xcode.sh +83 -0
  172. package/scripts/compose-then-inject.js +113 -0
  173. package/scripts/hermes-sourcemaps.js +634 -0
  174. package/scripts/hermesc-preserve-js.cmd +6 -0
  175. package/scripts/hermesc-preserve-js.js +200 -0
  176. package/scripts/hermesc-preserve-js.sh +7 -0
  177. package/src/NativeBugsee.ts +622 -0
  178. package/src/apm/notify.ts +58 -0
  179. package/src/apm/span.ts +403 -0
  180. package/src/apm/types.ts +57 -0
  181. package/src/appearance/report.ts +171 -0
  182. package/src/architecture.ts +48 -0
  183. package/src/attributes/errors.ts +39 -0
  184. package/src/attributes/validate.ts +150 -0
  185. package/src/breadcrumbs/add.ts +58 -0
  186. package/src/breadcrumbs/data.ts +33 -0
  187. package/src/breadcrumbs/filter.ts +215 -0
  188. package/src/breadcrumbs/types.ts +64 -0
  189. package/src/bridge/json.ts +110 -0
  190. package/src/console/capture.ts +180 -0
  191. package/src/console/dedup.ts +175 -0
  192. package/src/data/validate.ts +259 -0
  193. package/src/errorName.ts +32 -0
  194. package/src/exceptions/ErrorBoundary.tsx +92 -0
  195. package/src/exceptions/RootErrorReporter.tsx +61 -0
  196. package/src/exceptions/debugIds.ts +118 -0
  197. package/src/exceptions/handlers.ts +236 -0
  198. package/src/exceptions/options.ts +85 -0
  199. package/src/exceptions/payload.ts +456 -0
  200. package/src/exceptions/report.ts +170 -0
  201. package/src/exceptions/sha1.ts +148 -0
  202. package/src/exceptions/stack.ts +355 -0
  203. package/src/index.ts +962 -0
  204. package/src/logs/filter.ts +90 -0
  205. package/src/logs/log.ts +36 -0
  206. package/src/network/add.ts +165 -0
  207. package/src/network/filter.ts +198 -0
  208. package/src/options/AndroidLaunchOptions.ts +78 -0
  209. package/src/options/BugseeLaunchOptions.ts +190 -0
  210. package/src/options/IOSLaunchOptions.ts +57 -0
  211. package/src/options/android-options-manifest.json +646 -0
  212. package/src/options/createDefaultLaunchOptions.ts +29 -0
  213. package/src/options/endpoint.ts +58 -0
  214. package/src/options/enums.ts +104 -0
  215. package/src/options/ios-enums.json +19 -0
  216. package/src/options/keys.ts +39 -0
  217. package/src/options/option-enums.json +34 -0
  218. package/src/options/option-keys.json +107 -0
  219. package/src/report/BugseeReport.ts +264 -0
  220. package/src/report/CreatedReport.ts +127 -0
  221. package/src/report/dispatcher.ts +95 -0
  222. package/src/report/errors.ts +48 -0
  223. package/src/report/fields.ts +40 -0
  224. package/src/report/types.ts +157 -0
  225. package/src/report/validate.ts +197 -0
  226. package/src/secure/BugseeSecure.tsx +202 -0
  227. package/src/secure/ancestry.ts +622 -0
  228. package/src/secure/drivers.ts +342 -0
  229. package/src/secure/followEvents.ts +57 -0
  230. package/src/secure/hold.ts +99 -0
  231. package/src/secure/measureLoop.ts +323 -0
  232. package/src/secure/nativeAnimation.ts +1005 -0
  233. package/src/secure/rectangles.ts +93 -0
  234. package/src/secure/registry.ts +237 -0
  235. package/src/secure/surface.ts +83 -0
  236. package/src/secure/tracker.ts +723 -0
  237. package/src/secure/transition.ts +488 -0
  238. package/src/secure/unit.ts +22 -0
  239. package/src/status.ts +16 -0
  240. package/src/version.ts +9 -0
  241. package/src/viewtree/anchor.tsx +105 -0
  242. package/src/viewtree/constants.ts +20 -0
  243. package/src/viewtree/fiber.ts +251 -0
  244. package/src/viewtree/requests.ts +347 -0
  245. package/src/viewtree/walk.ts +902 -0
  246. package/src/wrapper/channel.ts +33 -0
  247. package/src/wrapper/collect.ts +52 -0
  248. package/src/wrapper/events.ts +45 -0
  249. package/src/wrapper/identity.ts +54 -0
  250. package/src/wrapper/lifecycle-events.ts +89 -0
@@ -0,0 +1,685 @@
1
+ @import XCTest;
2
+ @import UIKit;
3
+ @import BugseeRNSupport;
4
+
5
+ /// A deadline the test fires by hand; records cancellation.
6
+ @interface BGSRNDataRequestManualTask : NSObject
7
+ @property (nonatomic, copy) dispatch_block_t block;
8
+ @property (nonatomic) int64_t delayMs;
9
+ @property (atomic) BOOL cancelled;
10
+ @end
11
+
12
+ @implementation BGSRNDataRequestManualTask
13
+ @end
14
+
15
+ /// A clock the test moves by hand.
16
+ @interface BGSRNDataRequestManualClock : NSObject
17
+ @property (atomic) int64_t now;
18
+ @end
19
+
20
+ @implementation BGSRNDataRequestManualClock
21
+ @end
22
+
23
+ /// The one property everything here protects: the SDK's callback runs exactly
24
+ /// once for every request, whichever of JS, the deadline or a detach gets
25
+ /// there first. Running it twice is ignored by the SDK, but never running it
26
+ /// costs the SDK its full timeout and, three times in a row, puts the wrapper
27
+ /// into the SDK's 50 ms "silent" mode.
28
+ ///
29
+ /// Mirrors Android's `DataRequestBridgeTest` by name. The log lines are the
30
+ /// plan's Phase 6 "Log lines", verbatim, behind iOS's `BugseeRN ` prefix.
31
+ @interface BGSRNDataRequestBridgeTests : XCTestCase
32
+ @end
33
+
34
+ @implementation BGSRNDataRequestBridgeTests {
35
+ NSMutableArray<BGSRNDataRequestManualTask *> *_tasks;
36
+ NSMutableArray<NSString *> *_lines;
37
+ NSMutableArray<NSDictionary *> *_requests;
38
+ NSMutableArray *_replies;
39
+ BGSRNDataRequestManualClock *_clock;
40
+ BGSRNDataRequestBridge *_bridge;
41
+ NSObject *_sink;
42
+ NSValue *_origin;
43
+ }
44
+
45
+ - (void)setUp {
46
+ [super setUp];
47
+ NSMutableArray<BGSRNDataRequestManualTask *> *tasks = [NSMutableArray array];
48
+ NSMutableArray<NSString *> *lines = [NSMutableArray array];
49
+ BGSRNDataRequestManualClock *clock = [BGSRNDataRequestManualClock new];
50
+ _tasks = tasks;
51
+ _lines = lines;
52
+ _clock = clock;
53
+ _requests = [NSMutableArray array];
54
+ _replies = [NSMutableArray array];
55
+ _bridge = [[BGSRNDataRequestBridge alloc]
56
+ initWithScheduler:^id(dispatch_block_t task, int64_t delayMs) {
57
+ BGSRNDataRequestManualTask *t = [BGSRNDataRequestManualTask new];
58
+ t.block = task;
59
+ t.delayMs = delayMs;
60
+ @synchronized(tasks) {
61
+ [tasks addObject:t];
62
+ }
63
+ return t;
64
+ }
65
+ cancel:^(id token) {
66
+ ((BGSRNDataRequestManualTask *)token).cancelled = YES;
67
+ }
68
+ clock:^int64_t {
69
+ return clock.now;
70
+ }
71
+ log:^(NSString *line) {
72
+ @synchronized(lines) {
73
+ [lines addObject:line];
74
+ }
75
+ }];
76
+ _sink = [NSObject new];
77
+ _origin = [NSValue valueWithCGPoint:CGPointMake(0, 0)];
78
+ }
79
+
80
+ #pragma mark - Helpers
81
+
82
+ - (void)attach:(id)sink into:(NSMutableArray<NSDictionary *> *)requests {
83
+ __weak __typeof(self) weakSelf = self;
84
+ [_bridge attach:sink
85
+ block:^BOOL(NSDictionary *request) {
86
+ [requests addObject:request];
87
+ return YES;
88
+ }
89
+ origin:^NSValue * {
90
+ __strong __typeof(weakSelf) strongSelf = weakSelf;
91
+ return strongSelf->_origin;
92
+ }];
93
+ }
94
+
95
+ - (void)attachAndEnable {
96
+ [self attach:_sink into:_requests];
97
+ _bridge.viewTreeEnabled = YES;
98
+ }
99
+
100
+ - (void)request:(NSString *)type {
101
+ NSMutableArray *replies = _replies;
102
+ [_bridge requestType:type
103
+ reply:^(NSString *data) {
104
+ @synchronized(replies) {
105
+ [replies addObject:data ?: NSNull.null];
106
+ }
107
+ }];
108
+ }
109
+
110
+ - (void)requestVh {
111
+ [self request:BGSRNDataRequestTypeViewHierarchy];
112
+ }
113
+
114
+ - (NSString *)lastId {
115
+ return _requests.lastObject[@"requestId"];
116
+ }
117
+
118
+ - (void)fireDue {
119
+ for (BGSRNDataRequestManualTask *t in [_tasks copy]) {
120
+ if (!t.cancelled) {
121
+ t.block();
122
+ }
123
+ }
124
+ }
125
+
126
+ /// The real race: the deadline has already started when `cancel` arrives, so
127
+ /// cancelling does not stop it.
128
+ - (void)fireEvenIfCancelled {
129
+ for (BGSRNDataRequestManualTask *t in [_tasks copy]) {
130
+ t.block();
131
+ }
132
+ }
133
+
134
+ #pragma mark - Constants
135
+
136
+ - (void)testTheTypeMatchesTheSdkConstant {
137
+ // BGSManagedHierarchyDataType, BGSCaptureDataProviderViewHierarchy.m:24
138
+ // (SDK 0d9c9d0a3); `static` there, so pinned by value.
139
+ XCTAssertEqualObjects(BGSRNDataRequestTypeViewHierarchy, @"vh");
140
+ }
141
+
142
+ - (void)testDeadlineIsBelowTheSdkBudget {
143
+ // BGSManagedHierarchyTimeoutMs, BGSCaptureDataProviderViewHierarchy.m:28.
144
+ XCTAssertLessThan(BGSRNDataRequestDeadlineMs, 500);
145
+ XCTAssertEqual(BGSRNDataRequestDeadlineMs, 450);
146
+ }
147
+
148
+ #pragma mark - Synchronous nil answers
149
+
150
+ - (void)testAnUnknownTypeRepliesNilSynchronously {
151
+ [self attachAndEnable];
152
+
153
+ [self request:@"other"];
154
+
155
+ XCTAssertEqualObjects(_replies, (@[ NSNull.null ]));
156
+ XCTAssertEqual(_requests.count, 0u);
157
+ XCTAssertEqual(_tasks.count, 0u);
158
+ XCTAssertEqualObjects(_lines, (@[ @"BugseeRN data request dr-1 completed by=unknown-type bytes=null ms=0" ]));
159
+ }
160
+
161
+ - (void)testNoSinkRepliesNilSynchronously {
162
+ _bridge.viewTreeEnabled = YES;
163
+
164
+ [self requestVh];
165
+
166
+ XCTAssertEqualObjects(_replies, (@[ NSNull.null ]));
167
+ XCTAssertEqual(_tasks.count, 0u);
168
+ XCTAssertEqualObjects(_lines, (@[ @"BugseeRN data request dr-1 completed by=no-js bytes=null ms=0" ]));
169
+ }
170
+
171
+ - (void)testADisabledViewTreeRepliesNilSynchronously {
172
+ [self attach:_sink into:_requests];
173
+
174
+ [self requestVh];
175
+
176
+ XCTAssertEqualObjects(_replies, (@[ NSNull.null ]));
177
+ XCTAssertEqual(_requests.count, 0u);
178
+ XCTAssertEqual(_tasks.count, 0u);
179
+ XCTAssertEqualObjects(_lines, (@[ @"BugseeRN data request dr-1 completed by=no-js bytes=null ms=0" ]));
180
+ }
181
+
182
+ - (void)testNoOriginRepliesNilSynchronously {
183
+ [self attachAndEnable];
184
+ _origin = nil;
185
+
186
+ [self requestVh];
187
+
188
+ XCTAssertEqualObjects(_replies, (@[ NSNull.null ]));
189
+ XCTAssertEqual(_requests.count, 0u);
190
+ XCTAssertEqual(_tasks.count, 0u);
191
+ XCTAssertEqualObjects(_lines, (@[ @"BugseeRN data request dr-1 completed by=no-origin bytes=null ms=0" ]));
192
+ }
193
+
194
+ /// The block is the module's, but the bridge does not trust it to hand back
195
+ /// a finite point.
196
+ - (void)testAnOriginThatIsNotAFinitePointRepliesNilSynchronously {
197
+ [self attachAndEnable];
198
+ _origin = [NSValue valueWithRange:NSMakeRange(1, 2)];
199
+ [self requestVh];
200
+ _origin = [NSValue valueWithCGPoint:CGPointMake(NAN, 0)];
201
+ [self requestVh];
202
+ _origin = (NSValue *)(id)@"0,0";
203
+ [self requestVh];
204
+
205
+ XCTAssertEqualObjects(_replies, (@[ NSNull.null, NSNull.null, NSNull.null ]));
206
+ XCTAssertEqual(_requests.count, 0u);
207
+ XCTAssertEqual(_bridge.outstanding, 0u);
208
+ }
209
+
210
+ #pragma mark - The round trip
211
+
212
+ - (void)testTheSinkGetsTheOrigin {
213
+ [self attachAndEnable];
214
+ _origin = [NSValue valueWithCGPoint:CGPointMake(12.5, 40)];
215
+
216
+ [self requestVh];
217
+
218
+ XCTAssertEqualObjects(_requests, (@[ @{
219
+ @"requestId" : @"dr-1",
220
+ @"type" : @"vh",
221
+ @"originX" : @12.5,
222
+ @"originY" : @40,
223
+ } ]));
224
+ XCTAssertEqual(_replies.count, 0u);
225
+ XCTAssertEqual(_tasks.count, 1u);
226
+ XCTAssertEqual(_tasks[0].delayMs, BGSRNDataRequestDeadlineMs);
227
+ XCTAssertEqual(_bridge.outstanding, 1u);
228
+ XCTAssertEqualObjects(_lines, (@[ @"BugseeRN data request dr-1 type=vh origin=12.5,40" ]));
229
+ }
230
+
231
+ - (void)testCompleteDeliversThePayloadExactlyOnce {
232
+ [self attachAndEnable];
233
+ [self requestVh];
234
+ NSString *requestId = [self lastId];
235
+
236
+ XCTAssertTrue([_bridge complete:requestId payload:@"{}"]);
237
+ XCTAssertFalse([_bridge complete:requestId payload:@"{\"again\":1}"]);
238
+ [self fireEvenIfCancelled];
239
+ [_bridge detach:_sink];
240
+
241
+ XCTAssertEqualObjects(_replies, (@[ @"{}" ]));
242
+ XCTAssertEqual(_bridge.outstanding, 0u);
243
+ // One outcome, too: a late deadline must not log a second `completed`.
244
+ XCTAssertEqualObjects(_lines, (@[
245
+ @"BugseeRN data request dr-1 type=vh origin=0,0",
246
+ @"BugseeRN data request dr-1 completed by=js bytes=2 ms=0",
247
+ ]));
248
+ }
249
+
250
+ - (void)testASecondCompleteIsANoOp {
251
+ [self attachAndEnable];
252
+ [self requestVh];
253
+ NSString *requestId = [self lastId];
254
+ [_bridge complete:requestId payload:nil];
255
+
256
+ XCTAssertFalse([_bridge complete:requestId payload:@"{}"]);
257
+ XCTAssertEqualObjects(_replies, (@[ NSNull.null ]));
258
+ }
259
+
260
+ - (void)testTheDeadlineRepliesNil {
261
+ [self attachAndEnable];
262
+ [self requestVh];
263
+ _clock.now = 450;
264
+
265
+ [self fireDue];
266
+
267
+ XCTAssertEqualObjects(_replies, (@[ NSNull.null ]));
268
+ XCTAssertEqualObjects(_lines.lastObject, @"BugseeRN data request dr-1 completed by=deadline bytes=null ms=450");
269
+ XCTAssertEqual(_bridge.outstanding, 0u);
270
+ }
271
+
272
+ - (void)testAReplyAfterTheDeadlineIsDropped {
273
+ [self attachAndEnable];
274
+ [self requestVh];
275
+ [self fireDue];
276
+
277
+ XCTAssertFalse([_bridge complete:[self lastId] payload:@"{}"]);
278
+ XCTAssertEqualObjects(_replies, (@[ NSNull.null ]));
279
+ }
280
+
281
+ - (void)testCompletingCancelsTheDeadline {
282
+ [self attachAndEnable];
283
+ [self requestVh];
284
+
285
+ [_bridge complete:[self lastId] payload:@"{}"];
286
+
287
+ XCTAssertTrue(_tasks[0].cancelled);
288
+ }
289
+
290
+ - (void)testAnUnknownIdIsIgnored {
291
+ [self attachAndEnable];
292
+ [self requestVh];
293
+
294
+ XCTAssertFalse([_bridge complete:@"dr-99" payload:@"{}"]);
295
+ XCTAssertFalse([_bridge complete:(NSString *)(id)NSNull.null payload:@"{}"]);
296
+ XCTAssertEqual(_replies.count, 0u);
297
+ XCTAssertEqual(_bridge.outstanding, 1u);
298
+ }
299
+
300
+ /// A dead bridge: the emit could not reach JS, so nothing will answer.
301
+ - (void)testASinkThatCannotDeliverRepliesNil {
302
+ [_bridge attach:_sink
303
+ block:^BOOL(NSDictionary *request) {
304
+ return NO;
305
+ }
306
+ origin:^NSValue * {
307
+ return [NSValue valueWithCGPoint:CGPointZero];
308
+ }];
309
+ _bridge.viewTreeEnabled = YES;
310
+
311
+ [self requestVh];
312
+
313
+ XCTAssertEqualObjects(_replies, (@[ NSNull.null ]));
314
+ XCTAssertEqualObjects(_lines.lastObject, @"BugseeRN data request dr-1 completed by=sink-threw bytes=null ms=0");
315
+ XCTAssertTrue(_tasks[0].cancelled);
316
+ XCTAssertEqual(_bridge.outstanding, 0u);
317
+ }
318
+
319
+ /// The reply is the SDK's; its fault must not reach JS's queue or the timer's.
320
+ - (void)testAThrowingReplyDoesNotEscape {
321
+ [self attachAndEnable];
322
+ [_bridge requestType:@"vh"
323
+ reply:^(NSString *data) {
324
+ [NSException raise:@"SDK" format:@"boom"];
325
+ }];
326
+
327
+ XCTAssertNoThrow([_bridge complete:[self lastId] payload:@"{}"]);
328
+ XCTAssertEqual(_bridge.outstanding, 0u);
329
+ XCTAssertNoThrow([_bridge requestType:@"other"
330
+ reply:^(NSString *data) {
331
+ [NSException raise:@"SDK" format:@"boom"];
332
+ }]);
333
+ }
334
+
335
+ /// `by=failed`: the bridge itself failed after the request was registered.
336
+ /// The SDK still gets exactly one nil, and nothing is left for a later detach
337
+ /// to answer a second time.
338
+ - (void)testASchedulerFailureAfterRegisteringStillRepliesExactlyOnceAndLeavesNothingOutstanding {
339
+ NSMutableArray<NSString *> *lines = _lines;
340
+ _bridge = [[BGSRNDataRequestBridge alloc]
341
+ initWithScheduler:^id(dispatch_block_t task, int64_t delayMs) {
342
+ [NSException raise:@"Scheduler" format:@"rejected"];
343
+ return [NSObject new];
344
+ }
345
+ cancel:^(id token) {
346
+ }
347
+ clock:^int64_t {
348
+ return 0;
349
+ }
350
+ log:^(NSString *line) {
351
+ [lines addObject:line];
352
+ }];
353
+ [self attachAndEnable];
354
+
355
+ XCTAssertNoThrow([self requestVh]);
356
+ [_bridge detach:_sink];
357
+
358
+ XCTAssertEqualObjects(_replies, (@[ NSNull.null ]));
359
+ XCTAssertEqual(_bridge.outstanding, 0u);
360
+ XCTAssertEqualObjects(_lines, (@[ @"BugseeRN data request dr-1 completed by=failed bytes=null ms=0" ]));
361
+ XCTAssertEqual(_requests.count, 0u);
362
+ }
363
+
364
+ /// An origin block that raises is the bridge failing, not "no origin".
365
+ - (void)testAThrowingOriginRepliesNilAsFailed {
366
+ [_bridge attach:_sink
367
+ block:^BOOL(NSDictionary *request) {
368
+ return YES;
369
+ }
370
+ origin:^NSValue * {
371
+ [NSException raise:@"UIKit" format:@"boom"];
372
+ return nil;
373
+ }];
374
+ _bridge.viewTreeEnabled = YES;
375
+
376
+ XCTAssertNoThrow([self requestVh]);
377
+
378
+ XCTAssertEqualObjects(_replies, (@[ NSNull.null ]));
379
+ XCTAssertEqual(_bridge.outstanding, 0u);
380
+ XCTAssertEqualObjects(_lines, (@[ @"BugseeRN data request dr-1 completed by=failed bytes=null ms=0" ]));
381
+ }
382
+
383
+ #pragma mark - Detach and attach
384
+
385
+ - (void)testDetachRepliesNilToEverythingOutstandingAndDisables {
386
+ [self attachAndEnable];
387
+ [self requestVh];
388
+ [self requestVh];
389
+
390
+ [_bridge detach:_sink];
391
+
392
+ XCTAssertEqualObjects(_replies, (@[ NSNull.null, NSNull.null ]));
393
+ XCTAssertTrue(_tasks[0].cancelled);
394
+ XCTAssertTrue(_tasks[1].cancelled);
395
+ XCTAssertEqual(_bridge.outstanding, 0u);
396
+ XCTAssertFalse(_bridge.viewTreeEnabled);
397
+ XCTAssertTrue([_lines containsObject:@"BugseeRN data request dr-1 completed by=detach bytes=null ms=0"]);
398
+ XCTAssertTrue([_lines containsObject:@"BugseeRN data request dr-2 completed by=detach bytes=null ms=0"]);
399
+
400
+ // And nothing reaches the detached sink afterwards.
401
+ _bridge.viewTreeEnabled = YES;
402
+ [self requestVh];
403
+ XCTAssertEqual(_requests.count, 2u);
404
+ XCTAssertEqualObjects(_lines.lastObject, @"BugseeRN data request dr-3 completed by=no-js bytes=null ms=0");
405
+ }
406
+
407
+ /// A fast reload attaches the new module before the old one is invalidated.
408
+ - (void)testDetachingAStaleSinkLeavesTheCurrentOne {
409
+ NSObject *stale = [NSObject new];
410
+ NSMutableArray<NSDictionary *> *staleRequests = [NSMutableArray array];
411
+ [self attach:stale into:staleRequests];
412
+ _bridge.viewTreeEnabled = YES;
413
+ [self requestVh];
414
+ NSString *staleId = staleRequests.lastObject[@"requestId"];
415
+
416
+ [self attachAndEnable];
417
+ [self requestVh];
418
+ [_bridge detach:stale];
419
+
420
+ XCTAssertEqualObjects(_replies, (@[ NSNull.null ]));
421
+ XCTAssertFalse([_bridge complete:staleId payload:@"{}"]);
422
+ XCTAssertTrue(_bridge.viewTreeEnabled);
423
+ XCTAssertEqual(_bridge.outstanding, 1u);
424
+ XCTAssertTrue([_bridge complete:[self lastId] payload:@"{}"]);
425
+ XCTAssertEqualObjects(_replies, (@[ NSNull.null, @"{}" ]));
426
+
427
+ [self requestVh];
428
+ XCTAssertEqual(_requests.count, 2u);
429
+ }
430
+
431
+ /// The old module's last disable must not stick. A reload attaches and
432
+ /// enables the new sink, then the old runtime's wrap cleanup still calls
433
+ /// setViewTreeEnabled:NO on the shared flag.
434
+ - (void)testAStaleModuleCannotDisableTheAttachedViewTree {
435
+ NSObject *stale = [NSObject new];
436
+ NSMutableArray<NSDictionary *> *staleRequests = [NSMutableArray array];
437
+ [self attach:stale into:staleRequests];
438
+ [_bridge setViewTreeEnabled:YES forSink:stale];
439
+
440
+ [self attachAndEnable];
441
+ [_bridge setViewTreeEnabled:NO forSink:stale];
442
+
443
+ [self requestVh];
444
+
445
+ XCTAssertTrue(_bridge.viewTreeEnabled);
446
+ XCTAssertEqual(_requests.count, 1u);
447
+ XCTAssertEqual(_replies.count, 0u);
448
+ XCTAssertTrue([_bridge complete:[self lastId] payload:@"{}"]);
449
+ XCTAssertEqualObjects(_replies, (@[ @"{}" ]));
450
+ }
451
+
452
+ /// Before the new runtime mounts its anchor there is nothing to walk.
453
+ - (void)testAFreshAttachDisablesTheViewTreeEvenBeforeTheOldModuleDetaches {
454
+ [self attachAndEnable];
455
+
456
+ [self attach:[NSObject new] into:[NSMutableArray array]];
457
+
458
+ XCTAssertFalse(_bridge.viewTreeEnabled);
459
+ [self requestVh];
460
+ XCTAssertEqualObjects(_replies, (@[ NSNull.null ]));
461
+ }
462
+
463
+ #pragma mark - Ids and log lines
464
+
465
+ - (void)testEachRequestGetsAFreshId {
466
+ [self attachAndEnable];
467
+ [self requestVh];
468
+ [self request:@"other"];
469
+ [self requestVh];
470
+
471
+ XCTAssertEqualObjects(_requests[0][@"requestId"], @"dr-1");
472
+ XCTAssertEqualObjects(_requests[1][@"requestId"], @"dr-3");
473
+ XCTAssertTrue([_lines containsObject:@"BugseeRN data request dr-2 completed by=unknown-type bytes=null ms=0"]);
474
+
475
+ // Completed ids are never handed out again.
476
+ [_bridge complete:@"dr-1" payload:nil];
477
+ [_bridge complete:@"dr-3" payload:nil];
478
+ [self requestVh];
479
+ XCTAssertEqualObjects(_requests[2][@"requestId"], @"dr-4");
480
+ }
481
+
482
+ - (void)testTheCompletedLineCarriesByBytesAndMs {
483
+ [self attachAndEnable];
484
+ _clock.now = 100;
485
+ [self requestVh];
486
+ _clock.now = 137;
487
+
488
+ [_bridge complete:[self lastId] payload:@"abc"];
489
+
490
+ XCTAssertEqualObjects(_lines.lastObject, @"BugseeRN data request dr-1 completed by=js bytes=3 ms=37");
491
+ }
492
+
493
+ - (void)testANilPayloadLogsNullBytes {
494
+ [self attachAndEnable];
495
+ [self requestVh];
496
+
497
+ [_bridge complete:[self lastId] payload:nil];
498
+
499
+ XCTAssertEqualObjects(_lines.lastObject, @"BugseeRN data request dr-1 completed by=js bytes=null ms=0");
500
+ }
501
+
502
+ /// The same definition as Android's, so the two platforms' lines compare.
503
+ - (void)testBytesIsTheUtf8ByteCountNotTheCharCount {
504
+ [self attachAndEnable];
505
+ [self requestVh];
506
+
507
+ [_bridge complete:[self lastId] payload:@"é"];
508
+
509
+ XCTAssertEqualObjects(_lines.lastObject, @"BugseeRN data request dr-1 completed by=js bytes=2 ms=0");
510
+ }
511
+
512
+ /// The plan: `ms` is measured from the moment `requestData` was entered.
513
+ - (void)testMsIsMeasuredFromEntryToRequestNotFromWhenTheRequestWasMinted {
514
+ BGSRNDataRequestManualClock *clock = _clock;
515
+ NSMutableArray<NSDictionary *> *requests = _requests;
516
+ [_bridge attach:_sink
517
+ block:^BOOL(NSDictionary *request) {
518
+ [requests addObject:request];
519
+ return YES;
520
+ }
521
+ origin:^NSValue * {
522
+ clock.now += 5;
523
+ return [NSValue valueWithCGPoint:CGPointZero];
524
+ }];
525
+ _bridge.viewTreeEnabled = YES;
526
+ [self requestVh];
527
+
528
+ [_bridge complete:[self lastId] payload:nil];
529
+
530
+ XCTAssertEqualObjects(_lines.lastObject, @"BugseeRN data request dr-1 completed by=js bytes=null ms=5");
531
+ }
532
+
533
+ - (void)testTheTypeLineComesBeforeTheEmit {
534
+ NSMutableArray<NSString *> *lines = _lines;
535
+ __block NSUInteger linesAtEmit = NSNotFound;
536
+ [_bridge attach:_sink
537
+ block:^BOOL(NSDictionary *request) {
538
+ linesAtEmit = lines.count;
539
+ return YES;
540
+ }
541
+ origin:^NSValue * {
542
+ return [NSValue valueWithCGPoint:CGPointMake(-3, 1.25)];
543
+ }];
544
+ _bridge.viewTreeEnabled = YES;
545
+
546
+ [self requestVh];
547
+
548
+ XCTAssertEqual(linesAtEmit, 1u);
549
+ XCTAssertEqualObjects(_lines[0], @"BugseeRN data request dr-1 type=vh origin=-3,1.25");
550
+ }
551
+
552
+ /// Exact, not `%g`'s 6 significant digits: the shortest text that parses
553
+ /// back to the same double, so a device test can recompute bounds from it.
554
+ - (void)testTheTypeLinePrintsTheOriginExactly {
555
+ [self attachAndEnable];
556
+ _origin = [NSValue valueWithCGPoint:CGPointMake(1024.333, 1.0 / 3.0)];
557
+
558
+ [self requestVh];
559
+
560
+ XCTAssertEqualObjects(_lines[0], @"BugseeRN data request dr-1 type=vh origin=1024.333,0.3333333333333333");
561
+ }
562
+
563
+ #pragma mark - Races, pinned deterministically
564
+
565
+ /// A detach that lands after the sink was read but before the request is
566
+ /// registered (here: from inside the origin read, which runs outside the
567
+ /// lock). The register-time re-check answers it at once.
568
+ - (void)testADetachDuringTheOriginReadRepliesNilAsDetach {
569
+ BGSRNDataRequestBridge *bridge = _bridge;
570
+ NSObject *sink = _sink;
571
+ NSMutableArray<NSDictionary *> *requests = _requests;
572
+ [_bridge attach:sink
573
+ block:^BOOL(NSDictionary *request) {
574
+ [requests addObject:request];
575
+ return YES;
576
+ }
577
+ origin:^NSValue * {
578
+ [bridge detach:sink];
579
+ return [NSValue valueWithCGPoint:CGPointZero];
580
+ }];
581
+ _bridge.viewTreeEnabled = YES;
582
+
583
+ [self requestVh];
584
+
585
+ XCTAssertEqualObjects(_replies, (@[ NSNull.null ]));
586
+ XCTAssertEqual(_requests.count, 0u);
587
+ XCTAssertEqual(_tasks.count, 0u);
588
+ XCTAssertEqual(_bridge.outstanding, 0u);
589
+ XCTAssertEqualObjects(_lines, (@[ @"BugseeRN data request dr-1 completed by=detach bytes=null ms=0" ]));
590
+ }
591
+
592
+ /// A detach that lands after the request is registered but before its
593
+ /// deadline is stored (here: from inside the scheduler). The deadline is
594
+ /// cancelled, and the request neither logs a `type=` line after its
595
+ /// `completed` line nor reaches the detached sink.
596
+ - (void)testADetachWhileArmingTheDeadlineCancelsItAndNeverEmits {
597
+ NSMutableArray<BGSRNDataRequestManualTask *> *tasks = _tasks;
598
+ NSMutableArray<NSString *> *lines = _lines;
599
+ NSObject *sink = _sink;
600
+ __block __weak BGSRNDataRequestBridge *weakBridge = nil;
601
+ _bridge = [[BGSRNDataRequestBridge alloc]
602
+ initWithScheduler:^id(dispatch_block_t task, int64_t delayMs) {
603
+ [weakBridge detach:sink];
604
+ BGSRNDataRequestManualTask *t = [BGSRNDataRequestManualTask new];
605
+ t.block = task;
606
+ [tasks addObject:t];
607
+ return t;
608
+ }
609
+ cancel:^(id token) {
610
+ ((BGSRNDataRequestManualTask *)token).cancelled = YES;
611
+ }
612
+ clock:^int64_t {
613
+ return 0;
614
+ }
615
+ log:^(NSString *line) {
616
+ [lines addObject:line];
617
+ }];
618
+ weakBridge = _bridge;
619
+ [self attachAndEnable];
620
+
621
+ [self requestVh];
622
+
623
+ XCTAssertEqualObjects(_replies, (@[ NSNull.null ]));
624
+ XCTAssertEqual(_tasks.count, 1u);
625
+ XCTAssertTrue(_tasks[0].cancelled);
626
+ XCTAssertEqual(_requests.count, 0u);
627
+ XCTAssertEqual(_bridge.outstanding, 0u);
628
+ XCTAssertEqualObjects(_lines, (@[ @"BugseeRN data request dr-1 completed by=detach bytes=null ms=0" ]));
629
+ }
630
+
631
+ #pragma mark - Locking
632
+
633
+ /// `reply` re-enters the SDK, and must never run under the registry lock:
634
+ /// here it waits on another thread that needs the lock. Holding the lock
635
+ /// across `reply` deadlocks that wait, which the timeout reports.
636
+ - (void)testAReplyMayWaitOnAnotherThreadThatUsesTheBridge {
637
+ [self attachAndEnable];
638
+ [self requestVh];
639
+ NSString *other = [self lastId];
640
+ BGSRNDataRequestBridge *bridge = _bridge;
641
+ __block BOOL timedOut = NO;
642
+ __block BOOL otherDelivered = NO;
643
+ [_bridge requestType:@"vh"
644
+ reply:^(NSString *data) {
645
+ dispatch_semaphore_t done = dispatch_semaphore_create(0);
646
+ dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
647
+ otherDelivered = [bridge complete:other payload:@"{}"];
648
+ dispatch_semaphore_signal(done);
649
+ });
650
+ timedOut = dispatch_semaphore_wait(done, dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC)) != 0;
651
+ }];
652
+
653
+ XCTAssertTrue([_bridge complete:[self lastId] payload:@"{}"]);
654
+
655
+ XCTAssertFalse(timedOut, @"reply ran while the registry lock was held");
656
+ XCTAssertTrue(otherDelivered);
657
+ XCTAssertEqualObjects(_replies, (@[ @"{}" ]));
658
+ XCTAssertEqual(_bridge.outstanding, 0u);
659
+ }
660
+
661
+ /// Every path at once, many times: exactly one reply per request.
662
+ - (void)testJsTheDeadlineAndADetachRacingStillReplyExactlyOnce {
663
+ for (int round = 0; round < 200; round++) {
664
+ [self setUp];
665
+ [self attachAndEnable];
666
+ [self requestVh];
667
+ NSString *requestId = [self lastId];
668
+ BGSRNDataRequestBridge *bridge = _bridge;
669
+ NSObject *sink = _sink;
670
+ BGSRNDataRequestManualTask *deadline = _tasks[0];
671
+ dispatch_apply(3, dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^(size_t i) {
672
+ if (i == 0) {
673
+ [bridge complete:requestId payload:@"{}"];
674
+ } else if (i == 1) {
675
+ deadline.block();
676
+ } else {
677
+ [bridge detach:sink];
678
+ }
679
+ });
680
+ XCTAssertEqual(_replies.count, 1u);
681
+ XCTAssertEqual(_bridge.outstanding, 0u);
682
+ }
683
+ }
684
+
685
+ @end