Skip to content

Commit c7aab1b

Browse files
committed
feat: add sleep support
1 parent da93546 commit c7aab1b

11 files changed

Lines changed: 83 additions & 0 deletions

File tree

3.37 KB
Binary file not shown.

packages/cre-sdk-javy-plugin/src/javy_chainlink_sdk/src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ use rand_chacha::ChaCha8Rng;
1313
use std::collections::HashMap;
1414
use std::env;
1515
use std::sync::{Mutex, OnceLock};
16+
use std::thread;
17+
use std::time::Duration;
1618

1719
static CURRENT_MODE: Mutex<i32> = Mutex::new(0);
1820
static RANDOM_GENERATORS: OnceLock<Mutex<HashMap<i32, ChaCha8Rng>>> = OnceLock::new();
@@ -322,6 +324,15 @@ pub fn modify_runtime(runtime: Runtime) -> Runtime {
322324
Ok(milliseconds as f64)
323325
}),
324326
);
327+
328+
extend_wasm_exports(
329+
&ctx,
330+
"sleep",
331+
Func::from(|_ctx: Ctx<'_>, ms: u64| -> Result<(), Error> {
332+
thread::sleep(Duration::from_millis(ms));
333+
Ok(())
334+
}),
335+
);
325336
});
326337

327338
runtime

packages/cre-sdk/src/sdk/impl/runtime-impl.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ function createRuntimeHelpersMock(overrides: Partial<RuntimeHelpers> = {}): Runt
7373
now: mock(() => {
7474
throw new Error('Method not implemented: now')
7575
}),
76+
sleep: mock(() => {
77+
throw new Error('Method not implemented: sleep')
78+
}),
7679
log: mock(() => {}),
7780
}
7881

@@ -357,6 +360,31 @@ describe('test now converts to date', () => {
357360
})
358361
})
359362

363+
describe('test sleep delegates to helpers', () => {
364+
test('sleep calls helpers.sleep with the provided milliseconds', () => {
365+
const sleepMock = mock(() => {})
366+
const helpers = createRuntimeHelpersMock({
367+
sleep: sleepMock,
368+
})
369+
370+
const runtime = new RuntimeImpl<unknown>({}, 1, helpers, anyMaxSize)
371+
runtime.sleep(500)
372+
expect(sleepMock).toHaveBeenCalledTimes(1)
373+
expect(sleepMock).toHaveBeenCalledWith(500)
374+
})
375+
376+
test('sleep passes zero milliseconds to helpers.sleep', () => {
377+
const sleepMock = mock(() => {})
378+
const helpers = createRuntimeHelpersMock({
379+
sleep: sleepMock,
380+
})
381+
382+
const runtime = new RuntimeImpl<unknown>({}, 1, helpers, anyMaxSize)
383+
runtime.sleep(0)
384+
expect(sleepMock).toHaveBeenCalledWith(0)
385+
})
386+
})
387+
360388
describe('test getSecret', () => {
361389
test('successfully gets secret with SecretRequest (proto message)', () => {
362390
const secretRequest = create(SecretRequestSchema, {

packages/cre-sdk/src/sdk/impl/runtime-impl.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,10 @@ export class BaseRuntimeImpl<C> implements BaseRuntime<C> {
207207
return new Date(this.helpers.now())
208208
}
209209

210+
sleep(ms: number): void {
211+
this.helpers.sleep(ms)
212+
}
213+
210214
log(message: string): void {
211215
this.helpers.log(message)
212216
}
@@ -461,6 +465,9 @@ export interface RuntimeHelpers {
461465
/** Returns current time in milliseconds since Unix epoch. */
462466
now(): number
463467

468+
/** Sleeps for the specified duration. */
469+
sleep(ms: number): void
470+
464471
/** Logs a message to the host environment. */
465472
log(message: string): void
466473
}

packages/cre-sdk/src/sdk/testutils/test-runtime.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,22 @@ describe('TestRuntime / helper layer', () => {
7171
expect(rt.now().getTime()).toBe(fixed)
7272
})
7373

74+
test('sleep() completes without throwing', () => {
75+
const rt = newTestRuntime()
76+
expect(() => rt.sleep(100)).not.toThrow()
77+
})
78+
79+
test('sleep() with zero milliseconds completes without throwing', () => {
80+
const rt = newTestRuntime()
81+
expect(() => rt.sleep(0)).not.toThrow()
82+
})
83+
84+
test('sleep() returns undefined (no-op in test runtime)', () => {
85+
const rt = newTestRuntime()
86+
const result = rt.sleep(250)
87+
expect(result).toBeUndefined()
88+
})
89+
7490
test('helper call returns false for unregistered capability', () => {
7591
const rt = newTestRuntime()
7692
const cap = new BasicActionCapability()

packages/cre-sdk/src/sdk/testutils/test-runtime.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,10 @@ function createTestRuntimeHelpers(
217217
return state.timeProvider ? state.timeProvider() : Date.now()
218218
}
219219

220+
function sleep(ms: number): void {
221+
return
222+
}
223+
220224
return {
221225
call(request: Parameters<RuntimeHelpers['call']>[0]): boolean {
222226
const handler = registry.get(request.id)
@@ -320,6 +324,8 @@ function createTestRuntimeHelpers(
320324

321325
now,
322326

327+
sleep,
328+
323329
log(message: string): void {
324330
testWriter.log(message)
325331
},

packages/cre-sdk/src/sdk/types/global.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ declare global {
8282
*/
8383
function now(): number
8484

85+
/**
86+
* Sleeps for the specified duration.
87+
* @param ms - Duration in milliseconds
88+
*/
89+
function sleep(ms: number): void
90+
8591
/**
8692
* Console API available in the QuickJS runtime
8793
*/

packages/cre-sdk/src/sdk/wasm/__snapshots__/host-bindings-contract.test.ts.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ exports[`JS host bindings contract 1`] = `
1010
"log",
1111
"now",
1212
"sendResponse",
13+
"sleep",
1314
"switchModes",
1415
"versionV2",
1516
]

packages/cre-sdk/src/sdk/wasm/host-bindings.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export const globalHostBindingsSchema = z.object({
2828
.returns(z.union([z.instanceof(Uint8Array), z.custom<Uint8Array<ArrayBufferLike>>()])),
2929
getWasiArgs: z.function().args().returns(z.string()),
3030
now: z.function().args().returns(z.number()),
31+
sleep: z.function().args(z.number()).returns(z.void()),
3132
})
3233

3334
type GlobalHostBindingsMap = z.infer<typeof globalHostBindingsSchema>

packages/cre-sdk/src/sdk/wasm/runner.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ const proxyHostBindings = {
104104
now: () => {
105105
throw new Error('now called unexpectedly in test')
106106
},
107+
sleep: () => {
108+
throw new Error('sleep called unexpectedly in test')
109+
},
107110
}
108111

109112
Object.assign(globalThis, proxyHostBindings)

0 commit comments

Comments
 (0)