|
1 | 1 | 'use strict' |
2 | 2 |
|
| 3 | +const assert = require('node:assert/strict') |
| 4 | +const { setTimeout: wait } = require('node:timers/promises') |
| 5 | + |
3 | 6 | const axios = require('axios') |
4 | | -const { expect } = require('chai') |
5 | 7 | const { describe, it, beforeEach, afterEach, before, after } = require('mocha') |
6 | 8 | const agent = require('../../dd-trace/test/plugins/agent') |
| 9 | +const { assertObjectContains } = require('../../../integration-tests/helpers') |
7 | 10 |
|
8 | 11 | describe('Push Subscription Plugin', () => { |
9 | 12 | let tracer |
@@ -71,33 +74,33 @@ describe('Push Subscription Plugin', () => { |
71 | 74 | ) |
72 | 75 | if (!trace) return |
73 | 76 |
|
74 | | - expect(handlerCalled).to.be.true |
| 77 | + assert.strictEqual(handlerCalled, true) |
75 | 78 |
|
76 | 79 | const httpSpan = trace.find(s => s.name === 'web.request') |
77 | 80 | const pubsubSpan = trace.find(s => s.name === 'pubsub.delivery') |
78 | 81 |
|
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') |
81 | 84 |
|
82 | 85 | // For raw HTTP, the active span might be web.request OR pubsub.delivery depending on timing |
83 | 86 | if (activeSpanInHandler) { |
84 | 87 | const spanName = activeSpanInHandler.context()._name |
85 | | - expect(['web.request', 'pubsub.delivery']).to.include(spanName) |
| 88 | + assert.ok(['web.request', 'pubsub.delivery'].includes(spanName)) |
86 | 89 | } |
87 | 90 |
|
88 | 91 | // For raw HTTP, parent-child relationship might not be established the same way |
89 | 92 | // as with framework-based servers (Express, Fastify, etc.) |
90 | 93 | // 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()) |
92 | 95 |
|
93 | | - expect(pubsubSpan.meta).to.include({ |
| 96 | + assertObjectContains(pubsubSpan.meta, { |
94 | 97 | 'span.kind': 'consumer', |
95 | 98 | component: 'google-cloud-pubsub', |
96 | 99 | 'pubsub.message_id': messageId, |
97 | 100 | 'pubsub.delivery_method': 'push' |
98 | 101 | }) |
99 | 102 |
|
100 | | - expect(httpSpan.meta).to.include({ |
| 103 | + assertObjectContains(httpSpan.meta, { |
101 | 104 | 'span.kind': 'server', |
102 | 105 | 'http.method': 'POST' |
103 | 106 | }) |
@@ -150,15 +153,15 @@ describe('Push Subscription Plugin', () => { |
150 | 153 | if (!trace) return |
151 | 154 |
|
152 | 155 | const pubsubSpan = trace.find(s => s.name === 'pubsub.delivery') |
153 | | - expect(pubsubSpan).to.exist |
| 156 | + assert.ok(pubsubSpan) |
154 | 157 |
|
155 | 158 | if (pubsubSpan.meta['_dd.span_links']) { |
156 | 159 | const spanLinks = JSON.parse(pubsubSpan.meta['_dd.span_links']) |
157 | | - expect(spanLinks).to.be.an('array') |
| 160 | + assert.ok(Array.isArray(spanLinks)) |
158 | 161 | const hasProducerLink = spanLinks.some(link => |
159 | 162 | link.trace_id && link.span_id |
160 | 163 | ) |
161 | | - expect(hasProducerLink).to.be.true |
| 164 | + assert.strictEqual(hasProducerLink, true) |
162 | 165 | } |
163 | 166 | }) |
164 | 167 | .then(done) |
@@ -207,14 +210,14 @@ describe('Push Subscription Plugin', () => { |
207 | 210 | if (!trace) return |
208 | 211 |
|
209 | 212 | const pubsubSpan = trace.find(s => s.name === 'pubsub.delivery') |
210 | | - expect(pubsubSpan).to.exist |
| 213 | + assert.ok(pubsubSpan) |
211 | 214 |
|
212 | | - expect(pubsubSpan.meta).to.include({ |
| 215 | + assertObjectContains(pubsubSpan.meta, { |
213 | 216 | 'pubsub.batch.description': 'Message 1 of 3', |
214 | 217 | 'pubsub.batch.request_trace_id': batchTraceId, |
215 | 218 | 'pubsub.batch.request_span_id': batchSpanId |
216 | 219 | }) |
217 | | - expect(pubsubSpan.metrics).to.include({ |
| 220 | + assertObjectContains(pubsubSpan.metrics, { |
218 | 221 | 'pubsub.batch.message_count': 3, |
219 | 222 | 'pubsub.batch.message_index': 0 |
220 | 223 | }) |
@@ -264,10 +267,10 @@ describe('Push Subscription Plugin', () => { |
264 | 267 | if (!trace) return |
265 | 268 |
|
266 | 269 | const pubsubSpan = trace.find(s => s.name === 'pubsub.delivery') |
267 | | - expect(pubsubSpan).to.exist |
| 270 | + assert.ok(pubsubSpan) |
268 | 271 |
|
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, { |
271 | 274 | '_dd.base_service': 'test', |
272 | 275 | '_dd.serviceoverride.type': 'integration' |
273 | 276 | }) |
@@ -309,9 +312,9 @@ describe('Push Subscription Plugin', () => { |
309 | 312 | const trace = traces.find(t => t.some(s => s.name === 'web.request')) |
310 | 313 | if (!trace) return |
311 | 314 |
|
312 | | - expect(trace).to.exist |
| 315 | + assert.ok(trace) |
313 | 316 | const pubsubSpan = trace.find(s => s.name === 'pubsub.delivery') |
314 | | - expect(pubsubSpan).to.not.exist |
| 317 | + assert.ok(!pubsubSpan) |
315 | 318 | }) |
316 | 319 | .then(done) |
317 | 320 | .catch(done) |
@@ -347,9 +350,9 @@ describe('Push Subscription Plugin', () => { |
347 | 350 | const trace = traces.find(t => t.some(s => s.name === 'web.request')) |
348 | 351 | if (!trace) return |
349 | 352 |
|
350 | | - expect(trace).to.exist |
| 353 | + assert.ok(trace) |
351 | 354 | const pubsubSpan = trace.find(s => s.name === 'pubsub.delivery') |
352 | | - expect(pubsubSpan).to.not.exist |
| 355 | + assert.ok(!pubsubSpan) |
353 | 356 | }) |
354 | 357 | .then(done) |
355 | 358 | .catch(done) |
@@ -413,21 +416,24 @@ describe('Push Subscription Plugin', () => { |
413 | 416 | }) |
414 | 417 |
|
415 | 418 | // Wait for request to complete |
416 | | - await new Promise(resolve => setTimeout(resolve, 100)) |
| 419 | + await wait(100) |
417 | 420 |
|
418 | 421 | // Force garbage collection |
| 422 | + // @ts-expect-error We expect the test to be started with --trace-gc |
419 | 423 | 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 |
421 | 426 | 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 |
423 | 429 | global.gc() |
424 | 430 |
|
425 | 431 | // Wait for FinalizationRegistry callback |
426 | | - await new Promise(resolve => setTimeout(resolve, 500)) |
| 432 | + await wait(500) |
427 | 433 |
|
428 | 434 | // Verify request was garbage collected |
429 | 435 | // This proves deliverySpans WeakMap doesn't prevent GC |
430 | | - expect(requestWasCollected).to.be.true |
| 436 | + assert.strictEqual(requestWasCollected, true) |
431 | 437 | done() |
432 | 438 | } catch (err) { |
433 | 439 | done(err) |
@@ -478,20 +484,24 @@ describe('Push Subscription Plugin', () => { |
478 | 484 | await Promise.all(promises) |
479 | 485 |
|
480 | 486 | // Wait for all requests to complete |
481 | | - await new Promise(resolve => setTimeout(resolve, 500)) |
| 487 | + await wait(500) |
482 | 488 |
|
483 | 489 | // Force GC |
| 490 | + // @ts-expect-error We expect the test to be started with --trace-gc |
484 | 491 | 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 |
486 | 494 | global.gc() |
487 | 495 |
|
488 | 496 | const afterMemory = process.memoryUsage().heapUsed |
489 | 497 | const memoryIncrease = afterMemory - initialMemory |
490 | 498 |
|
491 | 499 | // Memory should not increase significantly (less than 10MB for 100 requests) |
492 | 500 | // 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 | + ) |
495 | 505 |
|
496 | 506 | done() |
497 | 507 | } catch (err) { |
|
0 commit comments