Skip to content

Commit b00ab50

Browse files
authored
test: rewrite chai to Node.js assert (#7153)
There was a race condition of running the tests vs merging the PR that still contained chai tests.
1 parent 7a64f41 commit b00ab50

1 file changed

Lines changed: 40 additions & 30 deletions

File tree

packages/datadog-plugin-google-cloud-pubsub/test/pubsub-push-subscription.spec.js

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
'use strict'
22

3+
const assert = require('node:assert/strict')
4+
const { setTimeout: wait } = require('node:timers/promises')
5+
36
const axios = require('axios')
4-
const { expect } = require('chai')
57
const { describe, it, beforeEach, afterEach, before, after } = require('mocha')
68
const agent = require('../../dd-trace/test/plugins/agent')
9+
const { assertObjectContains } = require('../../../integration-tests/helpers')
710

811
describe('Push Subscription Plugin', () => {
912
let tracer
@@ -71,33 +74,33 @@ describe('Push Subscription Plugin', () => {
7174
)
7275
if (!trace) return
7376

74-
expect(handlerCalled).to.be.true
77+
assert.strictEqual(handlerCalled, true)
7578

7679
const httpSpan = trace.find(s => s.name === 'web.request')
7780
const pubsubSpan = trace.find(s => s.name === 'pubsub.delivery')
7881

79-
expect(httpSpan, 'HTTP server span must exist').to.exist
80-
expect(pubsubSpan, 'pubsub.delivery span must exist').to.exist
82+
assert.ok(httpSpan, 'HTTP server span must exist')
83+
assert.ok(pubsubSpan, 'pubsub.delivery span must exist')
8184

8285
// For raw HTTP, the active span might be web.request OR pubsub.delivery depending on timing
8386
if (activeSpanInHandler) {
8487
const spanName = activeSpanInHandler.context()._name
85-
expect(['web.request', 'pubsub.delivery']).to.include(spanName)
88+
assert.ok(['web.request', 'pubsub.delivery'].includes(spanName))
8689
}
8790

8891
// For raw HTTP, parent-child relationship might not be established the same way
8992
// as with framework-based servers (Express, Fastify, etc.)
9093
// Both spans should exist in the same trace though
91-
expect(pubsubSpan.trace_id.toString()).to.equal(httpSpan.trace_id.toString())
94+
assert.strictEqual(pubsubSpan.trace_id.toString(), httpSpan.trace_id.toString())
9295

93-
expect(pubsubSpan.meta).to.include({
96+
assertObjectContains(pubsubSpan.meta, {
9497
'span.kind': 'consumer',
9598
component: 'google-cloud-pubsub',
9699
'pubsub.message_id': messageId,
97100
'pubsub.delivery_method': 'push'
98101
})
99102

100-
expect(httpSpan.meta).to.include({
103+
assertObjectContains(httpSpan.meta, {
101104
'span.kind': 'server',
102105
'http.method': 'POST'
103106
})
@@ -150,15 +153,15 @@ describe('Push Subscription Plugin', () => {
150153
if (!trace) return
151154

152155
const pubsubSpan = trace.find(s => s.name === 'pubsub.delivery')
153-
expect(pubsubSpan).to.exist
156+
assert.ok(pubsubSpan)
154157

155158
if (pubsubSpan.meta['_dd.span_links']) {
156159
const spanLinks = JSON.parse(pubsubSpan.meta['_dd.span_links'])
157-
expect(spanLinks).to.be.an('array')
160+
assert.ok(Array.isArray(spanLinks))
158161
const hasProducerLink = spanLinks.some(link =>
159162
link.trace_id && link.span_id
160163
)
161-
expect(hasProducerLink).to.be.true
164+
assert.strictEqual(hasProducerLink, true)
162165
}
163166
})
164167
.then(done)
@@ -207,14 +210,14 @@ describe('Push Subscription Plugin', () => {
207210
if (!trace) return
208211

209212
const pubsubSpan = trace.find(s => s.name === 'pubsub.delivery')
210-
expect(pubsubSpan).to.exist
213+
assert.ok(pubsubSpan)
211214

212-
expect(pubsubSpan.meta).to.include({
215+
assertObjectContains(pubsubSpan.meta, {
213216
'pubsub.batch.description': 'Message 1 of 3',
214217
'pubsub.batch.request_trace_id': batchTraceId,
215218
'pubsub.batch.request_span_id': batchSpanId
216219
})
217-
expect(pubsubSpan.metrics).to.include({
220+
assertObjectContains(pubsubSpan.metrics, {
218221
'pubsub.batch.message_count': 3,
219222
'pubsub.batch.message_index': 0
220223
})
@@ -264,10 +267,10 @@ describe('Push Subscription Plugin', () => {
264267
if (!trace) return
265268

266269
const pubsubSpan = trace.find(s => s.name === 'pubsub.delivery')
267-
expect(pubsubSpan).to.exist
270+
assert.ok(pubsubSpan)
268271

269-
expect(pubsubSpan.service).to.equal('test-pubsub')
270-
expect(pubsubSpan.meta).to.include({
272+
assert.strictEqual(pubsubSpan.service, 'test-pubsub')
273+
assertObjectContains(pubsubSpan.meta, {
271274
'_dd.base_service': 'test',
272275
'_dd.serviceoverride.type': 'integration'
273276
})
@@ -309,9 +312,9 @@ describe('Push Subscription Plugin', () => {
309312
const trace = traces.find(t => t.some(s => s.name === 'web.request'))
310313
if (!trace) return
311314

312-
expect(trace).to.exist
315+
assert.ok(trace)
313316
const pubsubSpan = trace.find(s => s.name === 'pubsub.delivery')
314-
expect(pubsubSpan).to.not.exist
317+
assert.ok(!pubsubSpan)
315318
})
316319
.then(done)
317320
.catch(done)
@@ -347,9 +350,9 @@ describe('Push Subscription Plugin', () => {
347350
const trace = traces.find(t => t.some(s => s.name === 'web.request'))
348351
if (!trace) return
349352

350-
expect(trace).to.exist
353+
assert.ok(trace)
351354
const pubsubSpan = trace.find(s => s.name === 'pubsub.delivery')
352-
expect(pubsubSpan).to.not.exist
355+
assert.ok(!pubsubSpan)
353356
})
354357
.then(done)
355358
.catch(done)
@@ -413,21 +416,24 @@ describe('Push Subscription Plugin', () => {
413416
})
414417

415418
// Wait for request to complete
416-
await new Promise(resolve => setTimeout(resolve, 100))
419+
await wait(100)
417420

418421
// Force garbage collection
422+
// @ts-expect-error We expect the test to be started with --trace-gc
419423
global.gc()
420-
await new Promise(resolve => setTimeout(resolve, 100))
424+
await wait(100)
425+
// @ts-expect-error We expect the test to be started with --trace-gc
421426
global.gc()
422-
await new Promise(resolve => setTimeout(resolve, 100))
427+
await wait(100)
428+
// @ts-expect-error We expect the test to be started with --trace-gc
423429
global.gc()
424430

425431
// Wait for FinalizationRegistry callback
426-
await new Promise(resolve => setTimeout(resolve, 500))
432+
await wait(500)
427433

428434
// Verify request was garbage collected
429435
// This proves deliverySpans WeakMap doesn't prevent GC
430-
expect(requestWasCollected).to.be.true
436+
assert.strictEqual(requestWasCollected, true)
431437
done()
432438
} catch (err) {
433439
done(err)
@@ -478,20 +484,24 @@ describe('Push Subscription Plugin', () => {
478484
await Promise.all(promises)
479485

480486
// Wait for all requests to complete
481-
await new Promise(resolve => setTimeout(resolve, 500))
487+
await wait(500)
482488

483489
// Force GC
490+
// @ts-expect-error We expect the test to be started with --trace-gc
484491
global.gc()
485-
await new Promise(resolve => setTimeout(resolve, 100))
492+
await wait(100)
493+
// @ts-expect-error We expect the test to be started with --trace-gc
486494
global.gc()
487495

488496
const afterMemory = process.memoryUsage().heapUsed
489497
const memoryIncrease = afterMemory - initialMemory
490498

491499
// Memory should not increase significantly (less than 10MB for 100 requests)
492500
// If deliverySpans WeakMap is leaking, this would be much higher
493-
expect(memoryIncrease).to.be.lessThan(10 * 1024 * 1024,
494-
`Memory increase should be minimal but was ${(memoryIncrease / 1024 / 1024).toFixed(2)}MB`)
501+
assert.ok(
502+
memoryIncrease < (10 * 1024 * 1024),
503+
`Memory increase should be minimal but was ${(memoryIncrease / 1024 / 1024).toFixed(2)}MB`
504+
)
495505

496506
done()
497507
} catch (err) {

0 commit comments

Comments
 (0)