Skip to content

Commit

Permalink
Adding missing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
NachoEmbrace committed Oct 10, 2024
1 parent 1758cff commit c0a5f08
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Sources/EmbraceCore/Payload/Spans/SpanPayload.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ struct SpanPayload: Encodable {
self.spanId = span.spanId.hexString
self.parentSpanId = span.parentSpanId?.hexString
self.name = span.name
self.status = failed ? Status.sessionCrashedError().name : span.status.name
self.status = span.status == .ok ? span.status.name : (failed ? Status.sessionCrashedError().name : span.status.name)
self.startTime = span.startTime.nanosecondsSince1970Truncated
self.events = span.events.map { SpanEventPayload(from: $0) }
self.links = span.links.map { SpanLinkPayload(from: $0) }
Expand Down
28 changes: 28 additions & 0 deletions Tests/EmbraceCoreTests/Payload/SpansPayloadBuilderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ final class SpansPayloadBuilderTests: XCTestCase {
name: name ?? "test-span",
kind: .internal,
startTime: startTime,
status: endTime == nil ? .unset : .ok,
endTime: endTime ?? Date(),
hasEnded: endTime != nil
)
Expand Down Expand Up @@ -266,6 +267,33 @@ final class SpansPayloadBuilderTests: XCTestCase {
XCTAssertEqual(attribute!.value, "failure")
}

func test_closedSpan_withinCrashedSession() throws {
// given a crashed session
sessionRecord.crashReportId = "test"

// given a open span that started after the crashed session
let span = try addSpan(
startTime: Date(timeIntervalSince1970: 55),
endTime: Date(timeIntervalSince1970: 60)
)
let payload = SpanPayload(from: span, endTime: Date(timeIntervalSince1970: 60), failed: false)

// when building the spans payload
let (closed, open) = SpansPayloadBuilder.build(for: sessionRecord, storage: storage)

// then the spans are retrieved correctly
XCTAssertEqual(closed.count, 2)
XCTAssertEqual(closed[0].name, "emb-session") // session span always first
XCTAssertEqual(closed[0].status, "error")
XCTAssertEqual(closed[1], payload)
XCTAssertEqual(closed[1].status, "ok")
XCTAssertEqual(open.count, 0)

// then the error code attribute was added
let attribute = closed[1].attributes.first { $0.key == "emb.error_code" }
XCTAssertNil(attribute)
}

func test_hardLimit() throws {
// given more than 1000 spans
for _ in 1...1100 {
Expand Down
42 changes: 41 additions & 1 deletion Tests/EmbraceCoreTests/Session/SessionSpanUtilsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ final class SessionSpanUtilsTests: XCTestCase {
XCTAssertEqual(payload.traceId, TestConstants.traceId)
XCTAssertEqual(payload.spanId, TestConstants.spanId)
XCTAssertNil(payload.parentSpanId)
XCTAssertEqual(payload.status, Status.ok.name)
XCTAssertEqual(payload.status, "error")
XCTAssertEqual(payload.startTime, TestConstants.date.nanosecondsSince1970Truncated)
XCTAssertEqual(payload.endTime, endTime.nanosecondsSince1970Truncated)
XCTAssertEqual(payload.events.count, 0)
Expand Down Expand Up @@ -201,6 +201,46 @@ final class SessionSpanUtilsTests: XCTestCase {
XCTAssertEqual(crashIdAttribute!.value, "test")
}

func test_status() {
// test ok status
var session = SessionRecord(
id: TestConstants.sessionId,
state: .foreground,
processId: TestConstants.processId,
traceId: TestConstants.traceId,
spanId: TestConstants.spanId,
startTime: TestConstants.date,
endTime: Date(),
lastHeartbeatTime: Date(),
crashReportId: nil,
coldStart: true,
cleanExit: false,
appTerminated: true
)

var payload = SessionSpanUtils.payload(from: session, sessionNumber: 100)
XCTAssertEqual(payload.status, "ok")

// test error status
session = SessionRecord(
id: TestConstants.sessionId,
state: .foreground,
processId: TestConstants.processId,
traceId: TestConstants.traceId,
spanId: TestConstants.spanId,
startTime: TestConstants.date,
endTime: Date(),
lastHeartbeatTime: Date(),
crashReportId: "test",
coldStart: true,
cleanExit: false,
appTerminated: true
)

payload = SessionSpanUtils.payload(from: session, sessionNumber: 100)
XCTAssertEqual(payload.status, "error")
}

func test_payloadFromSession_shouldAddCustomePropertiesWithPrefixedKey() {
let session = givenSessionRecord()

Expand Down

0 comments on commit c0a5f08

Please sign in to comment.