Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #2361. Fix flaky tests on web-platforms #2362

Merged
merged 2 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion LibTest/async/Future/Future.delayed_A01_t02.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,30 @@
/// The computation will be executed after the given duration has passed, and
/// the future is completed with the result. If the duration is 0 or less, it
/// completes no sooner than in the next event-loop iteration.
///
/// @description Checks that execution of the supplied computation() function
/// happens after delay.
/// @author kaigorodov

import "dart:async";
import "../../../Utils/expect.dart";

// Most browsers can trigger timers too early. Test data shows instances where
// timers fire even 15ms early. We add a safety margin to prevent flakiness
// when running this test on affected platforms.
Duration safetyMargin = const bool.fromEnvironment('dart.library.js')
? Duration(milliseconds: 40)
: Duration.zero;

check(delayms, value) {
Duration delay = durationInMilliseconds(delayms);
Stopwatch sw = new Stopwatch();
asyncStart();
sw.start();
new Future.delayed(delay, () {
Duration elapsed = sw.elapsed;
Expect.isTrue(elapsed >= delay, "delay=$delay, elapsed=${elapsed}");
Expect.isTrue(elapsed + safetyMargin >= delay,
"delay=$delay, elapsed=${elapsed + safetyMargin}");
asyncEnd();
});
}
Expand Down
17 changes: 12 additions & 5 deletions LibTest/async/Stream/Stream.periodic_A01_t01.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,40 @@
/// [T computation(int computationCount)])
/// Creates a stream that repeatedly emits events at period intervals.
/// If computation is omitted the event values will all be null.
///
/// @description Checks that created stream emits events at period intervals.
/// Checks that if computation is omitted the event value is null.
/// @author kaigorodov

import "dart:async";
import "../../../Utils/expect.dart";

// Most browsers can trigger timers too early. Test data shows instances where
// timers fire even 15ms early. We add a safety margin to prevent flakiness
// when running this test on affected platforms.
Duration safetyMargin = const bool.fromEnvironment('dart.library.js')
? Duration(milliseconds: 40)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that you may want to also adjust the intervals below for additional coverage. When periodMs is 0, 1, 10, and 30, we are no longer validating much about the duration, only that the event was fired.

The case with 100 does validate that at least 60ms have passed. I'm personally OK with that, but if you want additional coverage, we could include another check at 200.

: Duration.zero;

void check(int periodMs) {
Duration period = durationInMilliseconds(periodMs);
const int maxCount = 5;
int count = 0;

Stopwatch sw = new Stopwatch();
sw.start();

asyncStart();

Stream s = new Stream.periodic(period);
StreamSubscription? ss;
late StreamSubscription ss;
ss = s.listen((var event) {
count++;
Expect.isNull(event);
Duration expected = period * count;
Duration actual = sw.elapsed;
Expect.isTrue(expected <= actual, "expected=$expected, actual=$actual");
Expect.isTrue(expected <= actual + safetyMargin,
"expected=$expected, actual=${actual + safetyMargin}");
if (count >= maxCount) {
ss?.cancel();
ss.cancel();
asyncEnd();
}
});
Expand Down
14 changes: 12 additions & 2 deletions LibTest/async/Timer/Timer_A01_t01.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,21 @@
/// @assertion factory Timer(Duration duration, void callback())
/// Creates a new timer.
/// The callback is invoked after the given duration.
/// @description Checks that callback function is called after the given duration.
///
/// @description Checks that callback function is called after the given
/// duration.
/// @author kaigorodov

import "dart:async";
import "../../../Utils/expect.dart";

// Most browsers can trigger timers too early. Test data shows instances where
// timers fire even 15ms early. We add a safety margin to prevent flakiness
// when running this test on affected platforms.
Duration safetyMargin = const bool.fromEnvironment('dart.library.js')
? Duration(milliseconds: 40)
: Duration.zero;

check(int delayms) {
Duration delay = durationInMilliseconds(delayms);
Stopwatch sw = new Stopwatch();
Expand All @@ -19,7 +28,8 @@ check(int delayms) {
asyncStart();
new Timer(delay, () {
Duration actual = sw.elapsed;
Expect.isTrue(delay <= actual, "expected=$delay, actual=$actual");
Expect.isTrue(delay <= actual + safetyMargin,
"expected=$delay, actual=${actual + safetyMargin}");
asyncEnd();
});
}
Expand Down
7 changes: 5 additions & 2 deletions LibTest/html/Element/onMouseOver_A01_t01.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,24 @@

/// @assertion ElementStream<MouseEvent> get onMouseOver
/// Stream of mouseover events handled by this Element.
///
/// @description Checks that correct events are delivered via the stream

import "dart:html";
import "../../../Utils/expect.dart";

main() {
bool fired = false;
var type = 'mouseover';
var x = document.body;
if (x != null) {
asyncStart();
x.onMouseOver.listen((e) {
Expect.equals(type, e.type);
asyncEnd();
if (!fired) {
asyncEnd();
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe you also need to include fired = true here?

});

var event = new MouseEvent(type);
x.dispatchEvent(event);
} else {
Expand Down