Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 0 additions & 135 deletions Tests/TracingTests/TracerTests+swift57.swift

This file was deleted.

146 changes: 42 additions & 104 deletions Tests/TracingTests/TracerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@ final class TracerTests: XCTestCase {
}

func testWithSpan_success() {
guard #available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) else {
return
}
let tracer = TestTracer()

var spanEnded = false
Expand Down Expand Up @@ -91,10 +88,6 @@ final class TracerTests: XCTestCase {
}

func testWithSpan_automaticBaggagePropagation_sync() throws {
guard #available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) else {
throw XCTSkip("Task locals are not supported on this platform.")
}

let tracer = TestTracer()

var spanEnded = false
Expand All @@ -114,10 +107,6 @@ final class TracerTests: XCTestCase {
}

func testWithSpan_automaticBaggagePropagation_sync_throws() throws {
guard #available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) else {
throw XCTSkip("Task locals are not supported on this platform.")
}

let tracer = TestTracer()

var spanEnded = false
Expand All @@ -137,11 +126,7 @@ final class TracerTests: XCTestCase {
XCTFail("Should have thrown")
}

func testWithSpan_automaticBaggagePropagation_async() throws {
guard #available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) else {
throw XCTSkip("Task locals are not supported on this platform.")
}

func testWithSpan_automaticBaggagePropagation_async() async throws {
let tracer = TestTracer()

let spanEnded: LockedValueBox<Bool> = .init(false)
Expand All @@ -151,22 +136,16 @@ final class TracerTests: XCTestCase {
"world"
}

try self.testAsync {
let value = try await tracer.withAnySpan("hello") { (span: any Tracing.Span) -> String in
XCTAssertEqual(span.context.traceID, ServiceContext.current?.traceID)
return try await operation(span)
}

XCTAssertEqual(value, "world")
XCTAssertTrue(spanEnded.withValue { $0 })
let value = try await tracer.withAnySpan("hello") { (span: any Tracing.Span) -> String in
XCTAssertEqual(span.context.traceID, ServiceContext.current?.traceID)
return try await operation(span)
}
}

func testWithSpan_enterFromNonAsyncCode_passBaggage_asyncOperation() throws {
guard #available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) else {
throw XCTSkip("Task locals are not supported on this platform.")
}
XCTAssertEqual(value, "world")
XCTAssertTrue(spanEnded.withValue { $0 })
}

func testWithSpan_enterFromNonAsyncCode_passBaggage_asyncOperation() async throws {
let tracer = TestTracer()

let spanEnded: LockedValueBox<Bool> = .init(false)
Expand All @@ -176,26 +155,20 @@ final class TracerTests: XCTestCase {
"world"
}

self.testAsync {
var fromNonAsyncWorld = ServiceContext.topLevel
fromNonAsyncWorld.traceID = "1234-5678"
let value = await tracer.withAnySpan("hello", context: fromNonAsyncWorld) {
(span: any Tracing.Span) -> String in
XCTAssertEqual(span.context.traceID, ServiceContext.current?.traceID)
XCTAssertEqual(span.context.traceID, fromNonAsyncWorld.traceID)
return await operation(span)
}

XCTAssertEqual(value, "world")
XCTAssertTrue(spanEnded.withValue { $0 })
var fromNonAsyncWorld = ServiceContext.topLevel
fromNonAsyncWorld.traceID = "1234-5678"
let value = await tracer.withAnySpan("hello", context: fromNonAsyncWorld) {
(span: any Tracing.Span) -> String in
XCTAssertEqual(span.context.traceID, ServiceContext.current?.traceID)
XCTAssertEqual(span.context.traceID, fromNonAsyncWorld.traceID)
return await operation(span)
}
}

func testWithSpan_automaticBaggagePropagation_async_throws() throws {
guard #available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) else {
throw XCTSkip("Task locals are not supported on this platform.")
}
XCTAssertEqual(value, "world")
XCTAssertTrue(spanEnded.withValue { $0 })
}

func testWithSpan_automaticBaggagePropagation_async_throws() async throws {
let tracer = TestTracer()

let spanEnded: LockedValueBox<Bool> = .init(false)
Expand All @@ -205,23 +178,17 @@ final class TracerTests: XCTestCase {
throw ExampleSpanError()
}

self.testAsync {
do {
_ = try await tracer.withAnySpan("hello", operation)
} catch {
XCTAssertTrue(spanEnded.withValue { $0 })
XCTAssertEqual(error as? ExampleSpanError, ExampleSpanError())
return
}
XCTFail("Should have thrown")
do {
_ = try await tracer.withAnySpan("hello", operation)
} catch {
XCTAssertTrue(spanEnded.withValue { $0 })
XCTAssertEqual(error as? ExampleSpanError, ExampleSpanError())
return
}
XCTFail("Should have thrown")
}

func test_static_Tracer_withSpan_automaticBaggagePropagation_async_throws() throws {
guard #available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) else {
throw XCTSkip("Task locals are not supported on this platform.")
}

func test_static_Tracer_withSpan_automaticBaggagePropagation_async_throws() async throws {
let tracer = TestTracer()

let spanEnded: LockedValueBox<Bool> = .init(false)
Expand All @@ -231,23 +198,17 @@ final class TracerTests: XCTestCase {
throw ExampleSpanError()
}

self.testAsync {
do {
_ = try await tracer.withSpan("hello", operation)
} catch {
XCTAssertTrue(spanEnded.withValue { $0 })
XCTAssertEqual(error as? ExampleSpanError, ExampleSpanError())
return
}
XCTFail("Should have thrown")
do {
_ = try await tracer.withSpan("hello", operation)
} catch {
XCTAssertTrue(spanEnded.withValue { $0 })
XCTAssertEqual(error as? ExampleSpanError, ExampleSpanError())
return
}
XCTFail("Should have thrown")
}

func test_static_Tracer_withSpan_automaticBaggagePropagation_throws() throws {
guard #available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) else {
throw XCTSkip("Task locals are not supported on this platform.")
}

func test_static_Tracer_withSpan_automaticBaggagePropagation_throws() async throws {
let tracer = TestTracer()

let spanEnded: LockedValueBox<Bool> = .init(false)
Expand All @@ -257,23 +218,17 @@ final class TracerTests: XCTestCase {
throw ExampleSpanError()
}

self.testAsync {
do {
_ = try await tracer.withSpan("hello", operation)
} catch {
XCTAssertTrue(spanEnded.withValue { $0 })
XCTAssertEqual(error as? ExampleSpanError, ExampleSpanError())
return
}
XCTFail("Should have thrown")
do {
_ = try await tracer.withSpan("hello", operation)
} catch {
XCTAssertTrue(spanEnded.withValue { $0 })
XCTAssertEqual(error as? ExampleSpanError, ExampleSpanError())
return
}
XCTFail("Should have thrown")
}

func testWithSpan_recordErrorWithAttributes() throws {
guard #available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) else {
throw XCTSkip("Task locals are not supported on this platform.")
}

let tracer = TestTracer()

var endedSpan: TestSpan?
Expand Down Expand Up @@ -318,23 +273,6 @@ final class TracerTests: XCTestCase {
let span = tracer.spans.first!
XCTAssertEqual(span.startTimestampNanosSinceEpoch, instant.nanosecondsSinceEpoch)
}

// @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
/// Helper method to execute async operations until we can use async tests (currently incompatible with the generated LinuxMain file).
/// - Parameter operation: The operation to test.
func testAsync(_ operation: @Sendable @escaping () async throws -> Void) rethrows {
let group = DispatchGroup()
group.enter()
Task.detached {
do {
try await operation()
} catch {
throw error
}
group.leave()
}
group.wait()
}
}

struct ExampleSpanError: Error, Equatable {}
Expand Down