| 
									
										
										
										
											2021-04-07 15:44:06 +08:00
										 |  |  | package server | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"context" | 
					
						
							|  |  |  | 	"errors" | 
					
						
							| 
									
										
										
										
											2023-08-03 21:19:01 +08:00
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2021-04-07 15:44:06 +08:00
										 |  |  | 	"testing" | 
					
						
							| 
									
										
										
										
											2021-04-30 16:55:59 +08:00
										 |  |  | 	"time" | 
					
						
							| 
									
										
										
										
											2021-04-07 15:44:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-07 05:34:23 +08:00
										 |  |  | 	"github.com/prometheus/client_golang/prometheus" | 
					
						
							| 
									
										
										
										
											2023-01-30 16:26:42 +08:00
										 |  |  | 	"github.com/stretchr/testify/require" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-03 21:19:01 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/registry" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/registry/backgroundsvcs" | 
					
						
							| 
									
										
										
										
											2022-09-06 00:15:47 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/accesscontrol/acimpl" | 
					
						
							| 
									
										
										
										
											2021-08-25 21:11:22 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/setting" | 
					
						
							| 
									
										
										
										
											2021-04-07 15:44:06 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-03 21:19:01 +08:00
										 |  |  | type testService struct { | 
					
						
							|  |  |  | 	started    chan struct{} | 
					
						
							|  |  |  | 	runErr     error | 
					
						
							|  |  |  | 	isDisabled bool | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func newTestService(runErr error, disabled bool) *testService { | 
					
						
							|  |  |  | 	return &testService{ | 
					
						
							|  |  |  | 		started:    make(chan struct{}), | 
					
						
							|  |  |  | 		runErr:     runErr, | 
					
						
							|  |  |  | 		isDisabled: disabled, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (s *testService) Run(ctx context.Context) error { | 
					
						
							|  |  |  | 	if s.isDisabled { | 
					
						
							|  |  |  | 		return fmt.Errorf("Shouldn't run disabled service") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if s.runErr != nil { | 
					
						
							|  |  |  | 		return s.runErr | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	close(s.started) | 
					
						
							|  |  |  | 	<-ctx.Done() | 
					
						
							|  |  |  | 	return ctx.Err() | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (s *testService) IsDisabled() bool { | 
					
						
							|  |  |  | 	return s.isDisabled | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func testServer(t *testing.T, services ...registry.BackgroundService) *Server { | 
					
						
							| 
									
										
										
										
											2021-08-25 21:11:22 +08:00
										 |  |  | 	t.Helper() | 
					
						
							| 
									
										
										
										
											2023-12-07 05:34:23 +08:00
										 |  |  | 	s, err := newServer(Options{}, setting.NewCfg(), nil, &acimpl.Service{}, nil, backgroundsvcs.NewBackgroundServiceRegistry(services...), prometheus.NewRegistry()) | 
					
						
							| 
									
										
										
										
											2021-08-25 21:11:22 +08:00
										 |  |  | 	require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2021-04-07 15:44:06 +08:00
										 |  |  | 	// Required to skip configuration initialization that causes
 | 
					
						
							|  |  |  | 	// DI errors in this test.
 | 
					
						
							|  |  |  | 	s.isInitialized = true | 
					
						
							|  |  |  | 	return s | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestServer_Run_Error(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2021-08-25 21:11:22 +08:00
										 |  |  | 	testErr := errors.New("boom") | 
					
						
							| 
									
										
										
										
											2023-08-03 21:19:01 +08:00
										 |  |  | 	s := testServer(t, newTestService(nil, false), newTestService(testErr, false)) | 
					
						
							|  |  |  | 	err := s.Run() | 
					
						
							|  |  |  | 	require.ErrorIs(t, err, testErr) | 
					
						
							| 
									
										
										
										
											2021-04-07 15:44:06 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestServer_Shutdown(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2021-04-30 16:55:59 +08:00
										 |  |  | 	ctx := context.Background() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-03 21:19:01 +08:00
										 |  |  | 	s := testServer(t, newTestService(nil, false), newTestService(nil, true)) | 
					
						
							| 
									
										
										
										
											2021-04-07 15:44:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-30 16:55:59 +08:00
										 |  |  | 	ch := make(chan error) | 
					
						
							| 
									
										
										
										
											2023-08-03 21:19:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-07 15:44:06 +08:00
										 |  |  | 	go func() { | 
					
						
							| 
									
										
										
										
											2021-04-30 16:55:59 +08:00
										 |  |  | 		defer close(ch) | 
					
						
							| 
									
										
										
										
											2023-08-03 21:19:01 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// Wait until all services launched.
 | 
					
						
							|  |  |  | 		for _, svc := range s.backgroundServices { | 
					
						
							|  |  |  | 			if !svc.(*testService).isDisabled { | 
					
						
							|  |  |  | 				<-svc.(*testService).started | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-04-30 16:55:59 +08:00
										 |  |  | 		ctx, cancel := context.WithTimeout(ctx, 3*time.Second) | 
					
						
							|  |  |  | 		defer cancel() | 
					
						
							|  |  |  | 		err := s.Shutdown(ctx, "test interrupt") | 
					
						
							|  |  |  | 		ch <- err | 
					
						
							| 
									
										
										
										
											2021-04-07 15:44:06 +08:00
										 |  |  | 	}() | 
					
						
							| 
									
										
										
										
											2023-08-03 21:19:01 +08:00
										 |  |  | 	err := s.Run() | 
					
						
							| 
									
										
										
										
											2021-04-07 15:44:06 +08:00
										 |  |  | 	require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2021-04-30 16:55:59 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	err = <-ch | 
					
						
							|  |  |  | 	require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2023-08-03 21:19:01 +08:00
										 |  |  | } |