Skip to content

Commit 9fbfc91

Browse files
Adds Messenger.onClose for client cleanup
1 parent a197672 commit 9fbfc91

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

Sources/GraphQLTransportWS/Messenger.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public protocol Messenger: AnyObject {
88
// AnyObject compliance requires that the implementing object is a class and we can reference it weakly
99
func send<S>(_ message: S) -> Void where S: Collection, S.Element == Character
1010
func onRecieve(callback: @escaping (String) -> Void) -> Void
11+
func onClose(callback: @escaping () -> Void) -> Void
1112
func close() -> Void
1213
func error(_ message: String, code: Int) -> Void
1314
}

Tests/GraphQLTransportWSTests/GraphQLTransportWSTests.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,35 @@ class GraphqlTransportWsTests: XCTestCase {
4343
)
4444
}
4545

46+
/// Tests that trying to run methods before `connection_init` is not allowed
47+
func testInitialize() throws {
48+
var messages = [String]()
49+
let completeExpectation = XCTestExpectation()
50+
51+
let client = Client(messenger: clientMessenger)
52+
client.onMessage { message, _ in
53+
messages.append(message)
54+
completeExpectation.fulfill()
55+
}
56+
57+
client.sendStart(
58+
payload: GraphQLRequest(
59+
query: """
60+
query {
61+
hello
62+
}
63+
"""
64+
),
65+
id: UUID().uuidString
66+
)
67+
68+
wait(for: [completeExpectation], timeout: 2)
69+
XCTAssertEqual(
70+
messages,
71+
["4407: Connection not initialized"]
72+
)
73+
}
74+
4675
/// Tests that throwing in the authorization callback forces an unauthorized error
4776
func testAuth() throws {
4877
server.auth { payload in

Tests/GraphQLTransportWSTests/TestMessenger.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import Foundation
1010
class TestMessenger: Messenger {
1111
weak var other: TestMessenger?
1212
var onRecieve: (String) -> Void = { _ in }
13+
var onClose: () -> Void = { }
1314
let queue: DispatchQueue = .init(label: "Test messenger")
1415

1516
init() {}
@@ -29,11 +30,16 @@ class TestMessenger: Messenger {
2930
self.onRecieve = callback
3031
}
3132

33+
func onClose(callback: @escaping () -> Void) {
34+
self.onClose = callback
35+
}
36+
3237
func error(_ message: String, code: Int) {
3338
self.send("\(code): \(message)")
3439
}
3540

3641
func close() {
3742
// This is a testing no-op
43+
self.onClose()
3844
}
3945
}

0 commit comments

Comments
 (0)