blob: e53fd9a1ed1c17b1128ef8120fb33b3917f9f267 [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.
// @dart = 2.9
/// @assertion Future forEach(void action(T element))
/// Executes action on each data event of the stream.
/// . . .
/// Completes the future with an error if the stream has an error event, or if
/// action throws.
///
/// @description Checks that the returned Future is completed with an error if
/// action throws.
/// @author ngl@unipro.ru
import "dart:io";
import "dart:async";
import "../../../Utils/expect.dart";
check(variant) {
asyncStart();
var address = InternetAddress.loopbackIPv4;
RawDatagramSocket.bind(address, 0).then((producer) {
RawDatagramSocket.bind(address, 0).then((receiver) {
int sent = 0;
int counter = 0;
void action1(e) => throw 1;
void action2(e) => e == RawSocketEvent.closed ? throw 2 : 0;
var action = variant == 1 ? action1 : action2;
producer.send([sent++], address, receiver.port);
producer.send([sent++], address, receiver.port);
producer.send([sent], address, receiver.port);
producer.close();
Stream bcs = receiver.asBroadcastStream();
bcs.forEach(action).then((_) {
Expect.fail('Should not be here');
}).catchError((e) {
Expect.equals(variant, e);
}).whenComplete(() {
asyncEnd();
});
new Timer(const Duration(milliseconds: 200), () {
Expect.isNull(receiver.receive());
receiver.close();
});
bcs.listen((event) {
counter++;
receiver.receive();
}).onDone(() {
Expect.equals(4, counter);
});
});
});
}
main() {
check(1);
check(2);
}