-
Notifications
You must be signed in to change notification settings - Fork 38
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
Singleton implementation for GetHostName. #85
Singleton implementation for GetHostName. #85
Conversation
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.
Looks good, maybe add a unit test or two?
Sorry for premature approval - forgot about testability, this will maintain current tight coupling which we should move away from, and would make it impossible to inject a value of hostname for unit testing those components which consume it. We should either pass the value as a parameter or abstract the singleton behind an interface. |
Cool, Tight coupling as to unit testing? I doubt if this can be go-mocked. (Hence bunch of legacy codes is interesting to unit-test) I tried adding test but until we mock Functionally: Current Working Unit Test Added and here are the Run Result With slight refactor here is the 100% unit test coverage of the functionality, also in future once RunCommandOnHost is replaced with actual package based runCommand, we need to remove any dependency of this command which start with |
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.
Callers are still tightly coupled to this implementation but think I'm not explaining it properly, and it would be a premature optimization anyway so this is perfect for now. Thanks!
This PR intends to Fix the multiple
GetHostName
calls to Singleton call usingSync.Once.Do
This PR also try to attempt and simplify the deep parameter passing mechanism. Why using
Sync.Once
because its a thread safe go existing pkg funct which we can use. https://pkg.go.dev/sync#Once .Please note: feel free to add your variable naming preferences but this way will ensure that the call for the actual
run command
only happens once. Advantage of this approach is that all changes reside underutils
umbrella, no extra func parameter will be required, and it will guarantee the actual code is ran only once.Another, pro is that the call to the method stays same, under the hood it only gets called once. For future any new diagnostics or anything which needs to call the
utils.GetHostName
don't need to worry about running many times, and no more parameter passing as well.This pR also simplifies this PR: #82
Thanks guys. 🙏
Unit test result