Line data Source code
1 : // Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file 2 : // for details. All rights reserved. Use of this source code is governed by a 3 : // BSD-style license that can be found in the LICENSE file. 4 : 5 : import 'package:stream_channel/stream_channel.dart'; 6 : 7 : /// A class that connects incoming and outgoing channels with the same names. 8 : class SuiteChannelManager { 9 : /// Connections from the test runner that have yet to connect to corresponding 10 : /// calls to [connectOut]. 11 : final _incomingConnections = <String, StreamChannel<Object?>>{}; 12 : 13 : /// Connections from calls to [connectOut] that have yet to connect to 14 : /// corresponding connections from the test runner. 15 : final _outgoingConnections = <String, StreamChannelCompleter<Object?>>{}; 16 : 17 : /// The channel names that have already been used. 18 : final _names = <String>{}; 19 : 20 : /// Creates a connection to the test runnner's channel with the given [name]. 21 0 : StreamChannel<Object?> connectOut(String name) { 22 0 : if (_incomingConnections.containsKey(name)) { 23 0 : return (_incomingConnections[name])!; 24 0 : } else if (_names.contains(name)) { 25 0 : throw StateError('Duplicate suiteChannel() connection "$name".'); 26 : } else { 27 0 : _names.add(name); 28 0 : var completer = StreamChannelCompleter<Object?>(); 29 0 : _outgoingConnections[name] = completer; 30 0 : return completer.channel; 31 : } 32 : } 33 : 34 : /// Connects [channel] to this worker's channel with the given [name]. 35 0 : void connectIn(String name, StreamChannel<Object?> channel) { 36 0 : if (_outgoingConnections.containsKey(name)) { 37 0 : _outgoingConnections.remove(name)!.setChannel(channel); 38 0 : } else if (_incomingConnections.containsKey(name)) { 39 0 : throw StateError('Duplicate RunnerSuite.channel() connection "$name".'); 40 : } else { 41 0 : _incomingConnections[name] = channel; 42 : } 43 : } 44 : }