From 840b76228ab8ec7146ab603bc8516127d9c8d5ce Mon Sep 17 00:00:00 2001 From: iofq Date: Sat, 9 Nov 2024 15:34:35 -0600 Subject: [PATCH] add shutdown context unit test --- cmd/api_serve.go | 1 + http/download_test.go | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/cmd/api_serve.go b/cmd/api_serve.go index 51bdd91b..d3ccdf77 100644 --- a/cmd/api_serve.go +++ b/cmd/api_serve.go @@ -65,6 +65,7 @@ func aptlyAPIServe(cmd *commander.Command, args []string) error { signal.Notify(sigchan, syscall.SIGINT, syscall.SIGTERM) go (func() { if _, ok := <-sigchan; ok { + fmt.Printf("\nShutdown signal received, waiting for background tasks...\n") context.TaskList().Wait() server.Shutdown(stdcontext.Background()) } diff --git a/http/download_test.go b/http/download_test.go index 9cde0716..d0feccf3 100644 --- a/http/download_test.go +++ b/http/download_test.go @@ -154,3 +154,13 @@ func (s *DownloaderSuite) TestGetLengthConnectError(c *C) { c.Assert(err, ErrorMatches, ".*no such host") } + +func (s *DownloaderSuite) TestContextCancel(c *C) { + ctx, cancel := context.WithCancel(s.ctx) + s.ctx = ctx + + cancel() + _, err := s.d.GetLength(s.ctx, "http://nosuch.host.invalid./") + + c.Assert(err, ErrorMatches, ".*context canceled.*") +}