Skip to content

Commit 4d155d9

Browse files
Add basic unit and integration tests for promise scheduler
1 parent 5677059 commit 4d155d9

File tree

4 files changed

+64
-1
lines changed

4 files changed

+64
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
require_relative "./unit/test_js"
22
require_relative "./unit/test_object"
33
require_relative "./unit/test_error"
4+
require_relative "./unit/test_async"
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
require "test-unit"
2+
require "js"
3+
4+
class JS::TestAsync < Test::Unit::TestCase
5+
def test_await_promise_resolve
6+
promise = JS.eval("return Promise.resolve(42)")
7+
assert_equal 42, promise.await.to_i
8+
end
9+
10+
def test_await_promise_reject
11+
promise = JS.eval("return Promise.reject(42)")
12+
e = assert_raise(JS::Error) { promise.await }
13+
assert_equal "42", e.message
14+
end
15+
16+
def test_await_promise_chained
17+
promise = JS.eval("return Promise.resolve(42).then(x => x + 1)")
18+
assert_equal 43, promise.await.to_i
19+
end
20+
21+
# def test_await_non_promise
22+
# assert_equal 42, JS.eval("return 42").await.to_i
23+
# end
24+
end

packages/npm-packages/ruby-wasm-wasi/test/vm.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,4 +191,33 @@ eval:11:in \`<main>'`);
191191
const hash = vm.eval(`Hash.new`);
192192
hash.call("store", vm.eval(`"key1"`), vm.wrap(new Object()));
193193
});
194+
195+
test("async eval over microtasks", async () => {
196+
const vm = await initRubyVM();
197+
const result = await vm.evalAsync(`
198+
require 'js'
199+
o = JS.eval(<<~JS)
200+
return {
201+
async_func: () => {
202+
return new Promise((resolve) => {
203+
queueMicrotask(() => {
204+
resolve(42)
205+
});
206+
});
207+
}
208+
}
209+
JS
210+
o.async_func.await
211+
`);
212+
expect(result.toString()).toBe("42");
213+
});
214+
215+
test("async eval multiple times", async () => {
216+
const vm = await initRubyVM();
217+
vm.eval(`require "js"`);
218+
const ret0 = await vm.evalAsync(`JS.global[:Promise].resolve(42).await`);
219+
expect(ret0.toString()).toBe("42");
220+
const ret1 = await vm.evalAsync(`JS.global[:Promise].resolve(43).await`);
221+
expect(ret1.toString()).toBe("43");
222+
});
194223
});

packages/npm-packages/ruby-wasm-wasi/tools/run-test-unit.mjs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ const instantiate = async (rootTestFile) => {
2323
const wasi = new WASI({
2424
stdio: "inherit",
2525
args: ["ruby.wasm"].concat(process.argv.slice(2)),
26-
env: process.env,
26+
env: {
27+
...process.env,
28+
// Extend fiber stack size to be able to run test-unit
29+
"RUBY_FIBER_MACHINE_STACK_SIZE": String(1024 * 1024 * 20),
30+
},
2731
preopens: preopens,
2832
});
2933

@@ -47,6 +51,9 @@ const main = async () => {
4751
const rootTestFile = "/__root__/test/test_unit.rb";
4852
const { vm } = await instantiate(rootTestFile);
4953

54+
Error.stackTraceLimit = Infinity;
55+
56+
// FIXME: require 'test/unit' fails with evalAsync due to Kernel#eval (?)
5057
vm.eval(`
5158
# HACK: Until we've fixed the issue in the test-unit or power_assert
5259
# See https://github.com/test-unit/test-unit/pull/221
@@ -62,6 +69,8 @@ const main = async () => {
6269
end
6370
6471
require 'test/unit'
72+
`);
73+
await vm.evalAsync(`
6574
require_relative '${rootTestFile}'
6675
Test::Unit::AutoRunner.run
6776
`);

0 commit comments

Comments
 (0)