Skip to content

Commit

Permalink
refactor: remove initial delay for health check
Browse files Browse the repository at this point in the history
This improves the startup time of wfx.

Signed-off-by: Michael Adler <[email protected]>
  • Loading branch information
michaeladler committed Aug 1, 2024
1 parent 4b26846 commit 16a3ab6
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 54 deletions.
26 changes: 13 additions & 13 deletions api/northbound_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestNorthboundDeleteWorkflowsNameHandle_NotFound(t *testing.T) {

workflow := "wfx.workflow.test"

dbMock := persistence.NewMockStorage(t)
dbMock := persistence.NewHealthyMockStorage(t)
dbMock.EXPECT().DeleteWorkflow(ctx, workflow).Return(fault.Wrap(errors.New("workflow not found"), ftag.With(ftag.NotFound)))

server := NewNorthboundServer(ctx, dbMock)
Expand All @@ -50,7 +50,7 @@ func TestNorthboundDeleteWorkflowsNameHandle_InternalError(t *testing.T) {

workflow := "wfx.workflow.test"

dbMock := persistence.NewMockStorage(t)
dbMock := persistence.NewHealthyMockStorage(t)
dbMock.EXPECT().DeleteWorkflow(ctx, workflow).Return(errors.New("something went wrong"))

server := NewNorthboundServer(ctx, dbMock)
Expand All @@ -64,7 +64,7 @@ func TestNorthboundPostWorkflows_InternalError(t *testing.T) {
defer cancel()

workflow := dau.DirectWorkflow()
dbMock := persistence.NewMockStorage(t)
dbMock := persistence.NewHealthyMockStorage(t)
dbMock.EXPECT().CreateWorkflow(ctx, workflow).Return(nil, errors.New("something went wrong"))

server := NewNorthboundServer(ctx, dbMock)
Expand All @@ -79,7 +79,7 @@ func TestNorthboundPostWorkflows_AlreadyExists(t *testing.T) {

workflow := dau.DirectWorkflow()

dbMock := persistence.NewMockStorage(t)
dbMock := persistence.NewHealthyMockStorage(t)
dbMock.EXPECT().CreateWorkflow(ctx, workflow).Return(nil, fault.Wrap(errors.New("already exists"), ftag.With(ftag.AlreadyExists)))

server := NewNorthboundServer(ctx, dbMock)
Expand All @@ -99,7 +99,7 @@ func TestNorthboundPostWorkflows_InvalidWorkflow(t *testing.T) {

workflow := &api.Workflow{Name: "foo"}

dbMock := persistence.NewMockStorage(t)
dbMock := persistence.NewHealthyMockStorage(t)

server := NewNorthboundServer(ctx, dbMock)
resp, err := server.PostWorkflows(ctx, api.PostWorkflowsRequestObject{Body: workflow})
Expand All @@ -119,7 +119,7 @@ func TestNorthboundPostJobs_BadRequest(t *testing.T) {
wf := dau.DirectWorkflow()
jobRequest := api.JobRequest{Workflow: wf.Name}

dbMock := persistence.NewMockStorage(t)
dbMock := persistence.NewHealthyMockStorage(t)
dbMock.EXPECT().GetWorkflow(ctx, wf.Name).Return(nil, fault.Wrap(errors.New("invalid"), ftag.With(ftag.NotFound)))

server := NewNorthboundServer(ctx, dbMock)
Expand All @@ -140,7 +140,7 @@ func TestNorthboundPostJobs_InternalError(t *testing.T) {
wf := dau.DirectWorkflow()
jobRequest := api.JobRequest{Workflow: wf.Name}

dbMock := persistence.NewMockStorage(t)
dbMock := persistence.NewHealthyMockStorage(t)
dbMock.EXPECT().GetWorkflow(ctx, wf.Name).Return(wf, nil)
dbMock.EXPECT().CreateJob(ctx, mock.Anything).Return(nil, errors.New("something went wrong"))

Expand All @@ -156,7 +156,7 @@ func TestNorthboundDeleteJobsID_NotFound(t *testing.T) {

jobID := "42"

dbMock := persistence.NewMockStorage(t)
dbMock := persistence.NewHealthyMockStorage(t)
dbMock.EXPECT().GetJob(ctx, jobID, persistence.FetchParams{}).Return(nil, fault.Wrap(errors.New("not found"), ftag.With(ftag.NotFound)))

server := NewNorthboundServer(ctx, dbMock)
Expand All @@ -176,7 +176,7 @@ func TestNorthboundDeleteJobsID_InternalError(t *testing.T) {

jobID := "42"

dbMock := persistence.NewMockStorage(t)
dbMock := persistence.NewHealthyMockStorage(t)
dbMock.EXPECT().GetJob(ctx, jobID, persistence.FetchParams{}).Return(&api.Job{ID: jobID}, nil)
dbMock.EXPECT().DeleteJob(ctx, jobID).Return(fault.Wrap(errors.New("something went wrong"), ftag.With(ftag.Internal)))

Expand All @@ -192,7 +192,7 @@ func TestNorthboundPostJobsIDTags_NotFound(t *testing.T) {

jobID := "42"

dbMock := persistence.NewMockStorage(t)
dbMock := persistence.NewHealthyMockStorage(t)
dbMock.EXPECT().GetJob(ctx, jobID, persistence.FetchParams{}).Return(nil, fault.Wrap(errors.New("not found"), ftag.With(ftag.NotFound)))

server := NewNorthboundServer(ctx, dbMock)
Expand All @@ -212,7 +212,7 @@ func TestNorthboundPostJobsIDTags_InternalError(t *testing.T) {

jobID := "42"

dbMock := persistence.NewMockStorage(t)
dbMock := persistence.NewHealthyMockStorage(t)
dbMock.EXPECT().GetJob(ctx, jobID, persistence.FetchParams{}).Return(nil, fault.Wrap(errors.New("something went wrong"), ftag.With(ftag.Internal)))

server := NewNorthboundServer(ctx, dbMock)
Expand All @@ -227,7 +227,7 @@ func TestNorthboundDeleteJobsIDTags_NotFound(t *testing.T) {

jobID := "42"

dbMock := persistence.NewMockStorage(t)
dbMock := persistence.NewHealthyMockStorage(t)
dbMock.EXPECT().GetJob(ctx, jobID, persistence.FetchParams{}).Return(nil, fault.Wrap(errors.New("not found"), ftag.With(ftag.NotFound)))

server := NewNorthboundServer(ctx, dbMock)
Expand All @@ -247,7 +247,7 @@ func TestNorthboundDeleteJobsIDTags_InternalError(t *testing.T) {

jobID := "42"

dbMock := persistence.NewMockStorage(t)
dbMock := persistence.NewHealthyMockStorage(t)
dbMock.EXPECT().GetJob(ctx, jobID, persistence.FetchParams{}).Return(nil, fault.Wrap(errors.New("not found"), ftag.With(ftag.Internal)))

server := NewNorthboundServer(ctx, dbMock)
Expand Down
38 changes: 18 additions & 20 deletions api/southbound_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"github.com/siemens/wfx/generated/api"
"github.com/siemens/wfx/persistence"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)

Expand All @@ -47,7 +46,7 @@ func TestGetJobsIDStatus_NotFound(t *testing.T) {

for _, orientation := range allOrientations {
t.Run(orientation, func(t *testing.T) {
dbMock := persistence.NewMockStorage(t)
dbMock := persistence.NewHealthyMockStorage(t)
dbMock.EXPECT().GetJob(ctx, jobID, persistence.FetchParams{}).Return(nil, fault.Wrap(fmt.Errorf("job with id %s does not exist", jobID), ftag.With(ftag.NotFound)))

server := createServerForTesting(ctx, orientation, dbMock)
Expand All @@ -70,7 +69,7 @@ func TestGetJobsIDStatus_InternalError(t *testing.T) {

for _, orientation := range allOrientations {
t.Run(orientation, func(t *testing.T) {
dbMock := persistence.NewMockStorage(t)
dbMock := persistence.NewHealthyMockStorage(t)
dbMock.EXPECT().GetJob(ctx, jobID, persistence.FetchParams{}).Return(nil, errors.New("something went wrong"))

server := createServerForTesting(ctx, orientation, dbMock)
Expand All @@ -88,7 +87,7 @@ func TestPutJobsIDStatus_NotFound(t *testing.T) {

for _, orientation := range allOrientations {
t.Run(orientation, func(t *testing.T) {
dbMock := persistence.NewMockStorage(t)
dbMock := persistence.NewHealthyMockStorage(t)
dbMock.EXPECT().GetJob(ctx, jobID, persistence.FetchParams{}).Return(nil, fault.Wrap(fmt.Errorf("job with id %s does not exist", jobID), ftag.With(ftag.NotFound)))

server := createServerForTesting(ctx, orientation, dbMock)
Expand All @@ -115,7 +114,7 @@ func TestPutJobsIDStatus_InternalError(t *testing.T) {

for _, orientation := range allOrientations {
t.Run(orientation, func(t *testing.T) {
dbMock := persistence.NewMockStorage(t)
dbMock := persistence.NewHealthyMockStorage(t)
dbMock.EXPECT().GetJob(ctx, jobID, persistence.FetchParams{}).Return(nil, errors.New("something went wrong"))

server := createServerForTesting(ctx, orientation, dbMock)
Expand All @@ -132,7 +131,7 @@ func TestGetJobs_InternalError(t *testing.T) {

for _, orientation := range allOrientations {
t.Run(orientation, func(t *testing.T) {
dbMock := persistence.NewMockStorage(t)
dbMock := persistence.NewHealthyMockStorage(t)
dbMock.EXPECT().
QueryJobs(ctx, persistence.FilterParams{}, persistence.SortParams{}, persistence.PaginationParams{Limit: 10}).
Return(nil, errors.New("something went wrong"))
Expand All @@ -152,7 +151,7 @@ func TestGetJobsID_NotFound(t *testing.T) {

for _, orientation := range allOrientations {
t.Run(orientation, func(t *testing.T) {
dbMock := persistence.NewMockStorage(t)
dbMock := persistence.NewHealthyMockStorage(t)
dbMock.EXPECT().GetJob(ctx, jobID, persistence.FetchParams{}).Return(nil, fault.Wrap(fmt.Errorf("job with id %s does not exist", jobID), ftag.With(ftag.NotFound)))

server := createServerForTesting(ctx, orientation, dbMock)
Expand All @@ -176,7 +175,7 @@ func TestGetJobsID_InternalError(t *testing.T) {
for _, orientation := range allOrientations {
t.Run(orientation, func(t *testing.T) {
history := true
dbMock := persistence.NewMockStorage(t)
dbMock := persistence.NewHealthyMockStorage(t)
dbMock.EXPECT().GetJob(ctx, jobID, persistence.FetchParams{History: history}).Return(nil, errors.New("something went wrong"))

server := createServerForTesting(ctx, orientation, dbMock)
Expand All @@ -194,7 +193,7 @@ func TestGetJobsIDDefinition_NotFound(t *testing.T) {

for _, orientation := range allOrientations {
t.Run(orientation, func(t *testing.T) {
dbMock := persistence.NewMockStorage(t)
dbMock := persistence.NewHealthyMockStorage(t)
dbMock.EXPECT().GetJob(ctx, jobID, persistence.FetchParams{}).Return(nil, fault.Wrap(fmt.Errorf("job with id %s does not exist", jobID), ftag.With(ftag.NotFound)))

server := createServerForTesting(ctx, orientation, dbMock)
Expand All @@ -217,7 +216,7 @@ func TestGetJobsIDDefinition_InternalError(t *testing.T) {

for _, orientation := range allOrientations {
t.Run(orientation, func(t *testing.T) {
dbMock := persistence.NewMockStorage(t)
dbMock := persistence.NewHealthyMockStorage(t)
dbMock.EXPECT().GetJob(ctx, jobID, persistence.FetchParams{}).Return(nil, errors.New("something went wrong"))

server := createServerForTesting(ctx, orientation, dbMock)
Expand All @@ -235,7 +234,7 @@ func TestPutJobsIDDefinition_NotFound(t *testing.T) {

for _, orientation := range allOrientations {
t.Run(orientation, func(t *testing.T) {
dbMock := persistence.NewMockStorage(t)
dbMock := persistence.NewHealthyMockStorage(t)
dbMock.EXPECT().GetJob(ctx, jobID, persistence.FetchParams{}).Return(nil, fault.Wrap(fmt.Errorf("job with id %s does not exist", jobID), ftag.With(ftag.NotFound)))

server := createServerForTesting(ctx, orientation, dbMock)
Expand All @@ -258,7 +257,7 @@ func TestPutJobsIDDefinition_InternalError(t *testing.T) {

for _, orientation := range allOrientations {
t.Run(orientation, func(t *testing.T) {
dbMock := persistence.NewMockStorage(t)
dbMock := persistence.NewHealthyMockStorage(t)
dbMock.EXPECT().GetJob(ctx, jobID, persistence.FetchParams{}).Return(nil, errors.New("something went wrong"))

server := createServerForTesting(ctx, orientation, dbMock)
Expand All @@ -276,7 +275,7 @@ func TestGetWorkflowsName_InternalError(t *testing.T) {

for _, orientation := range allOrientations {
t.Run(orientation, func(t *testing.T) {
dbMock := persistence.NewMockStorage(t)
dbMock := persistence.NewHealthyMockStorage(t)
dbMock.EXPECT().GetWorkflow(ctx, workflow).Return(nil, errors.New("something went wrong"))

server := createServerForTesting(ctx, orientation, dbMock)
Expand All @@ -293,7 +292,7 @@ func TestGetWorkflows_InternalError(t *testing.T) {

for _, orientation := range allOrientations {
t.Run(orientation, func(t *testing.T) {
dbMock := persistence.NewMockStorage(t)
dbMock := persistence.NewHealthyMockStorage(t)
dbMock.EXPECT().QueryWorkflows(ctx, persistence.PaginationParams{Limit: 10}).Return(nil, errors.New("something went wrong"))

server := createServerForTesting(ctx, orientation, dbMock)
Expand All @@ -310,7 +309,7 @@ func TestGetWorkflows_Empty(t *testing.T) {

for _, orientation := range allOrientations {
t.Run(orientation, func(t *testing.T) {
dbMock := persistence.NewMockStorage(t)
dbMock := persistence.NewHealthyMockStorage(t)
dbMock.EXPECT().QueryWorkflows(ctx, persistence.PaginationParams{Limit: 10}).Return(&api.PaginatedWorkflowList{}, nil)

server := createServerForTesting(ctx, orientation, dbMock)
Expand All @@ -333,7 +332,7 @@ func TestGetJobsIDTags_NotFound(t *testing.T) {

for _, orientation := range allOrientations {
t.Run(orientation, func(t *testing.T) {
dbMock := persistence.NewMockStorage(t)
dbMock := persistence.NewHealthyMockStorage(t)
dbMock.EXPECT().GetJob(ctx, jobID, persistence.FetchParams{}).Return(nil, fault.Wrap(errors.New("not found"), ftag.With(ftag.NotFound)))

server := createServerForTesting(ctx, orientation, dbMock)
Expand All @@ -356,7 +355,7 @@ func TestGetJobsIDTags_InternalError(t *testing.T) {

for _, orientation := range allOrientations {
t.Run(orientation, func(t *testing.T) {
dbMock := persistence.NewMockStorage(t)
dbMock := persistence.NewHealthyMockStorage(t)
dbMock.EXPECT().GetJob(ctx, jobID, persistence.FetchParams{}).Return(nil, fault.Wrap(errors.New("something went wrong"), ftag.With(ftag.Internal)))

server := createServerForTesting(ctx, orientation, dbMock)
Expand All @@ -372,8 +371,7 @@ func TestHealth(t *testing.T) {
defer cancel()
for _, orientation := range allOrientations {
t.Run(orientation, func(t *testing.T) {
dbMock := persistence.NewMockStorage(t)
dbMock.EXPECT().CheckHealth(mock.Anything).Return(nil)
dbMock := persistence.NewHealthyMockStorage(t)
server := createServerForTesting(ctx, orientation, dbMock)
var response *http.Response
for i := 0; i < 10; i++ {
Expand All @@ -395,9 +393,9 @@ func TestHealth(t *testing.T) {
func TestVersion(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
dbMock := persistence.NewHealthyMockStorage(t)
for _, orientation := range allOrientations {
t.Run(orientation, func(t *testing.T) {
dbMock := persistence.NewMockStorage(t)
server := createServerForTesting(ctx, orientation, dbMock)
resp, err := server.GetVersion(ctx, api.GetVersionRequestObject{})
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion api/wfx.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (server WfxServer) Shutdown() {
func newWfxServer(ctx context.Context, storage persistence.Storage) WfxServer {
checker := health.NewChecker(
health.WithTimeout(10*time.Second),
health.WithPeriodicCheck(30*time.Second, time.Second, health.Check{
health.WithPeriodicCheck(30*time.Second, 0, health.Check{
Name: "persistence",
Check: func(ctx context.Context) error {
return fault.Wrap(storage.CheckHealth(ctx))
Expand Down
4 changes: 2 additions & 2 deletions cmd/wfx/cmd/root/plugins_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func TestCreatePluginMiddlewares(t *testing.T) {
}

func TestCreateNorthServer_PluginsDir(t *testing.T) {
dbMock := persistence.NewMockStorage(t)
dbMock := persistence.NewHealthyMockStorage(t)
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
sc, err := createNorthServer(ctx, new(config.AppConfig), dbMock, []plugin.Plugin{})
Expand All @@ -195,7 +195,7 @@ func TestCreateNorthServer_PluginsDir(t *testing.T) {
}

func TestCreateSouthServer_PluginsDir(t *testing.T) {
dbMock := persistence.NewMockStorage(t)
dbMock := persistence.NewHealthyMockStorage(t)
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
sc, err := createSouthServer(ctx, new(config.AppConfig), dbMock, []plugin.Plugin{})
Expand Down
2 changes: 1 addition & 1 deletion internal/handler/job/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestDeleteJob_NotFound(t *testing.T) {
}

func TestDeleteJob_Error(t *testing.T) {
dbMock := persistence.NewMockStorage(t)
dbMock := persistence.NewHealthyMockStorage(t)
ctx := context.Background()
jobID := "42"
dbMock.EXPECT().GetJob(ctx, jobID, persistence.FetchParams{History: false}).Return(&api.Job{ID: jobID}, nil)
Expand Down
14 changes: 7 additions & 7 deletions internal/handler/job/tags/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,28 +53,28 @@ func TestAdd(t *testing.T) {
}

func TestAdd_FaultyStorageGet(t *testing.T) {
db := persistence.NewMockStorage(t)
dbMock := persistence.NewHealthyMockStorage(t)
ctx := context.Background()
expectedErr := errors.New("mock error")
db.On("GetJob", ctx, "1", persistence.FetchParams{History: false}).Return(nil, expectedErr)
dbMock.On("GetJob", ctx, "1", persistence.FetchParams{History: false}).Return(nil, expectedErr)

tags, err := Add(ctx, db, "1", []string{"foo", "bar"})
tags, err := Add(ctx, dbMock, "1", []string{"foo", "bar"})
assert.Nil(t, tags)
assert.NotNil(t, err)
}

func TestAdd_FaultyStorageUpdate(t *testing.T) {
db := persistence.NewMockStorage(t)
dbMock := persistence.NewHealthyMockStorage(t)
ctx := context.Background()

expectedErr := errors.New("mock error")
dummyJob := api.Job{ID: "1"}
tags := []string{"foo", "bar"}

db.On("GetJob", ctx, "1", persistence.FetchParams{History: false}).Return(&dummyJob, nil)
db.On("UpdateJob", ctx, &dummyJob, persistence.JobUpdate{AddTags: &tags}).Return(nil, expectedErr)
dbMock.On("GetJob", ctx, "1", persistence.FetchParams{History: false}).Return(&dummyJob, nil)
dbMock.On("UpdateJob", ctx, &dummyJob, persistence.JobUpdate{AddTags: &tags}).Return(nil, expectedErr)

tags, err := Add(ctx, db, "1", tags)
tags, err := Add(ctx, dbMock, "1", tags)
assert.Nil(t, tags)
assert.NotNil(t, err)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/handler/job/tags/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func TestDelete(t *testing.T) {
}

func TestDelete_FaultyStorageGet(t *testing.T) {
db := persistence.NewMockStorage(t)
db := persistence.NewHealthyMockStorage(t)
ctx := context.Background()
expectedErr := errors.New("mock error")
db.On("GetJob", ctx, "1", persistence.FetchParams{History: false}).Return(nil, expectedErr)
Expand All @@ -61,7 +61,7 @@ func TestDelete_FaultyStorageGet(t *testing.T) {
}

func TestDelete_FaultyStorageUpdate(t *testing.T) {
db := persistence.NewMockStorage(t)
db := persistence.NewHealthyMockStorage(t)
ctx := context.Background()

expectedErr := errors.New("mock error")
Expand Down
Loading

0 comments on commit 16a3ab6

Please sign in to comment.