Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(k6): Fix function context test #9368

Merged
merged 1 commit into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions tasks/k6-test/run-k6-tests.mts
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ const SETUPS_DIR = path.join(__dirname, 'setups')
const TESTS_DIR = path.join(__dirname, 'tests')
const API_SERVER_COMMANDS = [
{
cmd: `node ${path.resolve(REDWOODJS_FRAMEWORK_PATH, 'packages/cli/dist/index.js')} serve api`,
cmd: `node ${path.resolve(REDWOOD_PROJECT_DIRECTORY, 'node_modules/@redwoodjs/cli/dist/index.js')} serve api`,
host: 'http://localhost:8911',
},
{
cmd: `node ${path.resolve(REDWOODJS_FRAMEWORK_PATH, 'packages/api-server/dist/index.js')} api`,
cmd: `node ${path.resolve(REDWOOD_PROJECT_DIRECTORY, 'node_modules/@redwoodjs/api-server/dist/index.js')} api`,
host: 'http://localhost:8911'
},
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,19 @@ import type { MutationResolvers } from 'types/graphql'

import { setContext } from '@redwoodjs/graphql-server'

import { logger } from 'src/lib/logger'

export const magicNumber: MutationResolvers['magicNumber'] = async ({
value,
}) => {
setContext({
// ...context,
magicNumber: value,
})
// context.magicNumber = value

const sleep = Math.random() * 200
// logger.info(`Sleeping for ${sleep}ms`)
await new Promise((resolve) => setTimeout(resolve, sleep))

const numberFromContext = (context.magicNumber ?? -1) as number
if (value !== numberFromContext) {
logger.error(`Expected ${value} but got ${numberFromContext}`)
// throw new Error(`Expected ${value} but got ${numberFromContext}`)
throw new Error(`Expected ${value} but got ${numberFromContext}`)
}

return { value: numberFromContext }
Expand Down
4 changes: 1 addition & 3 deletions tasks/k6-test/setups/context_magic_number/templates/func.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import type { APIGatewayProxyEvent, Context } from 'aws-lambda'

import { setContext } from '@redwoodjs/graphql-server'

import { logger } from 'src/lib/logger'

export const handler = async (
event: APIGatewayProxyEvent,
_context: Context
Expand All @@ -19,7 +17,7 @@ export const handler = async (

const numberFromContext = (context.magicNumber ?? -1) as number
if (magicNumber !== numberFromContext) {
logger.error(`Expected ${magicNumber} but got ${numberFromContext}`)
throw new Error(`Expected ${magicNumber} but got ${numberFromContext}`)
}

return {
Expand Down
25 changes: 2 additions & 23 deletions tasks/k6-test/tests/context_functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,7 @@ export const options = {

export default function () {
const magicNumber = Math.floor(Math.random() * 16000000)

const payload = (value) => {
return JSON.stringify({
query: `mutation MagicNumber($value: Int!) {
magicNumber(value: $value) {
value
}
}`,
variables: {
value,
},
operationName: 'MagicNumber',
})
}

const url = `${__ENV.TEST_HOST}/func`
const params = {
headers: {
'Content-Type': 'application/json',
},
}
const res = http.post(url, payload(magicNumber), params)
const res = http.get(`${__ENV.TEST_HOST}/func?magicNumber=${magicNumber}`)

const requestPassed = check(res, {
'status was 200': (r) => r.status == 200,
Expand All @@ -51,7 +30,7 @@ export default function () {

const contextPassed = check(res, {
'correct magic number': (r) =>
r.body != null && r.body.includes(`"value":${magicNumber}}`),
r.body != null && r.body.includes(`"value":"${magicNumber}"}`),
})
if (!contextPassed) {
contextErrorCounter.add(1)
Expand Down
Loading