mirror of
https://github.com/go-delve/delve.git
synced 2025-11-02 21:40:22 +08:00
service/dap: add test coverage for Stop() (#2678)
Co-authored-by: Polina Sokolova <polinasok@users.noreply.github.com>
This commit is contained in:
@ -54,21 +54,27 @@ func runTestBuildFlags(t *testing.T, name string, test func(c *daptest.Client, f
|
||||
fixture := protest.BuildFixture(name, buildFlags)
|
||||
|
||||
// Start the DAP server.
|
||||
client := startDapServer(t)
|
||||
// client.Close will close the client connectinon, which will cause a connection error
|
||||
// on the server side and signal disconnect to unblock Stop() above.
|
||||
serverStopped := make(chan struct{})
|
||||
client := startDapServerWithClient(t, serverStopped)
|
||||
defer client.Close()
|
||||
|
||||
test(client, fixture)
|
||||
<-serverStopped
|
||||
}
|
||||
|
||||
func startDapServer(t *testing.T) *daptest.Client {
|
||||
func startDapServerWithClient(t *testing.T, serverStopped chan struct{}) *daptest.Client {
|
||||
listener, _ := startDapServer(t, serverStopped)
|
||||
client := daptest.NewClient(listener.Addr().String())
|
||||
return client
|
||||
}
|
||||
|
||||
func startDapServer(t *testing.T, serverStopped chan struct{}) (listener net.Listener, disconnectChan chan struct{}) {
|
||||
// Start the DAP server.
|
||||
listener, err := net.Listen("tcp", ":0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
disconnectChan := make(chan struct{})
|
||||
disconnectChan = make(chan struct{})
|
||||
server := NewServer(&service.Config{
|
||||
Listener: listener,
|
||||
DisconnectChan: disconnectChan,
|
||||
@ -81,12 +87,51 @@ func startDapServer(t *testing.T) *daptest.Client {
|
||||
// This helps us test that certain events cause the server to stop as
|
||||
// expected.
|
||||
go func() {
|
||||
defer func() {
|
||||
if serverStopped != nil {
|
||||
close(serverStopped)
|
||||
}
|
||||
}()
|
||||
<-disconnectChan
|
||||
server.Stop()
|
||||
}()
|
||||
|
||||
return listener, disconnectChan
|
||||
}
|
||||
|
||||
func TestForceQuitNoClient(t *testing.T) {
|
||||
serverStopped := make(chan struct{})
|
||||
_, disconnectChan := startDapServer(t, serverStopped)
|
||||
close(disconnectChan) // trigger server.Stop()
|
||||
<-serverStopped
|
||||
}
|
||||
|
||||
func TestForceQuitNoTarget(t *testing.T) {
|
||||
serverStopped := make(chan struct{})
|
||||
listener, disconnectChan := startDapServer(t, serverStopped)
|
||||
client := daptest.NewClient(listener.Addr().String())
|
||||
return client
|
||||
defer client.Close()
|
||||
|
||||
client.InitializeRequest()
|
||||
client.ExpectInitializeResponseAndCapabilities(t)
|
||||
close(disconnectChan)
|
||||
<-serverStopped
|
||||
}
|
||||
|
||||
func TestForceQuitWithTarget(t *testing.T) {
|
||||
serverStopped := make(chan struct{})
|
||||
listener, disconnectChan := startDapServer(t, serverStopped)
|
||||
client := daptest.NewClient(listener.Addr().String())
|
||||
defer client.Close()
|
||||
|
||||
client.InitializeRequest()
|
||||
client.ExpectInitializeResponseAndCapabilities(t)
|
||||
fixture := protest.BuildFixture("increment", protest.AllNonOptimized)
|
||||
client.LaunchRequest("exec", fixture.Path, stopOnEntry)
|
||||
client.ExpectInitializedEvent(t)
|
||||
client.ExpectLaunchResponse(t)
|
||||
close(disconnectChan)
|
||||
<-serverStopped
|
||||
}
|
||||
|
||||
// TestLaunchStopOnEntry emulates the message exchange that can be observed with
|
||||
@ -4272,8 +4317,9 @@ func TestNoDebug_AcceptNoRequestsButDisconnect(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestLaunchRequestWithRelativeBuildPath(t *testing.T) {
|
||||
client := startDapServer(t)
|
||||
defer client.Close() // will trigger Stop()
|
||||
serverStopped := make(chan struct{})
|
||||
client := startDapServerWithClient(t, serverStopped)
|
||||
defer client.Close()
|
||||
|
||||
fixdir := protest.FindFixturesDir()
|
||||
if filepath.IsAbs(fixdir) {
|
||||
@ -4288,6 +4334,7 @@ func TestLaunchRequestWithRelativeBuildPath(t *testing.T) {
|
||||
client.LaunchRequestWithArgs(map[string]interface{}{
|
||||
"mode": "debug", "program": program, "cwd": filepath.Dir(dlvwd)})
|
||||
})
|
||||
<-serverStopped
|
||||
}
|
||||
|
||||
func TestLaunchRequestWithRelativeExecPath(t *testing.T) {
|
||||
@ -4306,14 +4353,16 @@ func TestLaunchRequestWithRelativeExecPath(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestLaunchTestRequest(t *testing.T) {
|
||||
client := startDapServer(t)
|
||||
defer client.Close() // will trigger Stop()
|
||||
serverStopped := make(chan struct{})
|
||||
client := startDapServerWithClient(t, serverStopped)
|
||||
defer client.Close()
|
||||
runDebugSession(t, client, "launch", func() {
|
||||
fixtures := protest.FindFixturesDir()
|
||||
testdir, _ := filepath.Abs(filepath.Join(fixtures, "buildtest"))
|
||||
client.LaunchRequestWithArgs(map[string]interface{}{
|
||||
"mode": "test", "program": testdir, "output": "__mytestdir"})
|
||||
})
|
||||
<-serverStopped
|
||||
}
|
||||
|
||||
// Tests that 'args' from LaunchRequest are parsed and passed to the target
|
||||
@ -4469,6 +4518,9 @@ func TestUnupportedCommandResponses(t *testing.T) {
|
||||
|
||||
client.ModulesRequest()
|
||||
expectUnsupportedCommand("modules")
|
||||
|
||||
client.DisconnectRequest()
|
||||
client.ExpectDisconnectResponse(t)
|
||||
})
|
||||
}
|
||||
|
||||
@ -4873,6 +4925,9 @@ func TestOptionalNotYetImplementedResponses(t *testing.T) {
|
||||
|
||||
client.CancelRequest()
|
||||
expectNotYetImplemented("cancel")
|
||||
|
||||
client.DisconnectRequest()
|
||||
client.ExpectDisconnectResponse(t)
|
||||
})
|
||||
}
|
||||
|
||||
@ -5184,9 +5239,8 @@ func TestBadInitializeRequest(t *testing.T) {
|
||||
t.Helper()
|
||||
// Only one initialize request is allowed, so use a new server
|
||||
// for each test.
|
||||
client := startDapServer(t)
|
||||
// client.Close will close the client connectinon, which will cause a connection error
|
||||
// on the server side and signal disconnect to unblock Stop() above.
|
||||
serverStopped := make(chan struct{})
|
||||
client := startDapServerWithClient(t, serverStopped)
|
||||
defer client.Close()
|
||||
|
||||
client.InitializeRequestWithArgs(args)
|
||||
@ -5203,6 +5257,10 @@ func TestBadInitializeRequest(t *testing.T) {
|
||||
if response.Body.Error.Format != err {
|
||||
t.Errorf("\ngot %q\nwant %q", response.Body.Error.Format, err)
|
||||
}
|
||||
|
||||
client.DisconnectRequest()
|
||||
client.ExpectDisconnectResponse(t)
|
||||
<-serverStopped
|
||||
}
|
||||
|
||||
// Bad path format.
|
||||
@ -5264,5 +5322,8 @@ func TestBadlyFormattedMessageToServer(t *testing.T) {
|
||||
// Make sure that the unknown request did not kill the server.
|
||||
client.InitializeRequest()
|
||||
client.ExpectInitializeResponse(t)
|
||||
|
||||
client.DisconnectRequest()
|
||||
client.ExpectDisconnectResponse(t)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user