Skip to content

Commit

Permalink
[CIVIS-9813] implemented channel name fix (#101)
Browse files Browse the repository at this point in the history
* [CIVIS-9813] implemented channel name fix
* missing environment key to pass to UI tests
  • Loading branch information
ypopovych authored May 14, 2024
1 parent 91583ed commit 29082e5
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 25 deletions.
5 changes: 5 additions & 0 deletions Sources/DatadogSDKTesting/Config.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ final class Config {
var tracerTraceId: String? = nil
var tracerSpanId: String? = nil

/// UUID for message channels between App and tests
var messageChannelUUID: String? = nil

/// The framework has been launched with extra debug information
var extraDebug: Bool = false
var extraDebugCallStack: Bool = false
Expand Down Expand Up @@ -126,6 +129,8 @@ final class Config {
tracerTraceId = env[.tracerTraceId]
tracerSpanId = env[.tracerSpanId]

messageChannelUUID = env[.messageChannelUUID]

endpoint = env[.site] ?? env[.endpoint]
disableTracesExporting = env[.dontExport] ?? false
reportHostname = env[.ciVisibilityReportHostname] ?? false
Expand Down
41 changes: 32 additions & 9 deletions Sources/DatadogSDKTesting/DDTestMonitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ internal class DDTestMonitor {
var launchNotificationObserver: NSObjectProtocol?
var didBecomeActiveNotificationObserver: NSObjectProtocol?
var isRumActive: Bool = false
let messageChannelUUID: String

var crashedModuleInfo: CrashedModuleInformation?

Expand Down Expand Up @@ -92,6 +93,7 @@ internal class DDTestMonitor {
}

private var isGitUploadSucceded: Bool = false
private var serverTestingPort: CFMessagePort? = nil

static func installTestMonitor() -> Bool {
guard DDTestMonitor.config.apiKey != nil else {
Expand All @@ -118,6 +120,8 @@ internal class DDTestMonitor {
}

init() {
messageChannelUUID = DDTestMonitor.config.messageChannelUUID ?? UUID().uuidString

if DDTestMonitor.config.isBinaryUnderUITesting {
launchNotificationObserver = NotificationCenter.default.addObserver(
forName: launchNotificationName,
Expand All @@ -143,16 +147,18 @@ internal class DDTestMonitor {
#if os(iOS)
data[DDUISettingsTags.uiSettingsOrientation] = PlatformUtils.getOrientation()
#endif
let encoded = try? JSONSerialization.data(withJSONObject: data)
let timeout: CFTimeInterval = 1.0
let remotePort = CFMessagePortCreateRemote(nil, "DatadogTestingPort" as CFString)
if remotePort == nil {
guard let port = self.testingPort else {
Log.debug("DatadogTestingPort CFMessagePortCreateRemote failed")
return
}
let status = CFMessagePortSendRequest(remotePort,
guard let encoded = try? JSONSerialization.data(withJSONObject: data) else {
Log.debug("Json encoding failed for: \(data)")
return
}
let timeout: CFTimeInterval = 1.0
let status = CFMessagePortSendRequest(port,
DDCFMessageID.setCustomTags,
encoded as CFData?,
encoded as CFData,
timeout,
timeout,
nil,
Expand Down Expand Up @@ -349,8 +355,22 @@ internal class DDTestMonitor {
func stopStderrCapture() {
StderrCapture.stopCapturing()
}

var rumPort: CFMessagePort? {
guard isRumActive else { return nil }
return CFMessagePortCreateRemote(nil, "DatadogRUMTestingPort-\(messageChannelUUID)" as CFString)
}

private var testingPort: CFMessagePort? {
CFMessagePortCreateRemote(nil, "DatadogTestingPort-\(messageChannelUUID)" as CFString)
}

func startAttributeListener() {
rLock.lock()
defer { rLock.unlock() }

guard serverTestingPort == nil else { return }

func attributeCallback(port: CFMessagePort?, msgid: Int32, data: CFData?, info: UnsafeMutableRawPointer?) -> Unmanaged<CFData>? {
switch msgid {
case DDCFMessageID.setCustomTags:
Expand All @@ -373,12 +393,15 @@ internal class DDTestMonitor {
return nil
}

let port = CFMessagePortCreateLocal(nil, "DatadogTestingPort" as CFString, attributeCallback, nil, nil)
if port == nil {
serverTestingPort = CFMessagePortCreateLocal(
nil, "DatadogTestingPort-\(messageChannelUUID)" as CFString,
attributeCallback, nil, nil
)
guard let port = serverTestingPort else {
Log.debug("DatadogTestingPort CFMessagePortCreateLocal failed")
return
}
let runLoopSource = CFMessagePortCreateRunLoopSource(nil, port, 0)
CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, CFRunLoopMode.commonModes)
CFRunLoopAddSource(CFRunLoopGetMain(), runLoopSource, CFRunLoopMode.commonModes)
}
}
10 changes: 4 additions & 6 deletions Sources/DatadogSDKTesting/DDTracer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -307,12 +307,10 @@ internal class DDTracer {
}

func flush() {
if DDTestMonitor.instance?.isRumActive ?? false,
let remotePort = CFMessagePortCreateRemote(nil, "DatadogRUMTestingPort" as CFString)
{
if let rumPort = DDTestMonitor.instance?.rumPort {
let timeout: CFTimeInterval = 10.0
let status = CFMessagePortSendRequest(
remotePort,
rumPort,
DDCFMessageID.forceFlush, // Message ID for asking RUM to flush all data
nil,
timeout,
Expand All @@ -326,7 +324,7 @@ internal class DDTracer {
Log.debug("CFMessagePortCreateRemote request to DatadogRUMTestingPort failed")
}
}

self.tracerProviderSdk.forceFlush()
Log.debug("Tracer flush finished")
}
Expand Down Expand Up @@ -385,7 +383,7 @@ internal class DDTracer {
EnvironmentContextPropagator().inject(spanContext: propagationContext, carrier: &headers, setter: HeaderSetter())
if !DDTestMonitor.config.disableRUMIntegration {
headers.merge(datadogHeaders(forContext: propagationContext)) { current, _ in current }
headers["CI_VISIBILITY_TEST_EXECUTION_ID"] = String(propagationContext.traceId.rawLowerLong)
headers[EnvironmentKey.testExecutionId.rawValue] = String(propagationContext.traceId.rawLowerLong)
}
return headers
}
Expand Down
6 changes: 4 additions & 2 deletions Sources/DatadogSDKTesting/Environment/EnvironmentKeys.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ internal enum EnvironmentKey: String, CaseIterable {
case testOutputFile = "TEST_OUTPUT_FILE"
case tracerTraceId = "ENVIRONMENT_TRACER_TRACEID"
case tracerSpanId = "ENVIRONMENT_TRACER_SPANID"
case messageChannelUUID = "CI_VISIBILITY_MESSAGE_CHANNEL_UUID"
case testExecutionId = "CI_VISIBILITY_TEST_EXECUTION_ID"
}

extension EnvironmentKey {
Expand All @@ -58,8 +60,8 @@ extension EnvironmentKey {
.instrumentationExtraHeaders, .excludedURLs, .enableRecordPayload, disableNetworkCallStack,
.enableNetworkCallStackSymbolicated, .disableRumIntegration, .maxPayloadSize,
.enableCiVisibilityLogs, .enableStdoutInstrumentation, .enableStderrInstrumentation,
.disableSdkIosIntegration, .disableCrashHandler, .site, .endpoint, .dontExport, .traceDebug,
.traceDebugCallStack, .disableNTPClock]
.disableSdkIosIntegration, .disableCrashHandler, .disableMachCrashHandler,
.site, .endpoint, .dontExport, .traceDebug, .traceDebugCallStack, .disableNTPClock]
}
}

Expand Down
14 changes: 8 additions & 6 deletions Sources/DatadogSDKTesting/Utils/XCUIApplicationSwizzler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ extension XCUIApplication {
func swizzled_launch() {
DDTestMonitor.instance?.currentTest?.setIsUITest(true)
if let testSpanContext = DDTracer.activeSpan?.context {
self.launchEnvironment["ENVIRONMENT_TRACER_SPANID"] = testSpanContext.spanId.hexString
self.launchEnvironment["ENVIRONMENT_TRACER_TRACEID"] = testSpanContext.traceId.hexString
addPropagationsHeadersToEnvironment(tracer: DDTestMonitor.tracer)
for value in EnvironmentKey.childKeys {
addProcessEnvironmentToLaunch(value.rawValue)
}
self.launchEnvironment[EnvironmentKey.tracerSpanId.rawValue] = testSpanContext.spanId.hexString
self.launchEnvironment[EnvironmentKey.tracerTraceId.rawValue] = testSpanContext.traceId.hexString
}
addPropagationsHeadersToEnvironment(tracer: DDTestMonitor.tracer)
for value in EnvironmentKey.childKeys {
addProcessEnvironmentToLaunch(value.rawValue)
}
self.launchEnvironment[EnvironmentKey.messageChannelUUID.rawValue] =
DDTestMonitor.instance?.messageChannelUUID
DDTestMonitor.instance?.startAttributeListener()
swizzled_launch()
}
Expand Down
5 changes: 3 additions & 2 deletions Tests/DatadogSDKTesting/DDTracerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,9 @@ class DDTracerTests: XCTestCase {
XCTAssertEqual(environmentValues[DDHeaders.traceIDField.rawValue], String(spanData.traceId.rawLowerLong))
XCTAssertNotNil(environmentValues[DDHeaders.parentSpanIDField.rawValue])
XCTAssertEqual(environmentValues[DDHeaders.parentSpanIDField.rawValue], String(spanData.spanId.rawValue))
XCTAssertNotNil(environmentValues["CI_VISIBILITY_TEST_EXECUTION_ID"])
XCTAssertEqual(environmentValues["CI_VISIBILITY_TEST_EXECUTION_ID"], String(spanData.traceId.rawLowerLong))
XCTAssertNotNil(environmentValues[EnvironmentKey.testExecutionId.rawValue])
XCTAssertEqual(environmentValues[EnvironmentKey.testExecutionId.rawValue],
String(spanData.traceId.rawLowerLong))

span.end()
}
Expand Down

0 comments on commit 29082e5

Please sign in to comment.