blob: b61a8dbc86834d6f7d2700156d2f0f48a842ca51 [file] [log] [blame]
// Copyright (c) 2017, 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.
import 'dart:async';
import 'package:test/test.dart';
import 'package:stream_transform/stream_transform.dart';
import 'utils.dart';
void main() {
var streamTypes = {
'single subscription': () => new StreamController(),
'broadcast': () => new StreamController.broadcast()
};
for (var streamType in streamTypes.keys) {
group('Stream type [$streamType]', () {
StreamController values;
List emittedValues;
bool valuesCanceled;
bool isDone;
List errors;
Stream transformed;
StreamSubscription subscription;
void setUpStreams(StreamTransformer transformer) {
valuesCanceled = false;
values = streamTypes[streamType]()
..onCancel = () {
valuesCanceled = true;
};
emittedValues = [];
errors = [];
isDone = false;
transformed = values.stream.transform(transformer);
subscription = transformed
.listen(emittedValues.add, onError: errors.add, onDone: () {
isDone = true;
});
}
group('audit', () {
setUp(() async {
setUpStreams(audit(const Duration(milliseconds: 5)));
});
test('cancels values', () async {
await subscription.cancel();
expect(valuesCanceled, true);
});
test('swallows values that come faster than duration', () async {
values.add(1);
values.add(2);
await values.close();
await waitForTimer(5);
expect(emittedValues, [2]);
});
test('outputs multiple values spaced further than duration', () async {
values.add(1);
await waitForTimer(5);
values.add(2);
await waitForTimer(5);
expect(emittedValues, [1, 2]);
});
test('waits for pending value to close', () async {
values.add(1);
await values.close();
expect(isDone, false);
await waitForTimer(5);
expect(isDone, true);
});
test('closes output if there are no pending values', () async {
values.add(1);
await waitForTimer(5);
values.add(2);
await values.close();
expect(isDone, false);
await waitForTimer(5);
expect(isDone, true);
});
test('does not starve output if many values come closer than duration',
() async {
values.add(1);
await new Future.delayed(const Duration(milliseconds: 3));
values.add(2);
await new Future.delayed(const Duration(milliseconds: 3));
values.add(3);
await waitForTimer(5);
expect(emittedValues, [2, 3]);
});
if (streamType == 'broadcast') {
test('multiple listeners all get values', () async {
var otherValues = [];
transformed.listen(otherValues.add);
values.add(1);
values.add(2);
await waitForTimer(5);
expect(emittedValues, [2]);
expect(otherValues, [2]);
});
}
});
});
}
}