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

Added actions to execute at the end of the simulation #149

Merged
merged 1 commit into from
Aug 19, 2022
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
17 changes: 17 additions & 0 deletions lib/src/simulator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ class Simulator {
/// Functions to be executed as soon as possible by the [Simulator].
static final Queue<Function()> _injectedActions = Queue<Function()>();

/// Functions to be executed at the end of the simulation.
static final Queue<Function()> _endOfSimulationActions = Queue<Function()>();

/// Emits an event before any other actions take place on the tick.
static Stream get preTick => _preTickController.stream;

Expand Down Expand Up @@ -133,6 +136,13 @@ class Simulator {
_pendingTimestamps[timestamp]!.add(action);
}

/// Registers an arbitrary [action] to be executed at the end of the simulation.
///
/// The simulation will not be marked as ended until these actions complete.
static void registerEndOfSimulationAction(Function() action) {
_endOfSimulationActions.add(action);
}

/// Adds an arbitrary [action] to be executed as soon as possible, during the current
/// simulation tick if possible.
///
Expand Down Expand Up @@ -218,9 +228,16 @@ class Simulator {
(_maxSimTime < 0 || _currentTimestamp < _maxSimTime)) {
await tick(); // make this async so that await-ing events works
}

if (_currentTimestamp >= _maxSimTime && _maxSimTime > 0) {
logger.warning('Simulation ended due to maximum simulation time.');
}

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

_simulationEndedCompleter.complete();
await simulationEnded;
}
Expand Down
23 changes: 23 additions & 0 deletions test/simulator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
/// Author: Max Korbel <max.korbel@intel.com>
///

import 'dart:async';

import 'package:rohd/rohd.dart';
import 'package:test/test.dart';

Expand Down Expand Up @@ -56,4 +58,25 @@ void main() {
Simulator.registerAction(100, () => true);
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();
});

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