flutter/packages/flutter_tools/test/general.shard/commands/daemon_test.dart
Michael Goderbauer 5491c8c146
Auto-format Framework (#160545)
This auto-formats all *.dart files in the repository outside of the
`engine` subdirectory and enforces that these files stay formatted with
a presubmit check.

**Reviewers:** Please carefully review all the commits except for the
one titled "formatted". The "formatted" commit was auto-generated by
running `dev/tools/format.sh -a -f`. The other commits were hand-crafted
to prepare the repo for the formatting change. I recommend reviewing the
commits one-by-one via the "Commits" tab and avoiding Github's "Files
changed" tab as it will likely slow down your browser because of the
size of this PR.

---------

Co-authored-by: Kate Lovett <katelovett@google.com>
Co-authored-by: LongCatIsLooong <31859944+LongCatIsLooong@users.noreply.github.com>
2024-12-19 20:06:21 +00:00

101 lines
2.7 KiB
Dart

// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:async';
import 'package:flutter_tools/src/base/io.dart';
import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/commands/daemon.dart';
import 'package:test/fake.dart';
import '../../src/common.dart';
void main() {
testWithoutContext('binds on ipv4 normally', () async {
final FakeServerSocket socket = FakeServerSocket();
final BufferLogger logger = BufferLogger.test();
int bindCalledTimes = 0;
final List<Object?> bindAddresses = <Object?>[];
final List<int> bindPorts = <int>[];
final DaemonServer server = DaemonServer(
port: 123,
logger: logger,
bind: (Object? address, int port) async {
bindCalledTimes++;
bindAddresses.add(address);
bindPorts.add(port);
return socket;
},
);
await server.run();
expect(bindCalledTimes, 1);
expect(bindAddresses, <Object?>[InternetAddress.loopbackIPv4]);
expect(bindPorts, <int>[123]);
});
testWithoutContext('binds on ipv6 if ipv4 failed normally', () async {
final FakeServerSocket socket = FakeServerSocket();
final BufferLogger logger = BufferLogger.test();
int bindCalledTimes = 0;
final List<Object?> bindAddresses = <Object?>[];
final List<int> bindPorts = <int>[];
final DaemonServer server = DaemonServer(
port: 123,
logger: logger,
bind: (Object? address, int port) async {
bindCalledTimes++;
bindAddresses.add(address);
bindPorts.add(port);
if (address == InternetAddress.loopbackIPv4) {
throw const SocketException('fail');
}
return socket;
},
);
await server.run();
expect(bindCalledTimes, 2);
expect(bindAddresses, <Object?>[InternetAddress.loopbackIPv4, InternetAddress.loopbackIPv6]);
expect(bindPorts, <int>[123, 123]);
});
}
class FakeServerSocket extends Fake implements ServerSocket {
FakeServerSocket();
@override
int get port => 1;
bool closeCalled = false;
final StreamController<Socket> controller = StreamController<Socket>();
@override
StreamSubscription<Socket> listen(
void Function(Socket event)? onData, {
Function? onError,
void Function()? onDone,
bool? cancelOnError,
}) {
// Close the controller immediately for testing purpose.
scheduleMicrotask(() {
controller.close();
});
return controller.stream.listen(
onData,
onError: onError,
onDone: onDone,
cancelOnError: cancelOnError,
);
}
@override
Future<ServerSocket> close() async {
closeCalled = true;
return this;
}
}