Skip to content

Commit

Permalink
chore: use util.waitUntil in more wing tests (#5296)
Browse files Browse the repository at this point in the history
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)*.
  • Loading branch information
MarkMcCulloh authored Dec 21, 2023
1 parent dc2a2d8 commit 66962f2
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 87 deletions.
44 changes: 13 additions & 31 deletions examples/tests/sdk_tests/bucket/events.test.w
Original file line number Diff line number Diff line change
Expand Up @@ -44,41 +44,23 @@ 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;
source: Source;
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);
};


Expand All @@ -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";
7 changes: 4 additions & 3 deletions examples/tests/sdk_tests/function/invoke_async.test.w
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

Expand All @@ -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);
}
23 changes: 4 additions & 19 deletions examples/tests/sdk_tests/queue/purge.test.w
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
19 changes: 14 additions & 5 deletions examples/tests/sdk_tests/queue/push.test.w
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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";
33 changes: 4 additions & 29 deletions examples/tests/sdk_tests/queue/set_consumer.test.w
Original file line number Diff line number Diff line change
Expand Up @@ -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
));
}

0 comments on commit 66962f2

Please sign in to comment.