-
Naively, I wanted to write a test that verifies that on_message works bring cloud;
let q = new cloud.Queue();
let c = new cloud.Counter();
q.on_message(inflight (m: str): str => {
c.inc();
print("on_message got triggered with ${m}");
});
new cloud.Function(inflight () => {
print("before push counter is ${c.peek()}");
q.push("a messgae");
print("after push counter is ${c.peek()}");
assert(1 == c.peek());
}) as "test:something"; The result of fail ┌ a.w » root/test:something
│ before push counter is 0
│ after push counter is 0
│ Error: assertion failed: '(1 === (await c.peek()))'
│ at evalmachine.<anonymous>:34:23
│ at Handler.handle (evalmachine.<anonymous>:35:15)
│ at async exports.handler (evalmachine.<anonymous>:12:10)
│ at async Object.withTrace (/Users/eyalkeren/.nvm/versions/node/v18.12.1/lib/node_modules/winglang/node_modules/@winglang/sdk/lib/testing/simulator.js:114:38)
│ at async Simulator.runTest (/Users/eyalkeren/.nvm/versions/node/v18.12.1/lib/node_modules/winglang/node_modules/@winglang/sdk/lib/testing/simulator.js:303:13)
│ at async Simulator.runAllTests (/Users/eyalkeren/.nvm/versions/node/v18.12.1/lib/node_modules/winglang/node_modules/@winglang/sdk/lib/testing/simulator.js:278:26)
│ at async testOne (/Users/eyalkeren/.nvm/versions/node/v18.12.1/lib/node_modules/winglang/dist/commands/test.js:26:21)
└ at async test (/Users/eyalkeren/.nvm/versions/node/v18.12.1/lib/node_modules/winglang/dist/commands/test.js:12:9) The reason for this test to fail is because the on_message handler will be called async. How should I test the above scenario? |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 2 replies
-
I think to test this scenario you would need to add some kind of |
Beta Was this translation helpful? Give feedback.
-
Welcome to distributed computing :) |
Beta Was this translation helpful? Give feedback.
-
This is a common problem with async operations. There are many libraries that allow you to express waiting on a condition easily - for an example, see Awaitility in Java. You can add some built-in helpers to Wing (either to the standard library of the language, or the SDK), so your users can do something like: new cloud.Function(inflight () => {
print("before push counter is ${c.peek()}");
q.push("a message");
print("after push counter is ${c.peek()}");
assert.waitingUntil(() => c.peek() == 1, {
atMost: 2.seconds,
});
}) as "test:something"; Or something similar 🙂. |
Beta Was this translation helpful? Give feedback.
-
With
|
Beta Was this translation helpful? Give feedback.
With
extern
and some workarounds I was able to write a test that:queue/on_message.w