ReactiveX / rxdart

The Reactive Extensions for Dart
http://reactivex.io
Apache License 2.0
3.37k stars 270 forks source link

BehaviorSubject() doesn't provide value with defaultIfEmpty(...) #711

Closed agavrilko closed 1 year ago

agavrilko commented 1 year ago

The dartpad example: https://dartpad.dev/?id=2026d54c6966dc503dbe7b9eb3a148fc

Description

The stream returned from defaultIfEmpty method does not emit value if it was called on BehaviorSubject instance.

Example

An example below fails by timeout, which is not expected. Test should pass.

void main() {
  test(
    'BehaviorSubject() doesnt provide value with defaultIfEmpty(...)',
    () {
      expect(
        BehaviorSubject<int>().defaultIfEmpty(10),
        emitsInOrder(<int>[10]),
      );
    },
    timeout: const Timeout(Duration(seconds: 1)),
  );
}
00:04 +0 -1: BehaviorSubject() doesnt provide value with defaultIfEmpty(...) [E]                                                                                                                                                              
  TimeoutException after 0:00:01.000000: Test timed out after 1 seconds.
  dart:isolate  _RawReceivePortImpl._handleMessage

00:04 +0 -1: Some tests failed. 
agavrilko commented 1 year ago

I checked implementation of the transformer, and I see why it does not behave as I expected. The default element is sent when stream finishes.

  @override
  void close() {
    if (_isEmpty) {
      _outputSink.add(_defaultValue);
    }

    _outputSink.close();
  }

So considering implementation, the behavior is absolutely understandable and reasonable. Closing the issue.