Skip to content

Commit

Permalink
Fixes #2361. Fix flaky tests on web-platforms (#2362)
Browse files Browse the repository at this point in the history
Fix flaky tests on web-platforms
  • Loading branch information
sgrekhov authored Nov 9, 2023
1 parent c48470e commit 27ae4f9
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 10 deletions.
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)
: 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();
}
});

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

0 comments on commit 27ae4f9

Please sign in to comment.