Skip to content

Commit

Permalink
added more span assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
philprime committed Feb 24, 2025
1 parent ec41052 commit 4efcc1c
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 26 deletions.
24 changes: 12 additions & 12 deletions Sentry.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1954,6 +1954,9 @@
D41909942D490006002B83D0 /* SentryNSDictionarySanitize+Tests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "SentryNSDictionarySanitize+Tests.m"; sourceTree = "<group>"; };
D42E48562D48DF1600D251BC /* SentryBuildAppStartSpansTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryBuildAppStartSpansTests.swift; sourceTree = "<group>"; };
D42E48582D48FC8F00D251BC /* SentryNSDictionarySanitizeTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryNSDictionarySanitizeTests.swift; sourceTree = "<group>"; };
D43647EE2D5CF9DC001468E0 /* SentrySpanDataKey.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentrySpanDataKey.swift; sourceTree = "<group>"; };
D43647F02D5CFB71001468E0 /* SentrySpanKeyTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SentrySpanKeyTests.swift; sourceTree = "<group>"; };
D468C0612D3669A200964230 /* SentryFileIOTracker+SwiftHelpers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "SentryFileIOTracker+SwiftHelpers.swift"; sourceTree = "<group>"; };
D46D45E12D5F3FD600A1CB35 /* Sentry_Base.xctestplan */ = {isa = PBXFileReference; lastKnownFileType = text; path = Sentry_Base.xctestplan; sourceTree = "<group>"; };
D46D45E32D5F402100A1CB35 /* iOS-Swift_Base.xctestplan */ = {isa = PBXFileReference; lastKnownFileType = text; path = "iOS-Swift_Base.xctestplan"; sourceTree = "<group>"; };
D46D45E42D5F403E00A1CB35 /* iOS-Swift-Benchmarking_Base.xctestplan */ = {isa = PBXFileReference; lastKnownFileType = text; name = "iOS-Swift-Benchmarking_Base.xctestplan"; path = "Plans/iOS-Swift-Benchmarking_Base.xctestplan"; sourceTree = SOURCE_ROOT; };
Expand All @@ -1963,9 +1966,6 @@
D46D45E82D5F40FA00A1CB35 /* SentryProfilerTests_Base.xctestplan */ = {isa = PBXFileReference; lastKnownFileType = text; name = SentryProfilerTests_Base.xctestplan; path = Plans/SentryProfilerTests_Base.xctestplan; sourceTree = SOURCE_ROOT; };
D46D45E92D5F411700A1CB35 /* SentrySwiftUI_Base.xctestplan */ = {isa = PBXFileReference; lastKnownFileType = text; name = SentrySwiftUI_Base.xctestplan; path = Plans/SentrySwiftUI_Base.xctestplan; sourceTree = SOURCE_ROOT; };
D46D45EA2D5F412100A1CB35 /* SentryTests_Base.xctestplan */ = {isa = PBXFileReference; lastKnownFileType = text; name = SentryTests_Base.xctestplan; path = Plans/SentryTests_Base.xctestplan; sourceTree = SOURCE_ROOT; };
D43647EE2D5CF9DC001468E0 /* SentrySpanDataKey.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentrySpanDataKey.swift; sourceTree = "<group>"; };
D43647F02D5CFB71001468E0 /* SentrySpanKeyTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SentrySpanKeyTests.swift; sourceTree = "<group>"; };
D468C0612D3669A200964230 /* SentryFileIOTracker+SwiftHelpers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "SentryFileIOTracker+SwiftHelpers.swift"; sourceTree = "<group>"; };
D48724DA2D352591005DE483 /* SentryTraceOrigin.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryTraceOrigin.swift; sourceTree = "<group>"; };
D48724DC2D354934005DE483 /* SentrySpanOperation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentrySpanOperation.swift; sourceTree = "<group>"; };
D48724DF2D3549C6005DE483 /* SentrySpanOperationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentrySpanOperationTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -3808,6 +3808,15 @@
name = Transaction;
sourceTree = "<group>";
};
D468C0602D36699700964230 /* IO */ = {
isa = PBXGroup;
children = (
D4EDF9832D0B2A1D0071E7B3 /* Data+SentryTracing.swift */,
D468C0612D3669A200964230 /* SentryFileIOTracker+SwiftHelpers.swift */,
);
path = IO;
sourceTree = "<group>";
};
D46D45E22D5F3FD600A1CB35 /* Plans */ = {
isa = PBXGroup;
children = (
Expand All @@ -3832,15 +3841,6 @@
path = Plans;
sourceTree = "<group>";
};
D468C0602D36699700964230 /* IO */ = {
isa = PBXGroup;
children = (
D4EDF9832D0B2A1D0071E7B3 /* Data+SentryTracing.swift */,
D468C0612D3669A200964230 /* SentryFileIOTracker+SwiftHelpers.swift */,
);
path = IO;
sourceTree = "<group>";
};
D48724D92D35258A005DE483 /* Transactions */ = {
isa = PBXGroup;
children = (
Expand Down
9 changes: 4 additions & 5 deletions SentryTestUtils/TestCurrentDateProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ public class TestCurrentDateProvider: SentryCurrentDateProvider {
public var driftTimeForEveryRead = false
public var driftTimeInterval = 0.1
private var _systemUptime: TimeInterval = 0

public init() {
}

public func date() -> Date {

public func date() -> Date {
defer {
if driftTimeForEveryRead {
internalDate = internalDate.addingTimeInterval(driftTimeInterval)
Expand All @@ -28,7 +27,7 @@ public class TestCurrentDateProvider: SentryCurrentDateProvider {
setDate(date: TestCurrentDateProvider.defaultStartingDate)
internalSystemTime = 0
}

public func setDate(date: Date) {
internalDate = date
}
Expand All @@ -43,7 +42,7 @@ public class TestCurrentDateProvider: SentryCurrentDateProvider {
setDate(date: date().addingTimeInterval(nanoseconds.toTimeInterval()))
internalSystemTime += nanoseconds
}

public func advanceBy(interval: TimeInterval) {
setDate(date: date().addingTimeInterval(interval))
internalSystemTime += interval.toNanoSeconds()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@ extension SentryFileIOTracker {
guard let span = self.span(forPath: url.path, origin: origin, operation: SentrySpanOperation.fileRead) else {
return try method(url, options)
}
defer {
do {
let data = try method(url, options)
span.setData(value: data.count, key: SentrySpanDataKey.fileSize)
span.finish()
return data
} catch {
span.finish(status: .internalError)
throw error
}
let data = try method(url, options)
span.setData(value: data.count, key: SentrySpanDataKey.fileSize)
return data
}

func measureWritingData(
Expand All @@ -40,9 +43,12 @@ extension SentryFileIOTracker {
guard let span = self.span(forPath: url.path, origin: origin, operation: SentrySpanOperation.fileWrite, size: UInt(data.count)) else {
return try method(data, url, options)
}
defer {
do {
try method(data, url, options)
span.finish()
} catch {
span.finish(status: .internalError)
throw error
}
try method(data, url, options)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,24 @@ import XCTest

class DataSentryTracingIntegrationTests: XCTestCase {
private class Fixture {
let mockDateProvider: TestCurrentDateProvider = {
let provider = TestCurrentDateProvider()
provider.driftTimeForEveryRead = true
provider.driftTimeInterval = 0.25
return provider
}()

let data = "SOME DATA".data(using: .utf8)!

var fileUrlToRead: URL!
var fileUrlToWrite: URL!
var ignoredFileUrl: URL!

init() {}

func getSut(testName: String, isEnabled: Bool = true) throws -> Data {
SentryDependencyContainer.sharedInstance().dateProvider = mockDateProvider

SentrySDK.start { options in
options.dsn = TestConstants.dsnAsString(username: "DataSentryTracingIntegrationTests")
options.removeAllIntegrations()
Expand Down Expand Up @@ -86,21 +94,31 @@ class DataSentryTracingIntegrationTests: XCTestCase {

func testInitContentsOfUrlWithSentryTracing_shouldTraceManually() throws {
// -- Arrange --
let _ = try fixture.getSut(testName: self.name)
let expectedData = try fixture.getSut(testName: self.name)
let parentTransaction = try XCTUnwrap(SentrySDK.startTransaction(name: "Transaction", operation: "Test", bindToScope: true) as? SentryTracer)

// -- Act --
let refTimestamp = fixture.mockDateProvider.date()
let data = try Data(contentsOfUrlWithSentryTracing: fixture.fileUrlToRead)

// -- Assert --
XCTAssertEqual(data, fixture.data)
XCTAssertEqual(data, expectedData)

XCTAssertEqual(parentTransaction.children.count, 1)
let span = try XCTUnwrap(parentTransaction.children.first)

XCTAssertEqual(span.status, SentrySpanStatus.ok)
XCTAssertEqual(span.origin, SentryTraceOrigin.manualFileData)
XCTAssertEqual(span.operation, SentrySpanOperation.fileRead)
XCTAssertEqual(span.data["file.path"] as? String, fixture.fileUrlToRead.path)
XCTAssertEqual(span.data["file.size"] as? Int, fixture.data.count)

// As the date provider is used by multiple internal components, it is not possible to pin-point the exact timestamp.
// Therefore, we can only assert relative timestamps as the date provider uses an internal drift.
let startTimestamp = try XCTUnwrap(span.startTimestamp)
let endTimestamp = try XCTUnwrap(span.timestamp)
XCTAssertGreaterThan(startTimestamp.timeIntervalSince1970, refTimestamp.timeIntervalSince1970)
XCTAssertGreaterThan(endTimestamp.timeIntervalSince1970, startTimestamp.timeIntervalSince1970)
}

func testInitContentsOfUrlWithSentryTracing_throwsError_shouldTraceManuallyWithErrorRethrow() throws {
Expand All @@ -109,14 +127,24 @@ class DataSentryTracingIntegrationTests: XCTestCase {
let parentTransaction = try XCTUnwrap(SentrySDK.startTransaction(name: "Transaction", operation: "Test", bindToScope: true) as? SentryTracer)

// -- Act & Assert --
let refTimestamp = fixture.mockDateProvider.date()
XCTAssertThrowsError(try Data(contentsOfUrlWithSentryTracing: fixture.invalidFileUrlToRead))

XCTAssertEqual(parentTransaction.children.count, 1)
let span = try XCTUnwrap(parentTransaction.children.first)

XCTAssertEqual(span.status, SentrySpanStatus.internalError)
XCTAssertEqual(span.origin, SentryTraceOrigin.manualFileData)
XCTAssertEqual(span.operation, SentrySpanOperation.fileRead)
XCTAssertEqual(span.data["file.path"] as? String, fixture.invalidFileUrlToRead.path)
XCTAssertNil(span.data["file.size"])

// As the date provider is used by multiple internal components, it is not possible to pin-point the exact timestamp.
// Therefore, we can only assert relative timestamps as the date provider uses an internal drift.
let startTimestamp = try XCTUnwrap(span.startTimestamp)
let endTimestamp = try XCTUnwrap(span.timestamp)
XCTAssertGreaterThan(startTimestamp.timeIntervalSince1970, refTimestamp.timeIntervalSince1970)
XCTAssertGreaterThan(endTimestamp.timeIntervalSince1970, startTimestamp.timeIntervalSince1970)
}

func testInitContentsOfUrlWithSentryTracing_nonFileUrl_shouldNotTraceManually() throws {
Expand Down Expand Up @@ -179,11 +207,14 @@ class DataSentryTracingIntegrationTests: XCTestCase {
let parentTransaction = try XCTUnwrap(SentrySDK.startTransaction(name: "Transaction", operation: "Test", bindToScope: true) as? SentryTracer)

// -- Act --
let refTimestamp = fixture.mockDateProvider.date()
try sut.writeWithSentryTracing(to: fixture.fileUrlToWrite, options: .atomic)

// -- Assert --
XCTAssertEqual(parentTransaction.children.count, 1)
let span = try XCTUnwrap(parentTransaction.children.first)

XCTAssertEqual(span.status, SentrySpanStatus.ok)
XCTAssertEqual(span.origin, SentryTraceOrigin.manualFileData)
XCTAssertEqual(span.operation, SentrySpanOperation.fileWrite)
XCTAssertEqual(span.data["file.path"] as? String, fixture.fileUrlToWrite.path)
Expand All @@ -192,6 +223,13 @@ class DataSentryTracingIntegrationTests: XCTestCase {
// Reading the written data will create a span, so do it after asserting the transaction
let writtenData = try Data(contentsOf: fixture.fileUrlToWrite)
XCTAssertEqual(writtenData, fixture.data)

// As the date provider is used by multiple internal components, it is not possible to pin-point the exact timestamp.
// Therefore, we can only assert relative timestamps as the date provider uses an internal drift.
let startTimestamp = try XCTUnwrap(span.startTimestamp)
let endTimestamp = try XCTUnwrap(span.timestamp)
XCTAssertGreaterThan(startTimestamp.timeIntervalSince1970, refTimestamp.timeIntervalSince1970)
XCTAssertGreaterThan(endTimestamp.timeIntervalSince1970, startTimestamp.timeIntervalSince1970)
}

func testWriteWithSentryTracing_throwsError_shouldTraceManuallyWithErrorRethrow() throws {
Expand All @@ -200,14 +238,24 @@ class DataSentryTracingIntegrationTests: XCTestCase {
let parentTransaction = try XCTUnwrap(SentrySDK.startTransaction(name: "Transaction", operation: "Test", bindToScope: true) as? SentryTracer)

// -- Act & Assert --
let refTimestamp = fixture.mockDateProvider.date()
XCTAssertThrowsError(try sut.writeWithSentryTracing(to: fixture.invalidFileUrlToWrite))

XCTAssertEqual(parentTransaction.children.count, 1)
let span = try XCTUnwrap(parentTransaction.children.first)

XCTAssertEqual(span.status, SentrySpanStatus.internalError)
XCTAssertEqual(span.origin, SentryTraceOrigin.manualFileData)
XCTAssertEqual(span.operation, SentrySpanOperation.fileWrite)
XCTAssertEqual(span.data["file.path"] as? String, fixture.invalidFileUrlToWrite.path)
XCTAssertEqual(span.data["file.size"] as? Int, fixture.data.count)

// As the date provider is used by multiple internal components, it is not possible to pin-point the exact timestamp.
// Therefore, we can only assert relative timestamps as the date provider uses an internal drift.
let startTimestamp = try XCTUnwrap(span.startTimestamp)
let endTimestamp = try XCTUnwrap(span.timestamp)
XCTAssertGreaterThan(startTimestamp.timeIntervalSince1970, refTimestamp.timeIntervalSince1970)
XCTAssertGreaterThan(endTimestamp.timeIntervalSince1970, startTimestamp.timeIntervalSince1970)
}

func testWriteWithSentryTracing_nonFileUrl_shouldNotTraceManually() throws {
Expand Down

0 comments on commit 4efcc1c

Please sign in to comment.