@@ -1057,6 +1057,75 @@ async fn test_listener_cleanup() -> anyhow::Result<()> {
10571057 Ok ( ( ) )
10581058}
10591059
1060+ #[ sqlx_macros:: test]
1061+ async fn test_listener_try_recv_buffered ( ) -> anyhow:: Result < ( ) > {
1062+ use sqlx_core:: rt:: timeout;
1063+
1064+ use sqlx:: pool:: PoolOptions ;
1065+ use sqlx:: postgres:: PgListener ;
1066+
1067+ // Create a connection on which to send notifications
1068+ let mut notify_conn = new :: < Postgres > ( ) . await ?;
1069+
1070+ let pool = PoolOptions :: < Postgres > :: new ( )
1071+ . min_connections ( 1 )
1072+ . max_connections ( 1 )
1073+ . test_before_acquire ( true )
1074+ . connect ( & env:: var ( "DATABASE_URL" ) ?)
1075+ . await ?;
1076+
1077+ let mut listener = PgListener :: connect_with ( & pool) . await ?;
1078+ listener. listen ( "test_channel2" ) . await ?;
1079+
1080+ // Checks for a notification on the test channel
1081+ async fn try_recv ( listener : & mut PgListener ) -> anyhow:: Result < bool > {
1082+ match timeout ( Duration :: from_millis ( 100 ) , listener. recv ( ) ) . await {
1083+ Ok ( res) => {
1084+ res?;
1085+ Ok ( true )
1086+ }
1087+ Err ( _) => Ok ( false ) ,
1088+ }
1089+ }
1090+
1091+ // Check no notification is buffered, since we haven't sent one.
1092+ assert ! ( listener. next_buffered( ) . is_none( ) ) ;
1093+
1094+ // Send five notifications transactionally, so they all arrive at once.
1095+ {
1096+ let mut txn = notify_conn. begin ( ) . await ?;
1097+ for i in 0 ..5 {
1098+ txn. execute ( format ! ( "NOTIFY test_channel2, 'payload {i}'" ) . as_str ( ) )
1099+ . await ?;
1100+ }
1101+ txn. commit ( ) . await ?;
1102+ }
1103+
1104+ // Still no notifications buffered, since we haven't awaited the listener yet.
1105+ assert ! ( listener. next_buffered( ) . is_none( ) ) ;
1106+
1107+ // Activate connection.
1108+ sqlx:: query!( "SELECT 1 AS one" )
1109+ . fetch_all ( & mut listener)
1110+ . await ?;
1111+
1112+ // The next five notifications should now be buffered.
1113+ for i in 0 ..5 {
1114+ assert ! (
1115+ listener. next_buffered( ) . is_some( ) ,
1116+ "Notification {i} was not buffered"
1117+ ) ;
1118+ }
1119+
1120+ // Should be no more.
1121+ assert ! ( listener. next_buffered( ) . is_none( ) ) ;
1122+
1123+ // Even if we wait.
1124+ assert ! ( !try_recv( & mut listener) . await ?, "Notification received" ) ;
1125+
1126+ Ok ( ( ) )
1127+ }
1128+
10601129#[ sqlx_macros:: test]
10611130async fn test_pg_listener_allows_pool_to_close ( ) -> anyhow:: Result < ( ) > {
10621131 let pool = pool :: < Postgres > ( ) . await ?;
0 commit comments