@@ -794,6 +794,54 @@ describe('PaymasterWorker', () => {
794794 MockDate . reset ( ) ;
795795 } ) ;
796796
797+ test ( 'Should process every workspace when there are several batches' , async ( ) => {
798+ /**
799+ * 50 > WORKSPACE_PROCESSING_CONCURRENCY (25), so the subscription check
800+ * has to flush more than one batch.
801+ */
802+ const WORKSPACES_COUNT = 50 ;
803+ const currentDate = new Date ( '2005-12-22' ) ;
804+ const plan = createPlanMock ( {
805+ monthlyCharge : 0 ,
806+ isDefault : true ,
807+ } ) ;
808+
809+ const workspaces = Array . from ( { length : WORKSPACES_COUNT } , ( ) =>
810+ createWorkspaceMock ( {
811+ plan,
812+ subscriptionId : null ,
813+ lastChargeDate : new Date ( '2005-11-22' ) ,
814+ isBlocked : false ,
815+ billingPeriodEventsCount : 0 ,
816+ } )
817+ ) ;
818+
819+ await tariffCollection . insertOne ( plan ) ;
820+ await workspacesCollection . insertMany ( workspaces ) ;
821+
822+ MockDate . set ( currentDate ) ;
823+
824+ const worker = new PaymasterWorker ( ) ;
825+ const processSpy = jest
826+ . spyOn ( worker as any , 'processWorkspaceSubscriptionCheck' )
827+ . mockResolvedValue ( [ null , false ] ) ;
828+
829+ await worker . start ( ) ;
830+ await worker . handle ( WORKSPACE_SUBSCRIPTION_CHECK ) ;
831+ await worker . finish ( ) ;
832+
833+ expect ( processSpy ) . toHaveBeenCalledTimes ( WORKSPACES_COUNT ) ;
834+
835+ const calledIds = processSpy . mock . calls
836+ . map ( ( call ) => ( call [ 0 ] as WorkspaceDBScheme ) . _id . toString ( ) )
837+ . sort ( ) ;
838+ const expectedIds = workspaces . map ( ( w ) => w . _id . toString ( ) ) . sort ( ) ;
839+
840+ expect ( calledIds ) . toEqual ( expectedIds ) ;
841+
842+ MockDate . reset ( ) ;
843+ } ) ;
844+
797845 afterAll ( async ( ) => {
798846 await connection . close ( ) ;
799847 MockDate . reset ( ) ;
0 commit comments