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

Test example solutions endpoint & refactor #43470

Merged
merged 5 commits into from
Nov 16, 2021

Conversation

maureensturgeon
Copy link
Contributor

@maureensturgeon maureensturgeon commented Nov 9, 2021

Background: a week ago I merged in updates for loading level examples async but I made a mistake with the endpoint and it didn't load, I merged a fix but didn't add tests at that time. Also having the request as part of the promise.all wasn't the right move because we still want app options to load even if level examples don't load.

  • Add tests for updates made to example solutions endpoint made a week ago
  • Refactor how promises are handled in loadApp

Testing story

Tested locally

PR Checklist:

  • Tests provide adequate coverage
  • Privacy and Security impacts have been assessed
  • Code is well-commented
  • New features are translatable or updates will not break translations
  • Relevant documentation has been added or updated
  • User impact is well-understood and desirable
  • Pull Request is labeled appropriately
  • Follow-up work items (including potential tech debt) are tracked and linked

@maureensturgeon maureensturgeon requested a review from a team November 9, 2021 00:34
Copy link
Contributor

@jamescodeorg jamescodeorg 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 ok with this as-is but left a style-related comment below.

This is going to conflict with #43422 in kind of an annoying way. Would it be ok if I went first?

exampleSolutionsRequest
]);

if (exampleSolutionsResponse.status === 'rejected') {
Copy link
Contributor

Choose a reason for hiding this comment

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

(minor) I feel like this is somehow obscuring the logic a bit (possibly even more so than we just used a .then block). Would it be simpler if we just had two separate await blocks instead of using Promise.allSettled?

Side note: It looks like the handling is currently the same for both requests -- if the requests fails, we silently fail but continue to load the page. Long-term, I think we'll want to show an error to the user if userAppOptionsRequest fails (but maybe not if exampleSolutionsRequest fails), right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If we have two separate awaits then the requests will not run in parallel, each await will wait for the response before continuing. I can try refactoring to use .then, I generally prefer await because I think it's easier to read.

The way I wrote this, if example solutions request fails, we'll do a console log and continue to execute. If app options fails I return the app options that we have, which is how it was handled before if the app options request failed. I agree we should probably re-think what we want to happen if that request fails

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, I thought the requests get initiated (i.e. sent) when $.ajax is called and the await just waits for it to finish? (In general, I agree that await is a more modern pattern and we should favor it. I was just surprised that for some reason this feels harder to read.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

When you use await the next line isn't executed until the the request finishes.

const responseA = await RequestA();
const responseB = await RequestB();
doOtherStuff()

would be the same as

RequestA.then(responseA => {
  RequestB.then(responseB => {
    doOtherStuff()
  })
})

Let me know if that helps, also happy to do a call and discuss this more!

I think the reason this one is harder to read is because instead of getting a direct result from allSettled it returns objects with statuses and results which means we need to check another layer.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Let me see if I can replace allSettled with something that's easier to work with

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, I was thinking more along the lines of leaving everything before line 318 as-is and then replacing the rest of the function with something like:

try {
    exampleSolutions = await exampleSolutionsRequest
} catch (err) {
}
...
try {
    userAppOptions = await userAppOptionsRequest
} catch (err) {
}

(Not at all sure about this, just throwing it out there.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So if it was written like that, the example solutions request would be made and then after it returns and the stuff in between executes, then the request to appOptions would be made. The requests would not be made in parallel.. wait now I'm going to check that statement..

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, even if the result is assigned in a separate try block the execution is still blocked until the promise resolves

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Following up on this after a conversation James and I had in slack. These requests will run in parallel if ajax is called for each before the await statements, I've updated the PR

@maureensturgeon
Copy link
Contributor Author

I'm ok with this as-is but left a style-related comment below.

This is going to conflict with #43422 in kind of an annoying way. Would it be ok if I went first?

Go ahead and merge yours first

@maureensturgeon maureensturgeon merged commit 1832575 into staging Nov 16, 2021
@maureensturgeon maureensturgeon deleted the example-solutions-test-updates branch November 16, 2021 06:13
snickell pushed a commit that referenced this pull request Feb 3, 2024
…dates

Test example solutions endpoint & refactor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants