Skip to content
This repository was archived by the owner on Sep 3, 2025. It is now read-only.

Commit cd37b45

Browse files
committed
test: verify that bundle server started
Update the 'bundleServer.startWebServer()' method to wait for indication that the web server has successfully started (the "Server is running at address..." message) before returning successfully. If the process stops before that, or a timeout of 2s is reached, an error is thrown. However, because the "Server is running..." printout happens in parallel with the setup done in 'ListenAndServe[TLS]()', it is possible that the message is printed and immediately followed with an exit due to a server startup error. To mitigate that, add a 0.1s pause before printing the message and, in the tests, only set the status to 'ok' if the 'close' event has not been triggered by the time the startup message is processed. Signed-off-by: Victoria Dye <vdye@github.com>
1 parent b40169e commit cd37b45

3 files changed

Lines changed: 39 additions & 3 deletions

File tree

cmd/git-bundle-web-server/bundle-server.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,16 @@ func (b *bundleWebServer) StartServerAsync(ctx context.Context) {
185185
}
186186
}(ctx)
187187

188+
// Wait 0.1s before reporting that the server is started in case
189+
// 'listenAndServeFunc' exits immediately.
190+
//
191+
// It's a hack, but a necessary one because 'ListenAndServe[TLS]()' doesn't
192+
// have any mechanism of notifying if it starts successfully, only that it
193+
// fails. We could get around that by copying/reimplementing those functions
194+
// with a print statement inserted at the right place, but that's way more
195+
// cumbersome than just adding a delay here (see:
196+
// https://stackoverflow.com/questions/53332667/how-to-notify-when-http-server-starts-successfully).
197+
time.Sleep(time.Millisecond * 100)
188198
fmt.Println("Server is running at address " + b.server.Addr)
189199
}
190200

test/shared/classes/bundleServer.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,38 @@ export class BundleServer {
2121
this.bundleWebServerCmd = bundleWebServerCmd
2222
}
2323

24-
startWebServer(port: number): void {
24+
async startWebServer(port: number): Promise<void> {
2525
if (this.webServerProcess) {
2626
throw new Error("Tried to start web server, but web server is already running")
2727
}
28-
this.webServerProcess = child_process.spawn(this.bundleWebServerCmd, ["--port", String(port)])
28+
const webServerProcess = child_process.spawn(this.bundleWebServerCmd, ["--port", String(port)])
29+
this.webServerProcess = webServerProcess
2930
this.bundleUriBase = `http://localhost:${port}/`
31+
32+
// Now, ensure the server is running
33+
var timer: NodeJS.Timeout | undefined
34+
var ok: boolean | undefined
35+
36+
await Promise.race([
37+
new Promise<void>(resolve => webServerProcess.stdout.on('data', (data: string) => {
38+
if (data.includes("Server is running at address") && ok === undefined) {
39+
ok = true
40+
resolve() // server is running
41+
}
42+
})),
43+
new Promise<void>(resolve => webServerProcess.on('close', () => {
44+
ok = false
45+
resolve() // program failed to start/exited early
46+
})),
47+
new Promise(resolve => timer = setTimeout(resolve, 2000)) // fallback timeout
48+
])
49+
50+
// If it's still running, clear the timeout so it doesn't delay shutdown
51+
clearTimeout(timer)
52+
53+
if (!ok) {
54+
throw new Error('Failed to start web server')
55+
}
3056
}
3157

3258
init(remote: RemoteRepo, routePrefix: string, route: string = ""): child_process.SpawnSyncReturns<Buffer> {

test/shared/features/step_definitions/bundleServer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ import { Given } from '@cucumber/cucumber'
22
import { BundleServerWorldBase } from '../../support/world'
33

44
Given('the bundle web server was started at port {int}', async function (this: BundleServerWorldBase, port: number) {
5-
this.bundleServer.startWebServer(port)
5+
await this.bundleServer.startWebServer(port)
66
})

0 commit comments

Comments
 (0)