Skip to content

Commit af4069a

Browse files
kukushechkinktoso
andauthored
Avoid testing global InstrumentationSystem state (#196)
Co-authored-by: Konrad `ktoso` Malawski <[email protected]>
1 parent b912ed7 commit af4069a

10 files changed

+222
-219
lines changed

Tests/InstrumentationTests/InstrumentationSystemTests.swift

Lines changed: 0 additions & 94 deletions
This file was deleted.

Tests/TracingTests/ActorTracingTests.swift

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,8 @@ import XCTest
1818

1919
@testable import Instrumentation
2020

21+
/// This is a compile-time test
2122
final class ActorTracingTests: XCTestCase {
22-
override class func tearDown() {
23-
super.tearDown()
24-
InstrumentationSystem.bootstrapInternal(nil)
25-
}
26-
2723
func test() {}
2824
}
2925

Tests/TracingTests/DynamicTracepointTracerTests.swift

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,9 @@ import XCTest
1919
@testable import Instrumentation
2020

2121
final class DynamicTracepointTracerTests: XCTestCase {
22-
override class func tearDown() {
23-
super.tearDown()
24-
InstrumentationSystem.bootstrapInternal(nil)
25-
}
26-
2722
func test_adhoc_enableBySourceLoc() {
2823
let tracer = DynamicTracepointTestTracer()
2924

30-
InstrumentationSystem.bootstrapInternal(tracer)
31-
defer {
32-
InstrumentationSystem.bootstrapInternal(NoOpTracer())
33-
}
34-
3525
let fileID = #fileID
3626
let fakeLine: UInt = 77 // trick number, see withSpan below.
3727
let fakeNextLine: UInt = fakeLine + 11
@@ -71,19 +61,14 @@ final class DynamicTracepointTracerTests: XCTestCase {
7161
func test_adhoc_enableByFunction() {
7262
let tracer = DynamicTracepointTestTracer()
7363

74-
InstrumentationSystem.bootstrapInternal(tracer)
75-
defer {
76-
InstrumentationSystem.bootstrapInternal(NoOpTracer())
77-
}
78-
7964
let fileID = #fileID
80-
tracer.enableTracepoint(function: "traceMeLogic(fakeLine:)")
65+
tracer.enableTracepoint(function: "traceMeLogic(fakeLine:tracer:)")
8166

8267
let fakeLine: UInt = 66
8368
let fakeNextLine: UInt = fakeLine + 11
8469

85-
self.logic(fakeLine: 55)
86-
self.traceMeLogic(fakeLine: fakeLine)
70+
self.logic(fakeLine: 55, tracer: tracer)
71+
self.traceMeLogic(fakeLine: fakeLine, tracer: tracer)
8772

8873
XCTAssertEqual(tracer.spans.count, 2)
8974
for span in tracer.spans {
@@ -93,15 +78,15 @@ final class DynamicTracepointTracerTests: XCTestCase {
9378
XCTAssertEqual(tracer.spans[1].context.spanID, "span-id-fake-\(fileID)-\(fakeNextLine)")
9479
}
9580

96-
func logic(fakeLine: UInt) {
97-
InstrumentationSystem.tracer.withSpan("\(#function)-dont", line: fakeLine) { _ in
81+
func logic(fakeLine: UInt, tracer: any Tracer) {
82+
tracer.withSpan("\(#function)-dont", line: fakeLine) { _ in
9883
// inside
9984
}
10085
}
10186

102-
func traceMeLogic(fakeLine: UInt) {
103-
InstrumentationSystem.tracer.withSpan("\(#function)-yes", line: fakeLine) { _ in
104-
InstrumentationSystem.tracer.withSpan("\(#function)-yes-inside", line: fakeLine + 11) { _ in
87+
func traceMeLogic(fakeLine: UInt, tracer: any Tracer) {
88+
tracer.withSpan("\(#function)-yes", line: fakeLine) { _ in
89+
tracer.withSpan("\(#function)-yes-inside", line: fakeLine + 11) { _ in
10590
// inside
10691
}
10792
}

Tests/TracingTests/SpanTests.swift

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ final class SpanTests: XCTestCase {
8282
}
8383

8484
func testSpanAttributeIsExpressibleByArrayLiteral() {
85-
let s = InstrumentationSystem.legacyTracer.startAnySpan("", context: .topLevel)
85+
let tracer = TestTracer()
86+
let s = tracer.startAnySpan("", context: .topLevel)
8687
s.attributes["hi"] = [42, 21]
8788
s.attributes["hi"] = [42.10, 21.0]
8889
s.attributes["hi"] = [true, false]
@@ -91,12 +92,8 @@ final class SpanTests: XCTestCase {
9192
}
9293

9394
func testSpanAttributeSetEntireCollection() {
94-
InstrumentationSystem.bootstrapInternal(TestTracer())
95-
defer {
96-
InstrumentationSystem.bootstrapInternal(NoOpTracer())
97-
}
98-
99-
let s = InstrumentationSystem.legacyTracer.startAnySpan("", context: .topLevel)
95+
let tracer = TestTracer()
96+
let s = tracer.startAnySpan("", context: .topLevel)
10097
var attrs = s.attributes
10198
attrs["one"] = 42
10299
attrs["two"] = [1, 2, 34]

Tests/TracingTests/TestTracer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ extension ServiceContext {
118118

119119
/// Only intended to be used in single-threaded testing.
120120
final class TestSpan: Span {
121-
private let kind: SpanKind
121+
let kind: SpanKind
122122

123123
private var status: SpanStatus?
124124

Tests/TracingTests/TracedLock.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ final class TracedLock: @unchecked Sendable {
3030
self.underlyingLock = NSLock()
3131
}
3232

33-
func lock(context: ServiceContext) {
33+
func lock(context: ServiceContext, tracer: any Tracer) {
3434
// time here
3535
self.underlyingLock.lock()
36-
self.activeSpan = InstrumentationSystem.legacyTracer.startAnySpan(self.name, context: context)
36+
self.activeSpan = tracer.startSpan(self.name, context: context)
3737
}
3838

3939
func unlock(context: ServiceContext) {
@@ -42,8 +42,8 @@ final class TracedLock: @unchecked Sendable {
4242
self.underlyingLock.unlock()
4343
}
4444

45-
func withLock(context: ServiceContext, _ closure: () -> Void) {
46-
self.lock(context: context)
45+
func withLock(context: ServiceContext, tracer: any Tracer, _ closure: () -> Void) {
46+
self.lock(context: context, tracer: tracer)
4747
defer { self.unlock(context: context) }
4848
closure()
4949
}

Tests/TracingTests/TracedLockTests.swift

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,16 @@ import XCTest
1919
@testable import Instrumentation
2020

2121
final class TracedLockTests: XCTestCase {
22-
override class func tearDown() {
23-
super.tearDown()
24-
InstrumentationSystem.bootstrapInternal(nil)
25-
}
26-
2722
func test_tracesLockedTime() {
2823
let tracer = TracedLockPrintlnTracer()
29-
InstrumentationSystem.bootstrapInternal(tracer)
30-
3124
let lock = TracedLock(name: "my-cool-lock")
3225

3326
func launchTask(_ name: String) {
3427
DispatchQueue.global().async {
3528
var context = ServiceContext.topLevel
3629
context[TaskIDKey.self] = name
3730

38-
lock.lock(context: context)
31+
lock.lock(context: context, tracer: tracer)
3932
lock.unlock(context: context)
4033
}
4134
}

0 commit comments

Comments
 (0)