-
Notifications
You must be signed in to change notification settings - Fork 406
Expand file tree
/
Copy pathprocessor.spec.js
More file actions
633 lines (538 loc) · 20.7 KB
/
Copy pathprocessor.spec.js
File metadata and controls
633 lines (538 loc) · 20.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
'use strict'
const assert = require('node:assert/strict')
const { hostname } = require('node:os')
const { describe, it, beforeEach } = require('mocha')
const sinon = require('sinon')
const proxyquire = require('proxyquire')
require('../setup/core')
const { LogCollapsingLowestDenseDDSketch } = require('../../../../vendor/dist/@datadog/sketches-js')
const HIGH_ACCURACY_DISTRIBUTION = 0.0075
const pkg = require('../../../../package.json')
const DEFAULT_TIMESTAMP = Number(new Date('2023-04-20T16:20:00.000Z'))
const DEFAULT_LATENCY = 100000000
const DEFAULT_PARENT_HASH = Buffer.from('e858292fd15a41e4', 'hex')
const ANOTHER_PARENT_HASH = Buffer.from('e858292fd15a4100', 'hex')
const DEFAULT_CURRENT_HASH = Buffer.from('e858212fd11a41e5', 'hex')
const ANOTHER_CURRENT_HASH = Buffer.from('e851212fd11a21e9', 'hex')
const writer = {
flush: sinon.stub(),
}
const DataStreamsWriter = sinon.stub().returns(writer)
const {
CheckpointRegistry,
StatsPoint,
Backlog,
StatsBucket,
TimeBuckets,
DataStreamsProcessor,
getHeadersSize,
getMessageSize,
getSizeOrZero,
} = proxyquire('../../src/datastreams/processor', {
'./writer': { DataStreamsWriter },
})
const mockCheckpoint = {
currentTimestamp: DEFAULT_TIMESTAMP,
hash: DEFAULT_CURRENT_HASH,
parentHash: DEFAULT_PARENT_HASH,
edgeTags: ['service:service-name', 'env:env-name', 'topic:test-topic'],
edgeLatencyNs: DEFAULT_LATENCY,
pathwayLatencyNs: DEFAULT_LATENCY,
payloadSize: 100,
}
const anotherMockCheckpoint = {
currentTimestamp: DEFAULT_TIMESTAMP,
hash: ANOTHER_CURRENT_HASH, // todo: different hash
parentHash: ANOTHER_PARENT_HASH,
edgeTags: ['service:service-name', 'env:env-name', 'topic:test-topic'],
edgeLatencyNs: DEFAULT_LATENCY,
pathwayLatencyNs: DEFAULT_LATENCY,
payloadSize: 100,
}
describe('StatsPoint', () => {
it('should add latencies', () => {
const aggStats = new StatsPoint(mockCheckpoint.hash, mockCheckpoint.parentHash, mockCheckpoint.edgeTags)
aggStats.addLatencies(mockCheckpoint)
const edgeLatency = new LogCollapsingLowestDenseDDSketch(HIGH_ACCURACY_DISTRIBUTION)
const pathwayLatency = new LogCollapsingLowestDenseDDSketch(HIGH_ACCURACY_DISTRIBUTION)
const payloadSize = new LogCollapsingLowestDenseDDSketch(HIGH_ACCURACY_DISTRIBUTION)
edgeLatency.accept(DEFAULT_LATENCY / 1e9)
pathwayLatency.accept(DEFAULT_LATENCY / 1e9)
payloadSize.accept(100)
const encoded = aggStats.encode()
assert.strictEqual(encoded.Hash, DEFAULT_CURRENT_HASH.readBigUInt64LE())
assert.strictEqual(encoded.ParentHash, DEFAULT_PARENT_HASH.readBigUInt64LE())
assert.deepStrictEqual(encoded.EdgeTags, aggStats.edgeTags)
assert.deepStrictEqual(encoded.EdgeLatency, edgeLatency.toProto())
assert.deepStrictEqual(encoded.PathwayLatency, pathwayLatency.toProto())
assert.deepStrictEqual(encoded.PayloadSize, payloadSize.toProto())
})
})
describe('StatsBucket', () => {
describe('Checkpoints', () => {
let buckets
beforeEach(() => { buckets = new StatsBucket() })
it('should start empty', () => {
assert.strictEqual(buckets.checkpoints.size, 0)
})
it('should add a new entry when no matching key is found', () => {
const bucket = buckets.forCheckpoint(mockCheckpoint)
const checkpoints = buckets.checkpoints
assert.ok(bucket instanceof StatsPoint)
assert.strictEqual(checkpoints.size, 1)
const [key, value] = Array.from(checkpoints.entries())[0]
assert.strictEqual(key.toString(), mockCheckpoint.hash.toString())
assert.ok(value instanceof StatsPoint)
})
it('should not add a new entry if matching key is found', () => {
buckets.forCheckpoint(mockCheckpoint)
buckets.forCheckpoint(mockCheckpoint)
assert.strictEqual(buckets.checkpoints.size, 1)
})
it('should add a new entry when new checkpoint does not match existing agg keys', () => {
buckets.forCheckpoint(mockCheckpoint)
buckets.forCheckpoint(anotherMockCheckpoint)
assert.strictEqual(buckets.checkpoints.size, 2)
})
})
describe('Backlogs', () => {
let backlogBuckets
const mockBacklog = {
offset: 12,
type: 'kafka_consume',
consumer_group: 'test-consumer',
partition: 0,
topic: 'test-topic',
}
beforeEach(() => {
backlogBuckets = new StatsBucket()
})
it('should start empty', () => {
assert.strictEqual(backlogBuckets.backlogs.size, 0)
})
it('should add a new entry when empty', () => {
const bucket = backlogBuckets.forBacklog(mockBacklog)
const backlogs = backlogBuckets.backlogs
assert.ok(bucket instanceof Backlog)
const [, value] = Array.from(backlogs.entries())[0]
assert.ok(value instanceof Backlog)
})
it('should add a new entry when given different tags', () => {
const otherMockBacklog = {
offset: 1,
type: 'kafka_consume',
consumer_group: 'test-consumer',
partition: 1,
topic: 'test-topic',
}
backlogBuckets.forBacklog(mockBacklog)
backlogBuckets.forBacklog(otherMockBacklog)
assert.strictEqual(backlogBuckets.backlogs.size, 2)
})
it('should update the existing entry if offset is higher', () => {
const higherMockBacklog = {
offset: 16,
type: 'kafka_consume',
consumer_group: 'test-consumer',
partition: 0,
topic: 'test-topic',
}
backlogBuckets.forBacklog(mockBacklog)
const backlog = backlogBuckets.forBacklog(higherMockBacklog)
assert.strictEqual(backlog.offset, higherMockBacklog.offset)
assert.strictEqual(backlogBuckets.backlogs.size, 1)
})
it('should discard the passed backlog if offset is lower', () => {
const lowerMockBacklog = {
offset: 2,
type: 'kafka_consume',
consumer_group: 'test-consumer',
partition: 0,
topic: 'test-topic',
}
backlogBuckets.forBacklog(mockBacklog)
const backlog = backlogBuckets.forBacklog(lowerMockBacklog)
assert.strictEqual(backlog.offset, mockBacklog.offset)
assert.strictEqual(backlogBuckets.backlogs.size, 1)
})
})
})
describe('TimeBuckets', () => {
it('should acquire a span agg bucket for the given time', () => {
const buckets = new TimeBuckets()
assert.strictEqual(buckets.size, 0)
const bucket = buckets.forTime(12345)
assert.strictEqual(buckets.size, 1)
assert.ok(bucket instanceof StatsBucket)
})
})
describe('DataStreamsProcessor', () => {
let edgeLatency
let pathwayLatency
let processor
let payloadSize
const config = {
dsmEnabled: true,
hostname: '127.0.0.1',
port: 8126,
url: new URL('http://127.0.0.1:8126'),
env: 'test',
version: 'v1',
service: 'service1',
tags: { foo: 'foovalue', bar: 'barvalue' },
}
beforeEach(() => {
processor = new DataStreamsProcessor(config)
clearTimeout(processor.timer)
})
it('should construct', () => {
processor = new DataStreamsProcessor(config)
clearTimeout(processor.timer)
sinon.assert.calledWith(DataStreamsWriter, {
hostname: config.hostname,
port: config.port,
url: config.url,
})
assert.ok(processor.buckets instanceof TimeBuckets)
assert.strictEqual(processor.hostname, hostname())
assert.strictEqual(processor.enabled, config.dsmEnabled)
assert.strictEqual(processor.env, config.env)
assert.deepStrictEqual(processor.tags, config.tags)
})
it('should track backlogs', () => {
const mockBacklog = {
offset: 12,
type: 'kafka_consume',
consumer_group: 'test-consumer',
partition: 0,
topic: 'test-topic',
}
assert.strictEqual(processor.buckets.size, 0)
processor.recordOffset({ timestamp: DEFAULT_TIMESTAMP, ...mockBacklog })
assert.strictEqual(processor.buckets.size, 1)
const timeBucket = processor.buckets.values().next().value
assert.ok(timeBucket instanceof StatsBucket)
assert.strictEqual(timeBucket.backlogs.size, 1)
const backlog = timeBucket.forBacklog(mockBacklog)
assert.strictEqual(timeBucket.backlogs.size, 1)
assert.ok(backlog instanceof Backlog)
const encoded = backlog.encode()
assert.deepStrictEqual(encoded, {
Tags: [
'consumer_group:test-consumer', 'partition:0', 'topic:test-topic', 'type:kafka_consume',
],
Value: 12,
})
})
it('should track latency stats', () => {
assert.strictEqual(processor.buckets.size, 0)
processor.recordCheckpoint(mockCheckpoint)
assert.strictEqual(processor.buckets.size, 1)
const timeBucket = processor.buckets.values().next().value
assert.ok(timeBucket instanceof StatsBucket)
assert.strictEqual(timeBucket.checkpoints.size, 1)
const checkpointBucket = timeBucket.forCheckpoint(mockCheckpoint)
assert.strictEqual(timeBucket.checkpoints.size, 1)
assert.ok(checkpointBucket instanceof StatsPoint)
edgeLatency = new LogCollapsingLowestDenseDDSketch(0.00775)
pathwayLatency = new LogCollapsingLowestDenseDDSketch(0.00775)
payloadSize = new LogCollapsingLowestDenseDDSketch(0.00775)
edgeLatency.accept(mockCheckpoint.edgeLatencyNs / 1e9)
pathwayLatency.accept(mockCheckpoint.pathwayLatencyNs / 1e9)
payloadSize.accept(mockCheckpoint.payloadSize)
const encoded = checkpointBucket.encode()
assert.strictEqual(encoded.Hash, DEFAULT_CURRENT_HASH.readBigUInt64LE())
assert.strictEqual(encoded.ParentHash, DEFAULT_PARENT_HASH.readBigUInt64LE())
assert.deepStrictEqual(encoded.EdgeTags, mockCheckpoint.edgeTags)
assert.deepStrictEqual(encoded.EdgeLatency, edgeLatency.toProto())
assert.deepStrictEqual(encoded.PathwayLatency, pathwayLatency.toProto())
assert.deepStrictEqual(encoded.PayloadSize, payloadSize.toProto())
})
it('should export on interval', () => {
processor.recordCheckpoint(mockCheckpoint)
processor.onInterval()
assert.deepStrictEqual(writer.flush.lastCall.args[0], {
Env: 'test',
Service: 'service1',
Version: 'v1',
Stats: [{
Start: 1680000000000n,
Duration: 10000000000n,
Stats: [{
Hash: DEFAULT_CURRENT_HASH.readBigUInt64LE(),
ParentHash: DEFAULT_PARENT_HASH.readBigUInt64LE(),
EdgeTags: mockCheckpoint.edgeTags,
EdgeLatency: edgeLatency.toProto(),
PathwayLatency: pathwayLatency.toProto(),
PayloadSize: payloadSize.toProto(),
}],
Backlogs: [],
}],
TracerVersion: pkg.version,
Lang: 'javascript',
Tags: ['foo:foovalue', 'bar:barvalue'],
})
})
it('should include ProcessTags when propagation is enabled', () => {
const propagationHash = require('../../src/propagation-hash')
const processTags = require('../../src/process-tags')
// Configure and enable the feature
propagationHash.configure({ propagateProcessTags: { enabled: true } })
processor.recordCheckpoint(mockCheckpoint)
processor.onInterval()
const call = writer.flush.getCall(writer.flush.callCount - 1)
const payload = call.args[0]
assert.ok(payload.ProcessTags, 'ProcessTags should be present')
assert.deepStrictEqual(
payload.ProcessTags,
processTags.serialized.split(','),
'ProcessTags should match process-tags module as array'
)
// Cleanup
propagationHash.configure(null)
})
it('should not include ProcessTags when propagation is disabled', () => {
const propagationHash = require('../../src/propagation-hash')
// Ensure the feature is disabled
propagationHash.configure({ propagateProcessTags: { enabled: false } })
processor.recordCheckpoint(mockCheckpoint)
processor.onInterval()
const call = writer.flush.getCall(writer.flush.callCount - 1)
const payload = call.args[0]
assert.strictEqual(payload.ProcessTags, undefined, 'ProcessTags should not be present')
// Cleanup
propagationHash.configure(null)
})
})
describe('CheckpointRegistry', () => {
let registry
beforeEach(() => {
registry = new CheckpointRegistry()
})
it('assigns IDs sequentially starting at 1', () => {
assert.strictEqual(registry.getId('alpha'), 1)
assert.strictEqual(registry.getId('beta'), 2)
assert.strictEqual(registry.getId('gamma'), 3)
})
it('returns the same ID for repeated names', () => {
const first = registry.getId('alpha')
const second = registry.getId('alpha')
assert.strictEqual(first, second)
})
it('returns undefined when 254 names are exhausted', () => {
for (let i = 1; i <= 254; i++) {
registry.getId(`name-${i}`)
}
assert.strictEqual(registry.getId('overflow'), undefined)
})
it('encodedKeys returns correct [id][nameLen][name] wire bytes', () => {
registry.getId('foo')
registry.getId('bar')
const encoded = registry.encodedKeys
// 'foo': [0x01, 0x03, 'f', 'o', 'o']
// 'bar': [0x02, 0x03, 'b', 'a', 'r']
assert.strictEqual(encoded.length, 10)
assert.strictEqual(encoded.readUInt8(0), 1) // id
assert.strictEqual(encoded.readUInt8(1), 3) // nameLen
assert.strictEqual(encoded.toString('utf8', 2, 5), 'foo')
assert.strictEqual(encoded.readUInt8(5), 2) // id
assert.strictEqual(encoded.readUInt8(6), 3) // nameLen
assert.strictEqual(encoded.toString('utf8', 7, 10), 'bar')
})
it('encodedKeys returns empty Buffer when empty', () => {
const encoded = registry.encodedKeys
assert.ok(Buffer.isBuffer(encoded))
assert.strictEqual(encoded.length, 0)
})
it('truncates names longer than 255 bytes in encodedKeys', () => {
// Build a name that is 260 UTF-8 bytes (all ASCII)
const longName = 'a'.repeat(260)
registry.getId(longName)
const encoded = registry.encodedKeys
// [id uint8][nameLen uint8][name 255 bytes] = 257 bytes total
assert.strictEqual(encoded.length, 257)
assert.strictEqual(encoded.readUInt8(1), 255)
})
})
describe('DataStreamsProcessor.trackTransaction', () => {
const config = {
dsmEnabled: true,
hostname: '127.0.0.1',
port: 8126,
url: new URL('http://127.0.0.1:8126'),
env: 'test',
version: 'v1',
service: 'service1',
tags: {},
}
let processor
let clock
beforeEach(() => {
clock = sinon.useFakeTimers({ now: DEFAULT_TIMESTAMP, toFake: ['Date'] })
processor = new DataStreamsProcessor(config)
clearTimeout(processor.timer)
})
afterEach(() => {
clock.restore()
})
it('no-ops and warns when processor is disabled', () => {
const warnStub = sinon.stub()
const { DataStreamsProcessor: PatchedProcessor } = proxyquire('../../src/datastreams/processor', {
'./writer': { DataStreamsWriter },
'../log': { warn: warnStub },
})
const disabledProcessor = new PatchedProcessor({ ...config, dsmEnabled: false })
clearTimeout(disabledProcessor.timer)
disabledProcessor.trackTransaction('tx-001', 'ingested')
assert.strictEqual(disabledProcessor.buckets.size, 0)
sinon.assert.calledOnce(warnStub)
})
it('adds transaction to the correct time bucket', () => {
processor.trackTransaction('tx-001', 'ingested')
assert.strictEqual(processor.buckets.size, 1)
const bucket = processor.buckets.values().next().value
assert.ok(bucket.transactions !== null)
})
it('encodes correct binary wire format', () => {
processor.trackTransaction('tx-001', 'ingested')
const bucket = processor.buckets.values().next().value
const txBytes = bucket.transactions
// [checkpointId=1 uint8][timestamp int64 BE 8 bytes][idLen=6 uint8]['tx-001' 6 bytes]
assert.strictEqual(txBytes.readUInt8(0), 1) // checkpointId
const timestampNs = BigInt(DEFAULT_TIMESTAMP) * 1_000_000n
assert.strictEqual(txBytes.readBigInt64BE(1), timestampNs)
assert.strictEqual(txBytes.readUInt8(9), 6) // idLen for 'tx-001'
assert.strictEqual(txBytes.toString('utf8', 10, 16), 'tx-001')
assert.strictEqual(txBytes.length, 16)
})
it('truncates transactionId longer than 255 bytes', () => {
const longId = 'x'.repeat(300)
processor.trackTransaction(longId, 'ingested')
const bucket = processor.buckets.values().next().value
const txBytes = bucket.transactions
// [1 byte id][8 byte ts][1 byte len][255 bytes id] = 265 total
assert.strictEqual(txBytes.length, 265)
assert.strictEqual(txBytes.readUInt8(9), 255)
})
it('silently drops transaction when registry is full', () => {
// Fill registry with 254 unique names
for (let i = 1; i <= 254; i++) {
processor.trackTransaction('tx', `checkpoint-${i}`)
}
const bucketsBefore = processor.buckets.size
// 255th unique checkpoint name — registry is full
processor.trackTransaction('tx-overflow', 'checkpoint-255')
// No new bucket created for the dropped transaction
assert.strictEqual(processor.buckets.size, bucketsBefore)
})
it('concatenates multiple transactions within the same bucket', () => {
processor.trackTransaction('tx-001', 'ingested')
processor.trackTransaction('tx-002', 'ingested')
const bucket = processor.buckets.values().next().value
const txBytes = bucket.transactions
// Each entry: [1 id][8 ts][1 len][6 id bytes] = 16 bytes → total 32
assert.strictEqual(txBytes.length, 32)
})
it('sets DSM tags on span when span is provided', () => {
const span = { setTag: sinon.stub() }
processor.trackTransaction('tx-001', 'ingested', span)
sinon.assert.calledWith(span.setTag, 'dsm.transaction.id', 'tx-001')
sinon.assert.calledWith(span.setTag, 'dsm.transaction.checkpoint', 'ingested')
})
it('does not call setTag when no span is provided', () => {
// Should not throw; bucket is still written
processor.trackTransaction('tx-001', 'ingested')
assert.strictEqual(processor.buckets.size, 1)
})
})
describe('_serializeBuckets with transactions', () => {
const config = {
dsmEnabled: true,
hostname: '127.0.0.1',
port: 8126,
url: new URL('http://127.0.0.1:8126'),
env: 'test',
version: 'v1',
service: 'service1',
tags: {},
}
let processor
let clock
beforeEach(() => {
clock = sinon.useFakeTimers({ now: DEFAULT_TIMESTAMP, toFake: ['Date'] })
processor = new DataStreamsProcessor(config)
clearTimeout(processor.timer)
})
afterEach(() => {
clock.restore()
})
it('includes Transactions and TransactionCheckpointIds when transactions are present', () => {
processor.trackTransaction('tx-001', 'ingested')
const { Stats } = processor._serializeBuckets()
assert.strictEqual(Stats.length, 1)
assert.ok(Buffer.isBuffer(Stats[0].Transactions))
assert.ok(Buffer.isBuffer(Stats[0].TransactionCheckpointIds))
assert.ok(Stats[0].TransactionCheckpointIds.length > 0)
})
it('omits Transactions and TransactionCheckpointIds when no transactions in bucket', () => {
processor.recordCheckpoint(mockCheckpoint)
const { Stats } = processor._serializeBuckets()
assert.strictEqual(Stats.length, 1)
assert.strictEqual(Stats[0].Transactions, undefined)
assert.strictEqual(Stats[0].TransactionCheckpointIds, undefined)
})
it('both buckets share the same TransactionCheckpointIds snapshot when transactions span multiple buckets', () => {
processor.trackTransaction('tx-001', 'ingested')
// Advance clock to create a second time bucket
clock.tick(15000)
processor.trackTransaction('tx-002', 'processed')
const { Stats } = processor._serializeBuckets()
const bucketsWithTx = Stats.filter(b => b.Transactions !== undefined)
assert.strictEqual(bucketsWithTx.length, 2)
// Both buckets should have the same checkpoint ID mapping snapshot
assert.deepStrictEqual(bucketsWithTx[0].TransactionCheckpointIds, bucketsWithTx[1].TransactionCheckpointIds)
})
})
describe('getSizeOrZero', () => {
it('should return the size of a string', () => {
assert.strictEqual(getSizeOrZero('hello'), 5)
})
it('should handle unicode characters', () => {
// emoji is 4 bytes
assert.strictEqual(getSizeOrZero('hello 😀'), 10)
})
it('should return the size of an ArrayBuffer', () => {
const buffer = new ArrayBuffer(10)
assert.strictEqual(getSizeOrZero(buffer), 10)
})
it('should return the size of a Buffer', () => {
const buffer = Buffer.from('hello', 'utf-8')
assert.strictEqual(getSizeOrZero(buffer), 5)
})
})
describe('getHeadersSize', () => {
it('should return 0 for undefined/empty headers', () => {
assert.strictEqual(getHeadersSize(undefined), 0)
assert.strictEqual(getHeadersSize({}), 0)
})
it('should return the total size of all headers', () => {
const headers = {
'Content-Type': 'application/json',
'Content-Length': '100',
}
assert.strictEqual(getHeadersSize(headers), 45)
})
})
describe('getMessageSize', () => {
it('should return the size of a message', () => {
const message = {
key: 'key',
value: 'value',
headers: {
'Content-Type': 'application/json',
'Content-Length': '100',
},
}
assert.strictEqual(getMessageSize(message), 53)
})
})