@@ -11,9 +11,11 @@ import {
1111 type ArchiveCopyRecord ,
1212 type ArchiveSetUpsert ,
1313} from "./outflowArchive.js" ;
14+ import { startArchiveProxy } from "./testFixtures/archiveProxy.js" ;
1415import {
1516 ARCHIVE_STAND_IN_REFUSED_TASK ,
1617 archiveStandInUpsert ,
18+ createArchiveRole ,
1719 createArchiveStandIn ,
1820 readArchiveRow ,
1921} from "./testFixtures/archiveStandIn.js" ;
@@ -631,4 +633,99 @@ describe("outflow archive copy", () => {
631633 }
632634 }
633635 ) ;
636+
637+ // Catches: a ping that fails while the archive answers, or that waits past the write timeout while
638+ // it does not.
639+ heteroRunOpsPostgresTest (
640+ "answers a ping while the archive is up and rejects within the write timeout while it is down" ,
641+ async ( { uri17 } ) => {
642+ const proxy = await startArchiveProxy ( uri17 ) ;
643+ const writer = new OutflowArchiveWriter ( {
644+ connectionString : proxy . connectionString ,
645+ poolSize : 1 ,
646+ writeTimeoutMs : 300 ,
647+ reclaimIdleMs : 60_000 ,
648+ upsert : archiveStandInUpsert ,
649+ } ) ;
650+ try {
651+ await writer . ping ( ) ;
652+ proxy . setUp ( false ) ;
653+ // The first ping may take the pooled connection the outage ended; the second opens a new
654+ // one that gets no answer.
655+ for ( let i = 0 ; i < 2 ; i ++ ) {
656+ const started = performance . now ( ) ;
657+ await expect ( writer . ping ( ) ) . rejects . toThrow ( ) ;
658+ expect ( performance . now ( ) - started ) . toBeLessThan ( 300 + 200 ) ;
659+ }
660+ proxy . setUp ( true ) ;
661+ await writer . ping ( ) ;
662+ expect ( proxy . accepts . some ( ( accept ) => ! accept . up ) ) . toBe ( true ) ;
663+ } finally {
664+ await writer . close ( ) ;
665+ await proxy . close ( ) ;
666+ }
667+ }
668+ ) ;
669+
670+ // Catches: a ping that waits on a connection that stops answering after it was established, which
671+ // only the ping's own deadline ends (the pool's connect timeout covers connecting only).
672+ heteroRunOpsPostgresTest (
673+ "rejects a ping within the write timeout when an established connection goes silent" ,
674+ async ( { uri17 } ) => {
675+ const proxy = await startArchiveProxy ( uri17 ) ;
676+ const writer = new OutflowArchiveWriter ( {
677+ connectionString : proxy . connectionString ,
678+ poolSize : 1 ,
679+ writeTimeoutMs : 300 ,
680+ reclaimIdleMs : 60_000 ,
681+ upsert : archiveStandInUpsert ,
682+ } ) ;
683+ try {
684+ // The pool keeps this connection, and the next ping is sent on it.
685+ await writer . ping ( ) ;
686+ proxy . setSilent ( true ) ;
687+ const outcome = await Promise . race ( [
688+ writer . ping ( ) . then (
689+ ( ) => "answered" ,
690+ ( ) => "rejected"
691+ ) ,
692+ new Promise ( ( resolve ) => setTimeout ( ( ) => resolve ( "pending" ) , 300 + 200 ) ) ,
693+ ] ) ;
694+ expect ( outcome ) . toBe ( "rejected" ) ;
695+ expect ( proxy . accepts ) . toHaveLength ( 1 ) ;
696+ proxy . setSilent ( false ) ;
697+ await writer . ping ( ) ;
698+ } finally {
699+ // The proxy first: a ping still waiting holds its connection, and closing the pool waits for it.
700+ await proxy . close ( ) ;
701+ await writer . close ( ) ;
702+ }
703+ }
704+ ) ;
705+
706+ // Catches: a ping that answers on a session that cannot write, so a failing worker resumes claims
707+ // while every upsert is still rejected.
708+ heteroRunOpsPostgresTest (
709+ "rejects a ping on a read-only session and answers once the archive is writable" ,
710+ async ( { uri17 } ) => {
711+ const role = await createArchiveRole ( uri17 ) ;
712+ await role . setReadOnly ( true ) ;
713+ const writer = new OutflowArchiveWriter ( {
714+ connectionString : role . connectionString ,
715+ poolSize : 1 ,
716+ writeTimeoutMs : 10_000 ,
717+ reclaimIdleMs : 60_000 ,
718+ upsert : archiveStandInUpsert ,
719+ } ) ;
720+ try {
721+ await expect ( writer . ping ( ) ) . rejects . toThrow ( / r e a d - o n l y / ) ;
722+ // The rejected ping destroyed its session, so the next opens one in the new mode.
723+ await role . setReadOnly ( false ) ;
724+ await writer . ping ( ) ;
725+ } finally {
726+ await writer . close ( ) ;
727+ await role . drop ( ) ;
728+ }
729+ }
730+ ) ;
634731} ) ;
0 commit comments