Skip to content

Commit

Permalink
test: add in-process runner tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dnalborczyk committed Jul 5, 2022
1 parent 084d9e1 commit bc8bc90
Show file tree
Hide file tree
Showing 10 changed files with 211 additions and 2 deletions.
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')
})
})
31 changes: 31 additions & 0 deletions tests/lambda-run-mode/in-process/env-vars/serverless.yml
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
14 changes: 14 additions & 0 deletions tests/lambda-run-mode/in-process/env-vars/src/handler.js
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,
}
}
3 changes: 3 additions & 0 deletions tests/lambda-run-mode/in-process/env-vars/src/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "commonjs"
}
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)
})
})
})
27 changes: 27 additions & 0 deletions tests/lambda-run-mode/in-process/instances/serverless.yml
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
20 changes: 20 additions & 0 deletions tests/lambda-run-mode/in-process/instances/src/handler.js
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,
}
}
3 changes: 3 additions & 0 deletions tests/lambda-run-mode/in-process/instances/src/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "commonjs"
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {

const __dirname = dirname(fileURLToPath(import.meta.url))

describe('run mode with worker threads tests', function desc() {
describe('run mode with worker threads', function desc() {
beforeEach(() =>
setup({
env: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const setTimeoutPromise = promisify(setTimeout)

const __dirname = dirname(fileURLToPath(import.meta.url))

describe('run mode with worker threads tests', function desc() {
describe('run mode with worker threads', function desc() {
beforeEach(() =>
setup({
servicePath: resolve(__dirname),
Expand Down

0 comments on commit bc8bc90

Please sign in to comment.