- Notifications
You must be signed in to change notification settings - Fork1.1k
feat: Auto select workspace proxy based on lowest latency#7515
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
| // TestingScreen just mounts some components that we can check in the unit test. | ||
| constTestingScreen=()=>{ | ||
| const{ proxy, userProxy, isFetched, isLoading, clearProxy, setProxy}= | ||
| useProxy() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
You probably don't need this to test a hook. You can use therenderHook from@testing-library/react. I think it can make the test way easier to reason about. More about its usage:https://kentcdodds.com/blog/how-to-test-custom-react-hooks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
This looks like exactly what I was looking for.
In the example they have this:
const{result}=renderHook(()=>useUndo('one'))
The only caveat I have is I also need theuseDashboard provider present until this leaves experimental. So I'd need to souseProxy with the Dashboard context available 🤔
| constselectProxyButton=screen.getByTestId("userSelectProxy") | ||
| awaituser.click(selectProxyButton) | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Nice helpers!
Uh oh!
There was an error while loading.Please reload this page.
site/src/contexts/ProxyContext.tsx Outdated
| // This includes proxies being loaded, latencies being calculated, and the user selecting a proxy. | ||
| useEffect(()=>{ | ||
| setAndSaveProxy(savedProxy) | ||
| },[savedProxy,proxiesResp,proxyLatencies,setAndSaveProxy]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Not sure if I'm getting this correctly but let's say the latency changes while I'm on the page, would the proxy be changed automatically?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Yes it would.
| } | ||
| } | ||
| Object.defineProperty(window,"localStorage",{value:localStorageMock()}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Since this is being executed when the module is imported, maybe can be interesting to move all this code into the test setup file and mock it globally. We are doing that for a few default window interfaces.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
maybe, I just wanted to avoid putting things in the global if my test was the only one using it atm.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I just reviewed the code and left a few comments but nothing that should be considered as a blocker. Let me know if you need a QA.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Just having some CI issues with the test. Seems to be hanging and timing out? |
Kira-Pilot commentedMay 12, 2023 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
I had a little trouble following the flow of logic in the |
Emyrk commentedMay 12, 2023 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Will take a look! The nice thing is if the tests pass, then it's all still good |
Uh oh!
There was an error while loading.Please reload this page.
| // For some reason if I use 'userSavedProxy' here, | ||
| // the tests fail. It does not load "undefined" after a | ||
| // clear, but takes the previous value. | ||
| loadUserSelectedProxy(), |
Kira-PilotMay 15, 2023 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
setStates are async - when you set a value, it isn't set instantly. SouserSavedProxy may not be updated by the time you try to grab it here. That is probably why you're seeing the previous value. On top of that, there's a possibility we might be overwriting what the user is trying to set in localStorage by resetting to an older value onln 110.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I tried adding anawait setUserSaveProxy and still had this behavior.
Ln 110 only loads from local storage, it does not do any writes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
My bad about ln 110 - was confusingsetUserSavedProxy andsaveUserSelectedProxy as mentioned above.
Regarding yourawait: you can't await asetState because, although asynchronous, it doesn't return a promise. You could write a callback but I think what you have here is best. Do you mind removing the comment? I think this is expected behavior.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
We do not need to tackle this in your PR - can totally be future work, but just a heads up that we now have auseLocal hook that might be fun to add here some point down the line. |
Uh oh!
There was an error while loading.Please reload this page.
EDIT: This has been reverted and no longer true
What this does
This changes thedefault behavior of workspace proxies to auto select based on latency, rather than just using the name "primary".
Fixes:#7496
Continuation of:#7486
Testing
I added a suite of tests on a basic TestComponent:
coder/site/src/contexts/ProxyContext.test.tsx
Lines 139 to 165 in1305931
This is so I can test the context independent of any UI/UX and styling.
Extra
The
ProxyContexthas been fully commented:coder/site/src/contexts/ProxyContext.tsx
Lines 16 to 58 in1305931
Extra functionality:
clearProxyto remove a user's selected proxyuserProxyreturns the user's selected proxy. Since their choice might be overriden.