Skip to content

Commit

Permalink
fix(sdk): flakey datetime test (#3673)
Browse files Browse the repository at this point in the history
This test caused a failure in our CI/CD ([link](https://github.com/winglang/wing/actions/runs/5730576114/job/15529820778)), so I've tried to clean up the test a bit to avoid points where it could have nondeterministic behavior.

## Checklist

- [ ] Title matches [Winglang's style guide](https://www.winglang.io/contributing/start-here/pull_requests#how-are-pull-request-titles-formatted)
- [ ] Description explains motivation and solution
- [ ] Tests added (always)
- [ ] Docs updated (only required for features)
- [ ] Added `pr/e2e-full` label if this feature requires end-to-end testing

*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
Chriscbr authored Aug 1, 2023
1 parent 720bfda commit f8a5abf
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 32 deletions.
34 changes: 14 additions & 20 deletions examples/tests/sdk_tests/std/datetime.w
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ bring cloud;
bring util;
bring math;

let d1 = datetime.utcNow();
let d2 = datetime.systemNow();

assert(math.floor(d2.timestamp) == math.floor(d1.timestamp));
// check that dates in different timezones are comparable
let d1 = datetime.systemNow();
let d2 = d1.toUtc();
assert(d1.timestampMs == d2.timestampMs);

// create a date from an ISO timestamp
let d3 = datetime.fromIso("2023-07-18T20:18:25.177+03:00");

assert(d3.timestampMs == 1689700705177);
Expand All @@ -19,7 +20,7 @@ assert(d3.dayOfWeek == 2);
assert(d3.month == 6);
assert(d3.year == 2023);


// create a date from components
let d4 = datetime.fromComponents(year: 2023, month: 6, day: 18, hour: 19, min: 18, sec: 25, ms: 177, tz: -120);

assert(d4.timezone == -120);
Expand All @@ -36,15 +37,14 @@ assert(d4.year == 2023);
assert(d4.toUtc().hours == (d4.hours + (d4.timezone / 60)));

test "inflight datetime" {
let d5 = datetime.utcNow();
let d6 = datetime.systemNow();

assert(d2.timestamp == d1.timestamp);
// check that dates in different timezones are comparable
let d5 = datetime.systemNow();
let d6 = d5.toUtc();
assert(d5.timestampMs == d6.timestampMs);

let d7 = datetime.fromIso("2023-07-18T20:18:25.177-03:00");
let d8 = datetime.fromComponents(year: 2023, month: 6, day: 18, hour: 20, min: 18, sec: 25, ms: 177, tz: 180); // UTC-3:00


assert(d7.timestampMs == 1689722305177);
assert(d7.hours == 23);
assert(d7.min == 18);
Expand All @@ -56,20 +56,14 @@ test "inflight datetime" {
assert(d7.year == 2023);
assert(d8.hours == 20);

// check that timezone conversions work
assert(math.floor(d7.timestamp) == math.floor(d8.timestamp));
assert(d4.toUtc().hours == (d4.hours + (d4.timezone / 60)));
assert(d8.toUtc().hours == (d8.hours + (d8.timezone / 60)));


// check that systemNow doesn't return a fixed value
let beforeSleep = datetime.systemNow();
util.sleep(1s);
assert(math.floor(datetime.systemNow().timestamp - beforeSleep.timestamp) == 1);

let afterSleep = datetime.systemNow();
assert(afterSleep.timestampMs - beforeSleep.timestampMs > 0);
}







Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

## inflight.$Closure1.js
```js
module.exports = function({ $_d4_toUtc____hours, $d1_timestamp, $d2_timestamp, $d4_hours, $d4_timezone, $math_Util, $std_Datetime, $std_Duration, $util_Util }) {
module.exports = function({ $_d4_toUtc____hours, $d4_hours, $d4_timezone, $math_Util, $std_Datetime, $std_Duration, $util_Util }) {
class $Closure1 {
constructor({ }) {
const $obj = (...args) => this.handle(...args);
Object.setPrototypeOf($obj, this);
return $obj;
}
async handle() {
const d5 = (await $std_Datetime.utcNow());
const d6 = (await $std_Datetime.systemNow());
{((cond) => {if (!cond) throw new Error("assertion failed: d2.timestamp == d1.timestamp")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })($d2_timestamp,$d1_timestamp)))};
const d5 = (await $std_Datetime.systemNow());
const d6 = (await d5.toUtc());
{((cond) => {if (!cond) throw new Error("assertion failed: d5.timestampMs == d6.timestampMs")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(d5.timestampMs,d6.timestampMs)))};
const d7 = (await $std_Datetime.fromIso("2023-07-18T20:18:25.177-03:00"));
const d8 = (await $std_Datetime.fromComponents({ year: 2023, month: 6, day: 18, hour: 20, min: 18, sec: 25, ms: 177, tz: 180 }));
{((cond) => {if (!cond) throw new Error("assertion failed: d7.timestampMs == 1689722305177")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(d7.timestampMs,1689722305177)))};
Expand All @@ -30,7 +30,8 @@ module.exports = function({ $_d4_toUtc____hours, $d1_timestamp, $d2_timestamp, $
{((cond) => {if (!cond) throw new Error("assertion failed: d8.toUtc().hours == (d8.hours + (d8.timezone / 60))")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })((await d8.toUtc()).hours,(d8.hours + (d8.timezone / 60)))))};
const beforeSleep = (await $std_Datetime.systemNow());
(await $util_Util.sleep((await $std_Duration.fromSeconds(1))));
{((cond) => {if (!cond) throw new Error("assertion failed: math.floor(datetime.systemNow().timestamp - beforeSleep.timestamp) == 1")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })((await $math_Util.floor(((await $std_Datetime.systemNow()).timestamp - beforeSleep.timestamp))),1)))};
const afterSleep = (await $std_Datetime.systemNow());
{((cond) => {if (!cond) throw new Error("assertion failed: afterSleep.timestampMs - beforeSleep.timestampMs > 0")})(((afterSleep.timestampMs - beforeSleep.timestampMs) > 0))};
}
}
return $Closure1;
Expand Down Expand Up @@ -181,8 +182,6 @@ class $Root extends $stdlib.std.Resource {
return $stdlib.core.NodeJsCode.fromInline(`
require("./inflight.$Closure1.js")({
$_d4_toUtc____hours: ${context._lift((d4.toUtc()).hours)},
$d1_timestamp: ${context._lift(d1.timestamp)},
$d2_timestamp: ${context._lift(d2.timestamp)},
$d4_hours: ${context._lift(d4.hours)},
$d4_timezone: ${context._lift(d4.timezone)},
$math_Util: ${context._lift(math.Util)},
Expand All @@ -206,17 +205,15 @@ class $Root extends $stdlib.std.Resource {
_registerBind(host, ops) {
if (ops.includes("handle")) {
$Closure1._registerBindObject((d4.toUtc()).hours, host, []);
$Closure1._registerBindObject(d1.timestamp, host, []);
$Closure1._registerBindObject(d2.timestamp, host, []);
$Closure1._registerBindObject(d4.hours, host, []);
$Closure1._registerBindObject(d4.timezone, host, []);
}
super._registerBind(host, ops);
}
}
const d1 = (std.Datetime.utcNow());
const d2 = (std.Datetime.systemNow());
{((cond) => {if (!cond) throw new Error("assertion failed: math.floor(d2.timestamp) == math.floor(d1.timestamp)")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })((math.Util.floor(d2.timestamp)),(math.Util.floor(d1.timestamp)))))};
const d1 = (std.Datetime.systemNow());
const d2 = (d1.toUtc());
{((cond) => {if (!cond) throw new Error("assertion failed: d1.timestampMs == d2.timestampMs")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(d1.timestampMs,d2.timestampMs)))};
const d3 = (std.Datetime.fromIso("2023-07-18T20:18:25.177+03:00"));
{((cond) => {if (!cond) throw new Error("assertion failed: d3.timestampMs == 1689700705177")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(d3.timestampMs,1689700705177)))};
{((cond) => {if (!cond) throw new Error("assertion failed: d3.hours == 17")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(d3.hours,17)))};
Expand Down

0 comments on commit f8a5abf

Please sign in to comment.