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

add test + fix for chained async has many #7691

Merged
merged 5 commits into from
Dec 15, 2021

Conversation

sly7-7
Copy link
Contributor

@sly7-7 sly7-7 commented Aug 31, 2021

cc/ @Turbo87 Could you confirm your scenario is the same as this one ?

resolves #7684 (at least the mentioned test suite passes locally)

@@ -848,7 +848,7 @@ module('integration/relationships/has_many - Has-Many Relationships', function (
});
});

test('A hasMany relationship can be reloaded if it was fetched via a link', function (assert) {
test('A hasMany relationship can be reloaded if it was fetched via a link', async function (assert) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry for the noise, this one was just a refactor, getting rid of run().

Copy link
Member

Choose a reason for hiding this comment

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

No apology necessary, thanks a bunch for the cleanup?

@sly7-7
Copy link
Contributor Author

sly7-7 commented Aug 31, 2021

@Turbo87 At least the "fix" seem to make crates.io's tests happy ;)

Capture d’écran 2021-08-31 à 11 19 50

@sly7-7 sly7-7 changed the title add test on chained async has many (should reproduces #7684) add test + fix for chained async has many Aug 31, 2021
@igorT igorT added 🏷️ bug This PR primarily fixes a reported issue 🎯 beta PR should be backported to beta 🎯 canary PR is targeting canary (default) 🎯 release PR should be backported to release labels Aug 31, 2021
let adapter = store.adapterFor('application');

store.modelFor('user').reopen({
messages: hasMany('message', { polymorphic: true, async: true }),
Copy link
Member

Choose a reason for hiding this comment

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

why do we use polymorphic: true here? at least in crates.io we don't seem to have any polymorphic relationships 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

just because this is how the relation is defined at the top of the file. (

messages: hasMany('message', { polymorphic: true, async: false }),
)

I wanted to use the 'same' relation, but async.

igorT added a commit that referenced this pull request Sep 3, 2021
@igorT igorT mentioned this pull request Sep 3, 2021
igorT added a commit that referenced this pull request Sep 3, 2021
igorT added a commit that referenced this pull request Sep 3, 2021
@sly7-7 sly7-7 closed this Sep 3, 2021
@sly7-7 sly7-7 reopened this Sep 3, 2021
igorT added a commit to sly7-7/data that referenced this pull request Sep 3, 2021
@igorT
Copy link
Member

igorT commented Sep 3, 2021

@runspired this looks like the correct fix to me, as if you push the same record twice, the deleted branch of code marks the link as not stale so it does not get fetched, even though it had never been fetched. What are your thoughts

@sly7-7
Copy link
Contributor Author

sly7-7 commented Sep 3, 2021

@igorT Thanks again for looking into it. I've added an assertion to complete the tests (after @runspired pointed me that the state of the hasMany's promise looks not good).
It turns out, that without this "fix" the new assertion pass, but the relationship is not fetched, and with the "fix", the relationship is fetched, but the promise state looks wrong :(

sly7-7 pushed a commit to sly7-7/data that referenced this pull request Sep 7, 2021
@sly7-7
Copy link
Contributor Author

sly7-7 commented Sep 8, 2021

As I failed to make the test pass, I tried to make more debugging, to see what happen. I think there are subtle bugs on how the hasMany() computed property is cached/invalidated. For example, considering this code at the end of the added test in rest-adapter-test:

let commentsPromiseArray = post.comments;
let comments = await commentsPromiseArray;
assert.ok(commentsPromiseArray.isFulfilled, 'referenced commentsPromiseArray variable is fulfilled');
assert.equal(post.comments, commentsPromiseArray, 'promise arrays are the same');
assert.ok(commentsPromiseArray.isFulfilled, 'referenced commentsPromiseArray variable is still fulfilled');
assert.ok(post.comments.isFulfilled, 'post.comments promise is fulfilled');
 assert.equal(comments.length, 3, 'The correct records are in the array');

and the "fix" (don't set isStale to false), gives this result:

Capture d’écran 2021-09-08 à 17 44 57

See how the PromiseManyArray state is reset to not fulfilled just because we call again the post.comments cp.
I think it's because awaiting the promise invalidate the cp cache for the hasMany. In the end internalModel.getHasMany() is called, which call PromiseManyArray._update() which cause its flag to be reset. (Though, I think the underlying adapter.fetchHasMany won't be called a second time, which is expected)

cc/ @igorT

@Turbo87
Copy link
Member

Turbo87 commented Sep 24, 2021

@igorT any news on this? I've just hit the same issue in another project too 😅

@benedikt
Copy link
Contributor

We've ran into the issue as well and rolled back to 3.27 for now. If there's anything I can do to help with fixing this, please let me know.

@sly7-7
Copy link
Contributor Author

sly7-7 commented Sep 29, 2021

@benedikt Same here about sticking with 3.27.

If there's anything I can do to help with fixing this, please let me know.

I would say, if you find how to make the tests pass in this PR, it could be a potential fix for the issue. I really don't know what to do :(

});

let comments = await post.get('comments');
assert.ok(post.comments.isFulfilled, 'comments promise is fulfilled');
Copy link
Contributor

@benedikt benedikt Sep 30, 2021

Choose a reason for hiding this comment

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

It looks like post.comments will trigger an update of the promise proxy. That's why this assertion fails. Removing that proxy update fixes this particular test, but obviously breaks a few others.

I guess the question is what we expect to get from consecutive post.comments calls. Are they supposed to return the cached results or do they all trigger a new fetch?

As the relationship is marked as async, I guess the behavior is correct? 🤔

Copy link
Contributor Author

@sly7-7 sly7-7 Sep 30, 2021

Choose a reason for hiding this comment

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

As I said in a previous comment I don't know how consecutive calls of post.comments should behave :(. And it looks like that calling post.comments won't always trig the update you mentioned (since DS.hasMany() is a computed property).

Copy link
Contributor

Choose a reason for hiding this comment

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

I should have read all the comments before diving into the code 😬 🙈

@benedikt
Copy link
Contributor

Looks like the computed property gets invalidated while loading the data for the first time in the replace related record operation which eventually calls notifyPropertyChange.

The first call loads the data, which changes the state, which invalidates the property. The second time (probably?) also loads the data, but the state doesn't change (because it's the same data) and therefore doesn't invalidate the property.

I don't have a solution, but hopefully that explains why the post.comments call sometimes returns a new PromiseManyArray and sometimes not.

@esbanarango
Copy link
Contributor

👋. We're also blocked by this one. Back to 3.27. Thank you @sly7-7 and @igorT for putting the time to fix this 🙏🙏.

Copy link
Contributor

@runspired runspired left a comment

Choose a reason for hiding this comment

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

I'm good with the proposed deletion of the isStale = false state update branch

@sly7-7
Copy link
Contributor Author

sly7-7 commented Oct 14, 2021

@runspired In that case I must remove these checks https://github.com/emberjs/data/pull/7691/files#diff-e129eb0fe00707712a6ceb0f1fc5abbd5b0a90fdd95281f0923d8904ff39abd8R1335 from the tests, because they fail
Capture d’écran 2021-10-14 à 08 31 18
I'm really not sure what it implies for consuming apps, for example, when using this promise state flag in template. I think it will be wrongly false in some cases.

@runspired
Copy link
Contributor

@sly7-7 I don't think you need to remove the checks. Does need to be investigated as to why it fails, seems the promiseArray is unstable which it likely shouldn't be.

@sly7-7
Copy link
Contributor Author

sly7-7 commented Dec 13, 2021

@snewcomer Well, for me it's complicated. I think async relationships are somewhat broken since 3.28 (you can see #7684 and also issues linked to this one). With this change, the issue seemed to be fixed, but at the same time it surfaces an other one, where the state of the underlying promise is unstable (eg, calling twice post.comments.isFulfilled, gives different results). And for this, I don't know what should be done. (see #7691 (comment)).

@runspired runspired added 🎯 lts The PR should be backported to the most recent LTS bugfix-3-28 labels Dec 15, 2021
@runspired runspired merged commit 6ffdc85 into emberjs:master Dec 15, 2021
runspired added a commit that referenced this pull request Dec 15, 2021
* [bugfix]: fix for chained async has many

* add fix and update tests

* remove console.logs

* make work with flags off

* fix test for lts

Co-authored-by: Chris Thoburn <[email protected]>
runspired added a commit that referenced this pull request Dec 15, 2021
* deactivate broken partner tests

* feat: autotracking for reference id access (#7796)

* feat: autotracking for reference id access

* ensure references are torn down

* fix build

* add dep

* add to deps

* fix invalid json:api support and add valid json:api support

* autotracking tests and cleanup

* fix test failure, add comment

* skip tests when feature not available

* update test and fix lid reflection (#7800)

* update test and fix lid reflection

* remove debugger

* fix ff off branch

* add test and fix push of duplicate identifiers to a relationship (#7801)

* add test + fix for chained async has many (#7691)

* [bugfix]: fix for chained async has many

* add fix and update tests

* remove console.logs

* make work with flags off

* fix test for lts

Co-authored-by: Chris Thoburn <[email protected]>

* Fix: assign unknown properties in init after initialization is finished to ensure proper setup timing (#7771)

* Add failing test case which illustrates the createRecord bug

createRecord crashes when a setter which sets an attribute is involved
in the createRecord.

* update test location and add fix

Co-authored-by: Chris Thoburn <[email protected]>

* fix: A(PromiseManyArray) should have no-effect (#7802)

Co-authored-by: Sylvain Mina <[email protected]>
Co-authored-by: Andrey Fel <[email protected]>
runspired added a commit that referenced this pull request Dec 15, 2021
* deactivate broken partner tests

* feat: autotracking for reference id access (#7796)

* feat: autotracking for reference id access

* ensure references are torn down

* fix build

* add dep

* add to deps

* fix invalid json:api support and add valid json:api support

* autotracking tests and cleanup

* fix test failure, add comment

* skip tests when feature not available

* update test and fix lid reflection (#7800)

* update test and fix lid reflection

* remove debugger

* fix ff off branch

* add test and fix push of duplicate identifiers to a relationship (#7801)

* add test + fix for chained async has many (#7691)

* [bugfix]: fix for chained async has many

* add fix and update tests

* remove console.logs

* make work with flags off

* fix test for lts

Co-authored-by: Chris Thoburn <[email protected]>

* Fix: assign unknown properties in init after initialization is finished to ensure proper setup timing (#7771)

* Add failing test case which illustrates the createRecord bug

createRecord crashes when a setter which sets an attribute is involved
in the createRecord.

* update test location and add fix

Co-authored-by: Chris Thoburn <[email protected]>

* fix: A(PromiseManyArray) should have no-effect (#7802)

Co-authored-by: Sylvain Mina <[email protected]>
Co-authored-by: Andrey Fel <[email protected]>
runspired added a commit that referenced this pull request Dec 15, 2021
* deactivate broken partner tests

* feat: autotracking for reference id access (#7796)

* feat: autotracking for reference id access

* ensure references are torn down

* fix build

* add dep

* add to deps

* fix invalid json:api support and add valid json:api support

* autotracking tests and cleanup

* fix test failure, add comment

* skip tests when feature not available

* update test and fix lid reflection (#7800)

* update test and fix lid reflection

* remove debugger

* fix ff off branch

* add test and fix push of duplicate identifiers to a relationship (#7801)

* add test + fix for chained async has many (#7691)

* [bugfix]: fix for chained async has many

* add fix and update tests

* remove console.logs

* make work with flags off

* fix test for lts

Co-authored-by: Chris Thoburn <[email protected]>

* Fix: assign unknown properties in init after initialization is finished to ensure proper setup timing (#7771)

* Add failing test case which illustrates the createRecord bug

createRecord crashes when a setter which sets an attribute is involved
in the createRecord.

* update test location and add fix

Co-authored-by: Chris Thoburn <[email protected]>

* fix: A(PromiseManyArray) should have no-effect (#7802)

Co-authored-by: Sylvain Mina <[email protected]>
Co-authored-by: Andrey Fel <[email protected]>
runspired added a commit that referenced this pull request Dec 15, 2021
* deactivate broken partner tests

* feat: autotracking for reference id access (#7796)

* feat: autotracking for reference id access

* ensure references are torn down

* fix build

* add dep

* add to deps

* fix invalid json:api support and add valid json:api support

* autotracking tests and cleanup

* fix test failure, add comment

* skip tests when feature not available

* update test and fix lid reflection (#7800)

* update test and fix lid reflection

* remove debugger

* fix ff off branch

* add test and fix push of duplicate identifiers to a relationship (#7801)

* add test + fix for chained async has many (#7691)

* [bugfix]: fix for chained async has many

* add fix and update tests

* remove console.logs

* make work with flags off

* fix test for lts

Co-authored-by: Chris Thoburn <[email protected]>

* Fix: assign unknown properties in init after initialization is finished to ensure proper setup timing (#7771)

* Add failing test case which illustrates the createRecord bug

createRecord crashes when a setter which sets an attribute is involved
in the createRecord.

* update test location and add fix

Co-authored-by: Chris Thoburn <[email protected]>

* fix: A(PromiseManyArray) should have no-effect (#7802)

Co-authored-by: Sylvain Mina <[email protected]>
Co-authored-by: Andrey Fel <[email protected]>

Co-authored-by: Sylvain Mina <[email protected]>
Co-authored-by: Andrey Fel <[email protected]>
snewcomer pushed a commit that referenced this pull request Dec 15, 2021
* deactivate broken partner tests

* feat: autotracking for reference id access (#7796)

* feat: autotracking for reference id access

* ensure references are torn down

* fix build

* add dep

* add to deps

* fix invalid json:api support and add valid json:api support

* autotracking tests and cleanup

* fix test failure, add comment

* skip tests when feature not available

* update test and fix lid reflection (#7800)

* update test and fix lid reflection

* remove debugger

* fix ff off branch

* add test and fix push of duplicate identifiers to a relationship (#7801)

* add test + fix for chained async has many (#7691)

* [bugfix]: fix for chained async has many

* add fix and update tests

* remove console.logs

* make work with flags off

* fix test for lts

Co-authored-by: Chris Thoburn <[email protected]>

* Fix: assign unknown properties in init after initialization is finished to ensure proper setup timing (#7771)

* Add failing test case which illustrates the createRecord bug

createRecord crashes when a setter which sets an attribute is involved
in the createRecord.

* update test location and add fix

Co-authored-by: Chris Thoburn <[email protected]>

* fix: A(PromiseManyArray) should have no-effect (#7802)

Co-authored-by: Sylvain Mina <[email protected]>
Co-authored-by: Andrey Fel <[email protected]>
snewcomer pushed a commit that referenced this pull request Dec 15, 2021
* deactivate broken partner tests

* feat: autotracking for reference id access (#7796)

* feat: autotracking for reference id access

* ensure references are torn down

* fix build

* add dep

* add to deps

* fix invalid json:api support and add valid json:api support

* autotracking tests and cleanup

* fix test failure, add comment

* skip tests when feature not available

* update test and fix lid reflection (#7800)

* update test and fix lid reflection

* remove debugger

* fix ff off branch

* add test and fix push of duplicate identifiers to a relationship (#7801)

* add test + fix for chained async has many (#7691)

* [bugfix]: fix for chained async has many

* add fix and update tests

* remove console.logs

* make work with flags off

* fix test for lts

Co-authored-by: Chris Thoburn <[email protected]>

* Fix: assign unknown properties in init after initialization is finished to ensure proper setup timing (#7771)

* Add failing test case which illustrates the createRecord bug

createRecord crashes when a setter which sets an attribute is involved
in the createRecord.

* update test location and add fix

Co-authored-by: Chris Thoburn <[email protected]>

* fix: A(PromiseManyArray) should have no-effect (#7802)

Co-authored-by: Sylvain Mina <[email protected]>
Co-authored-by: Andrey Fel <[email protected]>
snewcomer added a commit that referenced this pull request Dec 15, 2021
* Backport Train for Beta (#7803)

* deactivate broken partner tests

* feat: autotracking for reference id access (#7796)

* feat: autotracking for reference id access

* ensure references are torn down

* fix build

* add dep

* add to deps

* fix invalid json:api support and add valid json:api support

* autotracking tests and cleanup

* fix test failure, add comment

* skip tests when feature not available

* update test and fix lid reflection (#7800)

* update test and fix lid reflection

* remove debugger

* fix ff off branch

* add test and fix push of duplicate identifiers to a relationship (#7801)

* add test + fix for chained async has many (#7691)

* [bugfix]: fix for chained async has many

* add fix and update tests

* remove console.logs

* make work with flags off

* fix test for lts

Co-authored-by: Chris Thoburn <[email protected]>

* Fix: assign unknown properties in init after initialization is finished to ensure proper setup timing (#7771)

* Add failing test case which illustrates the createRecord bug

createRecord crashes when a setter which sets an attribute is involved
in the createRecord.

* update test location and add fix

Co-authored-by: Chris Thoburn <[email protected]>

* fix: A(PromiseManyArray) should have no-effect (#7802)

Co-authored-by: Sylvain Mina <[email protected]>
Co-authored-by: Andrey Fel <[email protected]>

* fix

Co-authored-by: Chris Thoburn <[email protected]>
Co-authored-by: Sylvain Mina <[email protected]>
Co-authored-by: Andrey Fel <[email protected]>
@@ -902,11 +903,18 @@ export default class InternalModel {

notifyHasManyChange(key: string) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you so much @runspired. I would never have guessed the culprit was here. I don't know how you found that. All look so easy for you 😅

snewcomer pushed a commit that referenced this pull request Dec 16, 2021
* [bugfix]: fix for chained async has many

* add fix and update tests

* remove console.logs

* make work with flags off

* fix test for lts

Co-authored-by: Chris Thoburn <[email protected]>
runspired added a commit that referenced this pull request Apr 14, 2022
* deactivate broken partner tests

* feat: autotracking for reference id access (#7796)

* feat: autotracking for reference id access

* ensure references are torn down

* fix build

* add dep

* add to deps

* fix invalid json:api support and add valid json:api support

* autotracking tests and cleanup

* fix test failure, add comment

* skip tests when feature not available

* update test and fix lid reflection (#7800)

* update test and fix lid reflection

* remove debugger

* fix ff off branch

* add test and fix push of duplicate identifiers to a relationship (#7801)

* add test + fix for chained async has many (#7691)

* [bugfix]: fix for chained async has many

* add fix and update tests

* remove console.logs

* make work with flags off

* fix test for lts

Co-authored-by: Chris Thoburn <[email protected]>

* Fix: assign unknown properties in init after initialization is finished to ensure proper setup timing (#7771)

* Add failing test case which illustrates the createRecord bug

createRecord crashes when a setter which sets an attribute is involved
in the createRecord.

* update test location and add fix

Co-authored-by: Chris Thoburn <[email protected]>

* fix: A(PromiseManyArray) should have no-effect (#7802)

Co-authored-by: Sylvain Mina <[email protected]>
Co-authored-by: Andrey Fel <[email protected]>
runspired added a commit that referenced this pull request Apr 14, 2022
* Backport Train for Beta (#7803)

* deactivate broken partner tests

* feat: autotracking for reference id access (#7796)

* feat: autotracking for reference id access

* ensure references are torn down

* fix build

* add dep

* add to deps

* fix invalid json:api support and add valid json:api support

* autotracking tests and cleanup

* fix test failure, add comment

* skip tests when feature not available

* update test and fix lid reflection (#7800)

* update test and fix lid reflection

* remove debugger

* fix ff off branch

* add test and fix push of duplicate identifiers to a relationship (#7801)

* add test + fix for chained async has many (#7691)

* [bugfix]: fix for chained async has many

* add fix and update tests

* remove console.logs

* make work with flags off

* fix test for lts

Co-authored-by: Chris Thoburn <[email protected]>

* Fix: assign unknown properties in init after initialization is finished to ensure proper setup timing (#7771)

* Add failing test case which illustrates the createRecord bug

createRecord crashes when a setter which sets an attribute is involved
in the createRecord.

* update test location and add fix

Co-authored-by: Chris Thoburn <[email protected]>

* fix: A(PromiseManyArray) should have no-effect (#7802)

Co-authored-by: Sylvain Mina <[email protected]>
Co-authored-by: Andrey Fel <[email protected]>

* fix lint

* fix ie11

Co-authored-by: Sylvain Mina <[email protected]>
Co-authored-by: Andrey Fel <[email protected]>
@jrjohnson jrjohnson removed 🎯 beta PR should be backported to beta 🎯 release PR should be backported to release 🎯 lts The PR should be backported to the most recent LTS labels Sep 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🎯 canary PR is targeting canary (default) 🏷️ bug This PR primarily fixes a reported issue
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3.28: Awaiting async relationships does not work anymore
8 participants