@@ -15,11 +15,13 @@ import { APP_BASE_HREF } from '@angular/common';
1515import {
1616 Component ,
1717 DestroyRef ,
18+ EnvironmentInjector ,
1819 PendingTasks ,
1920 PlatformRef ,
2021 REQUEST ,
2122 RESPONSE_INIT ,
2223 inject ,
24+ provideAppInitializer ,
2325} from '@angular/core' ;
2426import { ActivatedRoute , CanActivateFn , Router } from '@angular/router' ;
2527import { setTimeout } from 'node:timers/promises' ;
@@ -30,6 +32,7 @@ import { setAngularAppTestingManifest } from './testing-utils';
3032describe ( 'AngularServerApp' , ( ) => {
3133 let app : AngularServerApp ;
3234 let slowRouteHook : ( ( ) => void ) | undefined ;
35+ let appInitializerHook : ( ( ) => Promise < void > ) | undefined ;
3336
3437 @Component ( {
3538 selector : 'app-home' ,
@@ -198,6 +201,7 @@ describe('AngularServerApp', () => {
198201 provide : APP_BASE_HREF ,
199202 useFactory : ( ) => inject ( REQUEST ) ?. headers . get ( 'X-Forwarded-Prefix' ) ,
200203 } ,
204+ provideAppInitializer ( ( ) => appInitializerHook ?.( ) ) ,
201205 ] ,
202206 ) ;
203207
@@ -340,6 +344,8 @@ describe('AngularServerApp', () => {
340344 } ) ;
341345 let appDestroyed = false ;
342346 let destroySpy : jasmine . Spy | undefined ;
347+ const abortTimeout = new AbortController ( ) ;
348+ spyOn ( AbortSignal , 'timeout' ) . and . returnValue ( abortTimeout . signal ) ;
343349
344350 slowRouteHook = ( ) => {
345351 destroySpy = spyOn ( inject ( PlatformRef ) , 'destroy' ) . and . callThrough ( ) ;
@@ -369,6 +375,11 @@ describe('AngularServerApp', () => {
369375 DOMException ,
370376 / R e q u e s t f o r : h t t p : \/ \/ l o c a l h o s t \/ p a g e - s l o w w a s a b o r t e d / ,
371377 ) ;
378+ await setTimeout ( 0 ) ;
379+ expect ( destroySpy ) . not . toHaveBeenCalled ( ) ;
380+
381+ // Simulate the abort timeout elapsing as the pending task never resolves
382+ abortTimeout . abort ( ) ;
372383
373384 // Wait for the macrotask queue to clear since platform destruction is scheduled via setTimeout(0)
374385 await setTimeout ( 0 ) ;
@@ -379,6 +390,103 @@ describe('AngularServerApp', () => {
379390 }
380391 } ) ;
381392
393+ it ( 'should destroy the platform once pending work settles when an in-flight request is aborted' , async ( ) => {
394+ const { promise : taskReady , resolve : resolveTaskReady } = Promise . withResolvers < void > ( ) ;
395+ const { promise : workDone , resolve : resolveWork } = Promise . withResolvers < void > ( ) ;
396+ const { promise : platformDestroyed , resolve : resolvePlatformDestroyed } =
397+ Promise . withResolvers < void > ( ) ;
398+ let pendingWork : Promise < unknown > | undefined ;
399+ let destroySpy : jasmine . Spy | undefined ;
400+
401+ slowRouteHook = ( ) => {
402+ const platformRef = inject ( PlatformRef ) ;
403+ destroySpy = spyOn ( platformRef , 'destroy' ) . and . callThrough ( ) ;
404+ platformRef . onDestroy ( resolvePlatformDestroyed ) ;
405+
406+ // Like an `HttpClient` request, this work holds a pending task and uses the application once it settles
407+ const injector = inject ( EnvironmentInjector ) ;
408+ const removeTask = inject ( PendingTasks ) . add ( ) ;
409+ pendingWork = workDone . then ( ( ) => injector . get ( Router ) ) . finally ( removeTask ) ;
410+ resolveTaskReady ( ) ;
411+ } ;
412+
413+ try {
414+ const controller = new AbortController ( ) ;
415+ const request = new Request ( 'http://localhost/page-slow' , { signal : controller . signal } ) ;
416+
417+ const handlePromise = app . handle ( request ) ;
418+
419+ // Abort the request while the work is still pending
420+ await taskReady ;
421+ controller . abort ( ) ;
422+
423+ await expectAsync ( handlePromise ) . toBeRejectedWithError (
424+ DOMException ,
425+ / R e q u e s t f o r : h t t p : \/ \/ l o c a l h o s t \/ p a g e - s l o w w a s a b o r t e d / ,
426+ ) ;
427+ await setTimeout ( 0 ) ;
428+ expect ( destroySpy ) . not . toHaveBeenCalled ( ) ;
429+
430+ resolveWork ( ) ;
431+ await expectAsync ( pendingWork ) . toBeResolved ( ) ;
432+
433+ await platformDestroyed ;
434+ expect ( destroySpy ) . toHaveBeenCalledTimes ( 1 ) ;
435+ } finally {
436+ slowRouteHook = undefined ;
437+ }
438+ } ) ;
439+
440+ it ( 'should wait for a pending app initializer before destroying the platform when the request is aborted' , async ( ) => {
441+ // Warm up the router so route extraction does not run the app initializer during this test
442+ const warmupResponse = await app . handle ( new Request ( 'http://localhost/home' ) ) ;
443+ await warmupResponse ?. text ( ) ;
444+ await setTimeout ( 0 ) ;
445+
446+ const { promise : initializerStarted , resolve : resolveInitializerStarted } =
447+ Promise . withResolvers < void > ( ) ;
448+ const { promise : initializerDone , resolve : resolveInitializer } =
449+ Promise . withResolvers < void > ( ) ;
450+ const { promise : platformDestroyed , resolve : resolvePlatformDestroyed } =
451+ Promise . withResolvers < void > ( ) ;
452+ let destroySpy : jasmine . Spy | undefined ;
453+
454+ appInitializerHook = ( ) => {
455+ const platformRef = inject ( PlatformRef ) ;
456+ destroySpy = spyOn ( platformRef , 'destroy' ) . and . callThrough ( ) ;
457+ platformRef . onDestroy ( resolvePlatformDestroyed ) ;
458+ resolveInitializerStarted ( ) ;
459+
460+ return initializerDone ;
461+ } ;
462+
463+ try {
464+ const controller = new AbortController ( ) ;
465+ const request = new Request ( 'http://localhost/home' , { signal : controller . signal } ) ;
466+
467+ const handlePromise = app . handle ( request ) ;
468+
469+ // Abort the request while the app initializer is still pending
470+ await initializerStarted ;
471+ controller . abort ( ) ;
472+
473+ await expectAsync ( handlePromise ) . toBeRejectedWithError (
474+ DOMException ,
475+ / R e q u e s t f o r : h t t p : \/ \/ l o c a l h o s t \/ h o m e w a s a b o r t e d / ,
476+ ) ;
477+ await setTimeout ( 0 ) ;
478+ expect ( destroySpy ) . not . toHaveBeenCalled ( ) ;
479+
480+ // Bootstrapping reads from the application injector once the initializer settles
481+ resolveInitializer ( ) ;
482+
483+ await platformDestroyed ;
484+ expect ( destroySpy ) . toHaveBeenCalledTimes ( 1 ) ;
485+ } finally {
486+ appInitializerHook = undefined ;
487+ }
488+ } ) ;
489+
382490 it ( 'should not bootstrap the platform if the request signal is already aborted' , async ( ) => {
383491 // Warm up the router so route extraction does not create a PlatformRef during this test
384492 const warmupResponse = await app . handle ( new Request ( 'http://localhost/home' ) ) ;
0 commit comments