Always close test HTTP server during cleanup (#2261)

## 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
This commit is contained in:
shreyas-goenka 2025-01-29 21:24:33 +05:30 committed by GitHub
parent ce965b22b2
commit 55c03cc119
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 3 additions and 14 deletions

View File

@ -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"), " ")

View File

@ -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"
@ -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{

View File

@ -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,
@ -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)