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

Fix bugs in Simulator error handling and end of simulation #515

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Fix bugs in Simulator error handling and end of simulation
  • Loading branch information
mkorbel1 committed Sep 27, 2024
commit 94c87fcc4a12e31cfa2828e826081768ab9cd3d2
23 changes: 21 additions & 2 deletions lib/src/simulator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -385,11 +385,22 @@ abstract class Simulator {
_simExceptions.isEmpty &&
!_simulationEndRequested &&
(_maxSimTime < 0 || _currentTimestamp < _maxSimTime)) {
await tick();
try {
await tick();
} catch (__, _) {
// trigger the end of simulation if an error occurred
_simulationEndedCompleter.complete();

rethrow;
}
}

for (final err in _simExceptions) {
logger.severe(err.exception.toString(), err.exception, err.stackTrace);

// trigger the end of simulation if an error occurred
_simulationEndedCompleter.complete();

throw err.exception;
}

Expand All @@ -399,7 +410,15 @@ abstract class Simulator {

while (_endOfSimulationActions.isNotEmpty) {
final endOfSimAction = _endOfSimulationActions.removeFirst();
await endOfSimAction();

try {
await endOfSimAction();
} catch (_) {
// trigger the end of simulation if an error occurred
_simulationEndedCompleter.complete();

rethrow;
}
}

_simulationEndedCompleter.complete();
Expand Down
110 changes: 93 additions & 17 deletions test/simulator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,25 +81,101 @@ void main() {
await Simulator.run();
});

test('simulator end of action waits before ending', () async {
var endOfSimActionExecuted = false;
Simulator.registerAction(100, () => true);
Simulator.registerEndOfSimulationAction(
() => endOfSimActionExecuted = true);
unawaited(Simulator.simulationEnded
.then((value) => expect(endOfSimActionExecuted, isTrue)));
await Simulator.run();
});
group('simulator end of', () {
test('action waits before ending', () async {
var endOfSimActionExecuted = false;
Simulator.registerAction(100, () => true);
Simulator.registerEndOfSimulationAction(
() => endOfSimActionExecuted = true);
unawaited(Simulator.simulationEnded
.then((value) => expect(endOfSimActionExecuted, isTrue)));
await Simulator.run();
});

test('simulator end of action waits async before ending', () async {
var endOfSimActionExecuted = false;
Simulator.registerAction(100, () => true);
Simulator.registerEndOfSimulationAction(() async {
await Future<void>.delayed(const Duration(microseconds: 10));
endOfSimActionExecuted = true;
test('action waits async before ending', () async {
var endOfSimActionExecuted = false;
Simulator.registerAction(100, () => true);
Simulator.registerEndOfSimulationAction(() async {
await Future<void>.delayed(const Duration(microseconds: 10));
endOfSimActionExecuted = true;
});
await Simulator.run();
expect(endOfSimActionExecuted, isTrue);
});
await Simulator.run();
expect(endOfSimActionExecuted, isTrue);

test('action throws', () async {
var endOfSimActionExecuted = false;
var errorThrown = false;

Simulator.registerAction(100, () => true);
Simulator.registerEndOfSimulationAction(() {
endOfSimActionExecuted = true;
throw Exception('End of sim action failed');
});

// the Future will hang if we don't properly trigger the completion
unawaited(Simulator.run().onError((_, __) {
errorThrown = true;
}));
await Simulator.simulationEnded;

expect(endOfSimActionExecuted, isTrue);
expect(errorThrown, isTrue);
});

test('action async throws', () async {
var endOfSimActionExecuted = false;
var errorThrown = false;

Simulator.registerAction(100, () => true);
Simulator.registerEndOfSimulationAction(() async {
await Future<void>.delayed(const Duration(microseconds: 10));
endOfSimActionExecuted = true;
throw Exception('End of sim action failed');
});

// the Future will hang if we don't properly trigger the completion
unawaited(Simulator.run().onError((_, __) {
errorThrown = true;
}));
await Simulator.simulationEnded;

expect(endOfSimActionExecuted, isTrue);
expect(errorThrown, isTrue);
});
});

test('registered action exception in simulator ends simulation', () async {
var errorThrown = false;

Simulator.registerAction(100, () => throw Exception('failed action'));
Simulator.registerAction(200, () => true);

unawaited(Simulator.run().onError((_, __) {
errorThrown = true;
}));

await Simulator.simulationEnded;

expect(errorThrown, isTrue);
});

test('simulator thrown exception ends simulation', () async {
var errorThrown = false;

Simulator.registerAction(
100,
() => Simulator.throwException(
Exception('simulator thrown exception'), StackTrace.current));
Simulator.registerAction(200, () => true);

unawaited(Simulator.run().onError((_, __) {
errorThrown = true;
}));

await Simulator.simulationEnded;

expect(errorThrown, isTrue);
});

test('simulator end simulation waits for simulation to end', () async {
Expand Down
Loading