diff --git a/Tests/TracingTests/TracerTests+swift57.swift b/Tests/TracingTests/TracerTests+swift57.swift deleted file mode 100644 index 0d2e622..0000000 --- a/Tests/TracingTests/TracerTests+swift57.swift +++ /dev/null @@ -1,135 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// This source file is part of the Swift Distributed Tracing open source project -// -// Copyright (c) 2020-2023 Apple Inc. and the Swift Distributed Tracing project authors -// Licensed under Apache License v2.0 -// -// See LICENSE.txt for license information -// See CONTRIBUTORS.txt for the list of Swift Distributed Tracing project authors -// -// SPDX-License-Identifier: Apache-2.0 -// -//===----------------------------------------------------------------------===// - -import Instrumentation -import ServiceContextModule -import Tracing -import XCTest - -// Specifically make sure we don't have to implement startAnySpan - -final class SampleSwift57Tracer: Tracer { - private(set) var spans = [SampleSwift57Span]() - var onEndSpan: (SampleSwift57Span) -> Void = { _ in } - - func startSpan( - _ operationName: String, - context: @autoclosure () -> ServiceContext, - ofKind kind: SpanKind, - at instant: @autoclosure () -> Instant, - function: String, - file fileID: String, - line: UInt - ) -> SampleSwift57Span { - let span = SampleSwift57Span( - operationName: operationName, - startTime: instant(), - context: context(), - kind: kind, - onEnd: self.onEndSpan - ) - self.spans.append(span) - return span - } - - public func forceFlush() {} - - func extract(_ carrier: Carrier, into context: inout ServiceContext, using extractor: Extract) - where - Extract: Extractor, - Carrier == Extract.Carrier - {} - - func inject(_ context: ServiceContext, into carrier: inout Carrier, using injector: Inject) - where - Inject: Injector, - Carrier == Inject.Carrier - {} -} - -/// Only intended to be used in single-threaded SampleSwift57ing. -final class SampleSwift57Span: Span { - private let kind: SpanKind - - private var status: SpanStatus? - - public let startTimeNanoseconds: UInt64 - public private(set) var endTimeNanoseconds: UInt64? - - private(set) var recordedErrors: [(Error, SpanAttributes)] = [] - - var operationName: String - let context: ServiceContext - - private(set) var events = [SpanEvent]() { - didSet { - self.isRecording = !self.events.isEmpty - } - } - - private(set) var links = [SpanLink]() - - var attributes: SpanAttributes = [:] { - didSet { - self.isRecording = !self.attributes.isEmpty - } - } - - private(set) var isRecording = false - - let onEnd: (SampleSwift57Span) -> Void - - init( - operationName: String, - startTime: Instant, - context: ServiceContext, - kind: SpanKind, - onEnd: @escaping (SampleSwift57Span) -> Void - ) { - self.operationName = operationName - self.startTimeNanoseconds = startTime.nanosecondsSinceEpoch - self.context = context - self.onEnd = onEnd - self.kind = kind - } - - func setStatus(_ status: SpanStatus) { - self.status = status - self.isRecording = true - } - - func addLink(_ link: SpanLink) { - self.links.append(link) - } - - func addEvent(_ event: SpanEvent) { - self.events.append(event) - } - - func recordError( - _ error: Error, - attributes: SpanAttributes, - at instant: @autoclosure () -> Instant - ) { - self.recordedErrors.append((error, attributes)) - } - - func end(at instant: @autoclosure () -> Instant) { - self.endTimeNanoseconds = instant().nanosecondsSinceEpoch - self.onEnd(self) - } -} - -extension SampleSwift57Tracer: @unchecked Sendable {} // only intended for single threaded SampleSwift57ing -extension SampleSwift57Span: @unchecked Sendable {} // only intended for single threaded SampleSwift57ing diff --git a/Tests/TracingTests/TracerTests.swift b/Tests/TracingTests/TracerTests.swift index b76a4aa..bd8eba8 100644 --- a/Tests/TracingTests/TracerTests.swift +++ b/Tests/TracingTests/TracerTests.swift @@ -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 @@ -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 @@ -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 @@ -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 = .init(false) @@ -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 = .init(false) @@ -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 = .init(false) @@ -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 = .init(false) @@ -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 = .init(false) @@ -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? @@ -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 {}