import 'dart:async'; import 'package:angel3_reactivex/angel3_reactivex.dart'; import 'package:test/test.dart'; import '../../utils.dart'; Stream getStream(int n) async* { var k = 0; while (k < n) { await Future.delayed(const Duration(milliseconds: 100)); yield k++; } } void main() { test('Rx.buffer', () async { await expectLater( getStream(4).buffer( Stream.periodic(const Duration(milliseconds: 160)).take(3)), emitsInOrder([ const [0, 1], const [2, 3], emitsDone ])); }); test('Rx.buffer.sampleBeforeEvent.shouldEmit', () async { await expectLater( Stream.fromFuture( Future.delayed(const Duration(milliseconds: 200)) .then((_) => 'end')).startWith('start').buffer( Stream.periodic(const Duration(milliseconds: 40)).take(10)), emitsInOrder([ const ['start'], // after 40ms const [], // 80ms const [], // 120ms const [], // 160ms const ['end'], // done emitsDone ])); }); test('Rx.buffer.shouldClose', () async { final controller = StreamController() ..add(0) ..add(1) ..add(2) ..add(3); scheduleMicrotask(controller.close); await expectLater( controller.stream .buffer(Stream.periodic(const Duration(seconds: 3))) .take(1), emitsInOrder([ const [0, 1, 2, 3], // done emitsDone ])); }); test('Rx.buffer.reusable', () async { final transformer = BufferStreamTransformer((_) => Stream.periodic(const Duration(milliseconds: 160)) .take(3) .asBroadcastStream()); await expectLater( getStream(4).transform(transformer), emitsInOrder([ const [0, 1], const [2, 3], emitsDone ])); await expectLater( getStream(4).transform(transformer), emitsInOrder([ const [0, 1], const [2, 3], emitsDone ])); }); test('Rx.buffer.asBroadcastStream', () async { final stream = getStream(4).asBroadcastStream().buffer( Stream.periodic(const Duration(milliseconds: 160)) .take(10) .asBroadcastStream()); // listen twice on same stream await expectLater( stream, emitsInOrder([ const [0, 1], const [2, 3], emitsDone ])); await expectLater(stream, emitsInOrder([emitsDone])); }); test('Rx.buffer.error.shouldThrowA', () async { await expectLater( Stream.error(Exception()) .buffer(Stream.periodic(const Duration(milliseconds: 160))), emitsError(isException)); }); test('Rx.buffer.nullable', () { nullableTest>( (s) => s.buffer(Stream.empty()), ); }); }