Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bugfix] Combine timeout for dispatcher retry #516

Merged
merged 1 commit into from
Sep 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 0 additions & 17 deletions Example/IntegrationTests/Auth/AuthTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,6 @@ final class AuthTests: XCTestCase {
app = makeClient(prefix: "👻 App")
let walletAccount = Account(chainIdentifier: "eip155:1", address: "0x724d0D2DaD3fbB0C168f947B87Fa5DBe36F1A8bf")!
wallet = makeClient(prefix: "🤑 Wallet", account: walletAccount)

let expectation = expectation(description: "Wait Clients Connected")
expectation.expectedFulfillmentCount = 2

app.socketConnectionStatusPublisher.sink { status in
if status == .connected {
expectation.fulfill()
}
}.store(in: &publishers)

wallet.socketConnectionStatusPublisher.sink { status in
if status == .connected {
expectation.fulfill()
}
}.store(in: &publishers)

wait(for: [expectation], timeout: 5)
}

func makeClient(prefix: String, account: Account? = nil) -> AuthClient {
Expand Down
21 changes: 0 additions & 21 deletions Example/IntegrationTests/Chat/ChatTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,6 @@ final class ChatTests: XCTestCase {
registry = KeyValueRegistry()
invitee = makeClient(prefix: "🦖 Registered")
inviter = makeClient(prefix: "🍄 Inviter")

waitClientsConnected()
}

private func waitClientsConnected() {
let expectation = expectation(description: "Wait Clients Connected")
expectation.expectedFulfillmentCount = 2

invitee.socketConnectionStatusPublisher.sink { status in
if status == .connected {
expectation.fulfill()
}
}.store(in: &publishers)

inviter.socketConnectionStatusPublisher.sink { status in
if status == .connected {
expectation.fulfill()
}
}.store(in: &publishers)

wait(for: [expectation], timeout: 5)
}

func makeClient(prefix: String) -> ChatClient {
Expand Down
15 changes: 0 additions & 15 deletions Example/IntegrationTests/Sign/SignClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,9 @@ final class SignClientTests: XCTestCase {
return ClientDelegate(client: client)
}

private func listenForConnection() async {
let group = DispatchGroup()
group.enter()
dapp.onConnected = {
group.leave()
}
group.enter()
wallet.onConnected = {
group.leave()
}
group.wait()
return
}

override func setUp() async throws {
dapp = Self.makeClientDelegate(name: "🍏P")
wallet = Self.makeClientDelegate(name: "🍎R")
await listenForConnection()
}

override func tearDown() {
Expand Down
27 changes: 17 additions & 10 deletions Sources/WalletConnectRelay/Dispatching.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ final class Dispatcher: NSObject, Dispatching {
socketConnectionStatusPublisherSubject.eraseToAnyPublisher()
}

private let concurrentQueue = DispatchQueue(label: "com.walletconnect.sdk.dispatcher", attributes: .concurrent)

init(socket: WebSocketConnecting,
socketConnectionHandler: SocketConnectionHandler,
logger: ConsoleLogging) {
Expand Down Expand Up @@ -53,16 +55,21 @@ final class Dispatcher: NSObject, Dispatching {
}

var cancellable: AnyCancellable?
cancellable = socketConnectionStatusPublisher.sink { [unowned self] status in
guard status == .connected else { return }
cancellable?.cancel()
send(string, completion: completion)
}

DispatchQueue.global().asyncAfter(deadline: .now() + .seconds(defaultTimeout)) {
completion(NetworkError.webSocketNotConnected)
cancellable?.cancel()
}
cancellable = socketConnectionStatusPublisher
.filter { $0 == .connected }
.setFailureType(to: NetworkError.self)
.timeout(.seconds(defaultTimeout), scheduler: concurrentQueue, customError: { .webSocketNotConnected })
.sink(receiveCompletion: { result in
switch result {
case .failure(let error):
cancellable?.cancel()
completion(error)
case .finished: break
}
}, receiveValue: { [unowned self] status in
cancellable?.cancel()
send(string, completion: completion)
})
}

func protectedSend(_ string: String) async throws {
Expand Down