blob: 92d16c5f28a633e67d2cb6add730eceaeaf0fcf6 [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> reduce(
/// RawSocketEvent combine(T previous, T element)
/// )
/// Combines a sequence of values by repeatedly applying combine.
///
/// @description Checks that method [reduce] reduces a sequence of values
/// according to method combine.
/// @author ngl@unipro.ru
import "dart:io";
import "dart:async";
import "../../../Utils/expect.dart";
check(combine(previous, element), expected) {
asyncStart();
var address = InternetAddress.loopbackIPv4;
RawDatagramSocket.bind(address, 0).then((producer) {
RawDatagramSocket.bind(address, 0).then((receiver) {
int sent = 0;
int counter = 0;
producer.send([sent++], address, receiver.port);
producer.send([sent++], address, receiver.port);
producer.close();
Stream bcs = receiver.asBroadcastStream();
Future future = bcs.reduce(combine);
future.then((event) {
Expect.equals(expected, event);
}).whenComplete(() {
asyncEnd();
});
bcs.listen((event) {
receiver.receive();
counter++;
}).onDone(() {
Expect.equals(3, counter);
});
new Timer(const Duration(milliseconds: 200), () {
Expect.isNull(receiver.receive());
receiver.close();
});
});
});
}
main() {
check((previous, element) => previous, RawSocketEvent.write);
check((previous, element) => element, RawSocketEvent.closed);
}