Skip to content

Commit

Permalink
test: allow starting/stopping txs from either main/bg threads (#3184)
Browse files Browse the repository at this point in the history
  • Loading branch information
armcknight authored Aug 7, 2023
1 parent d3630c3 commit 279841c
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 28 deletions.
54 changes: 34 additions & 20 deletions Samples/iOS-Swift/iOS-Swift/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,41 @@
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" distribution="equalSpacing" translatesAutoresizingMaskIntoConstraints="NO" id="Cod-iq-tSj">
<rect key="frame" x="89" y="137.5" width="142" height="308"/>
<rect key="frame" x="30.5" y="137.5" width="259" height="308"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="cdR-H3-8fr">
<rect key="frame" x="0.0" y="0.0" width="142" height="28"/>
<rect key="frame" x="0.0" y="0.0" width="259" height="28"/>
<fontDescription key="fontDescription" type="system" pointSize="13"/>
<state key="normal" title="Capture Transaction"/>
<connections>
<action selector="captureTransaction:" destination="BYZ-38-t0r" eventType="touchUpInside" id="kff-pT-Uf4"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="60A-0I-s24">
<rect key="frame" x="0.0" y="28" width="142" height="28"/>
<fontDescription key="fontDescription" type="system" pointSize="13"/>
<inset key="imageEdgeInsets" minX="0.0" minY="0.0" maxX="2.2250738585072014e-308" maxY="0.0"/>
<state key="normal" title="Start transaction"/>
<connections>
<action selector="startTransaction:" destination="BYZ-38-t0r" eventType="touchUpInside" id="QEI-9L-E5C"/>
</connections>
</button>
<stackView opaque="NO" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="I38-4R-JYf">
<rect key="frame" x="0.0" y="28" width="259" height="28"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="60A-0I-s24">
<rect key="frame" x="0.0" y="0.0" width="187" height="28"/>
<fontDescription key="fontDescription" type="system" pointSize="13"/>
<inset key="imageEdgeInsets" minX="0.0" minY="0.0" maxX="2.2250738585072014e-308" maxY="0.0"/>
<state key="normal" title="Start transaction (main thread)"/>
<connections>
<action selector="startTransaction:" destination="BYZ-38-t0r" eventType="touchUpInside" id="QEI-9L-E5C"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="80k-Lr-YpF">
<rect key="frame" x="187" y="0.0" width="72" height="28"/>
<fontDescription key="fontDescription" type="system" pointSize="13"/>
<inset key="imageEdgeInsets" minX="0.0" minY="0.0" maxX="2.2250738585072014e-308" maxY="0.0"/>
<state key="normal" title=" (bg thread)"/>
<connections>
<action selector="startTransactionFromOtherThread:" destination="BYZ-38-t0r" eventType="touchUpInside" id="ZEH-BE-vTP"/>
</connections>
</button>
</subviews>
</stackView>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="M4W-xc-mEo">
<rect key="frame" x="0.0" y="56" width="142" height="28"/>
<rect key="frame" x="0.0" y="56" width="259" height="28"/>
<fontDescription key="fontDescription" type="system" pointSize="13"/>
<inset key="imageEdgeInsets" minX="0.0" minY="0.0" maxX="2.2250738585072014e-308" maxY="0.0"/>
<state key="normal" title="Stop transaction"/>
Expand All @@ -48,7 +62,7 @@
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="LvU-yx-01i">
<rect key="frame" x="0.0" y="84" width="142" height="28"/>
<rect key="frame" x="0.0" y="84" width="259" height="28"/>
<accessibility key="accessibilityConfiguration" identifier="showNibButton"/>
<fontDescription key="fontDescription" type="system" pointSize="13"/>
<state key="normal" title="Nib Controller"/>
Expand All @@ -57,7 +71,7 @@
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="RjO-LN-eHj">
<rect key="frame" x="0.0" y="112" width="142" height="28"/>
<rect key="frame" x="0.0" y="112" width="259" height="28"/>
<accessibility key="accessibilityConfiguration" identifier="showTableViewButton"/>
<fontDescription key="fontDescription" type="system" pointSize="13"/>
<state key="normal" title="Table Controller"/>
Expand All @@ -66,7 +80,7 @@
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="ok0-hq-2kK">
<rect key="frame" x="0.0" y="140" width="142" height="28"/>
<rect key="frame" x="0.0" y="140" width="259" height="28"/>
<accessibility key="accessibilityConfiguration" identifier="showSplitViewButton"/>
<fontDescription key="fontDescription" type="system" pointSize="13"/>
<state key="normal" title="Split Controller"/>
Expand All @@ -75,7 +89,7 @@
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="Jwi-v3-e8T">
<rect key="frame" x="0.0" y="168" width="142" height="28"/>
<rect key="frame" x="0.0" y="168" width="259" height="28"/>
<accessibility key="accessibilityConfiguration" identifier="loremIpsumButton"/>
<fontDescription key="fontDescription" type="system" pointSize="13"/>
<state key="normal" title="Lorem Ipsum Controller"/>
Expand All @@ -84,7 +98,7 @@
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="cTC-V8-T1j">
<rect key="frame" x="0.0" y="196" width="142" height="28"/>
<rect key="frame" x="0.0" y="196" width="259" height="28"/>
<accessibility key="accessibilityConfiguration" identifier="useCoreData"/>
<fontDescription key="fontDescription" type="system" pointSize="13"/>
<state key="normal" title="Core Data Tests "/>
Expand All @@ -93,7 +107,7 @@
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="Ld2-mz-wQD">
<rect key="frame" x="0.0" y="224" width="142" height="28"/>
<rect key="frame" x="0.0" y="224" width="259" height="28"/>
<fontDescription key="fontDescription" type="system" pointSize="13"/>
<inset key="imageEdgeInsets" minX="0.0" minY="0.0" maxX="2.2250738585072014e-308" maxY="0.0"/>
<state key="normal" title="Performance scenarios"/>
Expand All @@ -102,7 +116,7 @@
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="8CV-WC-ffq">
<rect key="frame" x="0.0" y="252" width="142" height="28"/>
<rect key="frame" x="0.0" y="252" width="259" height="28"/>
<accessibility key="accessibilityConfiguration" identifier="testNavigationTransactionButton"/>
<fontDescription key="fontDescription" type="system" pointSize="13"/>
<state key="normal" title="Load Image Controller"/>
Expand All @@ -111,7 +125,7 @@
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="mjD-vZ-Wtp" userLabel="UIClick Transaction">
<rect key="frame" x="0.0" y="280" width="142" height="28"/>
<rect key="frame" x="0.0" y="280" width="259" height="28"/>
<accessibility key="accessibilityConfiguration" identifier="uiClickTransactionButton"/>
<fontDescription key="fontDescription" type="system" pointSize="13"/>
<state key="normal" title="UIClick Transaction"/>
Expand Down
69 changes: 62 additions & 7 deletions Samples/iOS-Swift/iOS-Swift/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@ class ViewController: UIViewController {
dataTask.resume()
}

var span: Span?
var spans = [Span]()
let profilerNotification = NSNotification.Name("SentryProfileCompleteNotification")

@IBAction func startTransaction(_ sender: UIButton) {
highlightButton(sender)
guard span == nil else { return }
span = SentrySDK.startTransaction(name: "Manual Transaction", operation: "Manual Operation")
startNewTransaction()
}

fileprivate func startNewTransaction() {
spans.append(SentrySDK.startTransaction(name: "Manual Transaction", operation: "Manual Operation"))

NotificationCenter.default.addObserver(forName: profilerNotification, object: nil, queue: nil) { note in
DispatchQueue.main.async {
Expand All @@ -49,12 +52,64 @@ class ViewController: UIViewController {
}
}

@IBAction func startTransactionFromOtherThread(_ sender: UIButton) {
highlightButton(sender)

Thread.detachNewThread {
self.startNewTransaction()
}
}

@IBAction func stopTransaction(_ sender: UIButton) {
highlightButton(sender)
span?.finish()
span = nil

NotificationCenter.default.removeObserver(self, name: profilerNotification, object: nil)
defer {
if spans.isEmpty {
NotificationCenter.default.removeObserver(self, name: profilerNotification, object: nil)
}
}

func showConfirmation(span: Span) {
DispatchQueue.main.async {
let confirmation = UIAlertController(title: "Finished span \(span.spanId.sentrySpanIdString)", message: nil, preferredStyle: .alert)
confirmation.addAction(UIAlertAction(title: "OK", style: .default))
self.present(confirmation, animated: true)
}
}

func finishSpan(span: Span) {
span.finish()
self.spans.remove(at: self.spans.firstIndex(where: { testSpan in
testSpan.spanId == span.spanId
})!)
showConfirmation(span: span)
}

if spans.count == 1 {
finishSpan(span: spans[0])
return
}

let alert = UIAlertController(title: "Choose span to stop", message: nil, preferredStyle: .actionSheet)
spans.forEach { span in
alert.addAction(UIAlertAction(title: span.spanId.sentrySpanIdString, style: .default, handler: { _ in
let threadPicker = UIAlertController(title: "From thread:", message: nil, preferredStyle: .actionSheet)
threadPicker.addAction(UIAlertAction(title: "Main thread", style: .default, handler: { _ in
DispatchQueue.main.async {
finishSpan(span: span)
}
}))
threadPicker.addAction(UIAlertAction(title: "BG thread", style: .default, handler: { _ in
Thread.detachNewThread {
finishSpan(span: span)
}
}))
threadPicker.addAction(UIAlertAction(title: "Cancel", style: .cancel))
self.present(threadPicker, animated: true)
}))
}
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel))
present(alert, animated: true)
}

@IBAction func captureTransaction(_ sender: UIButton) {
Expand Down
2 changes: 1 addition & 1 deletion Samples/iOS-Swift/iOS-SwiftUITests/ProfilingUITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ final class ProfilingUITests: XCTestCase {

XCUIApplication().tabBars["Tab Bar"].buttons["Extra"].tap()
XCUIApplication().tabBars["Tab Bar"].buttons["Transactions"].tap()
app.buttons["Start transaction"].afterWaitingForExistence("Couldn't find button to start transaction").tap()
app.buttons["Start transaction (main thread)"].afterWaitingForExistence("Couldn't find button to start transaction").tap()
XCUIApplication().tabBars["Tab Bar"].buttons["Extra"].tap()
app.buttons["ANR filling run loop"].afterWaitingForExistence("Couldn't find button to ANR").tap()
XCUIApplication().tabBars["Tab Bar"].buttons["Transactions"].tap()
Expand Down

0 comments on commit 279841c

Please sign in to comment.