Skip to content
File

Blob: src/workerd/api/tests/queue-metadata-test.js

javascript64 lines
1// Copyright (c) 2026 Cloudflare, Inc.
2// Licensed under the Apache 2.0 license found in the LICENSE file or at:
3// https://opensource.org/licenses/Apache-2.0
4 
5import assert from 'node:assert';
6 
7export default {
8 async queue(batch, env, ctx) {
9 assert.ok(batch.metadata, 'Expected batch.metadata to be defined');
10 assert.ok(
11 batch.metadata.metrics,
12 'Expected batch.metadata.metrics to be defined'
13 );
14 
15 if (
16 batch.metadata.metrics.backlogCount === 0 &&
17 batch.metadata.metrics.backlogBytes === 0 &&
18 batch.metadata.metrics.oldestMessageTimestamp === undefined
19 ) {
20 // If metadata is omitted → counts default to zero, timestamp is undefined
21 batch.ackAll();
22 return;
23 }
24 
25 // Explicit metadata path
26 assert.strictEqual(batch.metadata.metrics.backlogCount, 100);
27 assert.strictEqual(batch.metadata.metrics.backlogBytes, 2048);
28 assert.ok(
29 batch.metadata.metrics.oldestMessageTimestamp instanceof Date,
30 'Expected oldestMessageTimestamp to be a Date'
31 );
32 assert.strictEqual(
33 batch.metadata.metrics.oldestMessageTimestamp.getTime(),
34 1000000
35 );
36 batch.ackAll();
37 },
38 
39 async test(ctrl, env, ctx) {
40 const timestamp = new Date();
41 
42 const response1 = await env.SERVICE.queue(
43 'test-queue',
44 [{ id: '0', timestamp, body: 'hello', attempts: 1 }],
45 {
46 metrics: {
47 backlogCount: 100,
48 backlogBytes: 2048,
49 oldestMessageTimestamp: 1000000,
50 },
51 }
52 );
53 assert.strictEqual(response1.outcome, 'ok');
54 assert(response1.ackAll);
55 
56 // Test with omitted metadata
57 const response2 = await env.SERVICE.queue('test-queue', [
58 { id: '1', timestamp, body: 'world', attempts: 1 },
59 ]);
60 assert.strictEqual(response2.outcome, 'ok');
61 assert(response2.ackAll);
62 },
63};