Skip to content

Commit 35587e9

Browse files
Fixes LISTEN to quote channel name (#466)
Co-authored-by: Fabian Fett <[email protected]>
1 parent 8f8724e commit 35587e9

File tree

3 files changed

+25
-18
lines changed

3 files changed

+25
-18
lines changed

Sources/PostgresNIO/New/PostgresChannelHandler.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ final class PostgresChannelHandler: ChannelDuplexHandler {
594594
private func makeStartListeningQuery(channel: String, context: ChannelHandlerContext) -> PSQLTask {
595595
let promise = context.eventLoop.makePromise(of: PSQLRowStream.self)
596596
let query = ExtendedQueryContext(
597-
query: PostgresQuery(unsafeSQL: "LISTEN \(channel);"),
597+
query: PostgresQuery(unsafeSQL: #"LISTEN "\#(channel)";"#),
598598
logger: self.logger,
599599
promise: promise
600600
)
@@ -642,7 +642,7 @@ final class PostgresChannelHandler: ChannelDuplexHandler {
642642
private func makeUnlistenQuery(channel: String, context: ChannelHandlerContext) -> PSQLTask {
643643
let promise = context.eventLoop.makePromise(of: PSQLRowStream.self)
644644
let query = ExtendedQueryContext(
645-
query: PostgresQuery(unsafeSQL: "UNLISTEN \(channel);"),
645+
query: PostgresQuery(unsafeSQL: #"UNLISTEN "\#(channel)";"#),
646646
logger: self.logger,
647647
promise: promise
648648
)

Tests/IntegrationTests/AsyncTests.swift

+18-11
Original file line numberDiff line numberDiff line change
@@ -225,25 +225,32 @@ final class AsyncPostgresConnectionTests: XCTestCase {
225225
}
226226

227227
func testListenAndNotify() async throws {
228+
let channelNames = [
229+
"foo",
230+
"default"
231+
]
232+
228233
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
229234
defer { XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully()) }
230235
let eventLoop = eventLoopGroup.next()
231236

232-
try await self.withTestConnection(on: eventLoop) { connection in
233-
let stream = try await connection.listen("foo")
234-
var iterator = stream.makeAsyncIterator()
237+
for channelName in channelNames {
238+
try await self.withTestConnection(on: eventLoop) { connection in
239+
let stream = try await connection.listen(channelName)
240+
var iterator = stream.makeAsyncIterator()
235241

236-
try await self.withTestConnection(on: eventLoop) { other in
237-
try await other.query(#"NOTIFY foo, 'bar';"#, logger: .psqlTest)
242+
try await self.withTestConnection(on: eventLoop) { other in
243+
try await other.query(#"NOTIFY "\#(unescaped: channelName)", 'bar';"#, logger: .psqlTest)
238244

239-
try await other.query(#"NOTIFY foo, 'foo';"#, logger: .psqlTest)
240-
}
245+
try await other.query(#"NOTIFY "\#(unescaped: channelName)", 'foo';"#, logger: .psqlTest)
246+
}
241247

242-
let first = try await iterator.next()
243-
XCTAssertEqual(first?.payload, "bar")
248+
let first = try await iterator.next()
249+
XCTAssertEqual(first?.payload, "bar")
244250

245-
let second = try await iterator.next()
246-
XCTAssertEqual(second?.payload, "foo")
251+
let second = try await iterator.next()
252+
XCTAssertEqual(second?.payload, "foo")
253+
}
247254
}
248255
}
249256

Tests/PostgresNIOTests/New/PostgresConnectionTests.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class PostgresConnectionTests: XCTestCase {
5151
}
5252

5353
let listenMessage = try await channel.waitForUnpreparedRequest()
54-
XCTAssertEqual(listenMessage.parse.query, "LISTEN foo;")
54+
XCTAssertEqual(listenMessage.parse.query, #"LISTEN "foo";"#)
5555

5656
try await channel.writeInbound(PostgresBackendMessage.parseComplete)
5757
try await channel.writeInbound(PostgresBackendMessage.parameterDescription(.init(dataTypes: [])))
@@ -63,7 +63,7 @@ class PostgresConnectionTests: XCTestCase {
6363
try await channel.writeInbound(PostgresBackendMessage.notification(.init(backendPID: 12, channel: "foo", payload: "wooohooo")))
6464

6565
let unlistenMessage = try await channel.waitForUnpreparedRequest()
66-
XCTAssertEqual(unlistenMessage.parse.query, "UNLISTEN foo;")
66+
XCTAssertEqual(unlistenMessage.parse.query, #"UNLISTEN "foo";"#)
6767

6868
try await channel.writeInbound(PostgresBackendMessage.parseComplete)
6969
try await channel.writeInbound(PostgresBackendMessage.parameterDescription(.init(dataTypes: [])))
@@ -111,7 +111,7 @@ class PostgresConnectionTests: XCTestCase {
111111
}
112112

113113
let listenMessage = try await channel.waitForUnpreparedRequest()
114-
XCTAssertEqual(listenMessage.parse.query, "LISTEN foo;")
114+
XCTAssertEqual(listenMessage.parse.query, #"LISTEN "foo";"#)
115115

116116
try await channel.writeInbound(PostgresBackendMessage.parseComplete)
117117
try await channel.writeInbound(PostgresBackendMessage.parameterDescription(.init(dataTypes: [])))
@@ -124,7 +124,7 @@ class PostgresConnectionTests: XCTestCase {
124124
try await channel.writeInbound(PostgresBackendMessage.notification(.init(backendPID: 12, channel: "foo", payload: "wooohooo2")))
125125

126126
let unlistenMessage = try await channel.waitForUnpreparedRequest()
127-
XCTAssertEqual(unlistenMessage.parse.query, "UNLISTEN foo;")
127+
XCTAssertEqual(unlistenMessage.parse.query, #"UNLISTEN "foo";"#)
128128

129129
try await channel.writeInbound(PostgresBackendMessage.parseComplete)
130130
try await channel.writeInbound(PostgresBackendMessage.parameterDescription(.init(dataTypes: [])))
@@ -160,7 +160,7 @@ class PostgresConnectionTests: XCTestCase {
160160
}
161161

162162
let listenMessage = try await channel.waitForUnpreparedRequest()
163-
XCTAssertEqual(listenMessage.parse.query, "LISTEN foo;")
163+
XCTAssertEqual(listenMessage.parse.query, #"LISTEN "foo";"#)
164164

165165
try await channel.writeInbound(PostgresBackendMessage.parseComplete)
166166
try await channel.writeInbound(PostgresBackendMessage.parameterDescription(.init(dataTypes: [])))

0 commit comments

Comments
 (0)