|
| 1 | +package health |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | + "go.uber.org/mock/gomock" |
| 9 | + |
| 10 | + "github.com/authzed/spicedb/internal/dispatch" |
| 11 | + dispatchmocks "github.com/authzed/spicedb/internal/dispatch/mocks" |
| 12 | + "github.com/authzed/spicedb/pkg/datastore" |
| 13 | + datastoremocks "github.com/authzed/spicedb/pkg/datastore/mocks" |
| 14 | +) |
| 15 | + |
| 16 | +func TestNewHealthManager(t *testing.T) { |
| 17 | + ctrl := gomock.NewController(t) |
| 18 | + defer ctrl.Finish() |
| 19 | + |
| 20 | + dispatcher := dispatchmocks.NewMockDispatcher(ctrl) |
| 21 | + dsc := datastoremocks.NewMockDatastore(ctrl) |
| 22 | + |
| 23 | + manager := NewHealthManager(dispatcher, dsc) |
| 24 | + |
| 25 | + require.NotNil(t, manager) |
| 26 | + require.NotNil(t, manager.HealthSvc()) |
| 27 | +} |
| 28 | + |
| 29 | +func TestHealthManagerRegisterReportedService(t *testing.T) { |
| 30 | + ctrl := gomock.NewController(t) |
| 31 | + defer ctrl.Finish() |
| 32 | + |
| 33 | + dispatcher := dispatchmocks.NewMockDispatcher(ctrl) |
| 34 | + dsc := datastoremocks.NewMockDatastore(ctrl) |
| 35 | + manager := NewHealthManager(dispatcher, dsc) |
| 36 | + |
| 37 | + serviceName := "test-service" |
| 38 | + manager.RegisterReportedService(serviceName) |
| 39 | + |
| 40 | + // Verify service was registered by checking the internal map |
| 41 | + hm := manager.(*healthManager) |
| 42 | + _, exists := hm.serviceNames[serviceName] |
| 43 | + require.True(t, exists) |
| 44 | +} |
| 45 | + |
| 46 | +func TestHealthManagerCheckIsReady(t *testing.T) { |
| 47 | + testCases := []struct { |
| 48 | + name string |
| 49 | + datastoreReady bool |
| 50 | + datastoreError error |
| 51 | + dispatcherReady bool |
| 52 | + expectedResult bool |
| 53 | + }{ |
| 54 | + { |
| 55 | + name: "both ready", |
| 56 | + datastoreReady: true, |
| 57 | + datastoreError: nil, |
| 58 | + dispatcherReady: true, |
| 59 | + expectedResult: true, |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "datastore not ready", |
| 63 | + datastoreReady: false, |
| 64 | + datastoreError: nil, |
| 65 | + dispatcherReady: true, |
| 66 | + expectedResult: false, |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "dispatcher not ready", |
| 70 | + datastoreReady: true, |
| 71 | + datastoreError: nil, |
| 72 | + dispatcherReady: false, |
| 73 | + expectedResult: false, |
| 74 | + }, |
| 75 | + { |
| 76 | + name: "both not ready", |
| 77 | + datastoreReady: false, |
| 78 | + datastoreError: nil, |
| 79 | + dispatcherReady: false, |
| 80 | + expectedResult: false, |
| 81 | + }, |
| 82 | + { |
| 83 | + name: "datastore error dispatcher ready", |
| 84 | + datastoreReady: false, // doesn't matter when there's an error |
| 85 | + datastoreError: errors.New("datastore error"), |
| 86 | + dispatcherReady: true, |
| 87 | + expectedResult: false, |
| 88 | + }, |
| 89 | + { |
| 90 | + name: "datastore error dispatcher not ready", |
| 91 | + datastoreReady: false, // doesn't matter when there's an error |
| 92 | + datastoreError: errors.New("datastore error"), |
| 93 | + dispatcherReady: false, |
| 94 | + expectedResult: false, |
| 95 | + }, |
| 96 | + } |
| 97 | + |
| 98 | + for _, tc := range testCases { |
| 99 | + t.Run(tc.name, func(t *testing.T) { |
| 100 | + ctrl := gomock.NewController(t) |
| 101 | + defer ctrl.Finish() |
| 102 | + |
| 103 | + dispatcher := dispatchmocks.NewMockDispatcher(ctrl) |
| 104 | + dispatcher.EXPECT().ReadyState().Return(dispatch.ReadyState{IsReady: tc.dispatcherReady}).MaxTimes(1) |
| 105 | + |
| 106 | + dsc := datastoremocks.NewMockDatastore(ctrl) |
| 107 | + dsc.EXPECT().ReadyState(gomock.Any()).Return(datastore.ReadyState{IsReady: tc.datastoreReady}, tc.datastoreError).Times(1) |
| 108 | + |
| 109 | + hm := &healthManager{ |
| 110 | + dispatcher: dispatcher, |
| 111 | + dsc: dsc, |
| 112 | + } |
| 113 | + |
| 114 | + require.Equal(t, tc.expectedResult, hm.checkIsReady(t.Context())) |
| 115 | + }) |
| 116 | + } |
| 117 | +} |
0 commit comments