Skip to content
This repository has been archived by the owner on Oct 18, 2024. It is now read-only.

Fully update internal state before invoking periodic-timer callback #89

Merged
merged 1 commit into from
Oct 4, 2024
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
## 1.3.2-wip

* Require Dart 3.3
* Fix bug where a `flushTimers` or `elapse` call from within
the callback of a periodic timer would immediately invoke
the same timer.

## 1.3.1

Expand Down
2 changes: 1 addition & 1 deletion lib/fake_async.dart
Original file line number Diff line number Diff line change
Expand Up @@ -320,9 +320,9 @@ class FakeTimer implements Timer {
assert(isActive);
_tick++;
if (isPeriodic) {
_nextCall += duration;
// ignore: avoid_dynamic_calls
_callback(this);
_nextCall += duration;
} else {
cancel();
// ignore: avoid_dynamic_calls
Expand Down
17 changes: 17 additions & 0 deletions test/fake_async_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,23 @@ void main() {
expect(ticks, [1, 2]);
});
});

test('should update periodic timer state before invoking callback', () {
// Regression test for: https://github.com/dart-lang/fake_async/issues/88
FakeAsync().run((async) {
final log = <String>[];
Timer.periodic(const Duration(seconds: 2), (timer) {
log.add('periodic ${timer.tick}');
async.elapse(Duration.zero);
});
Timer(const Duration(seconds: 3), () {
log.add('single');
});

async.flushTimers(flushPeriodicTimers: false);
expect(log, ['periodic 1', 'single']);
});
});
});

group('clock', () {
Expand Down