File
Blob: src/workerd/api/tests/actor-alarms-delete-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 | // This test is aimed towards validating a correct deleteAlarm behavior in workerd. |
| 6 | // Currently running alarms cannot be deleted within alarm() handler, but can delete it everywhere |
| 7 | // else. |
| 8 | import * as assert from 'node:assert'; |
| 9 | |
| 10 | export class DurableObjectExample { |
| 11 | constructor(state) { |
| 12 | this.state = state; |
| 13 | } |
| 14 | |
| 15 | async waitForAlarm(scheduledTime) { |
| 16 | // eslint-disable-next-line @typescript-eslint/no-this-alias |
| 17 | let self = this; |
| 18 | const { promise, resolve, reject } = Promise.withResolvers(); |
| 19 | self.resolve = resolve; |
| 20 | self.reject = reject; |
| 21 | |
| 22 | try { |
| 23 | await promise; |
| 24 | if (Date.now() < scheduledTime.valueOf()) { |
| 25 | throw new Error( |
| 26 | `Date.now() is before scheduledTime! ${Date.now()} vs ${scheduledTime.valueOf()}` |
| 27 | ); |
| 28 | } |
| 29 | } catch (e) { |
| 30 | throw new Error( |
| 31 | `error waiting for alarm at ${scheduledTime.valueOf()}: ${e}` |
| 32 | ); |
| 33 | } |
| 34 | |
| 35 | let alarm = await this.state.storage.getAlarm(); |
| 36 | if (alarm != null) { |
| 37 | throw new Error(`alarm time not cleared when handler ends. ${alarm}`); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | async alarm() { |
| 42 | try { |
| 43 | this.state.alarmsTriggered++; |
| 44 | let time = await this.state.storage.getAlarm(); |
| 45 | if (time !== null) { |
| 46 | throw new Error(`time not null inside alarm handler ${time}`); |
| 47 | } |
| 48 | // Deleting an alarm inside `alarm()` will not have any effect, unless there's another queued alarm |
| 49 | // already. |
| 50 | await this.state.storage.deleteAlarm(); |
| 51 | |
| 52 | // On the other hand, if we have an alarm queued, it will be deleted. If this is working properly, |
| 53 | // we'll only have one alarm triggered. |
| 54 | await this.state.storage.setAlarm(Date.now() + 50); |
| 55 | await this.state.storage.deleteAlarm(); |
| 56 | |
| 57 | // All done inside `alarm()`. |
| 58 | this.resolve(); |
| 59 | } catch (e) { |
| 60 | this.reject(e); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | async fetch() { |
| 65 | this.state.alarmsTriggered = 0; |
| 66 | // We set an alarm that will never trigger because it gets deleted before running. |
| 67 | await this.state.storage.setAlarm(Date.now() + 500); |
| 68 | await this.state.storage.deleteAlarm(); |
| 69 | |
| 70 | // We set another alarm that will run in 0.5s to test that deleting an alarm inside its handler |
| 71 | // does not cause a crash. |
| 72 | const time = Date.now() + 500; |
| 73 | await this.state.storage.setAlarm(time); |
| 74 | assert.equal(await this.state.storage.getAlarm(), time); |
| 75 | |
| 76 | // We should wait for all alarms the run before returning. |
| 77 | await this.waitForAlarm(time); |
| 78 | |
| 79 | // We should have ran `alarm()` only once. |
| 80 | assert.equal(this.state.alarmsTriggered, 1); |
| 81 | |
| 82 | // All done, return "OK" because if we reach this, everything worked as expected. |
| 83 | return new Response('OK'); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | export default { |
| 88 | async test(ctrl, env, ctx) { |
| 89 | let id = env.ns.idFromName('A'); |
| 90 | let obj = env.ns.get(id); |
| 91 | let res = await obj.fetch('http://foo/test'); |
| 92 | let text = await res.text(); |
| 93 | assert.equal(text, 'OK'); |
| 94 | }, |
| 95 | }; |