blob: 59a82048a35b07cf7233be9249ca23d84e36bca2 [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 Stream<S> map<S>(S convert(T event))
/// Creates a new stream that converts each element of this stream to a new value
/// using the convert function.
///
/// For each data event, o, in this stream, the returned stream provides a data
/// event with the value convert(o).
///
/// @description Checks that for each event received with RawDatagramSocket
/// method asyncMap returns specified value.
/// @author ngl@unipro.ru
import "dart:io";
import "dart:async";
import "../../../Utils/expect.dart";
check(convert(event), List expected) {
asyncStart();
var address = InternetAddress.loopbackIPv4;
RawDatagramSocket.bind(address, 0).then((producer) {
RawDatagramSocket.bind(address, 0).then((receiver) {
int sent = 0;
int counter = 0;
Timer timer;
List list = [];
producer.send([sent++], address, receiver.port);
producer.send([sent++], address, receiver.port);
producer.send([sent++], address, receiver.port);
producer.close();
Stream s = receiver.map(convert);
s.listen((event) {
list.add(event);
receiver.receive();
counter++;
if (timer != null) {
timer.cancel();
}
timer = new Timer(const Duration(milliseconds: 200), () {
Expect.isNull(receiver.receive());
receiver.close();
});
}).onDone(() {
Expect.equals(4, counter);
Expect.listEquals(expected, list);
asyncEnd();
});
});
});
}
main() {
check((e) => e, [
RawSocketEvent.write,
RawSocketEvent.read,
RawSocketEvent.read,
RawSocketEvent.closed
]);
check((e) => [1, 2], [
[1, 2],
[1, 2],
[1, 2],
[1, 2]
]);
}