As part of pulumi/pulumi-fabric#331, we've been exploring just using undefined to indicate that a property value is absent during planning. We also considered blocking the message loop to simplify the overall programming model, so that all asynchrony is hidden. It turns out ThereBeDragons 🐲 anytime you try to block the message loop. So, we aren't quite sure about that bit. But the part we are convicted about is that this Computed/Property model is far too complex. Furthermore, it's very close to promises, and yet frustratingly so far away. Indeed, the original thinking in pulumi/pulumi-fabric#271 was simply to use promises, but we wanted to encourage dataflow styles, rather than control flow. But we muddied up our thinking by worrying about awaiting a promise that would never resolve. It turns out we can achieve a middle ground: resolve planning promises to undefined, so that they don't lead to hangs, but still use promises so that asynchrony is explicit in the system. This also avoids blocking the message loop. Who knows, this may actually be a fine final destination.
38 lines
1 KiB
TypeScript
38 lines
1 KiB
TypeScript
// Copyright 2016-2017, Pulumi Corporation. All rights reserved.
|
|
|
|
import * as assert from "assert";
|
|
|
|
export type MochaFunc = (err: Error) => void;
|
|
|
|
// A helper function for wrapping some of the boilerplate goo necessary to interface between Mocha's asynchronous
|
|
// testing and our TypeScript async tests.
|
|
export function asyncTest(test: () => Promise<void>): (func: MochaFunc) => void {
|
|
return (done: (err: any) => void) => {
|
|
let go = async () => {
|
|
let caught: Error | undefined;
|
|
try {
|
|
await test();
|
|
}
|
|
catch (err) {
|
|
caught = err;
|
|
}
|
|
finally {
|
|
done(caught);
|
|
}
|
|
};
|
|
go();
|
|
};
|
|
}
|
|
|
|
// A helper function for asynchronous tests that throw.
|
|
export async function assertAsyncThrows(test: () => Promise<void>): Promise<void> {
|
|
let thrown: boolean = false;
|
|
try {
|
|
await test();
|
|
}
|
|
catch (err) {
|
|
thrown = true;
|
|
}
|
|
assert(thrown, "Function was expected to throw, but didn't");
|
|
}
|
|
|