blob: edfaa78fcf258e82bdad2632815e87748a411823 [file] [log] [blame]
// 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
/// Future<RawSocketEvent> firstWhere (
/// bool test(T element), {
/// dynamic defaultValue(),
/// RawSocketEvent orElse()
/// })
/// Finds the first element of this stream matching test.
/// . . .
/// If no such element is found before this stream is done, and a orElse function
/// is provided, the result of calling orElse becomes the value of the future.
/// If orElse throws, the returned future is completed with that error.
///
/// @description Checks that if orElse throws, the returned future is completed
/// with that error.
/// @author ngl@unipro.ru
import "dart:io";
import "dart:async";
import "../../../Utils/expect.dart";
main() {
asyncStart();
var address = InternetAddress.loopbackIPv4;
RawDatagramSocket.bind(address, 0).then((producer) {
RawDatagramSocket.bind(address, 0).then((receiver) {
int sent = 0;
int counter = 0;
int expected = 0;
producer.send([sent++], address, receiver.port);
producer.send([sent++], address, receiver.port);
producer.send([sent], address, receiver.port);
producer.close();
Stream<RawSocketEvent> bcs = receiver.asBroadcastStream();
Future fValue = bcs.firstWhere((e) => e == null, orElse: () => throw 11);
fValue.then((value) {
Expect.fail('Error expected.');
}, onError: (error) {
expected = error;
}).whenComplete(() {
Expect.equals(11, expected);
asyncEnd();
});
new Timer(const Duration(milliseconds: 200), () {
Expect.isNull(receiver.receive());
receiver.close();
});
bcs.listen((event) {
counter++;
receiver.receive();
}).onDone(() {
Expect.equals(4, counter);
});
});
});
}