Skip to content
File

Blob: src/workerd/api/tests/tail-worker-test-jsrpc.js

javascript85 lines
1// Copyright (c) 2025 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
4import assert from 'node:assert';
5import { WorkerEntrypoint, DurableObject } from 'cloudflare:workers';
6 
7export class MyService extends WorkerEntrypoint {
8 // Define a property to test behavior of property accessors.
9 get nonFunctionProperty() {
10 // As a regression test for EW-9282, check that logging in a getter invoked via JSRPC is
11 // reflected in traces does not result in missing onset errors. This works through
12 // tail-worker-test, which calls into other tests to get traces from them and includes several
13 // tail worker implementations. See tail-worker-test.js for test output.
14 console.log('foo');
15 return { foo: 123 };
16 }
17 
18 // Test method that returns a transient object with methods.
19 // This tests that setJsRpcInfo is only called once for the entrypoint method,
20 // not again for transient object methods.
21 getCounter() {
22 console.log('getCounter called');
23 return {
24 increment() {
25 console.log('increment called on transient');
26 return 1;
27 },
28 getValue() {
29 console.log('getValue called on transient');
30 return 42;
31 },
32 };
33 }
34 
35 constructor(ctx, env) {
36 // As a regression test for EW-9282, check that logging in the constructor does not result in
37 // missing onset errors. This requires setting the onset event early on, before getting a
38 // handler to the entrypoint.
39 console.log('bar');
40 super(ctx, env);
41 }
42}
43 
44export class MyActor extends DurableObject {
45 async functionProperty() {}
46 
47 constructor(ctx, env) {
48 // As a regression test for EW-9282, check that logging in the constructor does not result in
49 // missing onset errors – for DOs, we need to report the onset even earlier.
50 super(ctx, env);
51 this.ctx.blockConcurrencyWhile(async () => {
52 console.log('baz');
53 });
54 }
55}
56 
57export default {
58 async test(controller, env, ctx) {
59 let getByName = (name) => {
60 return env.MyService.getRpcMethodForTestOnly(name);
61 };
62 
63 // Check what happens if we access something that's actually declared as a property on the
64 // class. The difference in error message proves to us that `env` and `ctx` weren't visible at
65 // all, which is what we want.
66 await assert.rejects(() => getByName('nonFunctionProperty')(), {
67 name: 'TypeError',
68 message: '"nonFunctionProperty" is not a function.',
69 });
70 
71 let id = env.MyActor.idFromName('foo');
72 let stub = env.MyActor.get(id);
73 await stub.functionProperty();
74 
75 // Test transient object methods - this should only report jsrpc.method for the initial
76 // getCounter() call, not for the subsequent increment() and getValue() calls on the
77 // returned transient object.
78 let counter = await env.MyService.getCounter();
79 let result1 = await counter.increment();
80 let result2 = await counter.getValue();
81 assert.strictEqual(result1, 1);
82 assert.strictEqual(result2, 42);
83 },
84};