@@ -2,7 +2,7 @@ import { PgClient } from "@effect/sql-pg"
22import * as SqlClient from "@effect/sql/SqlClient"
33import * as Statement from "@effect/sql/Statement"
44import { assert , expect , it } from "@effect/vitest"
5- import { Effect , Redacted , String } from "effect"
5+ import { Effect , Fiber , Redacted , String } from "effect"
66import * as Chunk from "effect/Chunk"
77import * as Stream from "effect/Stream"
88import * as TestServices from "effect/TestServices"
@@ -344,3 +344,37 @@ it.layer(PgContainer.ClientFromPoolLive, { timeout: "30 seconds" })("PgClient fr
344344 expect ( sql . config . database ) . toEqual ( parsedConfig . database )
345345 } ) )
346346} )
347+
348+ it . layer ( PgContainer . ClientSingleConnectionLive , { timeout : "30 seconds" } ) ( "PgClient listen" , ( it ) => {
349+ it . scoped ( "listen does not reserve a pool connection" , ( ) =>
350+ Effect . gen ( function * ( ) {
351+ const sql = yield * PgClient . PgClient
352+ const channel = "pool_connection_listen"
353+
354+ const listenFiber = yield * sql . listen ( channel ) . pipe (
355+ Stream . take ( 1 ) ,
356+ Stream . runCollect ,
357+ Effect . forkScoped
358+ )
359+
360+ yield * Effect . sleep ( "250 millis" )
361+
362+ const rows = yield * sql < { value : number } > `SELECT 1 as value` . pipe (
363+ Effect . timeoutFail ( {
364+ duration : "3 seconds" ,
365+ onTimeout : ( ) => new Error ( "query timed out while listener was active" )
366+ } )
367+ )
368+ expect ( rows ) . toEqual ( [ { value : 1 } ] )
369+
370+ yield * sql `SELECT pg_notify(${ channel } , ${ "payload" } )`
371+
372+ const payloads = yield * Fiber . join ( listenFiber ) . pipe (
373+ Effect . timeoutFail ( {
374+ duration : "3 seconds" ,
375+ onTimeout : ( ) => new Error ( "listener did not receive notification in time" )
376+ } )
377+ )
378+ expect ( Chunk . toReadonlyArray ( payloads ) ) . toEqual ( [ "payload" ] )
379+ } ) . pipe ( TestServices . provideLive ) )
380+ } )
0 commit comments