@@ -12,16 +12,41 @@ module.exports = function describe(workerType) {
1212 await workerNodes . call ( 'hello!' ) ;
1313
1414 // when
15- workerNodes . takeSnapshot ( ) ;
16- const getHeapSnapshotFilename = workerType === "thread" ?
17- ( ) => fs . readdirSync ( process . cwd ( ) ) . find ( name => name . includes ( '.heapsnapshot' ) && name . includes ( `-${ process . pid } -` ) ) :
18- ( ) => fs . readdirSync ( process . cwd ( ) ) . find ( name => name . includes ( '.heapsnapshot' ) && ! name . includes ( `-${ process . pid } -` ) ) ;
19- await eventually ( ( ) => getHeapSnapshotFilename ( ) !== undefined ) ;
15+ const filePath = await workerNodes . takeSnapshot ( ) ;
2016
21- const result = getHeapSnapshotFilename ( ) ;
22- t . truthy ( result ) ;
23- t . true ( result . length > 0 )
24- fs . unlinkSync ( result ) ;
17+ t . regex ( filePath , / ^ H e a p S n a p s h o t - \d + - \d + \. h e a p s n a p s h o t $ / ) ;
18+ t . true ( fs . existsSync ( filePath ) )
19+ fs . unlinkSync ( filePath ) ;
20+ } ) ;
21+
22+ test . serial ( `should restart worker after taking heap snapshot when restartWorker option was set` , async ( t ) => {
23+ // given
24+ const workerNodes = new WorkerNodes ( fixture ( 'echo-function-async' ) , { lazyStart : true , workerType } ) ;
25+ await workerNodes . ready ( ) ;
26+ await workerNodes . call ( 'hello!' ) ;
27+
28+ const workersBefore = workerNodes . getUsedWorkers ( ) ;
29+
30+ // when
31+ const filePath = await workerNodes . takeSnapshot ( { restartWorker : true } ) ;
32+ fs . unlinkSync ( filePath ) ;
33+
34+ // Waiting for a worker to restart
35+ await new Promise ( ( resolve ) => setTimeout ( resolve , 500 ) ) ;
36+
37+ const workersAfter = workerNodes . getUsedWorkers ( ) ;
38+ t . true ( workersBefore . length === workersAfter . length ) ;
39+ t . true ( workersBefore [ 0 ] !== workersAfter [ 0 ] ) ;
40+ } ) ;
41+
42+ test . serial ( `should let takeSnapshot throw an error when there is not enough heap` , async ( t ) => {
43+ const workerNodes = new WorkerNodes ( fixture ( 'mock-heap-statistics' ) , { lazyStart : true , workerType } ) ;
44+ await workerNodes . ready ( ) ;
45+ await workerNodes . call ( ) ;
46+
47+ await t . throwsAsync ( workerNodes . takeSnapshot ( ) , {
48+ message : 'Not enough memory to perform heap snapshot'
49+ } )
2550 } ) ;
2651
2752 test ( `should generate heap profiler result file` , async ( t ) => {
@@ -34,8 +59,8 @@ module.exports = function describe(workerType) {
3459
3560 await workerNodes . call ( 'hello!' ) ;
3661
37- const getCpuProfileFilename = workerType === "thread" ?
38- ( ) => fs . readdirSync ( process . cwd ( ) ) . find ( name => name . includes ( '.cpuprofile' ) && name . includes ( `-${ process . pid } -` ) ) :
62+ const getCpuProfileFilename = workerType === "thread" ?
63+ ( ) => fs . readdirSync ( process . cwd ( ) ) . find ( name => name . includes ( '.cpuprofile' ) && name . includes ( `-${ process . pid } -` ) ) :
3964 ( ) => fs . readdirSync ( process . cwd ( ) ) . find ( name => name . includes ( '.cpuprofile' ) && ! name . includes ( `-${ process . pid } -` ) ) ;
4065
4166 await eventually ( ( ) => getCpuProfileFilename ( ) !== undefined ) ;
@@ -46,4 +71,4 @@ module.exports = function describe(workerType) {
4671 t . true ( result . length > 0 )
4772 fs . unlinkSync ( result ) ;
4873 } ) ;
49- }
74+ }
0 commit comments