Skip to content

Commit 7dd9a4e

Browse files
committed
Trying to close pending Completers. #18
1 parent f631261 commit 7dd9a4e

File tree

5 files changed

+26
-12
lines changed

5 files changed

+26
-12
lines changed

lib/src/internal/event_action.dart

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ class EventAction extends TimeAction implements SimContext {
6464
event(this).then((_) {
6565
print('then $eventName ${sim.runState.name}');
6666
if (_eventCompleter != null && sim.runState==RunState.running) {
67+
SimDartHelper.removeCompleter(sim: sim, completer: _eventCompleter!.completer);
6768
//TODO method or throw?
6869
print('aqui 2?');
6970
SimDartHelper.error(
@@ -92,6 +93,7 @@ class EventAction extends TimeAction implements SimContext {
9293
@override
9394
Future<void> wait(int delay) async {
9495
if (_eventCompleter != null) {
96+
SimDartHelper.removeCompleter(sim: sim, completer: _eventCompleter!.completer);
9597
//TODO method or throw?
9698
print('aqui 1?');
9799
SimDartHelper.error(
@@ -149,6 +151,7 @@ class EventAction extends TimeAction implements SimContext {
149151

150152
@override
151153
SimCounter counter(String name) {
154+
print("counter");
152155
return sim.counter(name);
153156
}
154157

@@ -160,23 +163,25 @@ class EventAction extends TimeAction implements SimContext {
160163

161164
class EventCompleter {
162165
EventCompleter({required this.event}){
163-
SimDartHelper.addCompleter(sim: event.sim, completer: _completer);
166+
SimDartHelper.addCompleter(sim: event.sim, completer: completer);
164167
}
165168

166-
final Completer<void> _completer = Completer();
169+
final Completer<void> completer = Completer();
167170

168171
final EventAction event;
169172

170-
Future<void> get future => _completer.future;
173+
Future<void> get future {
174+
return completer.future;
175+
}
171176

172177
void complete() {
173178
event.sim.listener?.onEvent(
174179
name: event.eventName,
175180
time: event.sim.now,
176181
phase: EventPhase.resumed,
177182
executionHash: hashCode);
178-
_completer.complete();
183+
completer.complete();
179184
event._eventCompleter = null;
180-
SimDartHelper.removeCompleter(sim: event.sim, completer: _completer);
185+
SimDartHelper.removeCompleter(sim: event.sim, completer: completer);
181186
}
182187
}

lib/src/resources.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ class ResourcesContext extends Resources {
154154
/// - Returns: A [Future] that completes when the resource is acquired.
155155
Future<void> acquire(String name) async {
156156
if (_event.eventCompleter != null) {
157+
SimDartHelper.removeCompleter(sim: _sim, completer: _event.eventCompleter!.completer);
157158
//TODO method or throw?
158159
SimDartHelper.error(
159160
sim: _sim,

lib/src/simdart.dart

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ class SimDart implements SimDartInterface {
110110
/// - [until]: The time at which execution should stop. Execution will include events
111111
/// scheduled at this time (inclusive). If null, execution will continue indefinitely.
112112
Future<SimResult> run({int? until}) async {
113+
113114
if (runState != RunState.notStarted) {
114115
throw StateError('Simulation has already started.');
115116
}
@@ -131,13 +132,8 @@ class SimDart implements SimDartInterface {
131132
_scheduleNextAction();
132133
await _terminator.future;
133134

134-
try {
135-
_disposeCompleteList();
136-
}catch(e){
137-
print(e);
138-
}
139-
140-
print('foi: ${_error.runtimeType}: $_error');
135+
print('terminou');
136+
print('foi com erro? -> ${_error.runtimeType}: $_error');
141137
if (_error != null) {
142138
print('ue? erro?');
143139
_runState = RunState.error;
@@ -266,6 +262,7 @@ class SimDart implements SimDartInterface {
266262
_debugListener?.onNextAction();
267263
_nextActionScheduled = false;
268264
if (_actions.isEmpty || runState!=RunState.running) {
265+
_disposeCompleteList();
269266
if(!_terminator.isCompleted) {
270267
_terminator.complete();
271268
}

test/resource_test.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ void main() {
129129
'[20][C][finished]'
130130
]);
131131
});
132+
133+
134+
132135
test('without await', () async {
133136
expect(
134137
() async {

test/stop_test.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
import 'dart:async';
2+
13
import 'package:simdart/src/simdart.dart';
24
import 'package:test/test.dart';
35

46
import 'test_helper.dart';
57

8+
9+
610
void main() {
711
late SimDart sim;
812
TestHelper helper = TestHelper();
@@ -12,6 +16,8 @@ void main() {
1216
SimDartHelper.setDebugListener(sim: sim, listener: helper);
1317
});
1418

19+
20+
1521
group('Stop', () {
1622
test('Simple', () async {
1723
sim.process(
@@ -76,8 +82,10 @@ void main() {
7682
'scheduleNextAction',
7783
'nextAction'
7884
]);
85+
print("counter valor:${sim.counter('counter').value} ");
7986
expect(sim.counter('counter').value, 1);
8087
expect(helper.completerCount, 0);
88+
print('---------------');
8189
});
8290

8391
test('Resource', () async {

0 commit comments

Comments
 (0)