Skip to content

Commit db7ccc4

Browse files
committed
[red] reproduce native cleanup and same-device acquisition gaps
Signed-off-by: Viwat Vchirawongkwin <viwat.v@chula.ac.th>
1 parent 0235b92 commit db7ccc4

3 files changed

Lines changed: 221 additions & 1 deletion

File tree

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// SPDX-License-Identifier: MIT
2+
// Part of PyBLE (https://pyble.dev) — see /LICENSE.
3+
4+
import 'dart:async';
5+
import 'dart:typed_data';
6+
7+
import 'package:flutter_test/flutter_test.dart';
8+
import 'package:pyble/pble/engine.dart';
9+
import 'package:pyble/pble/frame.dart';
10+
import 'package:pyble/pble/pble_constants.dart';
11+
import 'package:pyble/pble/pble_exception.dart';
12+
13+
import '../support/fake_byte_transport.dart';
14+
15+
class _CancelFailureTransport extends FakeByteTransport {
16+
_CancelFailureTransport(Object failure)
17+
: input = StreamController<Uint8List>(onCancel: () async => throw failure);
18+
final StreamController<Uint8List> input;
19+
@override
20+
Stream<Uint8List> get inbound => input.stream;
21+
}
22+
23+
void main() {
24+
test(
25+
'inbound cancellation failure still fails pending work and closes events',
26+
() async {
27+
final StateError failure = StateError('inbound cancellation failed');
28+
final _CancelFailureTransport transport = _CancelFailureTransport(
29+
failure,
30+
);
31+
final PbleEngine engine = PbleEngine(transport);
32+
bool eventsDone = false;
33+
Object? requestFailure;
34+
final StreamSubscription<PbleFrame> events = engine.events.listen(
35+
(_) {},
36+
onDone: () => eventsDone = true,
37+
);
38+
addTearDown(() async {
39+
await events.cancel();
40+
await transport.input.close();
41+
await transport.dispose();
42+
});
43+
unawaited(
44+
engine
45+
.request(
46+
PbleFrame(
47+
type: Pble.typeCmd,
48+
opcode: PbleOpcode.deviceInfo.code,
49+
id: engine.nextId(),
50+
payload: Uint8List(0),
51+
),
52+
timeout: const Duration(milliseconds: 200),
53+
)
54+
.then<void>(
55+
(_) {},
56+
onError: (Object error) {
57+
requestFailure = error;
58+
},
59+
),
60+
);
61+
await pumpEventQueue();
62+
await expectLater(engine.dispose(), throwsA(same(failure)));
63+
await pumpEventQueue();
64+
expect(requestFailure, isA<PbleTimeoutException>());
65+
expect(eventsDone, isTrue);
66+
},
67+
);
68+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// SPDX-License-Identifier: MIT
2+
// Part of PyBLE (https://pyble.dev) — see /LICENSE.
3+
4+
import 'dart:async';
5+
6+
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
7+
import 'package:flutter_test/flutter_test.dart';
8+
import 'package:pyble/ble/ble_link.dart';
9+
import 'package:pyble/ble/fbp_ble_link.dart';
10+
import 'package:pyble/pble/pble_constants.dart';
11+
12+
class _Device extends BluetoothDevice {
13+
_Device(this.failure, {Object? cancelFailure})
14+
: changes = StreamController<BluetoothConnectionState>(
15+
onCancel: () async {
16+
if (cancelFailure != null) throw cancelFailure;
17+
},
18+
),
19+
super(
20+
remoteId: const DeviceIdentifier(
21+
'00000000-0000-0000-0000-000000000001',
22+
),
23+
);
24+
final Object failure;
25+
final StreamController<BluetoothConnectionState> changes;
26+
int disconnectCalls = 0;
27+
@override
28+
Stream<BluetoothConnectionState> get connectionState => changes.stream;
29+
@override
30+
Future<void> disconnect({
31+
int timeout = 35,
32+
bool queue = true,
33+
int androidDelay = 2000,
34+
}) async {
35+
disconnectCalls++;
36+
throw failure;
37+
}
38+
}
39+
40+
void main() {
41+
for (final bool failCancel in <bool>[false, true]) {
42+
test(
43+
'native disconnect failure releases subscription (cancel failure $failCancel)',
44+
() async {
45+
final StateError failure = StateError('physical disconnect failed');
46+
final _Device device = _Device(
47+
failure,
48+
cancelFailure: failCancel
49+
? StateError('subscription cancel failed')
50+
: null,
51+
);
52+
BluetoothCharacteristic characteristic(String uuid) =>
53+
BluetoothCharacteristic(
54+
remoteId: device.remoteId,
55+
serviceUuid: Guid(Pble.serviceUuid),
56+
characteristicUuid: Guid(uuid),
57+
);
58+
final FbpBleLink link = FbpBleLink(
59+
device,
60+
characteristic(Pble.rxUuid),
61+
characteristic(Pble.txUuid),
62+
);
63+
expect(device.changes.hasListener, isTrue);
64+
await expectLater(link.disconnect(), throwsA(same(failure)));
65+
expect(device.disconnectCalls, 1);
66+
expect(device.changes.hasListener, isFalse);
67+
expect(link.linkState.value, BleLinkState.connected);
68+
await device.changes.close();
69+
},
70+
);
71+
}
72+
}

‎app/test/unit/owned_connection_lifecycle_test.dart‎

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)