@@ -17,6 +17,7 @@ import 'package:pyble/pble/fragment.dart';
1717import 'package:pyble/pble/frame.dart' ;
1818import 'package:pyble/pble/pble_connection.dart' ;
1919import 'package:pyble/pble/pble_constants.dart' ;
20+ import 'package:pyble/pble/pble_exception.dart' ;
2021import 'package:pyble/pble/scanner.dart' ;
2122
2223import '../support/fake_readiness_seam.dart' ;
@@ -239,6 +240,37 @@ void main() {
239240 await link.releaseFixture ();
240241 });
241242
243+ test (
244+ 'disposal aborts an active download without another command' ,
245+ () async {
246+ final r = await _ready ();
247+ final Future <Uint8List > download = r.connection.getFile ('owned.bin' );
248+ final Future <void > rejected = expectLater (
249+ download,
250+ throwsA (isA <NotConnectedException >()),
251+ );
252+ await pumpEventQueue ();
253+ final PbleFrame begin = r.link.frames.last;
254+ r.link.deliver (
255+ PbleFrame (
256+ type: Pble .typeRsp,
257+ opcode: begin.opcode,
258+ id: begin.id,
259+ payload: Uint8List .fromList (< int > [0 , 1 , 0 , 0 , 0 ]),
260+ ),
261+ );
262+ await pumpEventQueue ();
263+ final int count = r.link.writes.length;
264+ await r.connection.dispose ();
265+ await rejected;
266+ await expectLater (
267+ r.connection.runFile ('owned.bin' ),
268+ throwsA (isA <NotConnectedException >()),
269+ );
270+ expect (r.link.writes, hasLength (count));
271+ },
272+ );
273+
242274 test (
243275 'terminal owned HELLO refusal closes physical link automatically' ,
244276 () async {
@@ -381,5 +413,50 @@ void main() {
381413 expect (manager.lastError, same (failure));
382414 expect (r.link.state.listeners, isEmpty);
383415 });
416+
417+ test ('failed old physical close blocks replacement factory' , () async {
418+ final a = await _ready ();
419+ final b = await _ready ();
420+ final StateError failure = StateError ('old board still connected' );
421+ bool openedB = false ;
422+ final manager = _manager ((String id) async {
423+ if (id == 'a' ) return a.connection;
424+ openedB = true ;
425+ return b.connection;
426+ });
427+ await manager.connect ('a' );
428+ a.link.closeFailure = failure;
429+ await expectLater (manager.connect ('b' ), throwsA (same (failure)));
430+ expect (openedB, isFalse);
431+ expect (manager.phase.value, ConnectPhase .failed);
432+ expect (manager.lastError, same (failure));
433+ expect (a.link.state.value, BleLinkState .connected);
434+ // An already-failed retirement is not proof of a physically closed link.
435+ await expectLater (manager.connect ('b' ), throwsA (same (failure)));
436+ expect (openedB, isFalse);
437+ expect (a.link.disconnectCalls, 1 );
438+ });
439+
440+ test (
441+ 'manager disposal shares failure and still closes facade streams' ,
442+ () async {
443+ final r = await _ready ();
444+ final StateError failure = StateError ('close failure' );
445+ final manager = _manager ((_) async => r.connection);
446+ await manager.connect ('a' );
447+ r.link.closeFailure = failure;
448+ final Future <void > streams = Future .wait (< Future <void >> [
449+ manager.connection.console.drain <void >(),
450+ manager.connection.runState.drain <void >(),
451+ manager.scanResults.drain <void >(),
452+ ]);
453+ final Future <void > first = manager.dispose ();
454+ expect (identical (first, manager.dispose ()), isTrue);
455+ await expectLater (first, throwsA (same (failure)));
456+ await streams;
457+ await expectLater (manager.dispose (), throwsA (same (failure)));
458+ expect (r.link.disconnectCalls, 1 );
459+ },
460+ );
384461 });
385462}
0 commit comments