winglang / wing

A programming language for the cloud ☁️ A unified programming model, combining infrastructure and runtime code into one language ⚡
https://winglang.io
Other
4.8k stars 189 forks source link

tests might finish before async processes are finished #5298

Open skorfmann opened 6 months ago

skorfmann commented 6 months ago

I tried this:

just realized that the cloud.Topic is a bit tricky within tests. If there's no blocking waiter somewhere, the tests might finish before messages were processed. Makes sense when thinking about it, it's still pretty confusing when the console works as expected since it just keeps running forever and tests are not.

bring cloud;
bring util;

let bucket = new cloud.Bucket();
let topic = new cloud.Topic();

topic.onMessage(inflight (message) => {
  log("I won't show up in console or cli without a waiter");
  bucket.put("foo", "bar");
});

test "finishes before done" {
  topic.publish(Json.stringify({
    "my-key": "yeha"
  }));

  assert(true);
}

playground

This happened:

the logs are not showing up, since the test is finished and exits

I expected this:

some way to force waiting

Is there a workaround?

add a waiter

bring cloud;
bring util;

let bucket = new cloud.Bucket();
let topic = new cloud.Topic();

topic.onMessage(inflight (message) => {
  log("I won't show up in console or cli without a waiter");
  bucket.put("foo", "bar");
});

test "finishes before done" {
  topic.publish(Json.stringify({
    "my-key": "yeha"
  }));

  util.waitUntil(inflight () => {
    return bucket.exists("foo");
  }, {
    timeout: 5s
  });

  assert(true);
}

playground

Anything else?

even if fixed on sim, this might be still confusing on cloud targets.

https://winglang.slack.com/archives/C048QCN2XLJ/p1703151191040499

Wing Version

0.53.4

Node.js Version

18.7

Platform(s)

MacOS

Community Notes

Chriscbr commented 6 months ago

IMO this isn't a bug -- if we waited for async processes to finish, then tests ran locally would behave differently from running tests on the cloud. I'm also not sure if there's a reliable way to wait for all ongoing compute to finish on AWS (see e.g. https://stackoverflow.com/questions/53311165/is-there-a-way-to-programmatically-know-how-many-concurrent-instances-of-a-lambd) or other clouds.

Of course, we could try to make this more transparent in the simulator. For example, maybe we could hint that there was still ongoing work:

$ wing test main.w
pass ─ main.wsim » root/env0/test:test (2 non-idle resources)

Tests 1 passed (1)
Test Files 1 passed (1)
Duration 0m0.54s

what do you think @skorfmann?

skorfmann commented 6 months ago

@Chriscbr yes, I like that idea 👍 Would be pretty interesting which resources. Would be looking for an easy way to dig deeper as a user.