File
Blob: src/workerd/api/tests/queue-metrics-test.js
| 1 | // Copyright (c) 2023 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 | |
| 5 | import assert from 'node:assert'; |
| 6 | |
| 7 | export default { |
| 8 | async fetch(request, env, ctx) { |
| 9 | const { pathname } = new URL(request.url); |
| 10 | if (pathname === '/metrics') { |
| 11 | assert.strictEqual(request.method, 'GET'); |
| 12 | // Regression test: read the request body before responding, matching |
| 13 | // production behavior. If we omit expectedBodySize on the GET request, |
| 14 | // the body pipe is never closed and this arrayBuffer() call deadlocks |
| 15 | await request.arrayBuffer(); |
| 16 | return Response.json({ |
| 17 | backlogCount: 100, |
| 18 | backlogBytes: 2048, |
| 19 | oldestMessageTimestamp: 1000000, |
| 20 | }); |
| 21 | } |
| 22 | return new Response(); |
| 23 | }, |
| 24 | |
| 25 | async test(ctrl, env, ctx) { |
| 26 | assert.strictEqual(typeof env.QUEUE.metrics, 'function'); |
| 27 | const metrics = await env.QUEUE.metrics(); |
| 28 | assert.strictEqual(metrics.backlogCount, 100); |
| 29 | assert.strictEqual(metrics.backlogBytes, 2048); |
| 30 | assert.ok( |
| 31 | metrics.oldestMessageTimestamp instanceof Date, |
| 32 | 'Expected oldestMessageTimestamp to be a Date' |
| 33 | ); |
| 34 | assert.strictEqual(metrics.oldestMessageTimestamp.getTime(), 1000000); |
| 35 | }, |
| 36 | }; |