Skip to content

test(rest_executor): add unit test for handleRun #149

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

Merged
merged 2 commits into from
May 21, 2025

Conversation

DNEGEL3125
Copy link
Contributor

No description provided.

Comment on lines 69 to 79
func mapEqual(a, b map[string]string) bool {
if len(a) != len(b) {
return false
}
for k, v := range a {
if b[k] != v {
return false
}
}
return true
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may use maps.Equal instead.

Comment on lines 145 to 148
logger, err := zap.NewDevelopment(zap.Development())
if err != nil {
t.Fatalf("Failed to create logger: %v", err)
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may use zaptest.NewLogger to create test logger.

Comment on lines 39 to 37
func (m *mockWorker) Cancel(_ context.Context, _ string) error {
return nil
}

func (m *mockWorker) Close() {
}

func (m *mockWorker) Start() {
}

func (m *mockWorker) Execute(_ context.Context, _ *worker.Request) <-chan worker.Response {
return nil
}

func (m *mockWorker) Stat() worker.Stat {
return worker.Stat{
Queue: 10,
Running: 2,
}
}

func (m *mockWorker) Shutdown() {
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can just embed an interface and override the part you want to test to avoid duplicates. For example,

type mockWorker struct {
    Result worker.Result
    worker.Worker
}

@DNEGEL3125
Copy link
Contributor Author

@criyle You are right. I will update my code.

Comment on lines 31 to 37
rtCh := make(chan worker.Response)
go func() {
rtCh <- worker.Response{
RequestID: req.RequestID,
Results: []worker.Result{m.Result},
}
}()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need a go routine for this as you can create a buffered channel:

rtCh := make(chan worker.Response, 1)
rtCh <- worker.Response{...
}

Also, do we want to check the request like what we did for results (assertWorkerResultEqualsModelResult) to ensure the api endpoint is parsing the request correctly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe, but I haven't figured out how to test the request.

@criyle criyle merged commit 6d77d53 into criyle:master May 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants