Skip to content

Commit a6729ef

Browse files
committed
test/integration/goDebug: speed up setUpRemoteProgram
Instead of sleeping for fixed 10sec, watch the stdout and look for the server start signature message. Either Delve's start message (if --continue flag is not used) i.e., DAP server listening at: or API server listening at: or the helloWorldServer's log message (if --continue flag is used) helloWorldServer starting to listen on The logic is inspired from the current debug adapter's dlv spawning logic. In most cases, this approach makes tests complete fast. If server takes more than 10sec to start up, this approach is more robust as long as the server setup can be done within hard 30sec limit. Change-Id: Ie880bdf50cb33e75873e2d8fb4e92ca9c53e633f Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/418896 Run-TryBot: Hyang-Ah Hana Kim <[email protected]> Reviewed-by: Jamal Carvalho <[email protected]> TryBot-Result: kokoro <[email protected]>
1 parent 78c2fac commit a6729ef

File tree

2 files changed

+41
-10
lines changed

2 files changed

+41
-10
lines changed

test/integration/goDebug.test.ts

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -413,14 +413,45 @@ const testAll = (ctx: Mocha.Context, isDlvDap: boolean, withConsole?: string) =>
413413
if (continueOnStart) {
414414
args.push('--continue');
415415
}
416-
const childProcess = cp.spawn(toolPath, args, {
417-
cwd: serverFolder,
418-
env: { PORT: `${serverPort}`, ...process.env }
419-
});
420416

421-
// Give dlv a few seconds to start.
422-
await new Promise((resolve) => setTimeout(resolve, 10_000));
423-
return childProcess;
417+
const promise = new Promise<cp.ChildProcess>((resolve, reject) => {
418+
const p = cp.spawn(toolPath, args, {
419+
cwd: serverFolder,
420+
env: { PORT: `${serverPort}`, ...process.env }
421+
});
422+
423+
let started = false;
424+
const timeoutToken: NodeJS.Timer = setTimeout(() => {
425+
console.log(`dlv debug server (PID: ${p.pid}) is not responding`);
426+
reject(new Error('timed out while waiting for DAP server to start'));
427+
}, 30_000);
428+
429+
const stopWaitingForServerToStart = () => {
430+
clearTimeout(timeoutToken);
431+
started = true;
432+
resolve(p);
433+
};
434+
435+
if (continueOnStart) {
436+
// wait till helloWorldServer starts and prints its log message to STDERR.
437+
p.stderr.on('data', (chunk) => {
438+
const msg = chunk.toString();
439+
if (!started && msg.includes('helloWorldServer starting to listen on')) {
440+
stopWaitingForServerToStart();
441+
}
442+
console.log(msg);
443+
});
444+
} else {
445+
p.stdout.on('data', (chunk) => {
446+
const msg = chunk.toString();
447+
if (!started && msg.includes('listening at:')) {
448+
stopWaitingForServerToStart();
449+
}
450+
console.log(msg);
451+
});
452+
}
453+
});
454+
return promise;
424455
}
425456

426457
/**

test/testdata/helloWorldServer/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ func main() {
1616
if p := os.Getenv("PORT"); p != "" {
1717
addr = ":" + p
1818
}
19-
20-
log.Printf("server starting to listen on %s", addr)
19+
// NOTE: goDebug.test.ts setupRemoteProgram expects this log message.
20+
log.Printf("helloWorldServer starting to listen on %s", addr)
2121
http.HandleFunc("/", home)
2222
if err := http.ListenAndServe(addr, nil); err != nil {
2323
log.Fatalf("server listen error: %+v", err)
@@ -26,6 +26,6 @@ func main() {
2626

2727
// home logs the received request and returns a simple response.
2828
func home(w http.ResponseWriter, r *http.Request) {
29-
log.Printf("received request: %s %s", r.Method, r.URL.Path)
29+
log.Printf("received request: %s %s", r.Method, r.URL.Path) // Breakpoint
3030
fmt.Fprintf(w, "Hello, world!")
3131
}

0 commit comments

Comments
 (0)