Fix UI tests hanging. (#429)

* Only use Client for UI test signalling.

Make the server listen and return a client.

* Refactor to Listener and Connection.

Matches Network.framework naming.

* Wait until the connection is ready before setting the established connection.

* Docs and fix method name.

* Fix CI issues.

- Use Bonjour to allow multiple devices running simultaneously.
- Sleep after establishing the connection to fix issues on Intel.
This commit is contained in:
Doug
2023-01-10 14:10:30 +00:00
committed by GitHub
parent d70a240869
commit edc06bf662
5 changed files with 261 additions and 234 deletions
@@ -32,11 +32,11 @@ class MockRoomTimelineController: RoomTimelineControllerProtocol {
var timelineItems: [RoomTimelineItemProtocol] = RoomTimelineItemFixtures.default
private var client: UITestsSignalling.Client?
private var connection: UITestsSignalling.Connection?
init(listenForSignals: Bool = false) {
if listenForSignals {
client = .init()
connection = .init()
Task { try await startListening() }
}
}
@@ -85,15 +85,15 @@ class MockRoomTimelineController: RoomTimelineControllerProtocol {
/// Allows the simulation of server responses by listening for signals from UI tests.
private func startListening() async throws {
try await client?.connect()
try await connection?.connect()
Task {
while let client {
while let connection {
do {
try await handleSignal(client.receive())
try await handleSignal(connection.receive())
} catch {
client.disconnect()
self.client = nil
connection.disconnect()
self.connection = nil
}
}
}
@@ -119,7 +119,7 @@ class MockRoomTimelineController: RoomTimelineControllerProtocol {
timelineItems.append(incomingItem)
callbacks.send(.updatedTimelineItems)
try? await client?.send(.success)
try? await connection?.send(.success)
}
/// Prepends the next chunk of items to the `timelineItems` array.
@@ -131,6 +131,6 @@ class MockRoomTimelineController: RoomTimelineControllerProtocol {
callbacks.send(.updatedTimelineItems)
callbacks.send(.finishedBackPaginating)
try? await client?.send(.success)
try? await connection?.send(.success)
}
}
@@ -1,191 +0,0 @@
//
// Copyright 2022 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
import Network
enum UITestsSignal: String {
/// An internal signal used to bring up the connection.
case connect
/// Ask the app to back paginate.
case paginate
/// Ask the app to simulate an incoming message.
case incomingMessage
/// The operation has completed successfully.
case success
}
enum UITestsSignalError: Error {
/// An unknown error occurred.
case unknown
/// Signalling could not be used as is hasn't been enabled.
case disabled
/// The connection was cancelled.
case cancelled
/// The connection hasn't been established.
case notConnected
/// Attempted to receive multiple signals at once.
case awaitingAnotherSignal
/// A network error occurred.
case nwError(NWError)
/// An unexpected signal was received. This error isn't used internally.
case unexpected
}
enum UITestsSignalling {
/// A UDP server that can be used for signalling between the UI tests jig and the app.
/// The server should be instantiated on the UI tests side.
class Server: UITestsSignallingProtocol {
private let listener: NWListener
var connection: NWConnection
var nextMessageContinuation: CheckedContinuation<UITestsSignal, Error>?
/// Creates a new signalling server.
init() throws {
connection = NWConnection(host: "127.0.0.1", port: 0, using: .udp)
listener = try NWListener(using: .udp, on: 1234)
}
/// Listens for a client and attempts to negotiate a connection when one is discovered.
func connect() async throws {
guard listener.state == .setup else { return }
return try await withCheckedThrowingContinuation { [weak self] continuation in
listener.newConnectionHandler = { connection in
connection.start(queue: .main)
connection.stateUpdateHandler = { state in
switch state {
case .ready:
self?.listener.cancel()
self?.receiveNextMessage()
continuation.resume()
case .failed(let error):
self?.listener.cancel()
continuation.resume(with: .failure(error))
default:
break
}
}
self?.connection = connection
}
listener.start(queue: .main)
}
}
/// Stops the connection (or the listener if a connection hasn't been established).
func disconnect() {
listener.cancel()
connection.cancel()
if let nextMessageContinuation {
nextMessageContinuation.resume(throwing: UITestsSignalError.cancelled)
self.nextMessageContinuation = nil
}
}
}
/// A UDP client that can be used for signalling between the app and the UI tests jig.
/// The client should be instantiated on the app side.
class Client: UITestsSignallingProtocol {
let connection: NWConnection
var nextMessageContinuation: CheckedContinuation<UITestsSignal, Error>?
/// Creates a new signalling client.
init() {
connection = NWConnection(host: "127.0.0.1", port: 1234, using: .udp)
}
/// Attempts to connect to a server.
func connect() async throws {
guard connection.state == .setup else { return }
return try await withCheckedThrowingContinuation { continuation in
connection.start(queue: .main)
connection.stateUpdateHandler = { state in
switch state {
case .ready:
self.receiveNextMessage()
continuation.resume()
Task { try await self.send(.connect) }
case .failed(let error):
continuation.resume(with: .failure(error))
default:
break
}
}
}
}
/// Stops the connection.
func disconnect() {
connection.cancel()
if let nextMessageContinuation {
nextMessageContinuation.resume(throwing: UITestsSignalError.cancelled)
self.nextMessageContinuation = nil
}
}
}
}
/// A shared implementation for sending/receiving signals between a client and a server.
protocol UITestsSignallingProtocol: AnyObject {
var connection: NWConnection { get }
var nextMessageContinuation: CheckedContinuation<UITestsSignal, Error>? { get set }
}
extension UITestsSignallingProtocol {
/// Sends a message to the connected client/server.
func send(_ signal: UITestsSignal) async throws {
guard connection.state == .ready else { throw UITestsSignalError.notConnected }
let data = signal.rawValue.data(using: .utf8)
connection.send(content: data, completion: .idempotent)
}
/// Returns the next message received by the client/server.
func receive() async throws -> UITestsSignal {
guard connection.state == .ready else { throw UITestsSignalError.notConnected }
guard nextMessageContinuation == nil else { throw UITestsSignalError.awaitingAnotherSignal }
return try await withCheckedThrowingContinuation { continuation in
self.nextMessageContinuation = continuation
}
}
/// Processes the next message received by the client/server
fileprivate func receiveNextMessage() {
connection.receiveMessage { [weak self] completeContent, _, isComplete, error in
guard let self else { return }
guard isComplete else { fatalError("Partial messages not supported") }
guard let completeContent,
let message = String(data: completeContent, encoding: .utf8),
let signal = UITestsSignal(rawValue: message)
else {
let error: UITestsSignalError = error.map { .nwError($0) } ?? .unknown
self.nextMessageContinuation?.resume(with: .failure(error))
self.nextMessageContinuation = nil
return
}
if signal != .connect {
self.nextMessageContinuation?.resume(returning: signal)
self.nextMessageContinuation = nil
}
self.receiveNextMessage()
}
}
}
@@ -0,0 +1,213 @@
//
// Copyright 2022 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Network
import SwiftUI
enum UITestsSignal: String {
/// An internal signal used to bring up the connection.
case connect
/// Ask the app to back paginate.
case paginate
/// Ask the app to simulate an incoming message.
case incomingMessage
/// The operation has completed successfully.
case success
}
enum UITestsSignalError: Error {
/// An unknown error occurred.
case unknown
/// Signalling could not be used as is hasn't been enabled.
case disabled
/// The connection was cancelled.
case cancelled
/// The connection hasn't been established.
case notConnected
/// Attempted to receive multiple signals at once.
case awaitingAnotherSignal
/// A network error occurred.
case nwError(NWError)
/// An unexpected signal was received. This error isn't used internally.
case unexpected
}
enum UITestsSignalling {
/// The Bonjour service name used for the connection. The device name
/// is included to allow UI tests to run on multiple devices simultaneously.
private static let serviceName = "UITestsSignalling \(UIDevice.current.name)"
/// The Bonjour service type used for the connection.
private static let serviceType = "_signalling._udp."
/// The Bonjour domain used for the connection.
private static let domain = "local."
/// The DispatchQueue used for networking.
private static let queue: DispatchQueue = .main
/// A network listener that can be used in the UI tests runner to create a two-way `Connection` with the app.
class Listener {
/// The underlying network listener.
private let listener: NWListener
/// The established connection. This is stored in case the connection is established
/// before `connection()` is awaited and so the continuation is still `nil`.
private var establishedConnection: Connection?
/// The continuation to call when a connection is established.
private var connectionContinuation: CheckedContinuation<Connection, Error>?
/// Creates a new signalling `Listener` and starts listening.
init() throws {
let service = NWListener.Service(name: UITestsSignalling.serviceName, type: UITestsSignalling.serviceType, domain: UITestsSignalling.domain)
listener = try NWListener(service: service, using: .udp)
listener.newConnectionHandler = { [weak self] nwConnection in
let connection = Connection(nwConnection: nwConnection)
nwConnection.start(queue: UITestsSignalling.queue)
nwConnection.stateUpdateHandler = { state in
switch state {
case .ready:
connection.receiveNextMessage()
self?.establishedConnection = connection
self?.connectionContinuation?.resume(returning: connection)
case .failed(let error):
self?.connectionContinuation?.resume(with: .failure(error))
default:
break
}
}
self?.listener.cancel() // Stop listening for connections when one is discovered.
}
listener.start(queue: UITestsSignalling.queue)
}
/// Returns the negotiated `Connection` as and when it has been established.
func connection() async throws -> Connection {
guard listener.state == .setup else { throw UITestsSignalError.unknown }
if let establishedConnection {
return establishedConnection
}
return try await withCheckedThrowingContinuation { [weak self] continuation in
self?.connectionContinuation = continuation
}
}
/// Stops the listening when a connection hasn't been established.
func cancel() {
listener.cancel()
if let connectionContinuation {
connectionContinuation.resume(throwing: UITestsSignalError.cancelled)
self.connectionContinuation = nil
}
}
}
/// A two-way UDP connection that can be used for signalling between the app and the UI tests runner.
/// The connection should be created as follows:
/// - Create a `Listener` in the UI tests before launching the app. This will automatically start listening for a connection.
/// - With in the App, create a `Connection` and call `connect()` to establish a connection.
/// - Await the `connection()` on the `Listener` when you need to send the signal.
/// - The two `Connection` objects can now be used for two-way signalling.
class Connection {
/// The underlying network connection.
private let connection: NWConnection
/// A continuation to call each time a signal is received.
private var nextMessageContinuation: CheckedContinuation<UITestsSignal, Error>?
/// Creates a new signalling `Connection`.
init() {
let endpoint = NWEndpoint.service(name: UITestsSignalling.serviceName,
type: UITestsSignalling.serviceType,
domain: UITestsSignalling.domain,
interface: nil)
connection = NWConnection(to: endpoint, using: .udp)
}
/// Creates a new signalling `Connection` from an established `NWConnection`.
fileprivate init(nwConnection: NWConnection) {
connection = nwConnection
}
/// Attempts to establish a connection with a `Listener`.
func connect() async throws {
guard connection.state == .setup else { return }
return try await withCheckedThrowingContinuation { continuation in
connection.start(queue: UITestsSignalling.queue)
connection.stateUpdateHandler = { state in
switch state {
case .ready:
self.receiveNextMessage()
continuation.resume()
Task { try await self.send(.connect) }
case .failed(let error):
continuation.resume(with: .failure(error))
default:
break
}
}
}
}
/// Stops the connection.
func disconnect() {
connection.cancel()
if let nextMessageContinuation {
nextMessageContinuation.resume(throwing: UITestsSignalError.cancelled)
self.nextMessageContinuation = nil
}
}
/// Sends a message to the other side of the connection.
func send(_ signal: UITestsSignal) async throws {
guard connection.state == .ready else { throw UITestsSignalError.notConnected }
let data = signal.rawValue.data(using: .utf8)
connection.send(content: data, completion: .idempotent)
}
/// Returns the next message received from the other side of the connection.
func receive() async throws -> UITestsSignal {
guard connection.state == .ready else { throw UITestsSignalError.notConnected }
guard nextMessageContinuation == nil else { throw UITestsSignalError.awaitingAnotherSignal }
return try await withCheckedThrowingContinuation { continuation in
self.nextMessageContinuation = continuation
}
}
/// Processes the next message received by the connection.
fileprivate func receiveNextMessage() {
connection.receiveMessage { [weak self] completeContent, _, isComplete, error in
guard let self else { return }
guard isComplete else { fatalError("Partial messages not supported") }
guard let completeContent,
let message = String(data: completeContent, encoding: .utf8),
let signal = UITestsSignal(rawValue: message)
else {
let error: UITestsSignalError = error.map { .nwError($0) } ?? .unknown
self.nextMessageContinuation?.resume(with: .failure(error))
self.nextMessageContinuation = nil
return
}
if signal != .connect {
self.nextMessageContinuation?.resume(returning: signal)
self.nextMessageContinuation = nil
}
self.receiveNextMessage()
}
}
}
}