|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +// Part of PyBLE (https://pyble.dev) — see /LICENSE. |
| 3 | + |
| 4 | +import 'dart:async'; |
| 5 | +import 'dart:convert'; |
| 6 | +import 'dart:typed_data'; |
| 7 | + |
| 8 | +import 'package:flutter/foundation.dart'; |
| 9 | +import 'package:flutter_test/flutter_test.dart'; |
| 10 | +import 'package:pyble/ble/ble.dart'; |
| 11 | +import 'package:pyble/pble/ble_byte_transport.dart'; |
| 12 | +import 'package:pyble/pble/connection.dart'; |
| 13 | +import 'package:pyble/pble/connection_manager.dart'; |
| 14 | +import 'package:pyble/pble/conn_state.dart'; |
| 15 | +import 'package:pyble/pble/engine.dart'; |
| 16 | +import 'package:pyble/pble/fragment.dart'; |
| 17 | +import 'package:pyble/pble/frame.dart'; |
| 18 | +import 'package:pyble/pble/pble_connection.dart'; |
| 19 | +import 'package:pyble/pble/pble_constants.dart'; |
| 20 | +import 'package:pyble/pble/scanner.dart'; |
| 21 | + |
| 22 | +import '../support/fake_readiness_seam.dart'; |
| 23 | + |
| 24 | +class _TrackedState extends ValueNotifier<BleLinkState> { |
| 25 | + _TrackedState() : super(BleLinkState.disconnected); |
| 26 | + final Set<VoidCallback> listeners = <VoidCallback>{}; |
| 27 | + @override |
| 28 | + void addListener(VoidCallback listener) { |
| 29 | + listeners.add(listener); |
| 30 | + super.addListener(listener); |
| 31 | + } |
| 32 | + |
| 33 | + @override |
| 34 | + void removeListener(VoidCallback listener) { |
| 35 | + listeners.remove(listener); |
| 36 | + super.removeListener(listener); |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +/// Actual encoded HELLO/engine traffic, with only the physical byte pipe faked. |
| 41 | +class _OwnedLink implements BleLink { |
| 42 | + final _TrackedState state = _TrackedState(); |
| 43 | + final StreamController<List<int>> incoming = |
| 44 | + StreamController<List<int>>.broadcast(); |
| 45 | + final List<List<int>> writes = <List<int>>[]; |
| 46 | + int disconnectCalls = 0; |
| 47 | + Completer<void>? closeGate; |
| 48 | + Object? closeFailure; |
| 49 | + @override |
| 50 | + Stream<List<int>> get inbound => incoming.stream; |
| 51 | + @override |
| 52 | + ValueListenable<BleLinkState> get linkState => state; |
| 53 | + @override |
| 54 | + int get mtu => 247; |
| 55 | + @override |
| 56 | + Future<void> write(List<int> bytes, {required bool withoutResponse}) async { |
| 57 | + writes.add(List<int>.of(bytes)); |
| 58 | + } |
| 59 | + |
| 60 | + @override |
| 61 | + Future<void> disconnect() async { |
| 62 | + disconnectCalls++; |
| 63 | + await closeGate?.future; |
| 64 | + if (closeFailure case final Object failure) throw failure; |
| 65 | + state.value = BleLinkState.disconnected; |
| 66 | + } |
| 67 | + |
| 68 | + Future<void> releaseFixture() async { |
| 69 | + await incoming.close(); |
| 70 | + state.dispose(); |
| 71 | + } |
| 72 | + |
| 73 | + List<PbleFrame> get frames { |
| 74 | + final PbleReassembler parser = PbleReassembler(); |
| 75 | + return <PbleFrame>[ |
| 76 | + for (final List<int> packet in writes) |
| 77 | + if (parser.offer(Uint8List.fromList(packet)) case final Uint8List raw) |
| 78 | + decodeFrame(raw), |
| 79 | + ]; |
| 80 | + } |
| 81 | + |
| 82 | + void deliver(PbleFrame frame) { |
| 83 | + for (final Uint8List packet in PbleFragmenter( |
| 84 | + mtu: mtu, |
| 85 | + ).fragment(encodeFrame(frame))) { |
| 86 | + incoming.add(packet); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + void answerHello({int proto = 1}) { |
| 91 | + final PbleFrame hello = frames.lastWhere( |
| 92 | + (PbleFrame f) => f.opcode == PbleOpcode.hello.code, |
| 93 | + ); |
| 94 | + deliver( |
| 95 | + PbleFrame( |
| 96 | + type: Pble.typeRsp, |
| 97 | + opcode: hello.opcode, |
| 98 | + id: hello.id, |
| 99 | + payload: Uint8List.fromList(<int>[ |
| 100 | + 0, |
| 101 | + ...utf8.encode( |
| 102 | + 'proto=$proto\nchip=esp32-c3\nmpy=1.28.0\nfs_root=/\nmtu=247', |
| 103 | + ), |
| 104 | + ]), |
| 105 | + ), |
| 106 | + ); |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +Future<({PbleConnection connection, _OwnedLink link})> _ready() async { |
| 111 | + final _OwnedLink link = _OwnedLink(); |
| 112 | + final PbleConnection connection = PbleConnection.fromLink( |
| 113 | + link: link, |
| 114 | + appName: 'PyBLE', |
| 115 | + appVersion: '0.2.0', |
| 116 | + ); |
| 117 | + link.state.value = BleLinkState.connected; |
| 118 | + await pumpEventQueue(); |
| 119 | + link.answerHello(); |
| 120 | + await pumpEventQueue(); |
| 121 | + expect(connection.state.value, ConnState.ready); |
| 122 | + addTearDown(() async { |
| 123 | + if (link.closeGate case final Completer<void> gate when !gate.isCompleted) |
| 124 | + gate.complete(); |
| 125 | + try { |
| 126 | + await connection.dispose(); |
| 127 | + } catch (_) {} |
| 128 | + await link.releaseFixture(); |
| 129 | + }); |
| 130 | + return (connection: connection, link: link); |
| 131 | +} |
| 132 | + |
| 133 | +PbleConnectionManager _manager(ConnectionFactory factory) { |
| 134 | + final PbleConnectionManager manager = PbleConnectionManager( |
| 135 | + scanner: FakeScanner(), |
| 136 | + readiness: FakeSeamReadiness(BleReadiness.ready), |
| 137 | + connectionFactory: factory, |
| 138 | + ); |
| 139 | + addTearDown(() async { |
| 140 | + try { |
| 141 | + await manager.dispose(); |
| 142 | + } catch (_) {} |
| 143 | + }); |
| 144 | + return manager; |
| 145 | +} |
| 146 | + |
| 147 | +void main() { |
| 148 | + group('FR-CONNECT-7 owned physical link teardown', () { |
| 149 | + test( |
| 150 | + 'HELLO-ready dispose closes exact link and removes both listeners', |
| 151 | + () async { |
| 152 | + final r = await _ready(); |
| 153 | + expect(r.link.state.listeners, hasLength(2)); |
| 154 | + await r.connection.dispose(); |
| 155 | + expect(r.link.disconnectCalls, 1); |
| 156 | + expect(r.link.state.value, BleLinkState.disconnected); |
| 157 | + expect(r.link.state.listeners, isEmpty); |
| 158 | + }, |
| 159 | + ); |
| 160 | + |
| 161 | + test( |
| 162 | + 'concurrent and repeated disposal shares physical close completion', |
| 163 | + () async { |
| 164 | + final r = await _ready(); |
| 165 | + r.link.closeGate = Completer<void>(); |
| 166 | + final Future<void> first = r.connection.dispose(); |
| 167 | + final Future<void> second = r.connection.dispose(); |
| 168 | + bool done = false; |
| 169 | + unawaited( |
| 170 | + second.then((_) { |
| 171 | + done = true; |
| 172 | + }), |
| 173 | + ); |
| 174 | + await pumpEventQueue(); |
| 175 | + expect(identical(first, second), isTrue); |
| 176 | + expect(done, isFalse); |
| 177 | + expect(r.link.disconnectCalls, 1); |
| 178 | + r.link.closeGate!.complete(); |
| 179 | + await first; |
| 180 | + await r.connection.dispose(); |
| 181 | + expect(r.link.disconnectCalls, 1); |
| 182 | + }, |
| 183 | + ); |
| 184 | + |
| 185 | + test( |
| 186 | + 'close failure still releases streams/listeners and remains a failure', |
| 187 | + () async { |
| 188 | + final r = await _ready(); |
| 189 | + final StateError failure = StateError('physical close rejected'); |
| 190 | + r.link.closeFailure = failure; |
| 191 | + final Future<void> consoleDone = r.connection.console.drain<void>(); |
| 192 | + final Future<void> runDone = r.connection.runState.drain<void>(); |
| 193 | + await expectLater(r.connection.dispose(), throwsA(same(failure))); |
| 194 | + await Future.wait(<Future<void>>[consoleDone, runDone]); |
| 195 | + expect(r.link.state.listeners, isEmpty); |
| 196 | + expect(r.link.state.value, BleLinkState.connected); |
| 197 | + await expectLater(r.connection.dispose(), throwsA(same(failure))); |
| 198 | + expect(r.link.disconnectCalls, 1); |
| 199 | + }, |
| 200 | + ); |
| 201 | + |
| 202 | + test( |
| 203 | + 'direct engine constructor does not own external byte transport', |
| 204 | + () async { |
| 205 | + final _OwnedLink link = _OwnedLink(); |
| 206 | + final BleByteTransport transport = BleByteTransport(link); |
| 207 | + final PbleConnection connection = PbleConnection( |
| 208 | + engine: PbleEngine(transport), |
| 209 | + appName: 'PyBLE', |
| 210 | + appVersion: '0.2.0', |
| 211 | + ); |
| 212 | + await connection.dispose(); |
| 213 | + expect(link.disconnectCalls, 0); |
| 214 | + expect(link.state.listeners, hasLength(1)); |
| 215 | + transport.dispose(); |
| 216 | + await link.releaseFixture(); |
| 217 | + }, |
| 218 | + ); |
| 219 | + |
| 220 | + test('late HELLO after disposal never republishes ready', () async { |
| 221 | + final _OwnedLink link = _OwnedLink(); |
| 222 | + final PbleConnection connection = PbleConnection.fromLink( |
| 223 | + link: link, |
| 224 | + appName: 'PyBLE', |
| 225 | + appVersion: '0.2.0', |
| 226 | + ); |
| 227 | + link.state.value = BleLinkState.connected; |
| 228 | + await pumpEventQueue(); |
| 229 | + final List<ConnState> states = <ConnState>[]; |
| 230 | + connection.state.addListener(() { |
| 231 | + states.add(connection.state.value); |
| 232 | + }); |
| 233 | + await connection.dispose(); |
| 234 | + link.answerHello(); |
| 235 | + await pumpEventQueue(); |
| 236 | + expect(states, isNot(contains(ConnState.ready))); |
| 237 | + expect(link.disconnectCalls, 1); |
| 238 | + expect(link.state.listeners, isEmpty); |
| 239 | + await link.releaseFixture(); |
| 240 | + }); |
| 241 | + |
| 242 | + test( |
| 243 | + 'terminal owned HELLO refusal closes physical link automatically', |
| 244 | + () async { |
| 245 | + final _OwnedLink link = _OwnedLink(); |
| 246 | + final PbleConnection connection = PbleConnection.fromLink( |
| 247 | + link: link, |
| 248 | + appName: 'PyBLE', |
| 249 | + appVersion: '0.2.0', |
| 250 | + ); |
| 251 | + link.state.value = BleLinkState.connected; |
| 252 | + await pumpEventQueue(); |
| 253 | + link.answerHello(proto: 2); |
| 254 | + await pumpEventQueue(); |
| 255 | + expect(link.disconnectCalls, 1); |
| 256 | + expect(link.state.value, BleLinkState.disconnected); |
| 257 | + await connection.dispose(); |
| 258 | + await link.releaseFixture(); |
| 259 | + }, |
| 260 | + ); |
| 261 | + }); |
| 262 | + |
| 263 | + group('FR-CONNECT-7 manager ownership and late factories', () { |
| 264 | + test('ordinary manager Disconnect closes the real ready link', () async { |
| 265 | + final r = await _ready(); |
| 266 | + final manager = _manager((_) async => r.connection); |
| 267 | + await manager.connect('board-a'); |
| 268 | + await manager.disconnect(); |
| 269 | + expect(r.link.disconnectCalls, 1); |
| 270 | + expect(r.link.state.value, BleLinkState.disconnected); |
| 271 | + expect(manager.phase.value, ConnectPhase.idle); |
| 272 | + }); |
| 273 | + |
| 274 | + test( |
| 275 | + 'replacement waits for old physical close before opening new board', |
| 276 | + () async { |
| 277 | + final a = await _ready(); |
| 278 | + final b = await _ready(); |
| 279 | + bool openedB = false; |
| 280 | + final manager = _manager((String id) async { |
| 281 | + if (id == 'a') return a.connection; |
| 282 | + openedB = true; |
| 283 | + return b.connection; |
| 284 | + }); |
| 285 | + await manager.connect('a'); |
| 286 | + a.link.closeGate = Completer<void>(); |
| 287 | + final Future<void> replacement = manager.connect('b'); |
| 288 | + await pumpEventQueue(); |
| 289 | + expect(openedB, isFalse); |
| 290 | + expect(a.link.disconnectCalls, 1); |
| 291 | + a.link.closeGate!.complete(); |
| 292 | + await replacement; |
| 293 | + expect(openedB, isTrue); |
| 294 | + expect(b.link.disconnectCalls, 0); |
| 295 | + expect(manager.selected?.id, 'b'); |
| 296 | + }, |
| 297 | + ); |
| 298 | + |
| 299 | + test( |
| 300 | + 'late connection after Disconnect is closed and never attached', |
| 301 | + () async { |
| 302 | + final r = await _ready(); |
| 303 | + final Completer<Connection> pending = Completer<Connection>(); |
| 304 | + final manager = _manager((_) => pending.future); |
| 305 | + final Future<void> connect = manager.connect('late'); |
| 306 | + await pumpEventQueue(); |
| 307 | + await manager.disconnect(); |
| 308 | + pending.complete(r.connection); |
| 309 | + await connect; |
| 310 | + expect(r.link.disconnectCalls, 1); |
| 311 | + expect(manager.connection.state.value, ConnState.disconnected); |
| 312 | + expect(manager.selected, isNull); |
| 313 | + }, |
| 314 | + ); |
| 315 | + |
| 316 | + test('newest connection wins and stale result cannot close it', () async { |
| 317 | + final a = await _ready(); |
| 318 | + final b = await _ready(); |
| 319 | + final Completer<Connection> pendingA = Completer<Connection>(); |
| 320 | + final Completer<Connection> pendingB = Completer<Connection>(); |
| 321 | + final manager = _manager( |
| 322 | + (String id) => id == 'a' ? pendingA.future : pendingB.future, |
| 323 | + ); |
| 324 | + final Future<void> first = manager.connect('a'); |
| 325 | + await pumpEventQueue(); |
| 326 | + final Future<void> second = manager.connect('b'); |
| 327 | + await pumpEventQueue(); |
| 328 | + pendingB.complete(b.connection); |
| 329 | + await second; |
| 330 | + pendingA.complete(a.connection); |
| 331 | + await first; |
| 332 | + expect(a.link.disconnectCalls, 1); |
| 333 | + expect(b.link.disconnectCalls, 0); |
| 334 | + expect(manager.selected?.id, 'b'); |
| 335 | + expect(manager.phase.value, ConnectPhase.connected); |
| 336 | + }); |
| 337 | + |
| 338 | + test('old disconnect completion cannot clear a newer session', () async { |
| 339 | + final a = await _ready(); |
| 340 | + final b = await _ready(); |
| 341 | + final manager = _manager( |
| 342 | + (String id) async => id == 'a' ? a.connection : b.connection, |
| 343 | + ); |
| 344 | + await manager.connect('a'); |
| 345 | + a.link.closeGate = Completer<void>(); |
| 346 | + final Future<void> oldDisconnect = manager.disconnect(); |
| 347 | + await pumpEventQueue(); |
| 348 | + final Future<void> nextConnect = manager.connect('b'); |
| 349 | + a.link.closeGate!.complete(); |
| 350 | + await Future.wait(<Future<void>>[oldDisconnect, nextConnect]); |
| 351 | + expect(manager.selected?.id, 'b'); |
| 352 | + expect(manager.phase.value, ConnectPhase.connected); |
| 353 | + expect(a.link.disconnectCalls, 1); |
| 354 | + expect(b.link.disconnectCalls, 0); |
| 355 | + }); |
| 356 | + |
| 357 | + test( |
| 358 | + 'manager disposal closes a factory that completes after disposal', |
| 359 | + () async { |
| 360 | + final r = await _ready(); |
| 361 | + final Completer<Connection> pending = Completer<Connection>(); |
| 362 | + final manager = _manager((_) => pending.future); |
| 363 | + final Future<void> connecting = manager.connect('late'); |
| 364 | + await pumpEventQueue(); |
| 365 | + await manager.dispose(); |
| 366 | + pending.complete(r.connection); |
| 367 | + await expectLater(connecting, completes); |
| 368 | + expect(r.link.disconnectCalls, 1); |
| 369 | + await expectLater(manager.connect('forbidden'), throwsStateError); |
| 370 | + }, |
| 371 | + ); |
| 372 | + |
| 373 | + test('close errors are not reported as successful Disconnect', () async { |
| 374 | + final r = await _ready(); |
| 375 | + final StateError failure = StateError('close failure'); |
| 376 | + final manager = _manager((_) async => r.connection); |
| 377 | + await manager.connect('a'); |
| 378 | + r.link.closeFailure = failure; |
| 379 | + await expectLater(manager.disconnect(), throwsA(same(failure))); |
| 380 | + expect(manager.phase.value, ConnectPhase.failed); |
| 381 | + expect(manager.lastError, same(failure)); |
| 382 | + expect(r.link.state.listeners, isEmpty); |
| 383 | + }); |
| 384 | + }); |
| 385 | +} |
0 commit comments