| // Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file |
| // for details. All rights reserved. Use of this source code is governed by a |
| // BSD-style license that can be found in the LICENSE file. |
| |
| /// @assertion |
| /// StreamSubscription<RawSocketEvent> listen( |
| /// void onData( |
| /// T event |
| /// ), { |
| /// Function onError, |
| /// void onDone(), |
| /// bool cancelOnError |
| /// }) |
| /// Adds a subscription to this stream. |
| /// . . . |
| /// If onDone is null, nothing happens. |
| /// |
| /// @description Checks that onData can be null. |
| /// @author ngl@unipro.ru |
| |
| import "dart:io"; |
| import "dart:async"; |
| import "../../../Utils/expect.dart"; |
| |
| check(int n, expected) { |
| asyncStart(); |
| var address = InternetAddress.loopbackIPv4; |
| RawDatagramSocket.bind(address, 0).then((producer) { |
| RawDatagramSocket.bind(address, 0).then((receiver) { |
| int sent = 0; |
| int counter = 0; |
| List actual = []; |
| producer.send([sent++], address, receiver.port); |
| producer.send([sent++], address, receiver.port); |
| producer.send([sent], address, receiver.port); |
| producer.close(); |
| if (n == 0) { |
| receiver.close(); |
| } |
| |
| new Timer(const Duration(milliseconds: 200), () { |
| Expect.isNull(receiver.receive()); |
| receiver.close(); |
| }); |
| |
| receiver.listen((event) { |
| actual.add(event); |
| counter++; |
| receiver.receive(); |
| if (event == RawSocketEvent.closed) { |
| Expect.listEquals(expected, actual); |
| asyncEnd(); |
| } |
| if (counter == n) { |
| receiver.close(); |
| } |
| }, onDone: (null)); |
| }); |
| }); |
| } |
| |
| main() { |
| check(0, [RawSocketEvent.closed]); |
| check(1, [RawSocketEvent.write, RawSocketEvent.closed]); |
| check(2, [RawSocketEvent.write, RawSocketEvent.read, RawSocketEvent.closed]); |
| check(3, [ |
| RawSocketEvent.write, |
| RawSocketEvent.read, |
| RawSocketEvent.read, |
| RawSocketEvent.closed |
| ]); |
| } |