|
| 1 | +// |
| 2 | +// MemoryLeak_Tests.swift |
| 3 | +// |
| 4 | +// |
| 5 | +// Created by Brandon Sneed on 10/17/22. |
| 6 | +// |
| 7 | + |
| 8 | +import XCTest |
| 9 | +@testable import Segment |
| 10 | + |
| 11 | +final class MemoryLeak_Tests: XCTestCase { |
| 12 | + |
| 13 | + override func setUpWithError() throws { |
| 14 | + // Put setup code here. This method is called before the invocation of each test method in the class. |
| 15 | + } |
| 16 | + |
| 17 | + override func tearDownWithError() throws { |
| 18 | + // Put teardown code here. This method is called after the invocation of each test method in the class. |
| 19 | + } |
| 20 | + |
| 21 | + func testLeaksVerbose() throws { |
| 22 | + let analytics = Analytics(configuration: Configuration(writeKey: "1234")) |
| 23 | + |
| 24 | + waitUntilStarted(analytics: analytics) |
| 25 | + analytics.track(name: "test") |
| 26 | + |
| 27 | + RunLoop.main.run(until: Date(timeIntervalSinceNow: 1)) |
| 28 | + |
| 29 | + let segmentDest = analytics.find(pluginType: SegmentDestination.self)! |
| 30 | + let destMetadata = segmentDest.timeline.find(pluginType: DestinationMetadataPlugin.self)! |
| 31 | + let startupQueue = analytics.find(pluginType: StartupQueue.self)! |
| 32 | + let segmentLog = analytics.find(pluginType: SegmentLog.self)! |
| 33 | + |
| 34 | + let context = analytics.find(pluginType: Context.self)! |
| 35 | + |
| 36 | + #if !os(Linux) |
| 37 | + let deviceToken = analytics.find(pluginType: DeviceToken.self)! |
| 38 | + #endif |
| 39 | + #if os(iOS) || os(tvOS) || targetEnvironment(macCatalyst) |
| 40 | + let iosLifecycle = analytics.find(pluginType: iOSLifecycleEvents.self)! |
| 41 | + let iosMonitor = analytics.find(pluginType: iOSLifecycleMonitor.self)! |
| 42 | + #elseif os(watchOS) |
| 43 | + let watchLifecycle = analytics.find(pluginType: watchOSLifecycleEvents.self)! |
| 44 | + let watchMonitor = analytics.find(pluginType: watchOSLifecycleMonitor.self)! |
| 45 | + #elseif os(macOS) |
| 46 | + let macLifecycle = analytics.find(pluginType: macOSLifecycleEvents.self)! |
| 47 | + let macMonitor = analytics.find(pluginType: macOSLifecycleMonitor.self)! |
| 48 | + #endif |
| 49 | + |
| 50 | + analytics.remove(plugin: startupQueue) |
| 51 | + analytics.remove(plugin: segmentLog) |
| 52 | + analytics.remove(plugin: segmentDest) |
| 53 | + segmentDest.remove(plugin: destMetadata) |
| 54 | + |
| 55 | + analytics.remove(plugin: context) |
| 56 | + #if !os(Linux) |
| 57 | + analytics.remove(plugin: deviceToken) |
| 58 | + #endif |
| 59 | + #if os(iOS) || os(tvOS) || targetEnvironment(macCatalyst) |
| 60 | + analytics.remove(plugin: iosLifecycle) |
| 61 | + analytics.remove(plugin: iosMonitor) |
| 62 | + #elseif os(watchOS) |
| 63 | + analytics.remove(plugin: watchLifecycle) |
| 64 | + analytics.remove(plugin: watchMonitor) |
| 65 | + #elseif os(macOS) |
| 66 | + analytics.remove(plugin: macLifecycle) |
| 67 | + analytics.remove(plugin: macMonitor) |
| 68 | + #endif |
| 69 | + |
| 70 | + RunLoop.main.run(until: Date(timeIntervalSinceNow: 1)) |
| 71 | + |
| 72 | + checkIfLeaked(segmentLog) |
| 73 | + checkIfLeaked(segmentDest) |
| 74 | + checkIfLeaked(destMetadata) |
| 75 | + checkIfLeaked(startupQueue) |
| 76 | + |
| 77 | + checkIfLeaked(context) |
| 78 | + #if !os(Linux) |
| 79 | + checkIfLeaked(deviceToken) |
| 80 | + #endif |
| 81 | + #if os(iOS) || os(tvOS) || targetEnvironment(macCatalyst) |
| 82 | + checkIfLeaked(iosLifecycle) |
| 83 | + checkIfLeaked(iosMonitor) |
| 84 | + #elseif os(watchOS) |
| 85 | + checkIfLeaked(watchLifecycle) |
| 86 | + checkIfLeaked(watchMonitor) |
| 87 | + #elseif os(macOS) |
| 88 | + checkIfLeaked(macLifecycle) |
| 89 | + checkIfLeaked(macMonitor) |
| 90 | + #endif |
| 91 | + |
| 92 | + checkIfLeaked(analytics) |
| 93 | + } |
| 94 | + |
| 95 | + func testLeaksSimple() throws { |
| 96 | + let analytics = Analytics(configuration: Configuration(writeKey: "1234")) |
| 97 | + |
| 98 | + waitUntilStarted(analytics: analytics) |
| 99 | + analytics.track(name: "test") |
| 100 | + |
| 101 | + RunLoop.main.run(until: Date(timeIntervalSinceNow: 1)) |
| 102 | + |
| 103 | + checkIfLeaked(analytics) |
| 104 | + } |
| 105 | + |
| 106 | +} |
0 commit comments