Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finalize callbacks firing order makes testing impossible #4931

Closed
gravityctrl opened this issue Jul 25, 2019 · 2 comments
Closed

Finalize callbacks firing order makes testing impossible #4931

gravityctrl opened this issue Jul 25, 2019 · 2 comments

Comments

@gravityctrl
Copy link

Bug Report

Current Behavior
When piping multiple finalize actions into an observable, the order of the finalize calls is reversed and does not occur before the completion step. This makes the finalize step untestable because there is no correct place to call the done method in jasmine or jest.

Reproduction
Execute the following test (typescript):

it('does not call finalize in correct order', () => {
  of({})
    .pipe(
      finalize(() => console.info('finalize 1')),
      finalize(() => console.info('finalize 2'))
    )
    .pipe(
      finalize(() => console.info('finalize 3')),
      finalize(() => console.info('finalize 4'))
    )
    .subscribe(
      () => console.info('onNext'),
      () => console.info('onError'),
      () => console.info('onComplete')
    );
});

This is the console output:

onNext
onComplete
finalize 4
finalize 3
finalize 2
finalize 1

Expected behavior
Optimally the call order should mirror this console output:

onNext
finalize 1
finalize 2
finalize 3
finalize 4
onComplete

If it is intended, that the finalize calls are happening after the completion, this would be the alternative console output:

onNext
onComplete
finalize 1
finalize 2
finalize 3
finalize 4

Reasoning

  • If there is a finalize step to be tested, the test will always finish before the finalize code branch was executed. There seems to be no way to prevent this.
  • Application logic might depend on the correct execution order the finalize steps. Especially if observables are handed through different layers.

Environment

  • Runtime: Angular v8.0.1, Chrome v75.0.3770.142]
  • RxJS version: v6.5.2
@cartant
Copy link
Collaborator

cartant commented Jul 25, 2019

Callbacks passed to finalize are called in the order in which unsubscription occurs and that is the order shown in the initial console log. The order is not incorrect. It might not have been what you expected, but it's not incorrect. In any case, the behaviour cannot be changed, as doing so could break existing code.

@kwonoj
Copy link
Member

kwonoj commented Feb 2, 2020

Application logic might depend on the correct execution order the finalize steps

finalize operator is not an operator designed to provide gauranteed order across multiple subsequent finalize operator chain. It only guarantees 1. called regardless of observable error / completes so some of teardown could be handled in there. 2. called after any complete / error observercallback. Using multiple finalize in a single operator chain is up to consumer, but that's not rxjs guarantees.

@kwonoj kwonoj closed this as completed Feb 2, 2020
@lock lock bot locked as resolved and limited conversation to collaborators Mar 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants