Skip to content

Add a assertSegmentDuration function in custom assertions #3862

@amychisholm03

Description

@amychisholm03

Description

As we migrate over our instrumentation to use the tracing channel, we need an easy and standard way to assert segment durations. So far different test suites do it a bit differently, but the closest one that could be used across packages is assertSegmentState in the memcached versioned tests:

/**
* Asserts that the current segment has the expected name, has ended,
* and has a duration within a reasonable threshold of the actual measured time.
* @param {TraceSegment} segment segment to check
* @param {string} expectedName expected segment name
* @param {Array} actualTime process.hrtime() duration of the actual call
*/
function assertSegmentState(segment, expectedName, actualTime) {
assert.equal(segment.name, expectedName)
assert.equal(segment._isEnded(), true, 'segment should have ended')
const segmentDuration = segment.getDurationInMillis()
const actualDuration = hrToMillis(actualTime)
assert.ok(
Math.abs(segmentDuration - actualDuration) < 1,
`segment duration (${segmentDuration}ms) should be within 1ms of actual duration (${actualDuration}ms)`
)
}
.

We need to create this function in custom-assertions and implement it across our test suites. Instead of a strict 1ms (or similar constant) threshold, we should use a percentage threshold.

Acceptance Criteria

  • all test suites that test segment durations (excluding ones where you can explicitly test duration like sleep(2) in mysql2) use this new function and they still pass

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

Status

In progress: Issues being worked on

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions