import { strict as assert } from "assert" ; import { EventEmitter } from "events" ; import { IContext , IQueuedMessage , IPartitionLambda , IPartitionLambdaConfig , IPartitionLambdaFactory , ISequencedOperationMessage , } from "@fluidframework/server-services-core" ; export class TestLambda implements IPartitionLambda { public handleCalls = 0 ; private readonly documentId : string ; private failHandler = false ; private throwHandler = false ; constructor ( config , private readonly context ) { this . documentId = config . documentId ; assert ( this . documentId ) ; } public handler ( message ) { this . handleCalls ++ ; const sequencedMessage = message . value as ISequencedOperationMessage ; assert . equal ( this . documentId , sequencedMessage . documentId ) ; if ( this . failHandler ) { this . context . error ( "Test failure" , { restart : true } ) ; } else if ( this . throwHandler ) { throw new Error ( "Test Error" ) ; } else { this . context . checkpoint ( message ) ; } return undefined ; } public close ( ) { return ; } public setThrowExceptionInHandler ( value ) { this . throwHandler = value ; } public setFailHandlers ( value ) { this . failHandler = value ; } } export class TestLambdaFactory extends EventEmitter implements IPartitionLambdaFactory { public lambdas : TestLambda [ ] = [ ] ; public disposed = false ; private failCreatelambda = false ; constructor ( ) { super ( ) ; } public async create ( config , context ) { if ( this . failCreatelambda ) { return Promise . reject ( new Error ( "Test failure" ) ) ; } else { const lambda = new TestLambda ( config , context ) ; this . lambdas . push ( lambda ) ; return lambda ; } } public async dispose ( ) { this . disposed = true ; return ; } public setFailCreateLambda ( value ) { this . failCreatelambda = value ; } public setThrowExceptionInHandler ( value ) { for ( const lambda of this . lambdas ) { lambda . setThrowExceptionInHandler ( value ) ; } } public setFailHandlers ( value ) { for ( const lambda of this . lambdas ) { lambda . setFailHandlers ( value ) ; } } } export const create = ( ) : IPartitionLambdaFactory => new TestLambdaFactory ( ) ; export interface ITestLambdaModule { create : ( ) => TestLambdaFactory ; factories : TestLambdaFactory [ ] ; } export function createTestModule ( ) { const factories = [ ] ; return { create : ( ) => { const factory = new TestLambdaFactory ( ) ; factories . push ( factory ) ; return factory ; } , factories } ; }