Răsfoiți Sursa

refactor(api): make shutdown timeout configurable for tests (#9980)

This is extracted from PR #9175, which adds some tests that seem to
need a longer shutdown timeout when running on GitHub Actions.
Emil Lundberg 7 luni în urmă
părinte
comite
435f2d2178
2 a modificat fișierele cu 11 adăugiri și 1 ștergeri
  1. 3 1
      lib/api/api.go
  2. 8 0
      lib/api/api_test.go

+ 3 - 1
lib/api/api.go

@@ -92,6 +92,7 @@ type service struct {
 	listenerAddr         net.Addr
 	listenerAddr         net.Addr
 	exitChan             chan *svcutil.FatalErr
 	exitChan             chan *svcutil.FatalErr
 	miscDB               *db.NamespacedKV
 	miscDB               *db.NamespacedKV
+	shutdownTimeout      time.Duration
 
 
 	guiErrors logger.Recorder
 	guiErrors logger.Recorder
 	systemLog logger.Recorder
 	systemLog logger.Recorder
@@ -129,6 +130,7 @@ func New(id protocol.DeviceID, cfg config.Wrapper, assetDir, tlsDefaultCommonNam
 		startedOnce:          make(chan struct{}),
 		startedOnce:          make(chan struct{}),
 		exitChan:             make(chan *svcutil.FatalErr, 1),
 		exitChan:             make(chan *svcutil.FatalErr, 1),
 		miscDB:               miscDB,
 		miscDB:               miscDB,
+		shutdownTimeout:      100 * time.Millisecond,
 	}
 	}
 }
 }
 
 
@@ -451,7 +453,7 @@ func (s *service) Serve(ctx context.Context) error {
 	}
 	}
 	// Give it a moment to shut down gracefully, e.g. if we are restarting
 	// Give it a moment to shut down gracefully, e.g. if we are restarting
 	// due to a config change through the API, let that finish successfully.
 	// due to a config change through the API, let that finish successfully.
-	timeout, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
+	timeout, cancel := context.WithTimeout(context.Background(), s.shutdownTimeout)
 	defer cancel()
 	defer cancel()
 	if err := srv.Shutdown(timeout); err == timeout.Err() {
 	if err := srv.Shutdown(timeout); err == timeout.Err() {
 		srv.Close()
 		srv.Close()

+ 8 - 0
lib/api/api_test.go

@@ -937,6 +937,10 @@ func TestApiCache(t *testing.T) {
 }
 }
 
 
 func startHTTP(cfg config.Wrapper) (string, context.CancelFunc, error) {
 func startHTTP(cfg config.Wrapper) (string, context.CancelFunc, error) {
+	return startHTTPWithShutdownTimeout(cfg, 0)
+}
+
+func startHTTPWithShutdownTimeout(cfg config.Wrapper, shutdownTimeout time.Duration) (string, context.CancelFunc, error) {
 	m := new(modelmocks.Model)
 	m := new(modelmocks.Model)
 	assetDir := "../../gui"
 	assetDir := "../../gui"
 	eventSub := new(eventmocks.BufferedSubscription)
 	eventSub := new(eventmocks.BufferedSubscription)
@@ -964,6 +968,10 @@ func startHTTP(cfg config.Wrapper) (string, context.CancelFunc, error) {
 	svc := New(protocol.LocalDeviceID, cfg, assetDir, "syncthing", m, eventSub, diskEventSub, events.NoopLogger, discoverer, connections, urService, mockedSummary, errorLog, systemLog, false, kdb).(*service)
 	svc := New(protocol.LocalDeviceID, cfg, assetDir, "syncthing", m, eventSub, diskEventSub, events.NoopLogger, discoverer, connections, urService, mockedSummary, errorLog, systemLog, false, kdb).(*service)
 	svc.started = addrChan
 	svc.started = addrChan
 
 
+	if shutdownTimeout > 0*time.Millisecond {
+		svc.shutdownTimeout = shutdownTimeout
+	}
+
 	// Actually start the API service
 	// Actually start the API service
 	supervisor := suture.New("API test", suture.Spec{
 	supervisor := suture.New("API test", suture.Spec{
 		PassThroughPanics: true,
 		PassThroughPanics: true,