-
-
Notifications
You must be signed in to change notification settings - Fork 794
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
084d9e1
commit bc8bc90
Showing
10 changed files
with
211 additions
and
2 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
tests/lambda-run-mode/in-process/env-vars/in-process-env-vars.test.js
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,37 @@ | ||
import assert from 'node:assert' | ||
import { dirname, resolve } from 'node:path' | ||
import { env } from 'node:process' | ||
import { fileURLToPath } from 'node:url' | ||
import { | ||
joinUrl, | ||
setup, | ||
teardown, | ||
} from '../../../integration/_testHelpers/index.js' | ||
|
||
const __dirname = dirname(fileURLToPath(import.meta.url)) | ||
|
||
describe('run mode with worker threads', function desc() { | ||
beforeEach(() => | ||
setup({ | ||
env: { | ||
SHOULD_BE_SHARED: 'SHOULD_BE_SHARED', | ||
}, | ||
servicePath: resolve(__dirname), | ||
}), | ||
) | ||
|
||
afterEach(() => teardown()) | ||
|
||
it('should share env', async () => { | ||
const url = joinUrl(env.TEST_BASE_URL, 'dev/foo') | ||
|
||
const response = await fetch(url) | ||
assert.equal(response.status, 200) | ||
|
||
const json = await response.json() | ||
assert.equal(json.env.ENV_FUNCTIONS_FOO, 'ENV_FUNCTIONS_BAR') | ||
assert.equal(json.env.ENV_PROVIDER_FOO, 'ENV_PROVIDER_BAR') | ||
assert.equal(json.env.IS_OFFLINE, 'true') | ||
assert.equal(json.env.SHOULD_BE_SHARED, 'SHOULD_BE_SHARED') | ||
}) | ||
}) |
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,31 @@ | ||
service: run-mode-tests | ||
|
||
configValidationMode: error | ||
|
||
plugins: | ||
- ../../../../ | ||
|
||
custom: | ||
serverless-offline: | ||
allowCache: true | ||
useInProcess: true | ||
|
||
provider: | ||
environment: | ||
ENV_PROVIDER_FOO: 'ENV_PROVIDER_BAR' | ||
memorySize: 128 | ||
name: aws | ||
region: us-east-1 # default | ||
runtime: nodejs16.x | ||
stage: dev | ||
versionFunctions: false | ||
|
||
functions: | ||
foo: | ||
environment: | ||
ENV_FUNCTIONS_FOO: 'ENV_FUNCTIONS_BAR' | ||
events: | ||
- http: | ||
method: get | ||
path: foo | ||
handler: src/handler.foo |
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,14 @@ | ||
'use strict' | ||
|
||
const { env } = require('process') | ||
|
||
const { stringify } = JSON | ||
|
||
exports.foo = async function foo() { | ||
return { | ||
body: stringify({ | ||
env, | ||
}), | ||
statusCode: 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"type": "commonjs" | ||
} |
74 changes: 74 additions & 0 deletions
74
tests/lambda-run-mode/in-process/instances/in-process-instances.test.js
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,74 @@ | ||
import assert from 'node:assert' | ||
import { dirname, resolve } from 'node:path' | ||
import { env } from 'node:process' | ||
import { fileURLToPath } from 'node:url' | ||
import { promisify } from 'node:util' | ||
import { | ||
joinUrl, | ||
setup, | ||
teardown, | ||
} from '../../../integration/_testHelpers/index.js' | ||
|
||
const setTimeoutPromise = promisify(setTimeout) | ||
|
||
const __dirname = dirname(fileURLToPath(import.meta.url)) | ||
|
||
describe('run mode with in-process', function desc() { | ||
beforeEach(() => | ||
setup({ | ||
servicePath: resolve(__dirname), | ||
}), | ||
) | ||
|
||
afterEach(() => teardown()) | ||
|
||
it('does not create a new lambda instance, instead uses same', async () => { | ||
const url = joinUrl(env.TEST_BASE_URL, 'dev/foo') | ||
|
||
const responses = await Promise.all( | ||
Array.from(Array(10).keys()).map(() => fetch(url)), | ||
) | ||
|
||
responses.forEach((response) => { | ||
assert.equal(response.status, 200) | ||
}) | ||
|
||
const jsons = await Promise.all( | ||
responses.map((response) => response.json()), | ||
) | ||
|
||
jsons.forEach((json) => { | ||
assert.deepEqual(json, 10) | ||
}) | ||
}) | ||
|
||
it('re-uses existing lambda instance when idle', async () => { | ||
const url = joinUrl(env.TEST_BASE_URL, 'dev/foo') | ||
|
||
const results = [] | ||
|
||
// eslint-disable-next-line no-unused-vars | ||
for (const _ of Array(5)) { | ||
// eslint-disable-next-line no-await-in-loop | ||
await setTimeoutPromise(2000) | ||
results.push(fetch(url)) | ||
} | ||
|
||
const responses = await Promise.all(results) | ||
|
||
responses.forEach((response) => { | ||
assert.equal(response.status, 200) | ||
}) | ||
|
||
const jsons = await Promise.all( | ||
responses.map((response) => response.json()), | ||
) | ||
|
||
let sort = 0 | ||
|
||
jsons.sort().forEach((json) => { | ||
sort += 1 | ||
assert.deepEqual(json, sort) | ||
}) | ||
}) | ||
}) |
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,27 @@ | ||
service: run-mode-tests | ||
|
||
configValidationMode: error | ||
|
||
plugins: | ||
- ../../../../ | ||
|
||
custom: | ||
serverless-offline: | ||
allowCache: true | ||
useInProcess: true | ||
|
||
provider: | ||
memorySize: 128 | ||
name: aws | ||
region: us-east-1 # default | ||
runtime: nodejs16.x | ||
stage: dev | ||
versionFunctions: false | ||
|
||
functions: | ||
foo: | ||
events: | ||
- http: | ||
method: get | ||
path: foo | ||
handler: src/handler.foo |
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,20 @@ | ||
'use strict' | ||
|
||
const { promisify } = require('node:util') | ||
|
||
const setTimeoutPromise = promisify(setTimeout) | ||
|
||
const { stringify } = JSON | ||
|
||
let counter = 0 | ||
|
||
exports.foo = async function foo() { | ||
counter += 1 | ||
|
||
await setTimeoutPromise(1000, 'result') | ||
|
||
return { | ||
body: stringify(counter), | ||
statusCode: 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"type": "commonjs" | ||
} |
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