Skip to content

Commit

Permalink
RUM-1040 Stop core instance
Browse files Browse the repository at this point in the history
  • Loading branch information
maxep committed Jan 8, 2024
1 parent d5d9967 commit 04f24d0
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 10 deletions.
7 changes: 6 additions & 1 deletion DatadogCore/Sources/Core/DatadogCore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,12 @@ internal final class DatadogCore {
allUploads.forEach { $0.flushAndTearDown() }
allStorages.forEach { $0.setIgnoreFilesAgeWhenReading(to: false) }

// Deallocate all Features and their storage & upload units:
stop()
}

/// Stops all processes for this instance of the Datadog core by
/// deallocating all Features and their storage & upload units.
func stop() {
stores = [:]
features = [:]
}
Expand Down
17 changes: 11 additions & 6 deletions DatadogCore/Sources/Datadog.swift
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,14 @@ public enum Datadog {
core?.clearAllData()
}

/// Stops the initialized SDK instance attached to the given name.
///
/// - Parameter instanceName: The core instance name
public static func stopInstance(named instanceName: String = CoreRegistry.defaultInstanceName) {
let core = CoreRegistry.unregisterInstance(named: instanceName) as? DatadogCore
core?.stop()
}

/// Initializes the Datadog SDK.
///
/// You **must** initialize the core instance of the Datadog SDK prior to enabling any Product.
Expand Down Expand Up @@ -489,13 +497,10 @@ public enum Datadog {
#endif

internal static func internalFlushAndDeinitialize(instanceName: String = CoreRegistry.defaultInstanceName) {
assert(CoreRegistry.instance(named: instanceName) is DatadogCore, "SDK must be first initialized.")

// Unregister core instance:
let core = CoreRegistry.unregisterInstance(named: instanceName) as? DatadogCore
// Flush and tear down SDK core:
(CoreRegistry.instance(named: instanceName) as? DatadogCore)?.flushAndTearDown()

// Deinitialize `Datadog`:
CoreRegistry.unregisterInstance(named: instanceName)
core?.flushAndTearDown()
}
}

Expand Down
39 changes: 36 additions & 3 deletions DatadogCore/Tests/Datadog/DatadogCore/DatadogCoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ class DatadogCoreTests: XCTestCase {
maxBatchesPerUpload: .mockRandom(min: 1, max: 100),
backgroundTasksEnabled: .mockAny()
)
defer { core.flushAndTearDown() }

let requestBuilderSpy = FeatureRequestBuilderSpy()
try core.register(feature: FeatureMock(requestBuilder: requestBuilderSpy))
Expand Down Expand Up @@ -93,7 +92,6 @@ class DatadogCoreTests: XCTestCase {
maxBatchesPerUpload: .mockRandom(min: 1, max: 100),
backgroundTasksEnabled: .mockAny()
)
defer { core.flushAndTearDown() }

let requestBuilderSpy = FeatureRequestBuilderSpy()
try core.register(feature: FeatureMock(requestBuilder: requestBuilderSpy))
Expand Down Expand Up @@ -148,7 +146,6 @@ class DatadogCoreTests: XCTestCase {
maxBatchesPerUpload: .mockRandom(min: 1, max: 100),
backgroundTasksEnabled: .mockAny()
)
defer { core.flushAndTearDown() }

let requestBuilderSpy = FeatureRequestBuilderSpy()
try core.register(feature: FeatureMock(requestBuilder: requestBuilderSpy))
Expand Down Expand Up @@ -243,4 +240,40 @@ class DatadogCoreTests: XCTestCase {
XCTAssertEqual(storage2?.authorizedFilesOrchestrator.performance.maxFileAgeForWrite, 95)
XCTAssertEqual(storage2?.authorizedFilesOrchestrator.performance.minFileAgeForRead, 105)
}

func testWhenStoppingInstance_itDoesNotUploadEvents() throws {
// Given
let core = DatadogCore(
directory: temporaryCoreDirectory,
dateProvider: SystemDateProvider(),
initialConsent: .granted,
performance: .mockRandom(),
httpClient: HTTPClientMock(),
encryption: nil,
contextProvider: .mockAny(),
applicationVersion: .mockAny(),
maxBatchesPerUpload: .mockAny(),
backgroundTasksEnabled: .mockAny()
)

let requestBuilderSpy = FeatureRequestBuilderSpy()
try core.register(feature: FeatureMock(requestBuilder: requestBuilderSpy))
let scope = try XCTUnwrap(core.scope(for: FeatureMock.name))

// When
scope.eventWriteContext { context, writer in
writer.write(value: FeatureMock.Event(event: "should not be sent"))
}

core.stop()

scope.eventWriteContext { context, writer in
writer.write(value: FeatureMock.Event(event: "should not be sent"))
}

// Then
XCTAssertNil(core.scope(for: FeatureMock.name))
core.flushAndTearDown()
XCTAssertEqual(requestBuilderSpy.requestParameters.count, 0, "It should not send any request")
}
}
18 changes: 18 additions & 0 deletions DatadogCore/Tests/Datadog/DatadogTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,24 @@ class DatadogTests: XCTestCase {
XCTAssertTrue(CoreRegistry.instance(named: "test") is DatadogCore)
}

func testStopSDKInstance() throws {
// Given
Datadog.initialize(
with: defaultConfig,
trackingConsent: .mockRandom(),
instanceName: "test"
)

// Then
XCTAssertTrue(CoreRegistry.instance(named: "test") is DatadogCore)

// When
Datadog.stopInstance(named: "test")

// Then
XCTAssertTrue(CoreRegistry.instance(named: "test") is NOPDatadogCore)
}

func testGivenDefaultSDKInstanceInitialized_customOneCanBeInitializedAfterIt() throws {
let defaultConfig = Datadog.Configuration(clientToken: "abc-123", env: "default")
let customConfig = Datadog.Configuration(clientToken: "def-456", env: "custom")
Expand Down

0 comments on commit 04f24d0

Please sign in to comment.