Skip to content

Commit

Permalink
test: E2E test check single events
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriziodemaria committed Jul 5, 2024
1 parent 0b1779e commit ac4de85
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
5 changes: 4 additions & 1 deletion Sources/Confidence/RemoteConfidenceClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,14 @@ public class RemoteConfidenceClient: ConfidenceClient {
switch result {
case .success(let successData):
let status = successData.response.statusCode
let eventNames = events.map { event in
event.eventDefinition
}
switch status {
case 200:
// clean up in case of success
debugLogger?.logMessage(
message: "Event upload: HTTP status 200",
message: "Event upload: HTTP status 200. Events: \(eventNames.joined(separator: ","))",
isWarning: false
)
return true
Expand Down
5 changes: 3 additions & 2 deletions Tests/ConfidenceTests/ConfidenceIntegrationTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,9 @@ class ConfidenceIntegrationTests: XCTestCase {
)

confidence.flush()
try logger.waitUploadSuccessCount(value: 1, timeout: 5.0)
XCTAssertEqual(logger.getUploadSuccessCount(), 1)
try logger.waitUploadBatchSuccessCount(value: 1, timeout: 5.0)
XCTAssertEqual(logger.getUploadBatchSuccessCount(), 1)
XCTAssertEqual(logger.uploadedEvents, ["all-types"])
}

func testConfidenceFeatureApplies() async throws {
Expand Down
36 changes: 28 additions & 8 deletions Tests/ConfidenceTests/Helpers/DebugLoggerFake.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ import Foundation
@testable import Confidence

internal class DebugLoggerFake: DebugLogger {
private let uploadSuccessCounter = ThreadSafeCounter()
private let uploadBatchSuccessCounter = ThreadSafeCounter()
public var uploadedEvents: [String] = [] // Holds the "eventDefinition" name of each uploaded event

func logEvent(action: String, event: ConfidenceEvent?) {
// no-op
}

func logMessage(message: String, isWarning: Bool) {
if message == "Event upload: HTTP status 200" {
uploadSuccessCounter.increment()
if message.starts(with: "Event upload: HTTP status 200") {
uploadedEvents.append(contentsOf: parseEvents(fromString: message))
uploadBatchSuccessCounter.increment()
}
}

Expand All @@ -23,14 +25,32 @@ internal class DebugLoggerFake: DebugLogger {
// no-op
}

func getUploadSuccessCount() -> Int {
return uploadSuccessCounter.get()
func getUploadBatchSuccessCount() -> Int {
return uploadBatchSuccessCounter.get()
}

func waitUploadSuccessCount(value: Int32, timeout: TimeInterval) throws {
try uploadSuccessCounter.waitUntil(value: value, timeout: timeout)
func waitUploadBatchSuccessCount(value: Int32, timeout: TimeInterval) throws {
try uploadBatchSuccessCounter.waitUntil(value: value, timeout: timeout)
}

/**
Example
Input: "Event upload: HTTP status 200. Events: event-name1, event-name2"
Output: ["event-name1", "event-name2"]
*/
private func parseEvents(fromString message: String) -> [String] {
guard let eventsStart = message.range(of: "Events:") else {
return []
}

Check failure on line 45 in Tests/ConfidenceTests/Helpers/DebugLoggerFake.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)
let startIndex = message.index(eventsStart.upperBound, offsetBy: 1)
let endIndex = message.endIndex
let eventsString = message[startIndex..<endIndex]

Check failure on line 49 in Tests/ConfidenceTests/Helpers/DebugLoggerFake.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)
return eventsString.components(separatedBy: ",")
.map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }
}

Check failure on line 53 in Tests/ConfidenceTests/Helpers/DebugLoggerFake.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)
private class ThreadSafeCounter {
private let queue = DispatchQueue(label: "ThreadSafeCounterQueue")
private var count = 0
Expand All @@ -50,8 +70,8 @@ internal class DebugLoggerFake: DebugLogger {
func waitUntil(value: Int32, timeout: TimeInterval) throws {
let deadline = DispatchTime.now() + timeout

// TODO There might be more efficient ways than a while true loop
repeat {
Thread.sleep(forTimeInterval: 0.1) // Shortcut to reduce CPU usage, probably needs refactoring
guard deadline > DispatchTime.now() else {
throw TimeoutError(message: "Timed out waiting for counter to reach \(value)")
}
Expand Down

0 comments on commit ac4de85

Please sign in to comment.