Skip to content

Commit 01b5653

Browse files
committed
fix context issue
1 parent 9cde765 commit 01b5653

1 file changed

Lines changed: 28 additions & 14 deletions

File tree

artillery/notify_entrypoint.mjs

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,33 +52,47 @@ function generateValidNhsNumber() {
5252
}
5353
}
5454

55-
export function initUser(_, vuContext) {
55+
// Apparently Math.sampleNormal isn't a function? Do a quick Box-Muller transform instead
56+
function sampleNormal(mean = 0, sd = 1) {
57+
let u = 0, v = 0;
58+
// avoid zeros because of log(0)
59+
while (u === 0) u = Math.random();
60+
while (v === 0) v = Math.random();
61+
const z = Math.sqrt(-2 * Math.log(u)) * Math.cos(2 * Math.PI * v);
62+
return z * sd + mean;
63+
}
64+
65+
export function initUser(context, events, done) {
5666
// Generate data for a patient
57-
vuContext.vars.odsCode = fullOdsCodes[Math.floor(Math.random()*fullOdsCodes.length)]
58-
vuContext.vars.nhsNumber = generateValidNhsNumber()
67+
context.vars.odsCode = fullOdsCodes[Math.floor(Math.random()*fullOdsCodes.length)]
68+
context.vars.nhsNumber = generateValidNhsNumber()
5969

60-
let prescriptionCount = Math.round(Math.sampleNormal(3,1))
70+
let prescriptionCount = Math.round(sampleNormal(3,1))
6171
if (prescriptionCount < 1) prescriptionCount = 1 // just truncate at 1.
62-
vuContext.vars.prescriptionCount = prescriptionCount
72+
context.vars.prescriptionCount = prescriptionCount
73+
74+
done()
6375
}
6476

6577
// beforeEach request
66-
export function generatePrescData(requestParams, vuContext) {
78+
export function generatePrescData(requestParams, context, ee, next) {
6779
const body = getBody(
68-
true, /* isValid */
69-
"ready to collect", /* status */
70-
vuContext.vars.odsCode, /* odsCode */
71-
vuContext.vars.nhsNumber /* nhsNumber */
80+
true, /* isValid */
81+
"ready to collect", /* status */
82+
context.vars.odsCode, /* odsCode */
83+
context.vars.nhsNumber /* nhsNumber */
7284
)
7385

7486
requestParams.json = body
75-
vuContext.vars.x_request_id = uuidv4()
76-
vuContext.vars.x_correlation_id = uuidv4()
87+
context.vars.x_request_id = uuidv4()
88+
context.vars.x_correlation_id = uuidv4()
7789

7890
// Wait this long between requests
79-
let delay = Math.sampleNormal(150, 60)
91+
let delay = sampleNormal(150, 60)
8092
if (delay < 0) delay = 0
81-
vuContext.vars.nextDelay = delay
93+
context.vars.nextDelay = delay
94+
95+
next()
8296
}
8397

8498
export { getSharedAuthToken }

0 commit comments

Comments
 (0)