@@ -107,12 +107,15 @@ class _OwnedLink implements BleLink {
107107 }
108108}
109109
110- Future <({PbleConnection connection, _OwnedLink link})> _ready () async {
110+ Future <({PbleConnection connection, _OwnedLink link})> _ready ({
111+ Duration dataTimeout = const Duration (seconds: 8 ),
112+ }) async {
111113 final _OwnedLink link = _OwnedLink ();
112114 final PbleConnection connection = PbleConnection .fromLink (
113115 link: link,
114116 appName: 'PyBLE' ,
115117 appVersion: '0.2.0' ,
118+ dataTimeout: dataTimeout,
116119 );
117120 link.state.value = BleLinkState .connected;
118121 await pumpEventQueue ();
@@ -271,6 +274,27 @@ void main() {
271274 },
272275 );
273276
277+ test ('queued GET begin cannot create a transfer after disposal' , () async {
278+ final r = await _ready (dataTimeout: const Duration (milliseconds: 20 ));
279+ final Future <Uint8List > download = r.connection.getFile ('owned.bin' );
280+ final Future <void > rejected = expectLater (
281+ download,
282+ throwsA (isA <NotConnectedException >()),
283+ );
284+ await pumpEventQueue ();
285+ final PbleFrame begin = r.link.frames.last;
286+ r.link.deliver (
287+ PbleFrame (
288+ type: Pble .typeRsp,
289+ opcode: begin.opcode,
290+ id: begin.id,
291+ payload: Uint8List .fromList (< int > [0 , 1 , 0 , 0 , 0 ]),
292+ ),
293+ );
294+ await r.connection.dispose ();
295+ await rejected;
296+ });
297+
274298 test (
275299 'terminal owned HELLO refusal closes physical link automatically' ,
276300 () async {
@@ -293,6 +317,62 @@ void main() {
293317 });
294318
295319 group ('FR-CONNECT-7 manager ownership and late factories' , () {
320+ test ('disposal stops the owned scanner once' , () async {
321+ final FakeScanner scanner = FakeScanner ();
322+ final manager = PbleConnectionManager (
323+ scanner: scanner,
324+ readiness: FakeSeamReadiness (BleReadiness .ready),
325+ connectionFactory: (_) => throw StateError ('not requested' ),
326+ );
327+ addTearDown (scanner.dispose);
328+ await manager.startScan ();
329+ expect (scanner.scanning, isTrue);
330+ await manager.dispose ();
331+ await manager.dispose ();
332+ expect (scanner.scanning, isFalse);
333+ expect (scanner.stopScanCount, 1 );
334+ });
335+
336+ test (
337+ 'same physical ID waits for pending acquisition and stale close' ,
338+ () async {
339+ final a = await _ready ();
340+ final Completer <Connection > firstFactory = Completer <Connection >();
341+ int factoryCalls = 0 ;
342+ final manager = _manager ((_) async {
343+ factoryCalls++ ;
344+ if (factoryCalls == 1 ) return firstFactory.future;
345+ // Wrappers share one physical radio, as the native plugin does by ID.
346+ a.link.state.value = BleLinkState .connected;
347+ return PbleConnection .fromLink (
348+ link: a.link,
349+ appName: 'PyBLE' ,
350+ appVersion: '0.2.0' ,
351+ );
352+ });
353+ final Future <void > first = manager.connect ('same-board' );
354+ await pumpEventQueue ();
355+ final Future <void > second = manager.connect ('same-board' );
356+ await pumpEventQueue ();
357+ final int beforeFirstResult = factoryCalls;
358+ a.link.closeGate = Completer <void >();
359+ firstFactory.complete (a.connection);
360+ await pumpEventQueue ();
361+ final int beforeStaleClose = factoryCalls;
362+ a.link.closeGate! .complete ();
363+ await Future .wait (< Future <void >> [first, second]);
364+ await pumpEventQueue ();
365+ a.link.answerHello ();
366+ await pumpEventQueue ();
367+ expect (beforeFirstResult, 1 );
368+ expect (beforeStaleClose, 1 );
369+ expect (factoryCalls, 2 );
370+ expect (a.link.disconnectCalls, 1 );
371+ expect (a.link.state.value, BleLinkState .connected);
372+ expect (manager.phase.value, ConnectPhase .connected);
373+ },
374+ );
375+
296376 test ('ordinary manager Disconnect closes the real ready link' , () async {
297377 final r = await _ready ();
298378 final manager = _manager ((_) async => r.connection);
0 commit comments