22// Licensed under the MIT License.
33
44import { expect } from 'chai' ;
5+ import * as fs from 'fs/promises' ;
56import 'mocha' ;
7+ import * as path from 'path' ;
68import { AzureFunctionsRpcMessages as rpc } from '../../azure-functions-language-worker-protobuf/src/rpc' ;
79import { getLegacyFunction } from '../../src/LegacyFunctionLoader' ;
810import { worker } from '../../src/WorkerContext' ;
@@ -11,6 +13,7 @@ import { nonNullValue } from '../../src/utils/nonNull';
1113import { RegExpStreamingMessage , TestEventStream } from './TestEventStream' ;
1214import { beforeEventHandlerSuite } from './beforeEventHandlerSuite' ;
1315import { msg } from './msg' ;
16+ import { tempFile , testAppSrcPath } from './testAppUtils' ;
1417
1518describe ( 'FunctionLoadHandler' , ( ) => {
1619 let stream : TestEventStream ;
@@ -44,22 +47,31 @@ describe('FunctionLoadHandler', () => {
4447 it ( 'handles transient lstat function load exception' , async function ( this : Mocha . ITestCallbackContext ) : Promise < void > {
4548 // https://github.com/Azure/azure-functions-nodejs-worker/issues/693
4649
47- this . timeout ( 40 * 1000 ) ;
50+ this . timeout ( 15 * 1000 ) ;
4851
49- stream . addTestMessage ( msg . funcLoad . request ( 'throwLstatError.js' ) ) ;
52+ await fs . writeFile (
53+ path . join ( testAppSrcPath , tempFile ) ,
54+ `if (Date.now() < ${ Date . now ( ) + 5 * 1000 } )
55+ {
56+ throw new Error("UNKNOWN: unknown error, lstat 'D:\\\\home'");
57+ } else {
58+ module.exports = async () => { }
59+ }`
60+ ) ;
61+
62+ stream . addTestMessage ( msg . funcLoad . request ( tempFile ) ) ;
5063
5164 const errorMessage = "UNKNOWN: unknown error, lstat 'D:\\home'" ;
52- const msgs : ( rpc . IStreamingMessage | RegExpStreamingMessage ) [ ] = [ msg . funcLoad . receivedRequestLog ] ;
53- for ( let i = 2 ; i <= 10 ; i ++ ) {
54- msgs . push (
55- msg . warningLog ( `Warning: Failed to load file "throwLstatError.js" with error " ${ errorMessage } "` ) ,
56- msg . debugLog ( `Retrying load of file "throwLstatError.js". Attempt ${ i } /10` )
57- ) ;
65+ const msgs : ( rpc . IStreamingMessage | RegExpStreamingMessage ) [ ] = [
66+ msg . funcLoad . receivedRequestLog ,
67+ msg . warningLog ( `Warning: Failed to load file with error " ${ errorMessage } "` ) ,
68+ ] ;
69+ for ( let i = 2 ; i <= 5 ; i ++ ) {
70+ msgs . push ( msg . debugLog ( `Retrying file load. Attempt ${ i } /10` ) ) ;
5871 }
59- const message = `Worker was unable to load function testFuncName: '${ errorMessage } '` ;
60- msgs . push ( msg . errorLog ( message ) , msg . funcLoad . failedResponse ( message ) ) ;
72+ msgs . push ( msg . funcLoad . response ) ;
6173
62- await delay ( 30 * 1000 ) ;
74+ await delay ( 8 * 1000 ) ;
6375
6476 await stream . assertCalledWith ( ...msgs ) ;
6577 } ) ;
0 commit comments