Skip to content

Commit 3d8aa58

Browse files
test(jco): refactor tests, and explicitly shared instances
1 parent aa7e4c7 commit 3d8aa58

5 files changed

Lines changed: 588 additions & 395 deletions

File tree

packages/jco/test/p3/future-lifts.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,7 @@ suite("future<T> lifts", () => {
4444

4545
esModule = setupRes.esModule;
4646
cleanup = setupRes.cleanup;
47-
//console.log("OUTPUT DIR", setupRes.outputDir);
48-
});
49-
50-
afterAll(async () => {
51-
await cleanup();
52-
});
5347

54-
beforeEach(async () => {
5548
instance = await esModule.instantiate(undefined, {
5649
...new WASIShim().getImportObject(),
5750
"jco:test-components/resources": {
@@ -60,6 +53,10 @@ suite("future<T> lifts", () => {
6053
});
6154
});
6255

56+
afterAll(async () => {
57+
await cleanup();
58+
});
59+
6360
test.concurrent("bool", async () => {
6461
assert.instanceOf(instance["jco:test-components/get-future-async"].getFutureBool, AsyncFunction);
6562
const vals = [true, false];
@@ -435,7 +432,10 @@ suite("future<T> lifts", () => {
435432

436433
let numDisposed = 0;
437434
for (const resource of resources) {
438-
assert.strictEqual(resource.getId(), await resource.getIdAsync());
435+
// sync functions can fail to lock the component state if run too soon after
436+
// async functions
437+
let expectedID = resource.getId();
438+
assert.strictEqual(expectedID, await resource.getIdAsync());
439439
assert.doesNotThrow(() => resource[disposeSymbol]());
440440
numDisposed += 1;
441441
assert.strictEqual(

packages/jco/test/p3/stream-lifts.js

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { join } from "node:path";
22

3-
import { suite, test, assert, beforeAll, beforeEach, afterAll, expect } from "vitest";
3+
import { suite, test, assert, beforeAll, afterAll, expect } from "vitest";
44

55
import { setupAsyncTest } from "../helpers.js";
66
import { AsyncFunction, LOCAL_TEST_COMPONENTS_DIR, checkStreamValues } from "../common.js";
77
import { WASIShim } from "@bytecodealliance/preview2-shim/instantiation";
88

99
suite("stream<T> lifts", () => {
10-
let esModule, cleanup, instance;
10+
let esModule, cleanup, getInstance, instance;
1111

1212
class ExampleResource {
1313
#id;
@@ -39,29 +39,41 @@ suite("stream<T> lifts", () => {
3939

4040
esModule = setupRes.esModule;
4141
cleanup = setupRes.cleanup;
42-
});
4342

44-
afterAll(async () => {
45-
await cleanup();
46-
});
47-
48-
beforeEach(async () => {
43+
// We use a completely shared instance because sibling re-entrance
44+
// is mediated by code in task.enter()
4945
instance = await esModule.instantiate(undefined, {
5046
...new WASIShim().getImportObject(),
5147
"jco:test-components/resources": {
5248
ExampleResource,
5349
},
5450
});
51+
getInstance = () => Promise.resolve(instance);
52+
53+
// NOTE: To use an explicitly new instance per-test (more stable), uncomment the lines below
54+
//
55+
// getInstance = async () => esModule.instantiate(undefined, {
56+
// ...new WASIShim().getImportObject(),
57+
// "jco:test-components/resources": {
58+
// ExampleResource,
59+
// },
60+
// });
61+
});
62+
63+
afterAll(async () => {
64+
await cleanup();
5565
});
5666

5767
test.concurrent("bool", async () => {
68+
const instance = await getInstance();
5869
assert.instanceOf(instance["jco:test-components/get-stream-async"].getStreamBool, AsyncFunction);
5970
const vals = [true, false];
6071
const stream = await instance["jco:test-components/get-stream-async"].getStreamBool(vals);
6172
await checkStreamValues({ stream, vals, typeName: "bool" });
6273
});
6374

6475
test.concurrent("u8/s8", async () => {
76+
const instance = await getInstance();
6577
assert.instanceOf(instance["jco:test-components/get-stream-async"].getStreamU8, AsyncFunction);
6678
assert.instanceOf(instance["jco:test-components/get-stream-async"].getStreamS8, AsyncFunction);
6779

@@ -89,6 +101,7 @@ suite("stream<T> lifts", () => {
89101
});
90102

91103
test.concurrent("u16/s16", async () => {
104+
const instance = await getInstance();
92105
assert.instanceOf(instance["jco:test-components/get-stream-async"].getStreamU16, AsyncFunction);
93106
assert.instanceOf(instance["jco:test-components/get-stream-async"].getStreamS16, AsyncFunction);
94107

@@ -102,6 +115,7 @@ suite("stream<T> lifts", () => {
102115
});
103116

104117
test.concurrent("u32/s32", async () => {
118+
const instance = await getInstance();
105119
assert.instanceOf(instance["jco:test-components/get-stream-async"].getStreamU32, AsyncFunction);
106120
assert.instanceOf(instance["jco:test-components/get-stream-async"].getStreamS32, AsyncFunction);
107121

@@ -115,6 +129,7 @@ suite("stream<T> lifts", () => {
115129
});
116130

117131
test.concurrent("u64/s64", async () => {
132+
const instance = await getInstance();
118133
assert.instanceOf(instance["jco:test-components/get-stream-async"].getStreamU64, AsyncFunction);
119134
assert.instanceOf(instance["jco:test-components/get-stream-async"].getStreamS64, AsyncFunction);
120135

@@ -142,6 +157,7 @@ suite("stream<T> lifts", () => {
142157
});
143158

144159
test.concurrent("f32/f64", async () => {
160+
const instance = await getInstance();
145161
assert.instanceOf(instance["jco:test-components/get-stream-async"].getStreamF64, AsyncFunction);
146162
assert.instanceOf(instance["jco:test-components/get-stream-async"].getStreamF32, AsyncFunction);
147163

@@ -169,6 +185,7 @@ suite("stream<T> lifts", () => {
169185
});
170186

171187
test.concurrent("string", async () => {
188+
const instance = await getInstance();
172189
assert.instanceOf(instance["jco:test-components/get-stream-async"].getStreamString, AsyncFunction);
173190

174191
let vals = ["hello", "world", "!"];
@@ -177,6 +194,7 @@ suite("stream<T> lifts", () => {
177194
});
178195

179196
test.concurrent("record", async () => {
197+
const instance = await getInstance();
180198
assert.instanceOf(instance["jco:test-components/get-stream-async"].getStreamRecord, AsyncFunction);
181199

182200
let vals = [
@@ -189,6 +207,7 @@ suite("stream<T> lifts", () => {
189207
});
190208

191209
test.concurrent("variant", async () => {
210+
const instance = await getInstance();
192211
assert.instanceOf(instance["jco:test-components/get-stream-async"].getStreamVariant, AsyncFunction);
193212

194213
const vals = [
@@ -238,6 +257,7 @@ suite("stream<T> lifts", () => {
238257
});
239258

240259
test.concurrent("tuple", async () => {
260+
const instance = await getInstance();
241261
assert.instanceOf(instance["jco:test-components/get-stream-async"].getStreamTuple, AsyncFunction);
242262

243263
let vals = [
@@ -250,6 +270,7 @@ suite("stream<T> lifts", () => {
250270
});
251271

252272
test.concurrent("flags", async () => {
273+
const instance = await getInstance();
253274
assert.instanceOf(instance["jco:test-components/get-stream-async"].getStreamFlags, AsyncFunction);
254275

255276
let vals = [
@@ -262,6 +283,7 @@ suite("stream<T> lifts", () => {
262283
});
263284

264285
test.concurrent("enum", async () => {
286+
const instance = await getInstance();
265287
assert.instanceOf(instance["jco:test-components/get-stream-async"].getStreamEnum, AsyncFunction);
266288

267289
let vals = ["first", "second", "third"];
@@ -270,6 +292,7 @@ suite("stream<T> lifts", () => {
270292
});
271293

272294
test.concurrent("option<string>", async () => {
295+
const instance = await getInstance();
273296
assert.instanceOf(instance["jco:test-components/get-stream-async"].getStreamOptionString, AsyncFunction);
274297

275298
let vals = ["present string", null];
@@ -288,6 +311,7 @@ suite("stream<T> lifts", () => {
288311
});
289312

290313
test.concurrent("list<u8>", async () => {
314+
const instance = await getInstance();
291315
assert.instanceOf(instance["jco:test-components/get-stream-async"].getStreamListU8, AsyncFunction);
292316
let vals = [[0x01, 0x02, 0x03, 0x04, 0x05], new Uint8Array([0x05, 0x04, 0x03, 0x02, 0x01]), []];
293317
let stream = await instance["jco:test-components/get-stream-async"].getStreamListU8(vals);
@@ -309,13 +333,15 @@ suite("stream<T> lifts", () => {
309333
// TODO(fix): add tests for optimized UintXArrays (js_array_ty)
310334

311335
test.concurrent("list<string>", async () => {
336+
const instance = await getInstance();
312337
assert.instanceOf(instance["jco:test-components/get-stream-async"].getStreamListString, AsyncFunction);
313338
let vals = [["first", "second", "third"], []];
314339
let stream = await instance["jco:test-components/get-stream-async"].getStreamListString(vals);
315340
await checkStreamValues({ stream, vals, typeName: "list<string>", assertEqFn: assert.deepEqual });
316341
});
317342

318343
test.concurrent("list<u32, 5>", async () => {
344+
const instance = await getInstance();
319345
assert.instanceOf(instance["jco:test-components/get-stream-async"].getStreamFixedListU32, AsyncFunction);
320346
let vals = [
321347
[1, 2, 3, 4, 5],
@@ -327,6 +353,7 @@ suite("stream<T> lifts", () => {
327353
});
328354

329355
test.concurrent("list<record>", async () => {
356+
const instance = await getInstance();
330357
assert.instanceOf(instance["jco:test-components/get-stream-async"].getStreamListRecord, AsyncFunction);
331358
let vals = [
332359
[{ id: 1, idStr: "one" }],
@@ -346,6 +373,7 @@ suite("stream<T> lifts", () => {
346373
});
347374

348375
test.concurrent("result<string>", async () => {
376+
const instance = await getInstance();
349377
assert.instanceOf(instance["jco:test-components/get-stream-async"].getStreamResultString, AsyncFunction);
350378
let vals = [
351379
{ tag: "ok", val: "present string" },
@@ -356,20 +384,24 @@ suite("stream<T> lifts", () => {
356384
});
357385

358386
test.concurrent("example-resource", async () => {
387+
const instance = await getInstance();
359388
assert.instanceOf(instance["jco:test-components/get-stream-async"].getStreamExampleResourceOwn, AsyncFunction);
360389
const disposeSymbol = Symbol.dispose || Symbol.for("dispose");
361390

362391
let vals = [2, 1, 0];
363392
let stream = await instance["jco:test-components/get-stream-async"].getStreamExampleResourceOwn(vals);
364393
const resources = [];
365-
for (const expectedResourceId of vals) {
366-
const { value: resource, done } = await stream.next();
367-
assert.isFalse(done);
394+
const retrievedIds = [];
395+
for await (const resource of stream) {
368396
assert.isNotNull(resource);
369397
assert.instanceOf(resource, instance["jco:test-components/get-stream-async"].ExampleGuestResource);
370-
assert.strictEqual(resource.getId(), expectedResourceId);
398+
retrievedIds.push(resource.getId());
371399
resources.push(resource);
372400
}
401+
assert.deepEqual(
402+
retrievedIds,
403+
vals,
404+
);
373405

374406
const finished = await stream.next();
375407
assert.isTrue(finished.done);
@@ -385,7 +417,10 @@ suite("stream<T> lifts", () => {
385417

386418
let numDisposed = 0;
387419
for (const resource of resources) {
388-
assert.strictEqual(resource.getId(), await resource.getIdAsync());
420+
// NOTE: if runing async operations and sync operations too quickly, the sync operation *can*
421+
// fail to lock the component async state.
422+
let expectedID = resource.getId();
423+
assert.strictEqual(expectedID, await resource.getIdAsync());
389424
assert.doesNotThrow(() => resource[disposeSymbol]());
390425
numDisposed += 1;
391426
assert.strictEqual(
@@ -396,6 +431,7 @@ suite("stream<T> lifts", () => {
396431
});
397432

398433
test.concurrent("example-resource#get-id", async () => {
434+
const instance = await getInstance();
399435
assert.instanceOf(
400436
instance["jco:test-components/get-stream-async"].getStreamExampleResourceOwnAttr,
401437
AsyncFunction,
@@ -412,6 +448,7 @@ suite("stream<T> lifts", () => {
412448
});
413449

414450
test.concurrent("stream<string>", async () => {
451+
const instance = await getInstance();
415452
assert.instanceOf(instance["jco:test-components/get-stream-async"].getStreamStreamString, AsyncFunction);
416453
let vals = ["first", "third", "second"];
417454
let stream = await instance["jco:test-components/get-stream-async"].getStreamStreamString(vals);

0 commit comments

Comments
 (0)