-
Notifications
You must be signed in to change notification settings - Fork 75
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
Conversation
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 | ||
} |
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.
You may use maps.Equal
instead.
logger, err := zap.NewDevelopment(zap.Development()) | ||
if err != nil { | ||
t.Fatalf("Failed to create logger: %v", err) | ||
} |
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.
You may use zaptest.NewLogger
to create test logger.
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() { | ||
} |
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.
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
}
@criyle You are right. I will update my code. |
c084ef0
to
2fb4dff
Compare
rtCh := make(chan worker.Response) | ||
go func() { | ||
rtCh <- worker.Response{ | ||
RequestID: req.RequestID, | ||
Results: []worker.Result{m.Result}, | ||
} | ||
}() |
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.
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?
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.
Maybe, but I haven't figured out how to test the request.
No description provided.