From 71c9cc10e7d74bd0d408c2c669f5861a3bc9871f Mon Sep 17 00:00:00 2001 From: Kevin Moore Date: Fri, 13 Dec 2024 19:33:07 -0600 Subject: [PATCH] Bump dart_flutter_team lints to latest across packages (#1679) --- .github/workflows/bazel_worker.yaml | 2 + pkgs/bazel_worker/benchmark/benchmark.dart | 19 +- .../e2e_test/bin/async_worker_in_isolate.dart | 5 +- .../e2e_test/lib/async_worker.dart | 5 +- .../lib/forwards_to_isolate_async_worker.dart | 6 +- pkgs/bazel_worker/e2e_test/pubspec.yaml | 2 +- pkgs/bazel_worker/e2e_test/test/e2e_test.dart | 18 +- pkgs/bazel_worker/example/client.dart | 12 +- .../lib/src/async_message_grouper.dart | 17 +- pkgs/bazel_worker/lib/src/driver/driver.dart | 104 ++++++----- .../lib/src/driver/driver_connection.dart | 11 +- pkgs/bazel_worker/lib/src/utils.dart | 5 +- .../lib/src/worker/async_worker_loop.dart | 20 +- .../lib/src/worker/sync_worker_loop.dart | 19 +- .../lib/src/worker/worker_connection.dart | 20 +- pkgs/bazel_worker/lib/testing.dart | 21 ++- pkgs/bazel_worker/test/driver_test.dart | 174 +++++++++++------- .../test/message_grouper_test.dart | 93 ++++------ pkgs/bazel_worker/test/worker_loop_test.dart | 67 ++++--- pkgs/clock/analysis_options.yaml | 1 - pkgs/coverage/analysis_options.yaml | 5 - pkgs/test_reflective_loader/pubspec.yaml | 2 +- 22 files changed, 360 insertions(+), 268 deletions(-) diff --git a/.github/workflows/bazel_worker.yaml b/.github/workflows/bazel_worker.yaml index 0eb06dad7..29c0a0d07 100644 --- a/.github/workflows/bazel_worker.yaml +++ b/.github/workflows/bazel_worker.yaml @@ -36,6 +36,8 @@ jobs: - uses: dart-lang/setup-dart@e630b99d28a3b71860378cafdc2a067c71107f94 with: sdk: ${{ matrix.sdk }} + - run: dart pub get - run: "dart format --output=none --set-exit-if-changed ." + if: ${{ matrix.sdk == dev }} - name: Test run: ./tool/travis.sh diff --git a/pkgs/bazel_worker/benchmark/benchmark.dart b/pkgs/bazel_worker/benchmark/benchmark.dart index 035e2b848..0a0312203 100644 --- a/pkgs/bazel_worker/benchmark/benchmark.dart +++ b/pkgs/bazel_worker/benchmark/benchmark.dart @@ -12,10 +12,7 @@ Future main() async { var path = 'blaze-bin/some/path/to/a/file/that/is/an/input/$i'; workRequest ..arguments.add('--input=$path') - ..inputs.add(Input( - path: '', - digest: List.filled(70, 0x11), - )); + ..inputs.add(Input(path: '', digest: List.filled(70, 0x11))); } // Serialize it. @@ -24,14 +21,20 @@ Future main() async { print('Request has $length requestBytes.'); // Add the length in front base 128 encoded as in the worker protocol. - requestBytes = - Uint8List.fromList(requestBytes.toList()..insertAll(0, _varInt(length))); + requestBytes = Uint8List.fromList( + requestBytes.toList()..insertAll(0, _varInt(length)), + ); // Split into 10000 byte chunks. var lists = []; for (var i = 0; i < requestBytes.length; i += 10000) { - lists.add(Uint8List.sublistView( - requestBytes, i, min(i + 10000, requestBytes.length))); + lists.add( + Uint8List.sublistView( + requestBytes, + i, + min(i + 10000, requestBytes.length), + ), + ); } // Time `AsyncMessageGrouper` and deserialization. diff --git a/pkgs/bazel_worker/e2e_test/bin/async_worker_in_isolate.dart b/pkgs/bazel_worker/e2e_test/bin/async_worker_in_isolate.dart index a94875d57..285b03d2a 100644 --- a/pkgs/bazel_worker/e2e_test/bin/async_worker_in_isolate.dart +++ b/pkgs/bazel_worker/e2e_test/bin/async_worker_in_isolate.dart @@ -17,7 +17,10 @@ import 'package:e2e_test/forwards_to_isolate_async_worker.dart'; Future main(List args, [SendPort? message]) async { var receivePort = ReceivePort(); await Isolate.spawnUri( - Uri.file('async_worker.dart'), [], receivePort.sendPort); + Uri.file('async_worker.dart'), + [], + receivePort.sendPort, + ); var worker = await ForwardsToIsolateAsyncWorker.create(receivePort); await worker.run(); diff --git a/pkgs/bazel_worker/e2e_test/lib/async_worker.dart b/pkgs/bazel_worker/e2e_test/lib/async_worker.dart index d48d87c8d..55f517134 100644 --- a/pkgs/bazel_worker/e2e_test/lib/async_worker.dart +++ b/pkgs/bazel_worker/e2e_test/lib/async_worker.dart @@ -16,9 +16,6 @@ class ExampleAsyncWorker extends AsyncWorkerLoop { @override Future performRequest(WorkRequest request) async { - return WorkResponse( - exitCode: 0, - output: request.arguments.join('\n'), - ); + return WorkResponse(exitCode: 0, output: request.arguments.join('\n')); } } diff --git a/pkgs/bazel_worker/e2e_test/lib/forwards_to_isolate_async_worker.dart b/pkgs/bazel_worker/e2e_test/lib/forwards_to_isolate_async_worker.dart index bb937b220..a4845cf9e 100644 --- a/pkgs/bazel_worker/e2e_test/lib/forwards_to_isolate_async_worker.dart +++ b/pkgs/bazel_worker/e2e_test/lib/forwards_to_isolate_async_worker.dart @@ -13,9 +13,11 @@ class ForwardsToIsolateAsyncWorker extends AsyncWorkerLoop { final IsolateDriverConnection _isolateDriverConnection; static Future create( - ReceivePort receivePort) async { + ReceivePort receivePort, + ) async { return ForwardsToIsolateAsyncWorker( - await IsolateDriverConnection.create(receivePort)); + await IsolateDriverConnection.create(receivePort), + ); } ForwardsToIsolateAsyncWorker(this._isolateDriverConnection); diff --git a/pkgs/bazel_worker/e2e_test/pubspec.yaml b/pkgs/bazel_worker/e2e_test/pubspec.yaml index 56f00cd4c..7eaa89a16 100644 --- a/pkgs/bazel_worker/e2e_test/pubspec.yaml +++ b/pkgs/bazel_worker/e2e_test/pubspec.yaml @@ -10,6 +10,6 @@ dependencies: dev_dependencies: cli_util: ^0.4.2 - dart_flutter_team_lints: ^1.0.0 + dart_flutter_team_lints: ^3.0.0 path: ^1.8.0 test: ^1.16.0 diff --git a/pkgs/bazel_worker/e2e_test/test/e2e_test.dart b/pkgs/bazel_worker/e2e_test/test/e2e_test.dart index caa813a46..6b79b5edb 100644 --- a/pkgs/bazel_worker/e2e_test/test/e2e_test.dart +++ b/pkgs/bazel_worker/e2e_test/test/e2e_test.dart @@ -12,14 +12,18 @@ import 'package:test/test.dart'; void main() { var dart = p.join(sdkPath, 'bin', 'dart'); - runE2eTestForWorker('sync worker', - () => Process.start(dart, [p.join('bin', 'sync_worker.dart')])); - runE2eTestForWorker('async worker', - () => Process.start(dart, [p.join('bin', 'async_worker.dart')])); runE2eTestForWorker( - 'async worker in isolate', - () => - Process.start(dart, [p.join('bin', 'async_worker_in_isolate.dart')])); + 'sync worker', + () => Process.start(dart, [p.join('bin', 'sync_worker.dart')]), + ); + runE2eTestForWorker( + 'async worker', + () => Process.start(dart, [p.join('bin', 'async_worker.dart')]), + ); + runE2eTestForWorker( + 'async worker in isolate', + () => Process.start(dart, [p.join('bin', 'async_worker_in_isolate.dart')]), + ); } void runE2eTestForWorker(String groupName, SpawnWorker spawnWorker) { diff --git a/pkgs/bazel_worker/example/client.dart b/pkgs/bazel_worker/example/client.dart index 7147fcbf0..326bb180b 100644 --- a/pkgs/bazel_worker/example/client.dart +++ b/pkgs/bazel_worker/example/client.dart @@ -5,10 +5,14 @@ import 'package:bazel_worker/driver.dart'; void main() async { var scratchSpace = await Directory.systemTemp.createTemp(); var driver = BazelWorkerDriver( - () => Process.start(Platform.resolvedExecutable, - [Platform.script.resolve('worker.dart').toFilePath()], - workingDirectory: scratchSpace.path), - maxWorkers: 4); + () => Process.start( + Platform.resolvedExecutable, + [ + Platform.script.resolve('worker.dart').toFilePath(), + ], + workingDirectory: scratchSpace.path), + maxWorkers: 4, + ); var response = await driver.doWork(WorkRequest(arguments: ['foo'])); if (response.exitCode != EXIT_CODE_OK) { print('Worker request failed'); diff --git a/pkgs/bazel_worker/lib/src/async_message_grouper.dart b/pkgs/bazel_worker/lib/src/async_message_grouper.dart index e1f0deaef..8fc47780a 100644 --- a/pkgs/bazel_worker/lib/src/async_message_grouper.dart +++ b/pkgs/bazel_worker/lib/src/async_message_grouper.dart @@ -86,13 +86,18 @@ class AsyncMessageGrouper implements MessageGrouper { // Copy as much as possible from the input buffer. Limit is the // smaller of the remaining length to fill in the message and the // remaining length in the buffer. - var lengthToCopy = min(_message.length - _messagePos, - _inputBuffer.length - _inputBufferPos); + var lengthToCopy = min( + _message.length - _messagePos, + _inputBuffer.length - _inputBufferPos, + ); _message.setRange( - _messagePos, - _messagePos + lengthToCopy, - _inputBuffer.sublist( - _inputBufferPos, _inputBufferPos + lengthToCopy)); + _messagePos, + _messagePos + lengthToCopy, + _inputBuffer.sublist( + _inputBufferPos, + _inputBufferPos + lengthToCopy, + ), + ); _messagePos += lengthToCopy; _inputBufferPos += lengthToCopy; diff --git a/pkgs/bazel_worker/lib/src/driver/driver.dart b/pkgs/bazel_worker/lib/src/driver/driver.dart index 4a780208a..06cf0feb1 100644 --- a/pkgs/bazel_worker/lib/src/driver/driver.dart +++ b/pkgs/bazel_worker/lib/src/driver/driver.dart @@ -44,9 +44,12 @@ class BazelWorkerDriver { /// Factory method that spawns a worker process. final SpawnWorker _spawnWorker; - BazelWorkerDriver(this._spawnWorker, - {int? maxIdleWorkers, int? maxWorkers, int? maxRetries}) - : _maxIdleWorkers = maxIdleWorkers ?? 4, + BazelWorkerDriver( + this._spawnWorker, { + int? maxIdleWorkers, + int? maxWorkers, + int? maxRetries, + }) : _maxIdleWorkers = maxIdleWorkers ?? 4, _maxWorkers = maxWorkers ?? 4, _maxRetries = maxRetries ?? 4; @@ -56,8 +59,10 @@ class BazelWorkerDriver { /// [request] has been actually sent to the worker. This allows the caller /// to determine when actual work is being done versus just waiting for an /// available worker. - Future doWork(WorkRequest request, - {void Function(Future)? trackWork}) { + Future doWork( + WorkRequest request, { + void Function(Future)? trackWork, + }) { var attempt = _WorkAttempt(request, trackWork: trackWork); _workQueue.add(attempt); _runWorkQueue(); @@ -69,9 +74,11 @@ class BazelWorkerDriver { for (var worker in _readyWorkers.toList()) { _killWorker(worker); } - await Future.wait(_spawningWorkers.map((worker) async { - _killWorker(await worker); - })); + await Future.wait( + _spawningWorkers.map((worker) async { + _killWorker(await worker); + }), + ); } /// Runs as many items in [_workQueue] as possible given the number of @@ -88,8 +95,10 @@ class BazelWorkerDriver { if (_workQueue.isEmpty) return; if (_numWorkers == _maxWorkers && _idleWorkers.isEmpty) return; if (_numWorkers > _maxWorkers) { - throw StateError('Internal error, created to many workers. Please ' - 'file a bug at https://github.com/dart-lang/bazel_worker/issues/new'); + throw StateError( + 'Internal error, created to many workers. Please ' + 'file a bug at https://github.com/dart-lang/bazel_worker/issues/new', + ); } // At this point we definitely want to run a task, we just need to decide @@ -137,48 +146,51 @@ class BazelWorkerDriver { void _runWorker(Process worker, _WorkAttempt attempt) { var rescheduled = false; - runZonedGuarded(() async { - var connection = _workerConnections[worker]!; + runZonedGuarded( + () async { + var connection = _workerConnections[worker]!; - connection.writeRequest(attempt.request); - var responseFuture = connection.readResponse(); - if (attempt.trackWork != null) { - attempt.trackWork!(responseFuture); - } - var response = await responseFuture; - - // It is possible for us to complete with an error response due to an - // unhandled async error before we get here. - if (!attempt.responseCompleter.isCompleted) { - if (response.exitCode == EXIT_CODE_BROKEN_PIPE) { + connection.writeRequest(attempt.request); + var responseFuture = connection.readResponse(); + if (attempt.trackWork != null) { + attempt.trackWork!(responseFuture); + } + var response = await responseFuture; + + // It is possible for us to complete with an error response due to an + // unhandled async error before we get here. + if (!attempt.responseCompleter.isCompleted) { + if (response.exitCode == EXIT_CODE_BROKEN_PIPE) { + rescheduled = _tryReschedule(attempt); + if (rescheduled) return; + stderr.writeln('Failed to run request ${attempt.request}'); + response = WorkResponse( + exitCode: EXIT_CODE_ERROR, + output: + 'Invalid response from worker, this probably means it wrote ' + 'invalid output or died.', + ); + } + attempt.responseCompleter.complete(response); + _cleanUp(worker); + } + }, + (e, s) { + // Note that we don't need to do additional cleanup here on failures. If + // the worker dies that is already handled in a generic fashion, we just + // need to make sure we complete with a valid response. + if (!attempt.responseCompleter.isCompleted) { rescheduled = _tryReschedule(attempt); if (rescheduled) return; - stderr.writeln('Failed to run request ${attempt.request}'); - response = WorkResponse( + var response = WorkResponse( exitCode: EXIT_CODE_ERROR, - output: - 'Invalid response from worker, this probably means it wrote ' - 'invalid output or died.', + output: 'Error running worker:\n$e\n$s', ); + attempt.responseCompleter.complete(response); + _cleanUp(worker); } - attempt.responseCompleter.complete(response); - _cleanUp(worker); - } - }, (e, s) { - // Note that we don't need to do additional cleanup here on failures. If - // the worker dies that is already handled in a generic fashion, we just - // need to make sure we complete with a valid response. - if (!attempt.responseCompleter.isCompleted) { - rescheduled = _tryReschedule(attempt); - if (rescheduled) return; - var response = WorkResponse( - exitCode: EXIT_CODE_ERROR, - output: 'Error running worker:\n$e\n$s', - ); - attempt.responseCompleter.complete(response); - _cleanUp(worker); - } - }); + }, + ); } /// Performs post-work cleanup for [worker]. diff --git a/pkgs/bazel_worker/lib/src/driver/driver_connection.dart b/pkgs/bazel_worker/lib/src/driver/driver_connection.dart index b419debbc..80d5c98cf 100644 --- a/pkgs/bazel_worker/lib/src/driver/driver_connection.dart +++ b/pkgs/bazel_worker/lib/src/driver/driver_connection.dart @@ -34,13 +34,16 @@ class StdDriverConnection implements DriverConnection { Future get done => _messageGrouper.done; - StdDriverConnection( - {Stream>? inputStream, StreamSink>? outputStream}) - : _messageGrouper = AsyncMessageGrouper(inputStream ?? stdin), + StdDriverConnection({ + Stream>? inputStream, + StreamSink>? outputStream, + }) : _messageGrouper = AsyncMessageGrouper(inputStream ?? stdin), _outputStream = outputStream ?? stdout; factory StdDriverConnection.forWorker(Process worker) => StdDriverConnection( - inputStream: worker.stdout, outputStream: worker.stdin); + inputStream: worker.stdout, + outputStream: worker.stdin, + ); /// Note: This will attempts to recover from invalid proto messages by parsing /// them as strings. This is a common error case for workers (they print a diff --git a/pkgs/bazel_worker/lib/src/utils.dart b/pkgs/bazel_worker/lib/src/utils.dart index 609b4356a..f67bbac1e 100644 --- a/pkgs/bazel_worker/lib/src/utils.dart +++ b/pkgs/bazel_worker/lib/src/utils.dart @@ -13,8 +13,9 @@ List protoToDelimitedBuffer(GeneratedMessage message) { var delimiterBuffer = CodedBufferWriter(); delimiterBuffer.writeInt32NoTag(messageBuffer.lengthInBytes); - var result = - Uint8List(messageBuffer.lengthInBytes + delimiterBuffer.lengthInBytes); + var result = Uint8List( + messageBuffer.lengthInBytes + delimiterBuffer.lengthInBytes, + ); delimiterBuffer.writeTo(result); messageBuffer.writeTo(result, delimiterBuffer.lengthInBytes); diff --git a/pkgs/bazel_worker/lib/src/worker/async_worker_loop.dart b/pkgs/bazel_worker/lib/src/worker/async_worker_loop.dart index 5182b5598..a95d09a1f 100644 --- a/pkgs/bazel_worker/lib/src/worker/async_worker_loop.dart +++ b/pkgs/bazel_worker/lib/src/worker/async_worker_loop.dart @@ -32,20 +32,20 @@ abstract class AsyncWorkerLoop implements WorkerLoop { var request = await connection.readRequest(); if (request == null) break; var printMessages = StringBuffer(); - response = await runZoned(() => performRequest(request), - zoneSpecification: - ZoneSpecification(print: (self, parent, zone, message) { - printMessages.writeln(); - printMessages.write(message); - })); + response = await runZoned( + () => performRequest(request), + zoneSpecification: ZoneSpecification( + print: (self, parent, zone, message) { + printMessages.writeln(); + printMessages.write(message); + }, + ), + ); if (printMessages.isNotEmpty) { response.output = '${response.output}$printMessages'; } } catch (e, s) { - response = WorkResponse( - exitCode: EXIT_CODE_ERROR, - output: '$e\n$s', - ); + response = WorkResponse(exitCode: EXIT_CODE_ERROR, output: '$e\n$s'); } connection.writeResponse(response); diff --git a/pkgs/bazel_worker/lib/src/worker/sync_worker_loop.dart b/pkgs/bazel_worker/lib/src/worker/sync_worker_loop.dart index a8571058c..51da684ab 100644 --- a/pkgs/bazel_worker/lib/src/worker/sync_worker_loop.dart +++ b/pkgs/bazel_worker/lib/src/worker/sync_worker_loop.dart @@ -30,19 +30,20 @@ abstract class SyncWorkerLoop implements WorkerLoop { var request = connection.readRequest(); if (request == null) break; var printMessages = StringBuffer(); - response = runZoned(() => performRequest(request), zoneSpecification: - ZoneSpecification(print: (self, parent, zone, message) { - printMessages.writeln(); - printMessages.write(message); - })); + response = runZoned( + () => performRequest(request), + zoneSpecification: ZoneSpecification( + print: (self, parent, zone, message) { + printMessages.writeln(); + printMessages.write(message); + }, + ), + ); if (printMessages.isNotEmpty) { response.output = '${response.output}$printMessages'; } } catch (e, s) { - response = WorkResponse( - exitCode: EXIT_CODE_ERROR, - output: '$e\n$s', - ); + response = WorkResponse(exitCode: EXIT_CODE_ERROR, output: '$e\n$s'); } connection.writeResponse(response); diff --git a/pkgs/bazel_worker/lib/src/worker/worker_connection.dart b/pkgs/bazel_worker/lib/src/worker/worker_connection.dart index b395316a3..fd5508e4a 100644 --- a/pkgs/bazel_worker/lib/src/worker/worker_connection.dart +++ b/pkgs/bazel_worker/lib/src/worker/worker_connection.dart @@ -29,13 +29,16 @@ abstract class AsyncWorkerConnection implements WorkerConnection { /// Creates a [StdAsyncWorkerConnection] with the specified [inputStream] /// and [outputStream], unless [sendPort] is specified, in which case /// creates a [SendPortAsyncWorkerConnection]. - factory AsyncWorkerConnection( - {Stream>? inputStream, - StreamSink>? outputStream, - SendPort? sendPort}) => + factory AsyncWorkerConnection({ + Stream>? inputStream, + StreamSink>? outputStream, + SendPort? sendPort, + }) => sendPort == null ? StdAsyncWorkerConnection( - inputStream: inputStream, outputStream: outputStream) + inputStream: inputStream, + outputStream: outputStream, + ) : SendPortAsyncWorkerConnection(sendPort); @override @@ -53,9 +56,10 @@ class StdAsyncWorkerConnection implements AsyncWorkerConnection { final AsyncMessageGrouper _messageGrouper; final StreamSink> _outputStream; - StdAsyncWorkerConnection( - {Stream>? inputStream, StreamSink>? outputStream}) - : _messageGrouper = AsyncMessageGrouper(inputStream ?? stdin), + StdAsyncWorkerConnection({ + Stream>? inputStream, + StreamSink>? outputStream, + }) : _messageGrouper = AsyncMessageGrouper(inputStream ?? stdin), _outputStream = outputStream ?? stdout; @override diff --git a/pkgs/bazel_worker/lib/testing.dart b/pkgs/bazel_worker/lib/testing.dart index 3ae4c1f4d..7aefabbd8 100644 --- a/pkgs/bazel_worker/lib/testing.dart +++ b/pkgs/bazel_worker/lib/testing.dart @@ -72,10 +72,18 @@ class TestStdinAsync implements TestStdin { } @override - StreamSubscription listen(void Function(Uint8List bytes)? onData, - {Function? onError, void Function()? onDone, bool? cancelOnError}) { - return _controller.stream.listen(onData, - onError: onError, onDone: onDone, cancelOnError: cancelOnError); + StreamSubscription listen( + void Function(Uint8List bytes)? onData, { + Function? onError, + void Function()? onDone, + bool? cancelOnError, + }) { + return _controller.stream.listen( + onData, + onError: onError, + onDone: onDone, + cancelOnError: cancelOnError, + ); } @override @@ -165,8 +173,9 @@ class TestAsyncWorkerConnection extends StdAsyncWorkerConnection final List responses = []; TestAsyncWorkerConnection( - Stream> inputStream, StreamSink> outputStream) - : super(inputStream: inputStream, outputStream: outputStream); + Stream> inputStream, + StreamSink> outputStream, + ) : super(inputStream: inputStream, outputStream: outputStream); @override void writeResponse(WorkResponse response) { diff --git a/pkgs/bazel_worker/test/driver_test.dart b/pkgs/bazel_worker/test/driver_test.dart index c3978304e..c3db55c66 100644 --- a/pkgs/bazel_worker/test/driver_test.dart +++ b/pkgs/bazel_worker/test/driver_test.dart @@ -23,27 +23,37 @@ void main() { await _doRequests(count: 1); }); - test('can run multiple batches of requests through multiple workers', - () async { - var maxWorkers = 4; - var maxIdleWorkers = 2; - driver = BazelWorkerDriver(MockWorker.spawn, - maxWorkers: maxWorkers, maxIdleWorkers: maxIdleWorkers); - for (var i = 0; i < 10; i++) { - await _doRequests(driver: driver); - expect(MockWorker.liveWorkers.length, maxIdleWorkers); - // No workers should be killed while there is ongoing work, but they - // should be cleaned up once there isn't any more work to do. - expect(MockWorker.deadWorkers.length, - (maxWorkers - maxIdleWorkers) * (i + 1)); - } - }); + test( + 'can run multiple batches of requests through multiple workers', + () async { + var maxWorkers = 4; + var maxIdleWorkers = 2; + driver = BazelWorkerDriver( + MockWorker.spawn, + maxWorkers: maxWorkers, + maxIdleWorkers: maxIdleWorkers, + ); + for (var i = 0; i < 10; i++) { + await _doRequests(driver: driver); + expect(MockWorker.liveWorkers.length, maxIdleWorkers); + // No workers should be killed while there is ongoing work, but they + // should be cleaned up once there isn't any more work to do. + expect( + MockWorker.deadWorkers.length, + (maxWorkers - maxIdleWorkers) * (i + 1), + ); + } + }, + ); test('can run multiple requests through one worker', () async { var maxWorkers = 1; var maxIdleWorkers = 1; - driver = BazelWorkerDriver(MockWorker.spawn, - maxWorkers: maxWorkers, maxIdleWorkers: maxIdleWorkers); + driver = BazelWorkerDriver( + MockWorker.spawn, + maxWorkers: maxWorkers, + maxIdleWorkers: maxIdleWorkers, + ); for (var i = 0; i < 10; i++) { await _doRequests(driver: driver); expect(MockWorker.liveWorkers.length, 1); @@ -52,8 +62,11 @@ void main() { }); test('can run one request through multiple workers', () async { - driver = - BazelWorkerDriver(MockWorker.spawn, maxWorkers: 4, maxIdleWorkers: 4); + driver = BazelWorkerDriver( + MockWorker.spawn, + maxWorkers: 4, + maxIdleWorkers: 4, + ); for (var i = 0; i < 10; i++) { await _doRequests(driver: driver, count: 1); expect(MockWorker.liveWorkers.length, 1); @@ -63,8 +76,11 @@ void main() { test('can run with maxIdleWorkers == 0', () async { var maxWorkers = 4; - driver = BazelWorkerDriver(MockWorker.spawn, - maxWorkers: maxWorkers, maxIdleWorkers: 0); + driver = BazelWorkerDriver( + MockWorker.spawn, + maxWorkers: maxWorkers, + maxIdleWorkers: 0, + ); for (var i = 0; i < 10; i++) { await _doRequests(driver: driver); expect(MockWorker.liveWorkers.length, 0); @@ -77,14 +93,15 @@ void main() { driver = BazelWorkerDriver(MockWorker.spawn, maxWorkers: maxWorkers); var tracking = []; await _doRequests( - driver: driver, - count: 10, - trackWork: (Future response) { - // We should never be tracking more than `maxWorkers` jobs at a time. - expect(tracking.length, lessThan(maxWorkers)); - tracking.add(response); - response.then((_) => tracking.remove(response)); - }); + driver: driver, + count: 10, + trackWork: (Future response) { + // We should never be tracking more than `maxWorkers` jobs at a time. + expect(tracking.length, lessThan(maxWorkers)); + tracking.add(response); + response.then((_) => tracking.remove(response)); + }, + ); }); group('failing workers', () { @@ -93,27 +110,39 @@ void main() { void createDriver({int maxRetries = 2, int numBadWorkers = 2}) { var numSpawned = 0; driver = BazelWorkerDriver( - () async => MockWorker(workerLoopFactory: (MockWorker worker) { - var connection = StdAsyncWorkerConnection( - inputStream: worker._stdinController.stream, - outputStream: worker._stdoutController.sink); - if (numSpawned < numBadWorkers) { - numSpawned++; - return ThrowingMockWorkerLoop( - worker, MockWorker.responseQueue, connection); - } else { - return MockWorkerLoop(MockWorker.responseQueue, - connection: connection); - } - }), - maxRetries: maxRetries); + () async => MockWorker( + workerLoopFactory: (MockWorker worker) { + var connection = StdAsyncWorkerConnection( + inputStream: worker._stdinController.stream, + outputStream: worker._stdoutController.sink, + ); + if (numSpawned < numBadWorkers) { + numSpawned++; + return ThrowingMockWorkerLoop( + worker, + MockWorker.responseQueue, + connection, + ); + } else { + return MockWorkerLoop( + MockWorker.responseQueue, + connection: connection, + ); + } + }, + ), + maxRetries: maxRetries, + ); } test('should retry up to maxRetries times', () async { createDriver(); var expectedResponse = WorkResponse(); - MockWorker.responseQueue.addAll( - [disconnectedResponse, disconnectedResponse, expectedResponse]); + MockWorker.responseQueue.addAll([ + disconnectedResponse, + disconnectedResponse, + expectedResponse, + ]); var actualResponse = await driver!.doWork(WorkRequest()); // The first 2 null responses are thrown away, and we should get the // third one. @@ -125,23 +154,29 @@ void main() { test('should fail if it exceeds maxRetries failures', () async { createDriver(maxRetries: 2, numBadWorkers: 3); - MockWorker.responseQueue.addAll( - [disconnectedResponse, disconnectedResponse, WorkResponse()]); + MockWorker.responseQueue.addAll([ + disconnectedResponse, + disconnectedResponse, + WorkResponse(), + ]); var actualResponse = await driver!.doWork(WorkRequest()); // Should actually get a bad response. expect(actualResponse.exitCode, 15); expect( - actualResponse.output, - 'Invalid response from worker, this probably means it wrote ' - 'invalid output or died.'); + actualResponse.output, + 'Invalid response from worker, this probably means it wrote ' + 'invalid output or died.', + ); expect(MockWorker.deadWorkers.length, 3); }); }); test('handles spawnWorker failures', () async { - driver = BazelWorkerDriver(() async => throw StateError('oh no!'), - maxRetries: 0); + driver = BazelWorkerDriver( + () async => throw StateError('oh no!'), + maxRetries: 0, + ); expect(driver!.doWork(WorkRequest()), throwsA(isA())); }); @@ -156,10 +191,11 @@ void main() { /// Runs [count] of fake work requests through [driver], and asserts that they /// all completed. -Future _doRequests( - {BazelWorkerDriver? driver, - int count = 100, - void Function(Future)? trackWork}) async { +Future _doRequests({ + BazelWorkerDriver? driver, + int count = 100, + void Function(Future)? trackWork, +}) async { // If we create a driver, we need to make sure and terminate it. var terminateDriver = driver == null; driver ??= BazelWorkerDriver(MockWorker.spawn); @@ -167,7 +203,8 @@ Future _doRequests( var responses = List.generate(count, (_) => WorkResponse()); MockWorker.responseQueue.addAll(responses); var actualResponses = await Future.wait( - requests.map((request) => driver!.doWork(request, trackWork: trackWork))); + requests.map((request) => driver!.doWork(request, trackWork: trackWork)), + ); expect(actualResponses, unorderedEquals(responses)); if (terminateDriver) await driver.terminateWorkers(); } @@ -191,9 +228,11 @@ class MockWorkerLoop extends AsyncWorkerLoop { class ThrowingMockWorkerLoop extends MockWorkerLoop { final MockWorker _mockWorker; - ThrowingMockWorkerLoop(this._mockWorker, Queue responseQueue, - AsyncWorkerConnection connection) - : super(responseQueue, connection: connection); + ThrowingMockWorkerLoop( + this._mockWorker, + Queue responseQueue, + AsyncWorkerConnection connection, + ) : super(responseQueue, connection: connection); /// Run the worker loop. The returned [Future] doesn't complete until /// [connection#readRequest] returns `null`. @@ -234,10 +273,13 @@ class MockWorker implements Process { liveWorkers.add(this); var workerLoop = workerLoopFactory != null ? workerLoopFactory(this) - : MockWorkerLoop(responseQueue, + : MockWorkerLoop( + responseQueue, connection: StdAsyncWorkerConnection( - inputStream: _stdinController.stream, - outputStream: _stdoutController.sink)); + inputStream: _stdinController.stream, + outputStream: _stdoutController.sink, + ), + ); workerLoop.run(); } @@ -260,8 +302,10 @@ class MockWorker implements Process { int get pid => throw UnsupportedError('Not needed.'); @override - bool kill( - [ProcessSignal processSignal = ProcessSignal.sigterm, int exitCode = 0]) { + bool kill([ + ProcessSignal processSignal = ProcessSignal.sigterm, + int exitCode = 0, + ]) { if (_killed) return false; () async { await _stdoutController.close(); diff --git a/pkgs/bazel_worker/test/message_grouper_test.dart b/pkgs/bazel_worker/test/message_grouper_test.dart index 475190ed8..fd99911d1 100644 --- a/pkgs/bazel_worker/test/message_grouper_test.dart +++ b/pkgs/bazel_worker/test/message_grouper_test.dart @@ -18,8 +18,10 @@ void main() { }); } -void runTests(TestStdin Function() stdinFactory, - MessageGrouper Function(Stdin) messageGrouperFactory) { +void runTests( + TestStdin Function() stdinFactory, + MessageGrouper Function(Stdin) messageGrouperFactory, +) { late MessageGrouper messageGrouper; late TestStdin stdinStream; @@ -52,16 +54,12 @@ void runTests(TestStdin Function() stdinFactory, }); test('Short message', () async { - await check([ - 5, - 10, - 20, - 30, - 40, - 50 - ], [ - [10, 20, 30, 40, 50] - ]); + await check( + [5, 10, 20, 30, 40, 50], + [ + [10, 20, 30, 40, 50], + ], + ); }); test('Message with 2-byte length', () async { @@ -79,57 +77,44 @@ void runTests(TestStdin Function() stdinFactory, }); test('Multiple messages', () async { - await check([ - 2, - 10, - 20, - 2, - 30, - 40 - ], [ - [10, 20], - [30, 40] - ]); + await check( + [2, 10, 20, 2, 30, 40], + [ + [10, 20], + [30, 40], + ], + ); }); test('Empty message at start', () async { - await check([ - 0, - 2, - 10, - 20 - ], [ - [], - [10, 20] - ]); + await check( + [0, 2, 10, 20], + [ + [], + [10, 20], + ], + ); }); test('Empty message at end', () async { - await check([ - 2, - 10, - 20, - 0 - ], [ - [10, 20], - [] - ]); + await check( + [2, 10, 20, 0], + [ + [10, 20], + [], + ], + ); }); test('Empty message in the middle', () async { - await check([ - 2, - 10, - 20, - 0, - 2, - 30, - 40 - ], [ - [10, 20], - [], - [30, 40] - ]); + await check( + [2, 10, 20, 0, 2, 30, 40], + [ + [10, 20], + [], + [30, 40], + ], + ); }); test('Handles the case when stdin gives an error instead of EOF', () async { diff --git a/pkgs/bazel_worker/test/worker_loop_test.dart b/pkgs/bazel_worker/test/worker_loop_test.dart index 50d21512e..24068b1d8 100644 --- a/pkgs/bazel_worker/test/worker_loop_test.dart +++ b/pkgs/bazel_worker/test/worker_loop_test.dart @@ -11,36 +11,45 @@ import 'package:test/test.dart'; void main() { group('SyncWorkerLoop', () { - runTests(TestStdinSync.new, TestSyncWorkerConnection.new, - TestSyncWorkerLoop.new); + runTests( + TestStdinSync.new, + TestSyncWorkerConnection.new, + TestSyncWorkerLoop.new, + ); }); group('AsyncWorkerLoop', () { - runTests(TestStdinAsync.new, TestAsyncWorkerConnection.new, - TestAsyncWorkerLoop.new); + runTests( + TestStdinAsync.new, + TestAsyncWorkerConnection.new, + TestAsyncWorkerLoop.new, + ); }); group('SyncWorkerLoopWithPrint', () { runTests( - TestStdinSync.new, - TestSyncWorkerConnection.new, - (TestSyncWorkerConnection connection) => - TestSyncWorkerLoop(connection, printMessage: 'Goodbye!')); + TestStdinSync.new, + TestSyncWorkerConnection.new, + (TestSyncWorkerConnection connection) => + TestSyncWorkerLoop(connection, printMessage: 'Goodbye!'), + ); }); group('AsyncWorkerLoopWithPrint', () { runTests( - TestStdinAsync.new, - TestAsyncWorkerConnection.new, - (TestAsyncWorkerConnection connection) => - TestAsyncWorkerLoop(connection, printMessage: 'Goodbye!')); + TestStdinAsync.new, + TestAsyncWorkerConnection.new, + (TestAsyncWorkerConnection connection) => + TestAsyncWorkerLoop(connection, printMessage: 'Goodbye!'), + ); }); } void runTests( - TestStdin Function() stdinFactory, - T Function(Stdin, Stdout) workerConnectionFactory, - TestWorkerLoop Function(T) workerLoopFactory) { + TestStdin Function() stdinFactory, + T Function(Stdin, Stdout) workerConnectionFactory, + TestWorkerLoop Function(T) workerLoopFactory, +) { late TestStdin stdinStream; late TestStdoutStream stdoutStream; late T connection; @@ -63,19 +72,29 @@ void runTests( // Make sure `print` never gets called in the parent zone. var printMessages = []; - await runZoned(() => workerLoop.run(), zoneSpecification: - ZoneSpecification(print: (self, parent, zone, message) { - printMessages.add(message); - })); - expect(printMessages, isEmpty, - reason: 'The worker loop should hide all print calls from the parent ' - 'zone.'); + await runZoned( + () => workerLoop.run(), + zoneSpecification: ZoneSpecification( + print: (self, parent, zone, message) { + printMessages.add(message); + }, + ), + ); + expect( + printMessages, + isEmpty, + reason: 'The worker loop should hide all print calls from the parent ' + 'zone.', + ); expect(connection.responses, hasLength(1)); expect(connection.responses[0], response); if (workerLoop.printMessage != null) { - expect(response.output, endsWith(workerLoop.printMessage!), - reason: 'Print messages should get appended to the response output.'); + expect( + response.output, + endsWith(workerLoop.printMessage!), + reason: 'Print messages should get appended to the response output.', + ); } // Check that a serialized version was written to std out. diff --git a/pkgs/clock/analysis_options.yaml b/pkgs/clock/analysis_options.yaml index 9ee7c2b6a..db6072da7 100644 --- a/pkgs/clock/analysis_options.yaml +++ b/pkgs/clock/analysis_options.yaml @@ -11,4 +11,3 @@ linter: rules: - avoid_private_typedef_functions - avoid_redundant_argument_values - - use_super_parameters diff --git a/pkgs/coverage/analysis_options.yaml b/pkgs/coverage/analysis_options.yaml index 82ce5e0a5..bb1afe05a 100644 --- a/pkgs/coverage/analysis_options.yaml +++ b/pkgs/coverage/analysis_options.yaml @@ -9,14 +9,9 @@ analyzer: linter: rules: - - always_declare_return_types - avoid_slow_async_io - cancel_subscriptions - - comment_references - literal_only_boolean_expressions - prefer_final_locals - sort_constructors_first - sort_unnamed_constructors_first - - test_types_in_equals - - throw_in_finally - - type_annotate_public_apis diff --git a/pkgs/test_reflective_loader/pubspec.yaml b/pkgs/test_reflective_loader/pubspec.yaml index 953ece944..569933f23 100644 --- a/pkgs/test_reflective_loader/pubspec.yaml +++ b/pkgs/test_reflective_loader/pubspec.yaml @@ -10,4 +10,4 @@ dependencies: test: ^1.16.0 dev_dependencies: - dart_flutter_team_lints: ^2.0.0 + dart_flutter_team_lints: ^3.0.0