Skip to content

Commit

Permalink
fix: Refactored benchmark tests to complete async functions (#2334)
Browse files Browse the repository at this point in the history
Signed-off-by: mrickard <maurice@mauricerickard.com>
  • Loading branch information
mrickard authored Jul 8, 2024
1 parent 7dab36d commit 57a4dfb
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 15 deletions.
15 changes: 12 additions & 3 deletions test/benchmark/datastore-shim/test-datastore.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,24 @@

class TestDatastore {
testOp(cb) {
setImmediate(cb)
if (typeof cb === 'function') {
return setImmediate(cb)
}
return cb || 'testOp'
}

testQuery(query, cb) {
setImmediate(cb)
if (typeof cb === 'function') {
return setImmediate(cb)
}
return cb || 'testQuery'
}

testBatch(query, cb) {
setImmediate(cb)
if (typeof cb === 'function') {
return setImmediate(cb)
}
return cb || 'testBatch'
}
}

Expand Down
54 changes: 46 additions & 8 deletions test/benchmark/promises/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ const tests = [
promises.push(prom.then(function first() {}))
promises.push(prom.then(function second() {}))
}
Promise.all(promises).then(cb)
Promise.all(promises).then(() => {
if (typeof cb === 'function') {
return cb()
}
return cb || true
})
}
},

Expand All @@ -37,7 +42,12 @@ const tests = [
for (let i = 0; i < NUM_PROMISES; ++i) {
prom = prom.then(function () {})
}
prom.then(cb)
prom.then(() => {
if (typeof cb === 'function') {
return cb()
}
return cb || true
})
}
},

Expand All @@ -47,7 +57,12 @@ const tests = [
for (let i = 0; i < NUM_PROMISES / 2; ++i) {
prom = prom.then(function () {}).catch(function () {})
}
prom.then(cb)
prom.then(() => {
if (typeof cb === 'function') {
return cb()
}
return cb || true
})
}
},

Expand All @@ -57,7 +72,14 @@ const tests = [
for (let i = 0; i < NUM_PROMISES - 1; ++i) {
prom = prom.then(function () {})
}
prom.catch(function () {}).then(cb)
prom
.catch(function () {})
.then(() => {
if (typeof cb === 'function') {
return cb()
}
return cb || true
})
}
},

Expand All @@ -69,7 +91,10 @@ const tests = [
res()
})
}
cb()
if (typeof cb === 'function') {
return cb()
}
return cb || true
}
},

Expand All @@ -87,7 +112,12 @@ const tests = [
})
)
}
Promise.all(promises).then(cb)
Promise.all(promises).then(() => {
if (typeof cb === 'function') {
return cb()
}
return cb || true
})
}
},

Expand All @@ -101,7 +131,12 @@ const tests = [
})
})
}
prom.then(cb)
prom.then(() => {
if (typeof cb === 'function') {
return cb()
}
return cb || true
})
}
},

Expand All @@ -110,7 +145,10 @@ const tests = [
new Promise(function () {
throw new Error('Whoops!')
}).catch(() => {})
cb()
if (typeof cb === 'function') {
return cb()
}
return cb || true
}
}
]
Expand Down
5 changes: 1 addition & 4 deletions test/lib/benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,6 @@ class Benchmark {
const prevCpu = process.cpuUsage()
const testFn = test.fn

if (test.async) {
return testFn(agent, () => after(test, next, executeCb, prevCpu))
}
await testFn(agent)
return after(test, next, executeCb, prevCpu)
}
Expand Down Expand Up @@ -108,7 +105,7 @@ class Benchmark {
if (idx >= suite.tests.length) {
return true
}
return initiator(initiator, suite.tests[idx], idx)
return await initiator(initiator, suite.tests[idx], idx)
}

const afterTestRuns = (initiator, test, samples, idx) => {
Expand Down

0 comments on commit 57a4dfb

Please sign in to comment.