@@ -1454,6 +1454,157 @@ mod tests {
14541454 id
14551455 }
14561456
1457+ async fn make_test_channel (
1458+ pool : & PgPool ,
1459+ community_id : Uuid ,
1460+ ttl_seconds : Option < i32 > ,
1461+ ) -> Uuid {
1462+ let id = Uuid :: new_v4 ( ) ;
1463+ sqlx:: query (
1464+ "INSERT INTO channels \
1465+ (id, community_id, name, created_by, ttl_seconds, ttl_deadline) \
1466+ VALUES ($1, $2, $3, $4, $5, \
1467+ CASE WHEN $5 IS NULL THEN NULL \
1468+ ELSE clock_timestamp() + make_interval(secs => $5) END)",
1469+ )
1470+ . bind ( id)
1471+ . bind ( community_id)
1472+ . bind ( format ! ( "event-ttl-test-{}" , id. simple( ) ) )
1473+ . bind ( vec ! [ 7_u8 ; 32 ] )
1474+ . bind ( ttl_seconds)
1475+ . execute ( pool)
1476+ . await
1477+ . expect ( "insert test channel" ) ;
1478+ id
1479+ }
1480+
1481+ #[ tokio:: test]
1482+ #[ ignore = "requires Postgres" ]
1483+ async fn event_insert_ttl_trigger_handles_permanent_ephemeral_duplicate_and_activation_race ( ) {
1484+ let pool = setup_pool ( ) . await ;
1485+ let community_uuid = make_test_community ( & pool) . await ;
1486+ let community = CommunityId :: from_uuid ( community_uuid) ;
1487+
1488+ let permanent = make_test_channel ( & pool, community_uuid, None ) . await ;
1489+ let permanent_event = make_text_event ( "permanent channel event" ) ;
1490+ assert ! (
1491+ insert_event( & pool, community, & permanent_event, Some ( permanent) )
1492+ . await
1493+ . expect( "insert permanent event" )
1494+ . 1
1495+ ) ;
1496+ let permanent_deadline: Option < DateTime < Utc > > = sqlx:: query_scalar (
1497+ "SELECT ttl_deadline FROM channels WHERE community_id = $1 AND id = $2" ,
1498+ )
1499+ . bind ( community_uuid)
1500+ . bind ( permanent)
1501+ . fetch_one ( & pool)
1502+ . await
1503+ . expect ( "read permanent deadline" ) ;
1504+ assert_eq ! ( permanent_deadline, None ) ;
1505+
1506+ let ephemeral = make_test_channel ( & pool, community_uuid, Some ( 60 ) ) . await ;
1507+ let initial_deadline: DateTime < Utc > = sqlx:: query_scalar (
1508+ "SELECT ttl_deadline FROM channels WHERE community_id = $1 AND id = $2" ,
1509+ )
1510+ . bind ( community_uuid)
1511+ . bind ( ephemeral)
1512+ . fetch_one ( & pool)
1513+ . await
1514+ . expect ( "read initial ephemeral deadline" ) ;
1515+ tokio:: time:: sleep ( std:: time:: Duration :: from_millis ( 20 ) ) . await ;
1516+ let ephemeral_event = make_text_event ( "ephemeral channel event" ) ;
1517+ assert ! (
1518+ insert_event( & pool, community, & ephemeral_event, Some ( ephemeral) )
1519+ . await
1520+ . expect( "insert ephemeral event" )
1521+ . 1
1522+ ) ;
1523+ let bumped_deadline: DateTime < Utc > = sqlx:: query_scalar (
1524+ "SELECT ttl_deadline FROM channels WHERE community_id = $1 AND id = $2" ,
1525+ )
1526+ . bind ( community_uuid)
1527+ . bind ( ephemeral)
1528+ . fetch_one ( & pool)
1529+ . await
1530+ . expect ( "read bumped ephemeral deadline" ) ;
1531+ assert ! ( bumped_deadline > initial_deadline) ;
1532+ assert ! (
1533+ !insert_event( & pool, community, & ephemeral_event, Some ( ephemeral) )
1534+ . await
1535+ . expect( "insert duplicate event" )
1536+ . 1
1537+ ) ;
1538+ let duplicate_deadline: DateTime < Utc > = sqlx:: query_scalar (
1539+ "SELECT ttl_deadline FROM channels WHERE community_id = $1 AND id = $2" ,
1540+ )
1541+ . bind ( community_uuid)
1542+ . bind ( ephemeral)
1543+ . fetch_one ( & pool)
1544+ . await
1545+ . expect ( "read deadline after duplicate" ) ;
1546+ assert_eq ! ( duplicate_deadline, bumped_deadline) ;
1547+
1548+ // Reproduce the blocked stale-prefetch ordering: ingest has already
1549+ // observed a permanent channel, then TTL activation locks/updates the
1550+ // row before the event INSERT reaches its trigger. The trigger must
1551+ // wait and refresh from the later event after activation commits.
1552+ let racing = make_test_channel ( & pool, community_uuid, None ) . await ;
1553+ let stale_ttl: Option < i32 > = sqlx:: query_scalar (
1554+ "SELECT ttl_seconds FROM channels WHERE community_id = $1 AND id = $2" ,
1555+ )
1556+ . bind ( community_uuid)
1557+ . bind ( racing)
1558+ . fetch_one ( & pool)
1559+ . await
1560+ . expect ( "prefetch permanent channel" ) ;
1561+ assert_eq ! ( stale_ttl, None ) ;
1562+
1563+ let mut activation = pool. begin ( ) . await . expect ( "begin TTL activation" ) ;
1564+ let activation_deadline: DateTime < Utc > = sqlx:: query_scalar (
1565+ "UPDATE channels \
1566+ SET ttl_seconds = 60, ttl_deadline = clock_timestamp() + interval '60 seconds' \
1567+ WHERE community_id = $1 AND id = $2 RETURNING ttl_deadline",
1568+ )
1569+ . bind ( community_uuid)
1570+ . bind ( racing)
1571+ . fetch_one ( & mut * activation)
1572+ . await
1573+ . expect ( "activate TTL while holding channel row lock" ) ;
1574+
1575+ let race_pool = pool. clone ( ) ;
1576+ let racing_event = make_text_event ( "event after stale permanent prefetch" ) ;
1577+ let insert = tokio:: spawn ( async move {
1578+ insert_event ( & race_pool, community, & racing_event, Some ( racing) ) . await
1579+ } ) ;
1580+ tokio:: time:: sleep ( std:: time:: Duration :: from_millis ( 100 ) ) . await ;
1581+ assert ! (
1582+ !insert. is_finished( ) ,
1583+ "event trigger must wait on TTL activation"
1584+ ) ;
1585+ activation. commit ( ) . await . expect ( "commit TTL activation" ) ;
1586+ assert ! (
1587+ insert
1588+ . await
1589+ . expect( "join racing insert" )
1590+ . expect( "racing insert" )
1591+ . 1
1592+ ) ;
1593+
1594+ let final_deadline: DateTime < Utc > = sqlx:: query_scalar (
1595+ "SELECT ttl_deadline FROM channels WHERE community_id = $1 AND id = $2" ,
1596+ )
1597+ . bind ( community_uuid)
1598+ . bind ( racing)
1599+ . fetch_one ( & pool)
1600+ . await
1601+ . expect ( "read deadline after racing event" ) ;
1602+ assert ! (
1603+ final_deadline > activation_deadline + chrono:: Duration :: milliseconds( 50 ) ,
1604+ "later event must extend TTL beyond activation deadline: activation={activation_deadline}, final={final_deadline}"
1605+ ) ;
1606+ }
1607+
14571608 #[ tokio:: test]
14581609 #[ ignore = "requires Postgres" ]
14591610 async fn get_event_by_id_is_scoped_when_event_id_collides_across_communities ( ) {
0 commit comments