forked from dart-lang/co19
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes dart-lang#2471. Fix timer constructor tests. Add check for micr…
…otasks (dart-lang#2472) Fix timer constructor tests. Add check for microtasks
- Loading branch information
Showing
6 changed files
with
204 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
/// @assertion | ||
/// factory Timer.periodic(Duration duration, void callback(Timer timer)) | ||
/// | ||
/// Creates a new repeating timer. | ||
/// | ||
/// The callback is invoked repeatedly with duration intervals until canceled | ||
/// with the cancel function. | ||
/// | ||
/// The exact timing depends on the underlying timer implementation. No more | ||
/// than n callbacks will be made in duration * n time, but the time between two | ||
/// consecutive callbacks can be shorter and longer than duration. | ||
/// | ||
/// In particular, an implementation may schedule the next callback, e.g., a | ||
/// duration after either when the previous callback ended, when the previous | ||
/// callback started, or when the previous callback was scheduled for - even if | ||
/// the actual callback was delayed. | ||
/// | ||
/// @description Checks that no more than `n` callbacks are made in | ||
/// `duration * n` time | ||
/// @author sgrekhov22@gmail.com | ||
import "dart:async"; | ||
import "../../../Utils/expect.dart"; | ||
|
||
void check(int durationMs, int n) { | ||
int callbacksCount = 0; | ||
Duration duration = Duration(milliseconds: durationMs); | ||
Stopwatch sw = Stopwatch(); | ||
|
||
sw.start(); | ||
Timer.periodic(duration, (Timer timer) { | ||
callbacksCount++; | ||
if (sw.elapsedMilliseconds <= n * durationMs) { | ||
Expect.isTrue( | ||
callbacksCount <= n, | ||
"Expected no more than $n callbacks for ${n * durationMs}ms, but " + | ||
"actually there were $callbacksCount for " + | ||
"${sw.elapsedMilliseconds}ms"); | ||
} else { | ||
timer.cancel(); | ||
asyncEnd(); | ||
} | ||
}); | ||
} | ||
|
||
void main() { | ||
asyncStart(4); | ||
check(1, 5); | ||
check(10, 5); | ||
check(50, 5); | ||
check(100, 5); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
/// @assertion | ||
/// factory Timer.periodic(Duration duration, void callback(Timer timer)) | ||
/// ... | ||
/// A negative duration is treated the same as [Duration.zero] | ||
/// | ||
/// @description Checks that negative duration is accepted and treated as | ||
/// [Duration.zero]. Test that timer waits for microtasks queue before | ||
/// triggering | ||
/// @author sgrekhov22@gmail.com | ||
import "dart:async"; | ||
import "../../../Utils/expect.dart"; | ||
|
||
const maxMicrotasks = 10; | ||
int microtasksCount = 0; | ||
|
||
void microtask() { | ||
if (++microtasksCount < maxMicrotasks) { | ||
scheduleMicrotask(microtask); | ||
} | ||
} | ||
|
||
Future<void> check(int durationInMs, int n) { | ||
Completer<void> completer = Completer(); | ||
int timerCount = 0; | ||
|
||
scheduleMicrotask(microtask); | ||
|
||
Timer.periodic(Duration(milliseconds: durationInMs), (Timer timer) { | ||
Expect.equals(maxMicrotasks, microtasksCount); | ||
microtasksCount = 0; | ||
if (++timerCount == n) { | ||
timer.cancel(); | ||
completer.complete(); | ||
asyncEnd(); | ||
} else { | ||
scheduleMicrotask(microtask); | ||
} | ||
}); | ||
return completer.future; | ||
} | ||
|
||
main() async { | ||
asyncStart(4); | ||
await check(0, 5); | ||
await check(-10, 5); | ||
await check(-100, 5); | ||
await check(-1000, 3); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
/// @assertion factory Timer(Duration duration, void callback()) | ||
/// | ||
/// A negative duration is treated the same as [Duration.zero] | ||
/// | ||
/// @description Checks that negative duration is accepted and treated as | ||
/// [Duration.zero]. Test that timer waits for microtask queue before the start | ||
/// @author sgrekhov@gmail.com | ||
import "dart:async"; | ||
import "../../../Utils/expect.dart"; | ||
|
||
const maxMicrotasks = 10; | ||
int microtasksCount = 0; | ||
|
||
void microtask() { | ||
if (++microtasksCount < maxMicrotasks) { | ||
scheduleMicrotask(microtask); | ||
} | ||
} | ||
|
||
Future<void> check(int durationInMs, int n) { | ||
Completer<void> completer = Completer(); | ||
|
||
scheduleMicrotask(microtask); | ||
|
||
Timer(Duration(milliseconds: durationInMs), () { | ||
Expect.equals(maxMicrotasks, microtasksCount); | ||
microtasksCount = 0; | ||
completer.complete(); | ||
asyncEnd(); | ||
}); | ||
return completer.future; | ||
} | ||
|
||
main() async { | ||
asyncStart(4); | ||
await check(0, 5); | ||
await check(-10, 5); | ||
await check(-100, 5); | ||
await check(-500, 3); | ||
} |