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

HY-4957 Alert devs about batchedRedraw race condition, and log if it happens in prod. ⚠️ #72

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/src/component_declaration/flux_component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
library over_react.component_declaration.flux_component;

import 'dart:async';
import 'package:meta/meta.dart';
import 'package:w_flux/w_flux.dart';
import './annotations.dart' as annotations;
import './transformer_helpers.dart';
Expand Down Expand Up @@ -88,6 +89,9 @@ abstract class _FluxComponentMixin<TProps extends FluxUiProps> implements Batche
///
/// These subscriptions are canceled when the component is unmounted.
List<StreamSubscription> _subscriptions = [];
@visibleForTesting
List<StreamSubscription> get subscriptions =>_subscriptions;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm open to better ways to check the subscriptions in unit tests.



void componentWillMount() {
/// Subscribe to all applicable stores.
Expand All @@ -101,8 +105,10 @@ abstract class _FluxComponentMixin<TProps extends FluxUiProps> implements Batche
value: (_) => (_) => redraw())..addAll(getStoreHandlers());

handlers.forEach((store, handler) {
StreamSubscription subscription = store.listen(handler);
_subscriptions.add(subscription);
if(!store.isDisposedOrDisposing) {
StreamSubscription subscription = store.listen(handler);
_subscriptions.add(subscription);
}
});
}

Expand Down
12 changes: 12 additions & 0 deletions test/over_react/component_declaration/flux_component_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ void main() {
reason: 'component should no longer be listening after unmount');
});

test('does not subscribe to a store that is disposed or disposing', () async {
var store = new TestStore();
var renderedInstance = render(TestDefault()..store = store);
TestDefaultComponent component = getDartComponent(renderedInstance);
expect(component.subscriptions.length, 1);

await store.dispose();
renderedInstance = render(TestDefault()..store = store);
component = getDartComponent(renderedInstance);
expect(component.subscriptions.length, 0);
Copy link

@stephenbush-wf stephenbush-wf Jun 8, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively (or supplementarily), this could test that rendering does not cause an exception (when the store is listened to while disposing). Technically, that tests the failure case that we're addressing, but it also asserts the expected behavior in a weird way that is dependent on behavior from another repo. But it also wouldn't require the subscriptions list to be exposed.

🤷‍♂️

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point; that could be done like so:

test('does not subscribe to a store that is disposed or disposing', () async {
  await store.dispose();
  expect(() {
    render(TestDefault()..store = store);
  }, returnsNormally);
});

});

test('subscribes to any stores returned in redrawOn', () async {
var stores = new TestStores();
var renderedInstance = render(TestRedrawOn()..store = stores);
Expand Down