-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Basic E2E test for events upload (#155)
* test: Basic E2E test for events upload * refactor: Rename test class * test: E2E test check single events * fix: E2E track test considers in-batch errors --------- Co-authored-by: Nicklas Lundin <[email protected]>
- Loading branch information
1 parent
917743b
commit 2e65c47
Showing
5 changed files
with
156 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import Foundation | ||
|
||
@testable import Confidence | ||
|
||
internal class DebugLoggerFake: DebugLogger { | ||
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.starts(with: "Event upload: HTTP status 200") { | ||
uploadedEvents.append(contentsOf: parseEvents(fromString: message)) | ||
uploadBatchSuccessCounter.increment() | ||
} | ||
} | ||
|
||
func logFlags(action: String, flag: String) { | ||
// no-op | ||
} | ||
|
||
func logContext(action: String, context: ConfidenceStruct) { | ||
// no-op | ||
} | ||
|
||
func getUploadBatchSuccessCount() -> Int { | ||
return uploadBatchSuccessCounter.get() | ||
} | ||
|
||
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 [] | ||
} | ||
|
||
let startIndex = message.index(eventsStart.upperBound, offsetBy: 1) | ||
let endIndex = message.endIndex | ||
let eventsString = message[startIndex..<endIndex] | ||
|
||
return eventsString.components(separatedBy: ",") | ||
.map { $0.trimmingCharacters(in: .whitespacesAndNewlines) } | ||
} | ||
|
||
private class ThreadSafeCounter { | ||
private let queue = DispatchQueue(label: "ThreadSafeCounterQueue") | ||
private var count = 0 | ||
|
||
func increment() { | ||
queue.sync { | ||
count += 1 | ||
} | ||
} | ||
|
||
func get() -> Int { | ||
queue.sync { | ||
return count | ||
} | ||
} | ||
|
||
func waitUntil(value: Int32, timeout: TimeInterval) throws { | ||
let deadline = DispatchTime.now() + timeout | ||
|
||
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)") | ||
} | ||
if (queue.sync { | ||
count >= value | ||
}) { | ||
return | ||
} | ||
} while true | ||
} | ||
} | ||
|
||
struct TimeoutError: Error { | ||
let message: String | ||
} | ||
} |