Fix race condition in TestAttachStopOnEntry (#2290)

Co-authored-by: Polina Sokolova <polinasok@users.noreply.github.com>
This commit is contained in:
polinasok 2021-01-06 09:07:47 -08:00 committed by GitHub
parent 6d1c00e56d
commit 3e0ddf2bd1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,6 +1,7 @@
package dap package dap
import ( import (
"bufio"
"flag" "flag"
"io" "io"
"net" "net"
@ -232,13 +233,23 @@ func TestAttachStopOnEntry(t *testing.T) {
} }
runTest(t, "loopprog", func(client *daptest.Client, fixture protest.Fixture) { runTest(t, "loopprog", func(client *daptest.Client, fixture protest.Fixture) {
// Start the program to attach to // Start the program to attach to
// TODO(polina): do I need to sanity check testBackend and runtime.GOOS?
cmd := exec.Command(fixture.Path) cmd := exec.Command(fixture.Path)
cmd.Stdout = os.Stdout stdout, err := cmd.StdoutPipe()
if err != nil {
t.Fatal(err)
}
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
if err := cmd.Start(); err != nil { if err := cmd.Start(); err != nil {
t.Fatal(err) t.Fatal(err)
} }
// Wait for output.
// This will give the target process time to initialize the runtime before we attach,
// so we can rely on having goroutines when they are requested on attach.
scanOut := bufio.NewScanner(stdout)
scanOut.Scan()
if scanOut.Text() != "past main" {
t.Errorf("expected loopprog.go to output \"past main\"")
}
// 1 >> initialize, << initialize // 1 >> initialize, << initialize
client.InitializeRequest() client.InitializeRequest()
@ -247,7 +258,7 @@ func TestAttachStopOnEntry(t *testing.T) {
t.Errorf("\ngot %#v\nwant Seq=0, RequestSeq=1", initResp) t.Errorf("\ngot %#v\nwant Seq=0, RequestSeq=1", initResp)
} }
// 2 >> attach, << initialized, << launch // 2 >> attach, << initialized, << attach
client.AttachRequest( client.AttachRequest(
map[string]interface{}{"mode": "local", "processId": cmd.Process.Pid, "stopOnEntry": true}) map[string]interface{}{"mode": "local", "processId": cmd.Process.Pid, "stopOnEntry": true})
initEvent := client.ExpectInitializedEvent(t) initEvent := client.ExpectInitializedEvent(t)
@ -290,33 +301,22 @@ func TestAttachStopOnEntry(t *testing.T) {
// 6 >> threads, << threads // 6 >> threads, << threads
client.ThreadsRequest() client.ThreadsRequest()
tResp := client.ExpectThreadsResponse(t) tResp := client.ExpectThreadsResponse(t)
if tResp.Seq != 0 || tResp.RequestSeq != 6 || len(tResp.Body.Threads) != 1 { // Expect main goroutine plus runtime at this point.
t.Errorf("\ngot %#v\nwant Seq=0, RequestSeq=6 len(Threads)=1", tResp) if tResp.Seq != 0 || tResp.RequestSeq != 6 || len(tResp.Body.Threads) < 2 {
} t.Errorf("\ngot %#v\nwant Seq=0, RequestSeq=6 len(Threads)>1", tResp)
if tResp.Body.Threads[0].Id != 1 || tResp.Body.Threads[0].Name != "Dummy" {
t.Errorf("\ngot %#v\nwant Id=1, Name=\"Dummy\"", tResp)
} }
// 7 >> threads, << threads // 7 >> threads, << threads
client.ThreadsRequest() client.ThreadsRequest()
tResp = client.ExpectThreadsResponse(t) client.ExpectThreadsResponse(t)
if tResp.Seq != 0 || tResp.RequestSeq != 7 || len(tResp.Body.Threads) != 1 {
t.Errorf("\ngot %#v\nwant Seq=0, RequestSeq=7 len(Threads)=1", tResp)
}
// 8 >> stackTrace, << error // 8 >> stackTrace, << response
client.StackTraceRequest(1, 0, 20) client.StackTraceRequest(1, 0, 20)
stResp := client.ExpectErrorResponse(t) client.ExpectStackTraceResponse(t)
if stResp.Seq != 0 || stResp.RequestSeq != 8 || stResp.Body.Error.Format != "Unable to produce stack trace: unknown goroutine 1" {
t.Errorf("\ngot %#v\nwant Seq=0, RequestSeq=8 Format=\"Unable to produce stack trace: unknown goroutine 1\"", stResp)
}
// 9 >> stackTrace, << error // 9 >> stackTrace, << response
client.StackTraceRequest(1, 0, 20) client.StackTraceRequest(1, 0, 20)
stResp = client.ExpectErrorResponse(t) client.ExpectStackTraceResponse(t)
if stResp.Seq != 0 || stResp.RequestSeq != 9 || stResp.Body.Error.Id != 2004 {
t.Errorf("\ngot %#v\nwant Seq=0, RequestSeq=9 Id=2004", stResp)
}
// 10 >> evaluate, << error // 10 >> evaluate, << error
client.EvaluateRequest("foo", 0 /*no frame specified*/, "repl") client.EvaluateRequest("foo", 0 /*no frame specified*/, "repl")