Fix unit and UI tests. (#460)

* Fix race condition on session verification tests.
* Fix clear button on text field.
* Add a timeout to UI test signalling.
* Use snapshot testing for login screen.
This commit is contained in:
Doug
2023-01-18 13:28:59 +00:00
committed by GitHub
parent b835b55394
commit 8f906c7be7
20 changed files with 175 additions and 237 deletions
@@ -39,6 +39,8 @@ enum UITestsSignalError: Error {
case notConnected
/// Attempted to receive multiple signals at once.
case awaitingAnotherSignal
/// Receiving the next signal timed out.
case timeout
/// A network error occurred.
case nwError(NWError)
/// An unexpected signal was received. This error isn't used internally.
@@ -123,6 +125,12 @@ enum UITestsSignalling {
private let connection: NWConnection
/// A continuation to call each time a signal is received.
private var nextMessageContinuation: CheckedContinuation<UITestsSignal, Error>?
/// A task to handle the timeout when receiving a signal.
private var nextMessageTimeoutTask: Task<Void, Never>? {
didSet {
oldValue?.cancel()
}
}
/// Creates a new signalling `Connection`.
init() {
@@ -165,6 +173,7 @@ enum UITestsSignalling {
if let nextMessageContinuation {
nextMessageContinuation.resume(throwing: UITestsSignalError.cancelled)
self.nextMessageContinuation = nil
nextMessageTimeoutTask = nil
}
}
@@ -182,6 +191,20 @@ enum UITestsSignalling {
return try await withCheckedThrowingContinuation { continuation in
self.nextMessageContinuation = continuation
// Add a 30 second timeout to stop tests from hanging
self.nextMessageTimeoutTask = Task { [weak self] in
guard let self else { return }
try? await Task.sleep(for: .seconds(30))
guard !Task.isCancelled,
let nextMessageContinuation = self.nextMessageContinuation
else { return }
nextMessageContinuation.resume(throwing: UITestsSignalError.timeout)
self.nextMessageContinuation = nil
self.nextMessageTimeoutTask = nil
}
}
}
@@ -198,12 +221,14 @@ enum UITestsSignalling {
let error: UITestsSignalError = error.map { .nwError($0) } ?? .unknown
self.nextMessageContinuation?.resume(with: .failure(error))
self.nextMessageContinuation = nil
self.nextMessageTimeoutTask = nil
return
}
if signal != .connect {
self.nextMessageContinuation?.resume(returning: signal)
self.nextMessageContinuation = nil
self.nextMessageTimeoutTask = nil
}
self.receiveNextMessage()