-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: AONE#54728515
- Loading branch information
Showing
6 changed files
with
178 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
'use strict'; | ||
|
||
addEventListener('fetch', event => { | ||
event.respondWith( | ||
Promise.resolve().then(async () => { | ||
const time = await event.request.text(); | ||
const body = new ReadableStream({ | ||
start(controller) { | ||
controller.enqueue('foobar'); | ||
setTimeout(() => { | ||
controller.enqueue('end'); | ||
controller.close(); | ||
}, parseInt(time, 10)); | ||
}, | ||
}); | ||
|
||
return new Response(body, { | ||
status: 200, | ||
}); | ||
}) | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import * as common from '#self/test/common'; | ||
import { baselineDir } from '#self/test/common'; | ||
import assert from 'assert'; | ||
import { bufferFromStream, sleep } from '#self/lib/util'; | ||
import { DefaultEnvironment } from '#self/test/env/environment'; | ||
|
||
describe(common.testName(__filename), function () { | ||
// Debug version of Node.js may take longer time to bootstrap. | ||
this.timeout(60_000); | ||
|
||
const env = new DefaultEnvironment({ | ||
config: common.extendDefaultConfig({ | ||
controlPlane: { | ||
useEmaScaling: true, | ||
dumpWorkerTrafficStats: true, | ||
}, | ||
dataPlane: { | ||
closeTrafficTimeout: 2000, | ||
}, | ||
}), | ||
}); | ||
|
||
it('should close worker', async () => { | ||
await env.agent.setFunctionProfile([ | ||
{ | ||
name: 'aworker_stream', | ||
runtime: 'aworker', | ||
url: `file://${baselineDir}/aworker_stream`, | ||
sourceFile: 'stream_with_delay.js', | ||
signature: 'md5:234234', | ||
worker: { | ||
maxActivateRequests: 1, | ||
disposable: true, | ||
}, | ||
resourceLimit: { | ||
memory: 200 * 1024 * 1024, | ||
}, | ||
}, | ||
]); | ||
|
||
await request('aworker_stream', env); | ||
|
||
await sleep(1000); | ||
|
||
const broker = env.data.dataFlowController.brokers.get( | ||
'aworker_stream$$noinspect' | ||
); | ||
|
||
assert.strictEqual(broker?.workerCount, 0); | ||
}); | ||
|
||
it('should close worker with timeout', async () => { | ||
await env.agent.setFunctionProfile([ | ||
{ | ||
name: 'aworker_stream_delay', | ||
runtime: 'aworker', | ||
url: `file://${baselineDir}/aworker_stream`, | ||
sourceFile: 'stream_with_delay.js', | ||
signature: 'md5:234234', | ||
worker: { | ||
maxActivateRequests: 1, | ||
concurrencyShrinkThreshold: 0.6, | ||
}, | ||
resourceLimit: { | ||
memory: 200 * 1024 * 1024, | ||
}, | ||
}, | ||
]); | ||
|
||
request('aworker_stream_delay', env, 30000).catch(error => { | ||
assert(error); | ||
assert(error.message.includes('Peer connection closed')); | ||
}); | ||
|
||
await sleep(1000); | ||
|
||
let broker = env.data.dataFlowController.brokers.get( | ||
'aworker_stream_delay$$noinspect' | ||
); | ||
|
||
const worker = Array.from( | ||
broker?.['_workerMap'].values() || [] | ||
).pop() as any; | ||
|
||
env.data.dataFlowController.closeTraffic([ | ||
{ | ||
functionName: 'aworker_stream_delay', | ||
inspector: false, | ||
workers: [ | ||
{ | ||
credential: worker!.worker!.credential, | ||
}, | ||
], | ||
}, | ||
]); | ||
|
||
await sleep(3000); | ||
|
||
broker = env.data.dataFlowController.brokers.get( | ||
'aworker_stream_delay$$noinspect' | ||
); | ||
|
||
assert.strictEqual(broker?.workerCount, 0); | ||
}); | ||
}); | ||
|
||
async function request( | ||
functionName: string, | ||
env: DefaultEnvironment, | ||
timeout = 0 | ||
) { | ||
const data = Buffer.from(timeout + ''); | ||
|
||
const response = await env.agent.invoke(functionName, data, { | ||
method: 'POST', | ||
timeout: 30000, | ||
}); | ||
|
||
assert.strictEqual(response.status, 200); | ||
|
||
return await bufferFromStream(response); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters