-
Notifications
You must be signed in to change notification settings - Fork 888
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
// TestingScreen just mounts some components that we can check in the unit test. | ||
const TestingScreen = () => { | ||
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 the renderHook
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 the useDashboard
provider present until this leaves experimental. So I'd need to so useProxy
with the Dashboard context available 🤔
const selectProxyButton = screen.getByTestId("userSelectProxy") | ||
await user.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!
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.
Just having some CI issues with the test. Seems to be hanging and timing out? |
I had a little trouble following the flow of logic in the
|
Will take a look! The nice thing is if the tests pass, then it's all still good |
// 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(), |
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.
setState
s are async - when you set a value, it isn't set instantly. So userSavedProxy
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 on ln 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 an await 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 confusing setUserSavedProxy
and saveUserSelectedProxy
as mentioned above.
Regarding your await
: you can't await a setState
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.
We do not need to tackle this in your PR - can totally be future work, but just a heads up that we now have a useLocal hook that might be fun to add here some point down the line. |
EDIT: This has been reverted and no longer true
What this does
This changes the default 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 in 1305931
This is so I can test the context independent of any UI/UX and styling.
Extra
The
ProxyContext
has been fully commented:coder/site/src/contexts/ProxyContext.tsx
Lines 16 to 58 in 1305931
Extra functionality:
clearProxy
to remove a user's selected proxyuserProxy
returns the user's selected proxy. Since their choice might be overriden.