@@ -284,6 +284,101 @@ final class AsyncPostgresConnectionTests: XCTestCase {
284284 }
285285 }
286286
287+ func testListenTwiceChannel( ) async throws {
288+ let eventLoopGroup = MultiThreadedEventLoopGroup ( numberOfThreads: 1 )
289+ defer { XCTAssertNoThrow ( try eventLoopGroup. syncShutdownGracefully ( ) ) }
290+ let eventLoop = eventLoopGroup. next ( )
291+
292+ try await self . withTestConnection ( on: eventLoop) { connection in
293+ // Concurrently listen on a channel that is initially closed
294+ async let stream1later = connection. listen ( " same-channel " )
295+ async let stream2later = connection. listen ( " same-channel " )
296+ let ( stream1, stream2) = try await ( stream1later, stream2later)
297+
298+ try await self . withTestConnection ( on: eventLoop) { other in
299+ try await other. query ( #"NOTIFY " \#( unescaped: " same-channel " ) ";"# , logger: . psqlTest)
300+ }
301+
302+ var stream1EventReceived = false
303+ var stream2EventReceived = false
304+
305+ for try await _ in stream1 {
306+ stream1EventReceived = true
307+ break
308+ }
309+
310+ for try await _ in stream2 {
311+ stream2EventReceived = true
312+ break
313+ }
314+
315+ XCTAssertTrue ( stream1EventReceived)
316+ XCTAssertTrue ( stream2EventReceived)
317+ }
318+ }
319+
320+ func testListenOnClosedChannel( ) async throws {
321+ let eventLoopGroup = MultiThreadedEventLoopGroup ( numberOfThreads: 1 )
322+ defer { XCTAssertNoThrow ( try eventLoopGroup. syncShutdownGracefully ( ) ) }
323+ let eventLoop = eventLoopGroup. next ( )
324+
325+ try await self . withTestConnection ( on: eventLoop) { connection in
326+ try await connection. close ( )
327+ do {
328+ _ = try await connection. listen ( " futile " )
329+ XCTFail ( " Expected not to get any events " )
330+ } catch let error as PSQLError where error. code == . listenFailed {
331+ // Expected
332+ }
333+ }
334+ }
335+
336+ func testListenThenCloseChannel( ) async throws {
337+ let eventLoopGroup = MultiThreadedEventLoopGroup ( numberOfThreads: 1 )
338+ defer { XCTAssertNoThrow ( try eventLoopGroup. syncShutdownGracefully ( ) ) }
339+ let eventLoop = eventLoopGroup. next ( )
340+
341+ try await self . withTestConnection ( on: eventLoop) { connection in
342+ let stream = try await connection. listen ( " hopeful " )
343+ try await connection. close ( )
344+ do {
345+ for try await _ in stream {
346+ XCTFail ( " Expected not to get any events " )
347+ }
348+ XCTFail ( " Expected not to have reached the end of stream " )
349+ } catch is PSQLError {
350+ // Expected
351+ }
352+ }
353+ }
354+
355+ func testListenThenClosingChannel( ) async throws {
356+ let eventLoopGroup = MultiThreadedEventLoopGroup ( numberOfThreads: 1 )
357+ defer { XCTAssertNoThrow ( try eventLoopGroup. syncShutdownGracefully ( ) ) }
358+ let eventLoop = eventLoopGroup. next ( )
359+
360+ try await self . withTestConnection ( on: eventLoop) { connection in
361+ _ = try await connection. listen ( " initial " )
362+ async let asyncClose : ( ) = connection. close ( )
363+ let stream : PostgresNotificationSequence
364+ do {
365+ stream = try await connection. listen ( " hopeful " )
366+ } catch let error as PSQLError where error. code == . listenFailed {
367+ // Expected
368+ return
369+ }
370+ try await asyncClose
371+ do {
372+ for try await _ in stream {
373+ XCTFail ( " Expected not to get any events " )
374+ }
375+ XCTFail ( " Expected not to have reached the end of stream " )
376+ } catch is PSQLError {
377+ // Expected
378+ }
379+ }
380+ }
381+
287382 #if canImport(Network)
288383 func testSelect10kRowsNetworkFramework( ) async throws {
289384 let eventLoopGroup = NIOTSEventLoopGroup ( )
0 commit comments