-
Notifications
You must be signed in to change notification settings - Fork 355
add tests for health check service #2468
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ecordell
wants to merge
1
commit into
authzed:main
Choose a base branch
from
ecordell:fixdbready
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| package health | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/authzed/spicedb/internal/dispatch" | ||
| "github.com/authzed/spicedb/pkg/datastore" | ||
| v1 "github.com/authzed/spicedb/pkg/proto/dispatch/v1" | ||
| ) | ||
|
|
||
| // fakeDatastoreChecker implements DatastoreChecker for testing | ||
| type fakeDatastoreChecker struct { | ||
| readyState datastore.ReadyState | ||
| err error | ||
| } | ||
|
|
||
| func (f *fakeDatastoreChecker) ReadyState(ctx context.Context) (datastore.ReadyState, error) { | ||
| return f.readyState, f.err | ||
| } | ||
|
|
||
| // fakeDispatcher implements dispatch.Dispatcher for testing | ||
| type fakeDispatcher struct { | ||
| readyState dispatch.ReadyState | ||
| } | ||
|
|
||
| func (f *fakeDispatcher) ReadyState() dispatch.ReadyState { | ||
| return f.readyState | ||
| } | ||
|
|
||
| func (f *fakeDispatcher) Close() error { return nil } | ||
|
|
||
| // The following methods are required by dispatch.Dispatcher interface but not used in health tests | ||
| func (f *fakeDispatcher) DispatchCheck(ctx context.Context, req *v1.DispatchCheckRequest) (*v1.DispatchCheckResponse, error) { | ||
| return nil, errors.New("not implemented") | ||
|
Check failure on line 38 in internal/services/health/health_test.go
|
||
| } | ||
|
|
||
| func (f *fakeDispatcher) DispatchExpand(ctx context.Context, req *v1.DispatchExpandRequest) (*v1.DispatchExpandResponse, error) { | ||
| return nil, errors.New("not implemented") | ||
|
Check failure on line 42 in internal/services/health/health_test.go
|
||
| } | ||
|
|
||
| func (f *fakeDispatcher) DispatchLookupSubjects(req *v1.DispatchLookupSubjectsRequest, stream dispatch.LookupSubjectsStream) error { | ||
| return errors.New("not implemented") | ||
| } | ||
|
|
||
| func (f *fakeDispatcher) DispatchLookupResources2(req *v1.DispatchLookupResources2Request, stream dispatch.LookupResources2Stream) error { | ||
| return errors.New("not implemented") | ||
| } | ||
|
|
||
| func TestNewHealthManager(t *testing.T) { | ||
| dispatcher := &fakeDispatcher{} | ||
| dsc := &fakeDatastoreChecker{} | ||
|
|
||
| manager := NewHealthManager(dispatcher, dsc) | ||
|
|
||
| require.NotNil(t, manager) | ||
| require.NotNil(t, manager.HealthSvc()) | ||
| } | ||
|
|
||
| func TestHealthManagerRegisterReportedService(t *testing.T) { | ||
| dispatcher := &fakeDispatcher{} | ||
| dsc := &fakeDatastoreChecker{} | ||
| manager := NewHealthManager(dispatcher, dsc) | ||
|
|
||
| serviceName := "test-service" | ||
| manager.RegisterReportedService(serviceName) | ||
|
|
||
| // Verify service was registered by checking the internal map | ||
| hm := manager.(*healthManager) | ||
| _, exists := hm.serviceNames[serviceName] | ||
| require.True(t, exists) | ||
| } | ||
|
|
||
| func TestHealthManagerCheckIsReady(t *testing.T) { | ||
| testCases := []struct { | ||
| name string | ||
| datastoreReady bool | ||
| datastoreError error | ||
| dispatcherReady bool | ||
| expectedResult bool | ||
| }{ | ||
| { | ||
| name: "both ready", | ||
| datastoreReady: true, | ||
| datastoreError: nil, | ||
| dispatcherReady: true, | ||
| expectedResult: true, | ||
| }, | ||
| { | ||
| name: "datastore not ready", | ||
| datastoreReady: false, | ||
| datastoreError: nil, | ||
| dispatcherReady: true, | ||
| expectedResult: false, | ||
| }, | ||
| { | ||
| name: "dispatcher not ready", | ||
| datastoreReady: true, | ||
| datastoreError: nil, | ||
| dispatcherReady: false, | ||
| expectedResult: false, | ||
| }, | ||
| { | ||
| name: "both not ready", | ||
| datastoreReady: false, | ||
| datastoreError: nil, | ||
| dispatcherReady: false, | ||
| expectedResult: false, | ||
| }, | ||
| { | ||
| name: "datastore error dispatcher ready", | ||
| datastoreReady: false, // doesn't matter when there's an error | ||
| datastoreError: errors.New("datastore error"), | ||
| dispatcherReady: true, | ||
| expectedResult: false, | ||
| }, | ||
| { | ||
| name: "datastore error dispatcher not ready", | ||
| datastoreReady: false, // doesn't matter when there's an error | ||
| datastoreError: errors.New("datastore error"), | ||
| dispatcherReady: false, | ||
| expectedResult: false, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range testCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| dispatcher := &fakeDispatcher{ | ||
| readyState: dispatch.ReadyState{ | ||
| IsReady: tc.dispatcherReady, | ||
| Message: func() string { | ||
| if tc.dispatcherReady { | ||
| return "dispatcher ready" | ||
| } | ||
| return "dispatcher not ready" | ||
| }(), | ||
| }, | ||
| } | ||
|
|
||
| dsc := &fakeDatastoreChecker{ | ||
| readyState: datastore.ReadyState{ | ||
| IsReady: tc.datastoreReady, | ||
| Message: func() string { | ||
| if tc.datastoreReady { | ||
| return "datastore ready" | ||
| } | ||
| return "datastore not ready" | ||
| }(), | ||
| }, | ||
| err: tc.datastoreError, | ||
| } | ||
|
|
||
| hm := &healthManager{ | ||
| dispatcher: dispatcher, | ||
| dsc: dsc, | ||
| } | ||
|
|
||
| require.Equal(t, tc.expectedResult, hm.checkIsReady(t.Context())) | ||
| }) | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change makes sense to me, but why aren't we doing both checks in parallel? Say the datastore takes forever to respond, but the dispatcher already errored out, we will be waiting
datastoreReadyTimeoutwhen we could have returned "false" very quickly.It also seems to me that
(hm *healthManager) Checkercan be simplified by usingbackoff.Timer.