|
| 1 | +// Copyright 2014 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:async'; |
| 6 | + |
| 7 | +import 'package:flutter_tools/src/base/io.dart'; |
| 8 | +import 'package:flutter_tools/src/base/logger.dart'; |
| 9 | +import 'package:flutter_tools/src/commands/daemon.dart'; |
| 10 | +import 'package:test/fake.dart'; |
| 11 | + |
| 12 | +import '../../src/common.dart'; |
| 13 | + |
| 14 | +void main() { |
| 15 | + testWithoutContext('binds on ipv4 normally', () async { |
| 16 | + final FakeServerSocket socket = FakeServerSocket(); |
| 17 | + final BufferLogger logger = BufferLogger.test(); |
| 18 | + |
| 19 | + int bindCalledTimes = 0; |
| 20 | + final List<Object?> bindAddresses = <Object?>[]; |
| 21 | + final List<int> bindPorts = <int>[]; |
| 22 | + |
| 23 | + final DaemonServer server = DaemonServer( |
| 24 | + port: 123, |
| 25 | + logger: logger, |
| 26 | + bind: (Object? address, int port) async { |
| 27 | + bindCalledTimes++; |
| 28 | + bindAddresses.add(address); |
| 29 | + bindPorts.add(port); |
| 30 | + return socket; |
| 31 | + }, |
| 32 | + ); |
| 33 | + await server.run(); |
| 34 | + expect(bindCalledTimes, 1); |
| 35 | + expect(bindAddresses, <Object?>[InternetAddress.loopbackIPv4]); |
| 36 | + expect(bindPorts, <int>[123]); |
| 37 | + }); |
| 38 | + |
| 39 | + testWithoutContext('binds on ipv6 if ipv4 failed normally', () async { |
| 40 | + final FakeServerSocket socket = FakeServerSocket(); |
| 41 | + final BufferLogger logger = BufferLogger.test(); |
| 42 | + |
| 43 | + int bindCalledTimes = 0; |
| 44 | + final List<Object?> bindAddresses = <Object?>[]; |
| 45 | + final List<int> bindPorts = <int>[]; |
| 46 | + |
| 47 | + final DaemonServer server = DaemonServer( |
| 48 | + port: 123, |
| 49 | + logger: logger, |
| 50 | + bind: (Object? address, int port) async { |
| 51 | + bindCalledTimes++; |
| 52 | + bindAddresses.add(address); |
| 53 | + bindPorts.add(port); |
| 54 | + if (address == InternetAddress.loopbackIPv4) { |
| 55 | + throw const SocketException('fail'); |
| 56 | + } |
| 57 | + return socket; |
| 58 | + }, |
| 59 | + ); |
| 60 | + await server.run(); |
| 61 | + expect(bindCalledTimes, 2); |
| 62 | + expect(bindAddresses, <Object?>[InternetAddress.loopbackIPv4, InternetAddress.loopbackIPv6]); |
| 63 | + expect(bindPorts, <int>[123, 123]); |
| 64 | + }); |
| 65 | +} |
| 66 | + |
| 67 | +class FakeServerSocket extends Fake implements ServerSocket { |
| 68 | + FakeServerSocket(); |
| 69 | + |
| 70 | + @override |
| 71 | + int get port => 1; |
| 72 | + |
| 73 | + bool closeCalled = false; |
| 74 | + final StreamController<Socket> controller = StreamController<Socket>(); |
| 75 | + |
| 76 | + @override |
| 77 | + StreamSubscription<Socket> listen( |
| 78 | + void Function(Socket event)? onData, { |
| 79 | + Function? onError, |
| 80 | + void Function()? onDone, |
| 81 | + bool? cancelOnError, |
| 82 | + }) { |
| 83 | + // Close the controller immediately for testing purpose. |
| 84 | + scheduleMicrotask(() { |
| 85 | + controller.close(); |
| 86 | + }); |
| 87 | + return controller.stream.listen(onData, |
| 88 | + onError: onError, onDone: onDone, cancelOnError: cancelOnError); |
| 89 | + } |
| 90 | + |
| 91 | + @override |
| 92 | + Future<ServerSocket> close() async { |
| 93 | + closeCalled = true; |
| 94 | + return this; |
| 95 | + } |
| 96 | +} |
0 commit comments