-
Notifications
You must be signed in to change notification settings - Fork 134
/
Copy pathTracingFeatureTests.swift
134 lines (118 loc) · 5.62 KB
/
TracingFeatureTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
* This product includes software developed at Datadog (https://www.datadoghq.com/).
* Copyright 2019-2020 Datadog, Inc.
*/
import XCTest
@testable import Datadog
class TracingFeatureTests: XCTestCase {
override func setUp() {
super.setUp()
XCTAssertNil(Datadog.instance)
XCTAssertNil(TracingFeature.instance)
temporaryFeatureDirectories.create()
}
override func tearDown() {
XCTAssertNil(Datadog.instance)
XCTAssertNil(TracingFeature.instance)
temporaryFeatureDirectories.delete()
super.tearDown()
}
// MARK: - HTTP Message
func testItUsesExpectedHTTPMessage() throws {
let randomApplicationName: String = .mockRandom(among: .alphanumerics)
let randomApplicationVersion: String = .mockRandom()
let randomSource: String = .mockRandom()
let randomOrigin: String = .mockRandom()
let randomSDKVersion: String = .mockRandom(among: .alphanumerics)
let randomUploadURL: URL = .mockRandom()
let randomClientToken: String = .mockRandom()
let randomDeviceModel: String = .mockRandom()
let randomDeviceOSName: String = .mockRandom()
let randomDeviceOSVersion: String = .mockRandom()
let server = ServerMock(delivery: .success(response: .mockResponseWith(statusCode: 200)))
// Given
TracingFeature.instance = .mockWith(
directories: temporaryFeatureDirectories,
configuration: .mockWith(
common: .mockWith(
applicationName: randomApplicationName,
applicationVersion: randomApplicationVersion,
source: randomSource,
origin: randomOrigin,
sdkVersion: randomSDKVersion
),
uploadURL: randomUploadURL,
clientToken: randomClientToken
),
dependencies: .mockWith(
mobileDevice: .mockWith(model: randomDeviceModel, osName: randomDeviceOSName, osVersion: randomDeviceOSVersion)
)
)
defer { TracingFeature.instance?.deinitialize() }
// When
let tracer = Tracer.initialize(configuration: .init()).dd
let span = tracer.startSpan(operationName: .mockAny())
span.finish()
// Then
let request = server.waitAndReturnRequests(count: 1)[0]
let requestURL = try XCTUnwrap(request.url)
XCTAssertEqual(request.httpMethod, "POST")
XCTAssertEqual(requestURL.absoluteString, randomUploadURL.absoluteString)
XCTAssertNil(requestURL.query)
XCTAssertEqual(
request.allHTTPHeaderFields?["User-Agent"],
"""
\(randomApplicationName)/\(randomApplicationVersion) CFNetwork (\(randomDeviceModel); \(randomDeviceOSName)/\(randomDeviceOSVersion))
"""
)
XCTAssertEqual(request.allHTTPHeaderFields?["Content-Type"], "text/plain;charset=UTF-8")
XCTAssertEqual(request.allHTTPHeaderFields?["Content-Encoding"], "deflate")
XCTAssertEqual(request.allHTTPHeaderFields?["DD-API-KEY"], randomClientToken)
XCTAssertEqual(request.allHTTPHeaderFields?["DD-EVP-ORIGIN"], randomOrigin)
XCTAssertEqual(request.allHTTPHeaderFields?["DD-EVP-ORIGIN-VERSION"], randomSDKVersion)
XCTAssertEqual(request.allHTTPHeaderFields?["DD-REQUEST-ID"]?.matches(regex: .uuidRegex), true)
}
// MARK: - HTTP Payload
func testItUsesExpectedPayloadFormatForUploads() throws {
let server = ServerMock(delivery: .success(response: .mockResponseWith(statusCode: 200)))
TracingFeature.instance = .mockWith(
directories: temporaryFeatureDirectories,
dependencies: .mockWith(
performance: .combining(
storagePerformance: StoragePerformanceMock(
maxFileSize: .max,
maxDirectorySize: .max,
maxFileAgeForWrite: .distantFuture, // write all spans to single file,
minFileAgeForRead: StoragePerformanceMock.readAllFiles.minFileAgeForRead,
maxFileAgeForRead: StoragePerformanceMock.readAllFiles.maxFileAgeForRead,
maxObjectsInFile: 3, // write 3 spans to payload,
maxObjectSize: .max
),
uploadPerformance: UploadPerformanceMock(
initialUploadDelay: 0.5, // wait enough until spans are written,
minUploadDelay: 1,
maxUploadDelay: 1,
uploadDelayChangeRate: 0
)
)
)
)
defer { TracingFeature.instance?.deinitialize() }
let tracer = Tracer.initialize(configuration: .init()).dd
tracer.startSpan(operationName: "operation 1").finish()
tracer.startSpan(operationName: "operation 2").finish()
tracer.startSpan(operationName: "operation 3").finish()
let payload = try XCTUnwrap(server.waitAndReturnRequests(count: 1)[0].httpBody)
// Expected payload format:
// ```
// span1JSON
// span2JSON
// span3JSON
// ```
let spanMatchers = try SpanMatcher.fromNewlineSeparatedJSONObjectsData(payload)
XCTAssertEqual(try spanMatchers[0].operationName(), "operation 1")
XCTAssertEqual(try spanMatchers[1].operationName(), "operation 2")
XCTAssertEqual(try spanMatchers[2].operationName(), "operation 3")
}
}