Skip to content

Commit

Permalink
Merge pull request #516 from WalletConnect/bugfix/dispatcher-retry
Browse files Browse the repository at this point in the history
[Bugfix] Combine timeout for dispatcher retry
  • Loading branch information
llbartekll authored Sep 27, 2022
2 parents 245d100 + 85cb411 commit 5307b48
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 63 deletions.
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

0 comments on commit 5307b48

Please sign in to comment.