From 66962f2a87db24bb63fe8282ce76fb64c0a2e20b Mon Sep 17 00:00:00 2001 From: Mark McCulloh Date: Thu, 21 Dec 2023 16:10:25 -0500 Subject: [PATCH] chore: use `util.waitUntil` in more wing tests (#5296) Noticed that the invokeAsync test may be fragile due to the way it used sleep. Changed it to waitUntil, and went through a few other tests to use it as well. Adding do-not-merge until testing in cloud as well *By submitting this pull request, I confirm that my contribution is made under the terms of the [Wing Cloud Contribution License](https://github.com/winglang/wing/blob/main/CONTRIBUTION_LICENSE.md)*. --- examples/tests/sdk_tests/bucket/events.test.w | 44 ++++++------------- .../sdk_tests/function/invoke_async.test.w | 7 +-- examples/tests/sdk_tests/queue/purge.test.w | 23 ++-------- examples/tests/sdk_tests/queue/push.test.w | 19 +++++--- .../tests/sdk_tests/queue/set_consumer.test.w | 33 ++------------ 5 files changed, 39 insertions(+), 87 deletions(-) diff --git a/examples/tests/sdk_tests/bucket/events.test.w b/examples/tests/sdk_tests/bucket/events.test.w index 56ebcca61d4..a3b417c4dc3 100644 --- a/examples/tests/sdk_tests/bucket/events.test.w +++ b/examples/tests/sdk_tests/bucket/events.test.w @@ -44,22 +44,6 @@ b.onEvent(inflight (key: str, event: cloud.BucketEventType) => { logHistory(key, "{event}()", Source.onEvent); }); -let wait = inflight (pred: inflight (): bool): bool => { - let var i = 0; - // waiting for up to 2 minutess, checking every 10 seconds - while i < 12 { - if pred() { - return true; - } - - util.sleep(10s); - - i = i + 1; - } - - return false; -}; - struct CheckHitCountOptions { key: str; type: str; @@ -67,18 +51,16 @@ struct CheckHitCountOptions { count: num; } - -let checkHitCount = inflight (opts: CheckHitCountOptions): inflight (): bool => { - return inflight (): bool => { +let checkHitCount = inflight (opts: CheckHitCountOptions): bool => { + return util.waitUntil(inflight () => { let var count = 0; for u in table.list() { - if (u.get("key") == opts.key && u.get("operation") == opts.type && u.get("source") == "{opts.source}") { count = count + 1; } } return count == opts.count; - }; + }, timeout: 2m, interval: 10s); }; @@ -92,19 +74,19 @@ new std.Test(inflight () => { // https://github.com/winglang/wing/issues/2724 if (util.env("WING_TARGET") != "tf-aws") { // assert that onCreate events about the "a", "b", and "c" objects were each produced exactly 1 time - assert(wait(checkHitCount(key: "a", type: "onCreate()", source: Source.anyEvent, count: 1))); - assert(wait(checkHitCount(key: "b", type: "onCreate()", source: Source.anyEvent, count: 1))); - assert(wait(checkHitCount(key: "c", type: "onCreate()", source: Source.anyEvent, count: 1))); + assert(checkHitCount(key: "a", type: "onCreate()", source: Source.anyEvent, count: 1)); + assert(checkHitCount(key: "b", type: "onCreate()", source: Source.anyEvent, count: 1)); + assert(checkHitCount(key: "c", type: "onCreate()", source: Source.anyEvent, count: 1)); - assert(wait(checkHitCount(key: "a", type: "onCreate()", source: Source.onEvent, count: 1))); - assert(wait(checkHitCount(key: "b", type: "onCreate()", source: Source.onEvent, count: 1))); - assert(wait(checkHitCount(key: "c", type: "onCreate()", source: Source.onEvent, count: 1))); + assert(checkHitCount(key: "a", type: "onCreate()", source: Source.onEvent, count: 1)); + assert(checkHitCount(key: "b", type: "onCreate()", source: Source.onEvent, count: 1)); + assert(checkHitCount(key: "c", type: "onCreate()", source: Source.onEvent, count: 1)); - assert(wait(checkHitCount(key: "b", type: "onUpdate()", source: Source.anyEvent, count: 1))); - assert(wait(checkHitCount(key: "c", type: "onDelete()", source: Source.anyEvent, count: 1))); + assert(checkHitCount(key: "b", type: "onUpdate()", source: Source.anyEvent, count: 1)); + assert(checkHitCount(key: "c", type: "onDelete()", source: Source.anyEvent, count: 1)); - assert(wait(checkHitCount(key: "b", type: "onUpdate()", source: Source.onEvent, count: 1))); - assert(wait(checkHitCount(key: "c", type: "onDelete()", source: Source.onEvent, count: 1))); + assert(checkHitCount(key: "b", type: "onUpdate()", source: Source.onEvent, count: 1)); + assert(checkHitCount(key: "c", type: "onDelete()", source: Source.onEvent, count: 1)); } }, timeout: 8m) as "hitCount is incremented according to the bucket event"; diff --git a/examples/tests/sdk_tests/function/invoke_async.test.w b/examples/tests/sdk_tests/function/invoke_async.test.w index c252ba91523..97ca3a15aa5 100644 --- a/examples/tests/sdk_tests/function/invoke_async.test.w +++ b/examples/tests/sdk_tests/function/invoke_async.test.w @@ -5,7 +5,7 @@ let counter = new cloud.Counter(); let fn = new cloud.Function(inflight (input: str) => { log("log inside fn"); - util.sleep(5s); + util.sleep(3s); counter.inc(); }); @@ -19,6 +19,7 @@ test "invokeAsync() returns without waiting for the function finishes" { assert(counter.peek() == 0); fn.invokeAsync(""); assert(counter.peek() == 0); - util.sleep(10s); - assert(counter.peek() == 1); + util.waitUntil(() => { + return counter.peek() == 1; + }, timeout: 15s); } diff --git a/examples/tests/sdk_tests/queue/purge.test.w b/examples/tests/sdk_tests/queue/purge.test.w index d6ace9a604a..2350637176a 100644 --- a/examples/tests/sdk_tests/queue/purge.test.w +++ b/examples/tests/sdk_tests/queue/purge.test.w @@ -3,33 +3,18 @@ bring util; let q = new cloud.Queue(); - test "purge" { q.push("foo"); q.push("bar"); q.push("baz"); - let wait = inflight (pred: inflight (): bool): bool => { - let var i = 0; - while i < 60 { - if pred() { - return true; - } - - util.sleep(1s); - i = i + 1; - } - - return false; - }; - - assert(wait(inflight (): bool => { + assert(util.waitUntil(inflight () => { return q.approxSize() == 3; - })); + }, timeout: 1m, interval: 1s)); q.purge(); - assert(wait(inflight (): bool => { + assert(util.waitUntil(inflight () => { return q.approxSize() == 0; - })); + }, timeout: 1m, interval: 1s)); } diff --git a/examples/tests/sdk_tests/queue/push.test.w b/examples/tests/sdk_tests/queue/push.test.w index ff2b51580f7..2092540dc58 100644 --- a/examples/tests/sdk_tests/queue/push.test.w +++ b/examples/tests/sdk_tests/queue/push.test.w @@ -4,7 +4,7 @@ bring util; let q = new cloud.Queue(); new std.Test(inflight () => { - let obj = Json { + let obj = { k1: 1, k2: "hello", k3: true, @@ -36,15 +36,24 @@ new std.Test(inflight () => { q.pop(); q.push("Bar", "Baz"); - assert(util.waitUntil((): bool => { + assert(util.waitUntil(() => { return q.approxSize() == 2; })); - q.purge(); // the message deletion process takes up to 60 seconds. (https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-sqs/classes/purgequeuecommand.html) - util.sleep(1m); + q.purge(); + if util.env("WING_TARGET") != "sim" { + // In a real cloud, purging is expensive so we should wait a minute regardless of .approxSize() + // e.g. See AWS docs for queue purging (https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-sqs/classes/purgequeuecommand.html) + util.sleep(1m); + } + + assert(util.waitUntil(() => { + return q.approxSize() == 0; + })); + q.push("123", "\r", "{obj}"); - assert(util.waitUntil((): bool => { + assert(util.waitUntil(() => { return q.approxSize() == 3; })); }, timeout: 3m) as "push"; diff --git a/examples/tests/sdk_tests/queue/set_consumer.test.w b/examples/tests/sdk_tests/queue/set_consumer.test.w index 5f1f1186e4e..d972a6b16aa 100644 --- a/examples/tests/sdk_tests/queue/set_consumer.test.w +++ b/examples/tests/sdk_tests/queue/set_consumer.test.w @@ -4,39 +4,14 @@ bring util; let q = new cloud.Queue(); let c = new cloud.Counter(); -// workaround for $stdlib is not defined compiler error -// had to wrap the inflight ():bool => { c.peek == 2 } inflight method with a the Predicate resource -class Predicate { - c: cloud.Counter; - new(c: cloud.Counter){ - this.c = c; - } - - pub inflight test(): bool { - return this.c.peek() == 2; - } -} - - - q.setConsumer(inflight (msg: str) => { c.inc(); }); - -let predicate = new Predicate(c); test "setConsumer" { - q.push("hello"); - q.push("world"); + q.push("hello", "world"); - let var i = 0; - while i < 600 { - i = i + 1; - if predicate.test() { - assert(predicate.test()); - return; - } - util.sleep(1s); - } - assert(predicate.test()); + assert(util.waitUntil( + inflight () => { return c.peek() == 2; }, timeout: 10m, interval: 1s + )); }