Merge pull request #3710 from jsternberg/dap-tests-race-condition

dap: fix the race condition in the dap unit tests
This commit is contained in:
Tõnis Tiigi
2026-03-10 18:24:55 -07:00
committed by GitHub
4 changed files with 13 additions and 14 deletions
+1 -2
View File
@@ -1,7 +1,6 @@
package commands package commands
import ( import (
"context"
"io" "io"
"net" "net"
"os" "os"
@@ -75,7 +74,7 @@ type adapterProtocolDebugger struct {
} }
func (d *adapterProtocolDebugger) Start(printer *progress.Printer, opts *BuildOptions) error { func (d *adapterProtocolDebugger) Start(printer *progress.Printer, opts *BuildOptions) error {
cfg, err := d.Adapter.Start(context.Background(), d.conn) cfg, err := d.Adapter.Start(d.conn)
if err != nil { if err != nil {
return errors.Wrap(err, "debug adapter did not start") return errors.Wrap(err, "debug adapter did not start")
} }
+3 -3
View File
@@ -67,10 +67,10 @@ func New[C LaunchConfig]() *Adapter[C] {
return d return d
} }
func (d *Adapter[C]) Start(ctx context.Context, conn Conn) (C, error) { func (d *Adapter[C]) Start(conn Conn) (C, error) {
d.eg, _ = errgroup.WithContext(ctx) d.eg, _ = errgroup.WithContext(context.Background())
d.eg.Go(func() error { d.eg.Go(func() error {
return d.srv.Serve(ctx, conn) return d.srv.Serve(conn)
}) })
<-d.initialized <-d.initialized
+7 -7
View File
@@ -24,7 +24,7 @@ func TestLaunch(t *testing.T) {
eg, _ := errgroup.WithContext(ctx) eg, _ := errgroup.WithContext(ctx)
eg.Go(func() error { eg.Go(func() error {
_, err := adapter.Start(ctx, conn) _, err := adapter.Start(conn)
assert.NoError(t, err) assert.NoError(t, err)
return nil return nil
}) })
@@ -57,13 +57,13 @@ func TestLaunch(t *testing.T) {
// We should have received the initialized event. // We should have received the initialized event.
select { select {
case <-initialized: case <-initialized:
default: case <-ctx.Done():
assert.Fail(t, "did not receive initialized event") assert.Fail(t, "did not receive initialized event")
} }
select { select {
case <-configurationDone: case <-configurationDone:
case <-time.After(10 * time.Second): case <-ctx.Done():
assert.Fail(t, "did not receive configurationDone response") assert.Fail(t, "did not receive configurationDone response")
} }
return nil return nil
@@ -82,7 +82,7 @@ func TestSetBreakpoints(t *testing.T) {
eg, _ := errgroup.WithContext(ctx) eg, _ := errgroup.WithContext(ctx)
eg.Go(func() error { eg.Go(func() error {
_, err := adapter.Start(ctx, conn) _, err := adapter.Start(conn)
assert.NoError(t, err) assert.NoError(t, err)
return nil return nil
}) })
@@ -118,7 +118,7 @@ func TestSetBreakpoints(t *testing.T) {
// We should have received the initialized event. // We should have received the initialized event.
select { select {
case <-initialized: case <-initialized:
default: case <-ctx.Done():
assert.Fail(t, "did not receive initialized event") assert.Fail(t, "did not receive initialized event")
} }
@@ -127,7 +127,7 @@ func TestSetBreakpoints(t *testing.T) {
assert.True(t, setBreakpointsResp.Success) assert.True(t, setBreakpointsResp.Success)
assert.Len(t, setBreakpointsResp.Body.Breakpoints, 0) assert.Len(t, setBreakpointsResp.Body.Breakpoints, 0)
assert.NotNil(t, setBreakpointsResp.Body.Breakpoints, "breakpoints should be an empty array instead of null in the JSON") assert.NotNil(t, setBreakpointsResp.Body.Breakpoints, "breakpoints should be an empty array instead of null in the JSON")
case <-time.After(10 * time.Second): case <-ctx.Done():
assert.Fail(t, "did not receive setBreakpoints response") assert.Fail(t, "did not receive setBreakpoints response")
} }
return nil return nil
@@ -249,11 +249,11 @@ func NewTestAdapter[C LaunchConfig](t *testing.T) (*Adapter[C], Conn, *daptest.C
t.Cleanup(func() { clientConn.Close() }) t.Cleanup(func() { clientConn.Close() })
adapter := New[C]() adapter := New[C]()
t.Cleanup(func() { adapter.Stop() })
client := daptest.NewClient(clientConn) client := daptest.NewClient(clientConn)
t.Cleanup(func() { client.Close() }) t.Cleanup(func() { client.Close() })
t.Cleanup(func() { adapter.Stop() })
return adapter, srvConn, client return adapter, srvConn, client
} }
+2 -2
View File
@@ -33,11 +33,11 @@ func NewServer(h Handler) *Server {
return &Server{h: h} return &Server{h: h}
} }
func (s *Server) Serve(ctx context.Context, conn Conn) error { func (s *Server) Serve(conn Conn) error {
writeCh := make(chan dap.Message) writeCh := make(chan dap.Message)
s.ch = writeCh s.ch = writeCh
s.ctx, s.cancel = context.WithCancelCause(ctx) s.ctx, s.cancel = context.WithCancelCause(context.Background())
// Start an error group to handle server-initiated tasks. // Start an error group to handle server-initiated tasks.
s.eg, _ = errgroup.WithContext(s.ctx) s.eg, _ = errgroup.WithContext(s.ctx)