- 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.
Changes from1 commit
159538c1305931d1c9f3fc739f24ae26777523e492877c0c98d266435c534faa3703e1c210e8da358258ec860c33197eb1ebb6a76549093eFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
Lotta comments
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
There are no files selected for viewing
This file was deleted.
Uh oh!
There was an error while loading.Please reload this page.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,192 @@ | ||
| import { | ||
| MockPrimaryWorkspaceProxy, | ||
| MockWorkspaceProxies, | ||
| MockHealthyWildWorkspaceProxy, | ||
| MockUnhealthyWildWorkspaceProxy, | ||
| } from "testHelpers/entities" | ||
| import { | ||
| getPreferredProxy, | ||
| ProxyProvider, | ||
| saveUserSelectedProxy, | ||
| useProxy, | ||
| } from "./ProxyContext" | ||
| import * as ProxyContextModule from "./ProxyContext" | ||
| import { | ||
| renderWithAuth, | ||
| waitForLoaderToBeRemoved, | ||
| } from "testHelpers/renderHelpers" | ||
| import { screen } from "@testing-library/react" | ||
| import { server } from "testHelpers/server" | ||
| import "testHelpers/localstorage" | ||
| import { rest } from "msw" | ||
| import { Region } from "api/typesGenerated" | ||
| describe("ProxyContextGetURLs", () => { | ||
| it.each([ | ||
| ["empty", [], undefined, "", ""], | ||
| // Primary has no path app URL. Uses relative links | ||
| [ | ||
| "primary", | ||
| [MockPrimaryWorkspaceProxy], | ||
| MockPrimaryWorkspaceProxy, | ||
| "", | ||
| MockPrimaryWorkspaceProxy.wildcard_hostname, | ||
| ], | ||
| [ | ||
| "regions selected", | ||
| MockWorkspaceProxies, | ||
| MockHealthyWildWorkspaceProxy, | ||
| MockHealthyWildWorkspaceProxy.path_app_url, | ||
| MockHealthyWildWorkspaceProxy.wildcard_hostname, | ||
| ], | ||
| // Primary is the default if none selected | ||
| [ | ||
| "no selected", | ||
| [MockPrimaryWorkspaceProxy], | ||
| undefined, | ||
| "", | ||
| MockPrimaryWorkspaceProxy.wildcard_hostname, | ||
| ], | ||
| [ | ||
| "regions no select primary default", | ||
| MockWorkspaceProxies, | ||
| undefined, | ||
| "", | ||
| MockPrimaryWorkspaceProxy.wildcard_hostname, | ||
| ], | ||
| // Primary is the default if the selected is unhealthy | ||
| [ | ||
| "unhealthy selection", | ||
| MockWorkspaceProxies, | ||
| MockUnhealthyWildWorkspaceProxy, | ||
| "", | ||
| MockPrimaryWorkspaceProxy.wildcard_hostname, | ||
| ], | ||
| // This should never happen, when there is no primary | ||
| ["no primary", [MockHealthyWildWorkspaceProxy], undefined, "", ""], | ||
| ])( | ||
| `%p`, | ||
| (_, regions, selected, preferredPathAppURL, preferredWildcardHostname) => { | ||
| const preferred = getPreferredProxy(regions, selected) | ||
| expect(preferred.preferredPathAppURL).toBe(preferredPathAppURL) | ||
| expect(preferred.preferredWildcardHostname).toBe( | ||
| preferredWildcardHostname, | ||
| ) | ||
| }, | ||
| ) | ||
| }) | ||
Emyrk marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| // interface ProxySelectTest { | ||
| // name: string | ||
| // actions: () | ||
| // } | ||
| const TestingComponent = () => { | ||
| return renderWithAuth( | ||
| <ProxyProvider> | ||
| <TestingScreen /> | ||
| </ProxyProvider>, | ||
| { | ||
| route: `/proxies`, | ||
| path: "/proxies", | ||
| }, | ||
Emyrk marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| ) | ||
| } | ||
| // TestingScreen just mounts some components that we can check in the unit test. | ||
| const TestingScreen = () => { | ||
| const { proxy, isFetched, isLoading } = useProxy() | ||
| return ( | ||
| <> | ||
| <div data-testid="isFetched" title={isFetched.toString()}></div> | ||
| <div data-testid="isLoading" title={isLoading.toString()}></div> | ||
| <div | ||
| data-testid="preferredProxy" | ||
| title={proxy.selectedProxy && proxy.selectedProxy.id} | ||
| ></div> | ||
| </> | ||
| ) | ||
| } | ||
| interface ProxyContextSelectionTest { | ||
| expSelectedProxyID: string | ||
| regions: Region[] | ||
| storageProxy: Region | undefined | ||
| } | ||
| describe("ProxyContextSelection", () => { | ||
| beforeEach(() => { | ||
| window.localStorage.clear() | ||
| }) | ||
| it.each([ | ||
| [ | ||
| "empty", | ||
| { | ||
| expSelectedProxyID: "", | ||
| regions: [], | ||
| storageProxy: undefined, | ||
| }, | ||
| ], | ||
| [ | ||
| "regions_no_selection", | ||
| { | ||
| expSelectedProxyID: MockPrimaryWorkspaceProxy.id, | ||
| regions: MockWorkspaceProxies, | ||
| storageProxy: undefined, | ||
| }, | ||
| ], | ||
| [ | ||
| "regions_selected_unhealthy", | ||
| { | ||
| expSelectedProxyID: MockPrimaryWorkspaceProxy.id, | ||
| regions: MockWorkspaceProxies, | ||
| storageProxy: MockUnhealthyWildWorkspaceProxy, | ||
| }, | ||
| ], | ||
| ] as [string, ProxyContextSelectionTest][])( | ||
| `%s`, | ||
| async (_, { expSelectedProxyID, regions, storageProxy }) => { | ||
| // Initial selection if present | ||
| if (storageProxy) { | ||
| saveUserSelectedProxy(storageProxy) | ||
| } | ||
| // Mock the API response | ||
| server.use( | ||
| rest.get("/api/v2/regions", async (req, res, ctx) => { | ||
| return res( | ||
| ctx.status(200), | ||
| ctx.json({ | ||
| regions: regions, | ||
| }), | ||
| ) | ||
| }), | ||
| ) | ||
| TestingComponent() | ||
| await waitForLoaderToBeRemoved() | ||
| await screen.findByTestId("isFetched").then((x) => { | ||
| expect(x.title).toBe("true") | ||
| }) | ||
| await screen.findByTestId("isLoading").then((x) => { | ||
| expect(x.title).toBe("false") | ||
| }) | ||
| await screen.findByTestId("preferredProxy").then((x) => { | ||
| expect(x.title).toBe(expSelectedProxyID) | ||
| }) | ||
| // const { proxy, proxies, isFetched, isLoading, proxyLatencies } = useProxy() | ||
| // expect(isLoading).toBe(false) | ||
| // expect(isFetched).toBe(true) | ||
| // expect(x).toBe(2) | ||
| // const preferred = getPreferredProxy(regions, selected) | ||
| // expect(preferred.preferredPathAppURL).toBe(preferredPathAppURL) | ||
| // expect(preferred.preferredWildcardHostname).toBe( | ||
| // preferredWildcardHostname, | ||
| // ) | ||
| }, | ||
| ) | ||
| }) | ||
Uh oh!
There was an error while loading.Please reload this page.