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

fix(usePersistedStateReducer.test): Fix flaky test of push tab retry #2191

Merged
merged 3 commits into from
Aug 22, 2023

Conversation

KaylaBrady
Copy link
Contributor

@KaylaBrady KaylaBrady commented Aug 17, 2023

ticket: https://app.asana.com/0/1148853526253437/1204432665252080/f

I was unable to reproduce the flaky test's failure across 1000+ runs, so it has been tricky confirming if these two changes address the root of the problem, but it seems to me that both of these changes would reduce the likeliness of flaky behavior.

  1. awaiting act- this allows us to remove the use of intermediate state variables & waitFor in the tests by waiting until all side effects are resolved. Act is still a bit mysterious to me, but I referenced this explanation linked from the rtl docs for this approach.
  2. useRef instead of useState in the usePersistedStateReducer so that updates to routeTabsPushRetriesLeft are synchronous. This prevents a possible race conditions between updating routeTabsPushRetriesLeft and dispatching the retry action.

@github-actions
Copy link

Coverage of commit 656984e

Summary coverage rate:
  lines......: 94.8% (2938 of 3100 lines)
  functions..: 74.5% (1221 of 1639 functions)
  branches...: no data found

Files changed coverage rate: n/a

Download coverage report

@KaylaBrady KaylaBrady requested a review from a team August 17, 2023 15:17
@@ -48,32 +48,31 @@ const usePersistedStateReducer = (): [State, Dispatch] => {

const { routeTabsToPush, routeTabsPushInProgress } = state

const [routeTabsPushRetriesLeft, setRouteTabsPushRetriesLeft] =
useState(routeTabsPushRetries)
const routeTabsPushRetriesLeft = useRef(routeTabsPushRetries)
Copy link
Member

Choose a reason for hiding this comment

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

question: I'm wondering if it would even be possible to simply use a local variable in usePersistedStateReducer rather than a ref, though I haven't thought it all the way through yet and my naive attempt at doing it locally isn't working. This logic is really hard to reason about and I'm hoping there's some way to simplify it a bit further but it might not be possible while fully obeying all of the rules of hooks and everything.

Copy link
Contributor Author

@KaylaBrady KaylaBrady Aug 18, 2023

Choose a reason for hiding this comment

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

I haven't been able to get it working using a local variable either - doing some logging in the useEffect, it seems that as a local variable, the value never is decremented. My hunch is that it has do with the local variable being reset when the state returned by useReducer changes and forces a re-render, whereas useRef will persist across re-renders

…instead of state

Prevents possible race conditions with between asynchronously updating the remaining retries count state & updating the tab state from dispatching actions
@github-actions
Copy link

Coverage of commit e039e78

Summary coverage rate:
  lines......: 94.8% (2938 of 3100 lines)
  functions..: 74.5% (1221 of 1639 functions)
  branches...: no data found

Files changed coverage rate: n/a

Download coverage report

@KaylaBrady KaylaBrady merged commit dddf906 into master Aug 22, 2023
8 checks passed
@KaylaBrady KaylaBrady deleted the kb-test-fix branch August 22, 2023 17:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants