44 lines
1.1 KiB
Dart
44 lines
1.1 KiB
Dart
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:yx_tracking_flutter/src/core/scheduler.dart';
|
|
|
|
void main() {
|
|
group('Scheduler', () {
|
|
test('start/stop 生命周期可工作且重复 start 不会重建', () async {
|
|
var ticks = 0;
|
|
final scheduler = Scheduler(
|
|
interval: const Duration(milliseconds: 10),
|
|
onTick: () async {
|
|
ticks += 1;
|
|
},
|
|
)
|
|
..start()
|
|
..start();
|
|
|
|
await Future<void>.delayed(const Duration(milliseconds: 35));
|
|
|
|
expect(scheduler.isRunning, isTrue);
|
|
expect(ticks, greaterThanOrEqualTo(1));
|
|
|
|
scheduler.stop();
|
|
expect(scheduler.isRunning, isFalse);
|
|
|
|
scheduler.dispose();
|
|
});
|
|
|
|
test('onTick 抛错会被捕获', () async {
|
|
var ticks = 0;
|
|
final scheduler = Scheduler(
|
|
interval: const Duration(milliseconds: 10),
|
|
onTick: () async {
|
|
ticks += 1;
|
|
throw StateError('tick failed');
|
|
},
|
|
)..start();
|
|
await Future<void>.delayed(const Duration(milliseconds: 25));
|
|
scheduler.stop();
|
|
|
|
expect(ticks, greaterThanOrEqualTo(1));
|
|
});
|
|
});
|
|
}
|