Skip to content

Commit

Permalink
Always close test HTTP server during cleanup (#2261)
Browse files Browse the repository at this point in the history
## Changes
This PR registers the `server.Close()` function to be run during test
cleanup in the server initialization function. This ensures that all
test servers are closed as soon as the test they are scoped to finish.

Motivated by https://github.com/databricks/cli/pull/2255/files where a
regression was introduced where we did not close the test server.

## Tests
N/A
  • Loading branch information
shreyas-goenka authored Jan 29, 2025
1 parent ce965b2 commit 55c03cc
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 14 deletions.
3 changes: 2 additions & 1 deletion acceptance/cmd_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import (
)

func StartCmdServer(t *testing.T) *testserver.Server {
server := StartServer(t)
server := testserver.New(t)

server.Handle("/", func(r *http.Request) (any, error) {
q := r.URL.Query()
args := strings.Split(q.Get("args"), " ")
Expand Down
9 changes: 0 additions & 9 deletions acceptance/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package acceptance_test

import (
"net/http"
"testing"

"github.com/databricks/cli/libs/testserver"
"github.com/databricks/databricks-sdk-go/service/catalog"
Expand All @@ -11,14 +10,6 @@ import (
"github.com/databricks/databricks-sdk-go/service/workspace"
)

func StartServer(t *testing.T) *testserver.Server {
server := testserver.New(t)
t.Cleanup(func() {
server.Close()
})
return server
}

func AddHandlers(server *testserver.Server) {
server.Handle("GET /api/2.0/policies/clusters/list", func(r *http.Request) (any, error) {
return compute.ListPoliciesResponse{
Expand Down
5 changes: 1 addition & 4 deletions libs/testserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Server struct {
func New(t testutil.TestingT) *Server {
mux := http.NewServeMux()
server := httptest.NewServer(mux)
t.Cleanup(server.Close)

return &Server{
Server: server,
Expand All @@ -28,10 +29,6 @@ func New(t testutil.TestingT) *Server {

type HandlerFunc func(req *http.Request) (resp any, err error)

func (s *Server) Close() {
s.Server.Close()
}

func (s *Server) Handle(pattern string, handler HandlerFunc) {
s.Mux.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) {
resp, err := handler(r)
Expand Down

0 comments on commit 55c03cc

Please sign in to comment.