Skip to content

"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files

httplib: add Close() method to shut the server down and use it in tests

Nick Craig-Wood334bf49d30a7
diff --git a/cmd/serve/http/http_test.go b/cmd/serve/http/http_test.go
index a66511c4a..3909716cb 100644
--- a/cmd/serve/http/http_test.go
+++ b/cmd/serve/http/http_test.go
@@ -21,7 +21,10 @@ import (
"github.com/stretchr/testify/require"
)
-var updateGolden = flag.Bool("updategolden", false, "update golden files for regression test")
+var (
+ updateGolden = flag.Bool("updategolden", false, "update golden files for regression test")
+ httpServer *server
+)
const (
testBindAddress = "localhost:51777"
@@ -31,8 +34,8 @@ const (
func startServer(t *testing.T, f fs.Fs) {
opt := httplib.DefaultOpt
opt.ListenAddr = testBindAddress
- s := newServer(f, &opt)
- go s.serve()
+ httpServer = newServer(f, &opt)
+ go httpServer.serve()
// try to connect to the test server
pause := time.Millisecond
@@ -228,3 +231,7 @@ func TestAddEntry(t *testing.T) {
{remote: "\"quotes\".txt", URL: "%22quotes%22.txt", Leaf: "\"quotes\".txt"},
}, es)
}
+
+func TestFinalise(t *testing.T) {
+ httpServer.srv.Close()
+}
diff --git a/cmd/serve/httplib/http_new.go b/cmd/serve/httplib/http_new.go
index 21cce8832..04f53428f 100644
--- a/cmd/serve/httplib/http_new.go
+++ b/cmd/serve/httplib/http_new.go
@@ -14,3 +14,8 @@ func initServer(s *http.Server) {
s.ReadHeaderTimeout = 10 * time.Second // time to send the headers
s.IdleTimeout = 60 * time.Second // time to keep idle connections open
}
+
+// closeServer closes the server in a non graceful way
+func closeServer(s *http.Server) error {
+ return s.Close()
+}
diff --git a/cmd/serve/httplib/http_old.go b/cmd/serve/httplib/http_old.go
index ddc37a1fd..66c935130 100644
--- a/cmd/serve/httplib/http_old.go
+++ b/cmd/serve/httplib/http_old.go
@@ -11,3 +11,8 @@ import (
// Initialise the http.Server for pre go1.8
func initServer(s *http.Server) {
}
+
+// closeServer closes the server in a non graceful way
+func closeServer(s *http.Server) error {
+ return nil
+}
diff --git a/cmd/serve/httplib/httplib.go b/cmd/serve/httplib/httplib.go
index e2e28ef92..e8d3e29ce 100644
--- a/cmd/serve/httplib/httplib.go
+++ b/cmd/serve/httplib/httplib.go
@@ -183,7 +183,15 @@ func (s *Server) Serve() {
} else {
err = s.httpServer.ListenAndServe()
}
- log.Fatalf("Fatal error while serving HTTP: %v", err)
+ log.Printf("Error while serving HTTP: %v", err)
+}
+
+// Close shuts the running server down
+func (s *Server) Close() {
+ err := closeServer(s.httpServer)
+ if err != nil {
+ log.Printf("Error on closing HTTP server: %v", err)
+ }
}
// URL returns the serving address of this server
diff --git a/cmd/serve/webdav/webdav_test.go b/cmd/serve/webdav/webdav_test.go
index 37d67eeff..831310611 100644
--- a/cmd/serve/webdav/webdav_test.go
+++ b/cmd/serve/webdav/webdav_test.go
@@ -39,11 +39,9 @@ func TestWebDav(t *testing.T) {
assert.NoError(t, err)
// Start the server
- go func() {
- w := newWebDAV(fremote, &opt)
- w.serve()
- }()
- // FIXME shut it down somehow?
+ w := newWebDAV(fremote, &opt)
+ go w.serve()
+ defer w.srv.Close()
// Change directory to run the tests
err = os.Chdir("../../../backend/webdav")