Skip to content

Commit 7926f94

Browse files
author
Michael Winberry
committed
#1570. Fix tunnels.go by removing the redundant makeTunnels() method and fixed the response status for ConnectTunnel. Update test/ui connect_doom to properly handle waiting for asynchronous click acttions for connect and disconnect.
1 parent 0374802 commit 7926f94

File tree

2 files changed

+8
-20
lines changed

2 files changed

+8
-20
lines changed

src/internal/api/tunnels/tunnels.go

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,12 @@ import (
1414
"github.com/go-chi/chi/v5"
1515
)
1616

17-
var tunnels map[string]*cluster.Tunnel
17+
// tunnels is a map of package names to tunnel objects used for storing connected tunnels
18+
var tunnels map[string]*cluster.Tunnel = make(map[string]*cluster.Tunnel)
1819

1920
// ListTunnels lists all tunnel names
2021
func ListTunnels(w http.ResponseWriter, _ *http.Request) {
2122
// make sure tunnels is initialized
22-
makeTunnels()
23-
2423
// get the tunnel names
2524
tunnelNames := make([]string, 0, len(tunnels))
2625
for name := range tunnels {
@@ -34,13 +33,10 @@ func ListTunnels(w http.ResponseWriter, _ *http.Request) {
3433
func ConnectTunnel(w http.ResponseWriter, r *http.Request) {
3534
name := chi.URLParam(r, "name")
3635

37-
// make sure tunnels is initialized
38-
makeTunnels()
39-
4036
// if the tunnel already exists, just launch the URL
4137
if tunnels[name] != nil {
4238
launchTunnelURL(tunnels[name], w, name)
43-
common.WriteJSONResponse(w, true, http.StatusCreated)
39+
common.WriteJSONResponse(w, true, http.StatusOK)
4440
return
4541
}
4642

@@ -60,7 +56,7 @@ func ConnectTunnel(w http.ResponseWriter, r *http.Request) {
6056
tunnels[name] = tunnel
6157
launchTunnelURL(tunnel, w, name)
6258

63-
common.WriteJSONResponse(w, true, http.StatusOK)
59+
common.WriteJSONResponse(w, true, http.StatusCreated)
6460
}
6561

6662
// DisconnectTunnel closes the tunnel for the requested resource
@@ -72,13 +68,6 @@ func DisconnectTunnel(w http.ResponseWriter, r *http.Request) {
7268
common.WriteJSONResponse(w, true, http.StatusOK)
7369
}
7470

75-
// makeTunnels initializes the tunnels map if it is nil
76-
func makeTunnels() {
77-
if tunnels == nil {
78-
tunnels = make(map[string]*cluster.Tunnel)
79-
}
80-
}
81-
8271
// launchTunnelURL launches the tunnel URL in the default browser
8372
func launchTunnelURL(tunnel *cluster.Tunnel, w http.ResponseWriter, name string) {
8473
if err := exec.LaunchURL(tunnel.FullURL()); err != nil {

src/test/ui/04_connect_doom.spec.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,13 @@ test.describe.serial('connect the dos-games package @connect', async () => {
2626
const connectButton = connectDialog.locator('button:has-text("Connect")');
2727

2828
// Click the Connect Button
29-
await connectButton.click();
30-
await page.waitForResponse('api/tunnels/connect/doom');
29+
await Promise.all([page.waitForResponse('api/tunnels/connect/doom'), connectButton.click()]);
3130

3231
menu = await openDosGamesMenu(page);
3332
expect(await menu.textContent()).toContain('Disconnect...');
3433
});
3534

36-
test('disconnect the dos-games package', async ({page}) => {
35+
test('disconnect the dos-games package', async ({ page }) => {
3736
// Dispose context once it's no longer needed.
3837
let menu = await openDosGamesMenu(page);
3938

@@ -48,9 +47,9 @@ test.describe.serial('connect the dos-games package @connect', async () => {
4847
const dialog = page.locator('.dialog-open');
4948
expect(await dialog.textContent()).toContain('Disconnect Resource');
5049
const disconnectButton = dialog.locator('.button-label:text-is("Disconnect")');
51-
50+
5251
// Click the Disconnect Button
53-
await disconnectButton.click();
52+
await Promise.all([page.waitForResponse('api/tunnels/disconnect/doom'), disconnectButton.click()])
5453

5554
// Ensure the menu no longer contains the Disconnect option
5655
menu = await openDosGamesMenu(page);

0 commit comments

Comments
 (0)