Skip to content

Commit

Permalink
Addressing a few PR comments and ensuring telemetry is disabled for m…
Browse files Browse the repository at this point in the history
…ost tests
  • Loading branch information
MichaelGHSeg committed Oct 16, 2024
1 parent 21b6725 commit e37d234
Show file tree
Hide file tree
Showing 17 changed files with 34 additions and 10 deletions.
14 changes: 7 additions & 7 deletions Sources/Segment/Utilities/Telemetry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public class Telemetry: Subscriber {
private var telemetryTimer: Timer?

func start() {
guard enable, !started, sampleRate != 0.0 else { return }
guard enable, !started, sampleRate > 0.0 && sampleRate <= 1.0 else { return }
started = true

if Double.random(in: 0...1) > sampleRate {
Expand Down Expand Up @@ -97,7 +97,7 @@ public class Telemetry: Subscriber {
var tags = [String: String]()
buildTags(&tags)

guard enable, sampleRate != 0.0, metric.hasPrefix(Telemetry.METRICS_BASE_TAG), !tags.isEmpty, queueHasSpace() else { return }
guard enable, sampleRate > 0.0 && sampleRate <= 1.0, metric.hasPrefix(Telemetry.METRICS_BASE_TAG), !tags.isEmpty, queueHasSpace() else { return }
if Double.random(in: 0...1) > sampleRate { return }

addRemoteMetric(metric: metric, tags: tags)
Expand All @@ -107,7 +107,7 @@ public class Telemetry: Subscriber {
var tags = [String: String]()
buildTags(&tags)

guard enable, sampleRate != 0.0, metric.hasPrefix(Telemetry.METRICS_BASE_TAG), !tags.isEmpty, queueHasSpace() else { return }
guard enable, sampleRate > 0.0 && sampleRate <= 1.0, metric.hasPrefix(Telemetry.METRICS_BASE_TAG), !tags.isEmpty, queueHasSpace() else { return }

var filteredTags = tags
if !sendWriteKeyOnError {
Expand Down Expand Up @@ -156,7 +156,7 @@ public class Telemetry: Subscriber {
}

private func send() throws {
guard sampleRate != 0.0 else { return }
guard sampleRate > 0.0 && sampleRate <= 1.0 else { return }

var sendQueue = [RemoteMetric]()
while !queue.isEmpty {
Expand Down Expand Up @@ -261,11 +261,11 @@ private func addRemoteMetric(metric: String, tags: [String: String], value: Int
}

private func queueHasSpace() -> Bool {
var over = false
var under = false
telemetryQueue.sync {
over = queue.count < maxQueueSize
under = queue.count < maxQueueSize
}
return over
return under
}

private func resetQueue() {
Expand Down
3 changes: 3 additions & 0 deletions Tests/Segment-Tests/Analytics_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import XCTest
@testable import Segment

final class Analytics_Tests: XCTestCase {
override func setUpWithError() throws {
Telemetry.shared.enable = false
}

func testBaseEventCreation() {
let analytics = Analytics(configuration: Configuration(writeKey: "test"))
Expand Down
3 changes: 3 additions & 0 deletions Tests/Segment-Tests/Atomic_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import XCTest
@testable import Segment

final class Atomic_Tests: XCTestCase {
override func setUpWithError() throws {
Telemetry.shared.enable = false
}

func testAtomicIncrement() {

Expand Down
1 change: 1 addition & 0 deletions Tests/Segment-Tests/CompletionGroup_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ final class CompletionGroup_Tests: XCTestCase {

override func setUpWithError() throws {
// Put setup code here. This method is called before the invocation of each test method in the class.
Telemetry.shared.enable = false
}

override func tearDownWithError() throws {
Expand Down
1 change: 1 addition & 0 deletions Tests/Segment-Tests/FlushPolicy_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class FlushPolicyTests: XCTestCase {

override func setUpWithError() throws {
// Put setup code here. This method is called before the invocation of each test method in the class.
Telemetry.shared.enable = false
}

override func tearDownWithError() throws {
Expand Down
1 change: 1 addition & 0 deletions Tests/Segment-Tests/HTTPClient_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class HTTPClientTests: XCTestCase {

override func setUpWithError() throws {
// Put setup code here. This method is called before the invocation of each test method in the class.
Telemetry.shared.enable = false
RestrictedHTTPSession.reset()
}

Expand Down
1 change: 1 addition & 0 deletions Tests/Segment-Tests/JSON_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class JSONTests: XCTestCase {

override func setUpWithError() throws {
// Put setup code here. This method is called before the invocation of each test method in the class.
Telemetry.shared.enable = false
}

override func tearDownWithError() throws {
Expand Down
1 change: 1 addition & 0 deletions Tests/Segment-Tests/KeyPath_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class KeyPath_Tests: XCTestCase {

override func setUpWithError() throws {
// Put setup code here. This method is called before the invocation of each test method in the class.
Telemetry.shared.enable = false
}

override func tearDownWithError() throws {
Expand Down
1 change: 1 addition & 0 deletions Tests/Segment-Tests/MemoryLeak_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ final class MemoryLeak_Tests: XCTestCase {

override func setUpWithError() throws {
// Put setup code here. This method is called before the invocation of each test method in the class.
Telemetry.shared.enable = false
}

override func tearDownWithError() throws {
Expand Down
1 change: 1 addition & 0 deletions Tests/Segment-Tests/ObjC_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class ObjC_Tests: XCTestCase {

override func setUpWithError() throws {
// Put setup code here. This method is called before the invocation of each test method in the class.
Telemetry.shared.enable = false
}

override func tearDownWithError() throws {
Expand Down
1 change: 1 addition & 0 deletions Tests/Segment-Tests/Storage_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class StorageTests: XCTestCase {

override func setUpWithError() throws {
// Put setup code here. This method is called before the invocation of each test method in the class.
Telemetry.shared.enable = false
}

override func tearDownWithError() throws {
Expand Down
1 change: 1 addition & 0 deletions Tests/Segment-Tests/StressTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class StressTests: XCTestCase {

override func setUpWithError() throws {
// Put setup code here. This method is called before the invocation of each test method in the class.
Telemetry.shared.enable = false
RestrictedHTTPSession.reset()
}

Expand Down
3 changes: 1 addition & 2 deletions Tests/Segment-Tests/Telemetry_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import XCTest
@testable import Segment

class TelemetryTests: XCTestCase {

var errors: [String] = []

override func setUpWithError() throws {
Expand Down Expand Up @@ -162,6 +161,6 @@ class URLSessionMock: RestrictedHTTPSession {
}

// Mock URLSessionDataTask
class URLSessionDataTaskMock: URLSessionDataTask {
class URLSessionDataTaskMock: URLSessionDataTask, @unchecked Sendable {
override func resume() {}
}
1 change: 1 addition & 0 deletions Tests/Segment-Tests/Timeline_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Timeline_Tests: XCTestCase {

override func setUpWithError() throws {
// Put setup code here. This method is called before the invocation of each test method in the class.
Telemetry.shared.enable = false
}

override func tearDownWithError() throws {
Expand Down
1 change: 1 addition & 0 deletions Tests/Segment-Tests/UserAgentTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ final class UserAgentTests: XCTestCase {

override func setUpWithError() throws {
// Put setup code here. This method is called before the invocation of each test method in the class.
Telemetry.shared.enable = false
}

override func tearDownWithError() throws {
Expand Down
5 changes: 5 additions & 0 deletions Tests/Segment-Tests/WindowsVendorSystem_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import XCTest
#if os(Windows)

final class WindowsVendorSystem_Tests: XCTestCase {
override func setUpWithError() throws {
// Put setup code here. This method is called before the invocation of each test method in the class.
Telemetry.shared.enable = false
}

func testScreenSizeReturnsNonEmpty() {
let system = WindowsVendorSystem()

Expand Down
5 changes: 4 additions & 1 deletion Tests/Segment-Tests/iOSLifecycle_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import XCTest

#if os(iOS) || os(tvOS) || os(visionOS)
final class iOSLifecycle_Tests: XCTestCase {

override func setUpWithError() throws {
Telemetry.shared.enable = false
}

func testInstallEventCreation() {
let analytics = Analytics(configuration: Configuration(writeKey: "test"))
let outputReader = OutputReaderPlugin()
Expand Down

0 comments on commit e37d234

Please sign in to comment.