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

Simulator end of sim actions and exception handling #558

Merged
merged 1 commit into from
Jan 27, 2025
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
20 changes: 12 additions & 8 deletions lib/src/simulator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ abstract class Simulator {
static bool _simulationEndRequested = false;

/// Tracks for [_SimulatorException] that are thrown during the simulation.
static List<_SimulatorException> _simExceptions = [];
static final List<_SimulatorException> _simExceptions = [];

/// The maximum time the simulation can run.
///
Expand Down Expand Up @@ -148,8 +148,6 @@ abstract class Simulator {
_currentTimestamp = 0;
_simulationEndRequested = false;

_simExceptions = [];

_maxSimTime = -1;
if (!_preTickController.isClosed) {
await _preTickController.close();
Expand All @@ -174,6 +172,9 @@ abstract class Simulator {
_pendingTimestamps.clear();
_phase = SimulatorPhase.outOfTick;
_injectedActions.clear();
_endOfSimulationActions.clear();
_simExceptions.clear();
_pendingList = ListQueue();

// make sure we've already passed the new completer so that listeners can
// get the latest
Expand Down Expand Up @@ -440,13 +441,9 @@ abstract class Simulator {
}
}

// initially, just log the exceptions
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;
}

if (_currentTimestamp >= _maxSimTime && _maxSimTime > 0) {
Expand All @@ -467,6 +464,13 @@ abstract class Simulator {
}

_simulationEndedCompleter.complete();

// now, rethrow any exceptions now that sim is over and end of sim actions
// have all executed
for (final err in _simExceptions) {
throw err.exception;
}

await simulationEnded;
}
}
Expand Down
58 changes: 57 additions & 1 deletion test/simulator_test.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (C) 2021-2024 Intel Corporation
// Copyright (C) 2021-2025 Intel Corporation
// Copyright (C) 2024 Adam Rose
// SPDX-License-Identifier: BSD-3-Clause
//
Expand Down Expand Up @@ -143,6 +143,62 @@ void main() {
expect(endOfSimActionExecuted, isTrue);
expect(errorThrown, isTrue);
});

test('actions still occur when simulation exception is thrown', () async {
var errorThrown = false;
var endOfSimActionExecuted = false;

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

Simulator.registerEndOfSimulationAction(() {
endOfSimActionExecuted = true;
});

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

await Simulator.simulationEnded;

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

test('actions are cleared at Simulator.reset even if exception occurs',
() async {
var endOfSimActionExecuted = false;
var errorThrown = false;

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

Simulator.registerEndOfSimulationAction(() {
endOfSimActionExecuted = true;
});

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

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

endOfSimActionExecuted = false;

await Simulator.reset();

Simulator.registerAction(100, () => true);

await Simulator.run();

expect(endOfSimActionExecuted, isFalse);
});
});

test('registered action exception in simulator ends simulation', () async {
Expand Down