-
-
Notifications
You must be signed in to change notification settings - Fork 64
Add BleCommandQueue to UniversalBlePeripheral #288
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ff11403
Add BleCommandQueue to UniversalBlePeripheral
fotiDim 87f1fc0
Case-insensitive deviceId lookup in UniversalBlePeripheral on Apple a…
fotiDim b8de7e1
Handle queue full and fallback to all subscribed centrals on Apple an…
fotiDim f2f2f9e
Add failed case to UniversalBlePeripheralError
fotiDim c0d6b28
Update changelog
fotiDim 5c264f5
Address PR review comments on pumpEventQueue and device lookup error …
fotiDim File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| import 'dart:async'; | ||
| import 'dart:typed_data'; | ||
| import 'package:flutter_test/flutter_test.dart'; | ||
| import 'package:universal_ble/universal_ble.dart'; | ||
|
|
||
| class MockPeripheralPlatform extends Fake implements UniversalBlePeripheralPlatform { | ||
| final List<String> callLog = []; | ||
| Completer<void>? notifyCompleter; | ||
|
|
||
| @override | ||
| void dispose() {} | ||
|
|
||
| @override | ||
| Future<void> updateCharacteristicValue({ | ||
| required String characteristicId, | ||
| required Uint8List value, | ||
| String? deviceId, | ||
| }) async { | ||
| callLog.add('update:$deviceId:${value.length}'); | ||
| if (notifyCompleter != null) { | ||
| await notifyCompleter!.future; | ||
| } | ||
| } | ||
|
|
||
| @override | ||
| Future<void> addService(dynamic service, {Duration? timeout}) async { | ||
| callLog.add('addService'); | ||
| } | ||
|
|
||
| @override | ||
| Future<void> startAdvertising({ | ||
| required List<String> services, | ||
| String? localName, | ||
| Duration? timeout, | ||
| dynamic manufacturerData, | ||
| dynamic platformConfig, | ||
| }) async { | ||
| callLog.add('startAdvertising'); | ||
| } | ||
|
|
||
| @override | ||
| Future<void> stopAdvertising() async { | ||
| callLog.add('stopAdvertising'); | ||
| } | ||
| } | ||
|
|
||
| void main() { | ||
| group('UniversalBlePeripheral Command Queue', () { | ||
| late MockPeripheralPlatform mockPlatform; | ||
|
|
||
| setUp(() { | ||
| mockPlatform = MockPeripheralPlatform(); | ||
| UniversalBlePeripheral.setInstance(mockPlatform); | ||
| UniversalBlePeripheral.queueType = QueueType.global; | ||
| UniversalBlePeripheral.timeout = const Duration(seconds: 5); | ||
| }); | ||
|
|
||
| tearDown(() { | ||
| UniversalBlePeripheral.clearQueue(); | ||
| UniversalBlePeripheral.queueType = QueueType.global; | ||
| }); | ||
|
|
||
| test('serializes updateCharacteristicValue in global queue mode', () async { | ||
| UniversalBlePeripheral.queueType = QueueType.global; | ||
| final release = Completer<void>(); | ||
| mockPlatform.notifyCompleter = release; | ||
|
|
||
| final first = UniversalBlePeripheral.updateCharacteristicValue( | ||
| characteristicId: 'fa5a0003', | ||
| value: Uint8List.fromList([1, 2, 3]), | ||
| deviceId: 'device-1', | ||
| ); | ||
| final second = UniversalBlePeripheral.updateCharacteristicValue( | ||
| characteristicId: 'fa5a0003', | ||
| value: Uint8List.fromList([4, 5, 6, 7]), | ||
| deviceId: 'device-2', | ||
| ); | ||
|
|
||
| await pumpEventQueue(); | ||
| // First is executing, second is queued | ||
| expect(mockPlatform.callLog, equals(['update:device-1:3'])); | ||
|
|
||
| // Release first | ||
| release.complete(); | ||
| await first; | ||
| await second; | ||
|
|
||
| expect(mockPlatform.callLog, equals(['update:device-1:3', 'update:device-2:4'])); | ||
| }); | ||
|
|
||
| test('isolates updateCharacteristicValue by deviceId in perDevice mode', () async { | ||
| UniversalBlePeripheral.queueType = QueueType.perDevice; | ||
| final releaseA = Completer<void>(); | ||
| mockPlatform.notifyCompleter = releaseA; | ||
|
|
||
| final firstA = UniversalBlePeripheral.updateCharacteristicValue( | ||
| characteristicId: 'fa5a0003', | ||
| value: Uint8List.fromList([1]), | ||
| deviceId: 'device-a', | ||
| ); | ||
| final secondA = UniversalBlePeripheral.updateCharacteristicValue( | ||
| characteristicId: 'fa5a0003', | ||
| value: Uint8List.fromList([2]), | ||
| deviceId: 'device-a', | ||
| ); | ||
|
|
||
| await pumpEventQueue(); | ||
| expect(mockPlatform.callLog, equals(['update:device-a:1'])); | ||
|
|
||
| // Set completer to null so device-b completes immediately | ||
| mockPlatform.notifyCompleter = null; | ||
| final firstB = UniversalBlePeripheral.updateCharacteristicValue( | ||
| characteristicId: 'fa5a0003', | ||
| value: Uint8List.fromList([3]), | ||
| deviceId: 'device-b', | ||
| ); | ||
|
|
||
| await firstB; | ||
| // device-b executed independently while device-a was blocked | ||
| expect(mockPlatform.callLog, equals(['update:device-a:1', 'update:device-b:1'])); | ||
|
|
||
| releaseA.complete(); | ||
| await firstA; | ||
| await secondA; | ||
|
|
||
| expect(mockPlatform.callLog, equals([ | ||
| 'update:device-a:1', | ||
| 'update:device-b:1', | ||
| 'update:device-a:1', | ||
| ])); | ||
| }); | ||
|
|
||
| test('clearQueue cancels pending peripheral commands', () async { | ||
| UniversalBlePeripheral.queueType = QueueType.global; | ||
| final release = Completer<void>(); | ||
| mockPlatform.notifyCompleter = release; | ||
|
|
||
| final first = UniversalBlePeripheral.updateCharacteristicValue( | ||
| characteristicId: 'fa5a0003', | ||
| value: Uint8List.fromList([1]), | ||
| deviceId: 'device-1', | ||
| ); | ||
| final pending = UniversalBlePeripheral.updateCharacteristicValue( | ||
| characteristicId: 'fa5a0003', | ||
| value: Uint8List.fromList([2]), | ||
| deviceId: 'device-1', | ||
| ); | ||
|
|
||
| await pumpEventQueue(); | ||
| UniversalBlePeripheral.clearQueue(); | ||
|
|
||
| await expectLater(pending, throwsA(isA<Exception>())); | ||
|
|
||
| release.complete(); | ||
| await first; | ||
| }); | ||
| }); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.