-
Notifications
You must be signed in to change notification settings - Fork 0
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
#5 add cli #12
#5 add cli #12
Conversation
XioZ
commented
May 3, 2024
•
edited
Loading
edited
- Implemented a CLI controller to start/stop a mino instance
- Closing Add Controller #5
# Conflicts: # mino/minows/mod_test.go
# Conflicts: # mino/minows/controller.go
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 - just some test optimizations.
# Conflicts: # mino/minows/rpc.go # mino/minows/session.go
Co-authored-by: Linus Gasser <[email protected]>
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.
Simple name change to make it clearer what the storage is for. Else it looks good.
mino/minows/controller_test.go
Outdated
func TestController_OnStart(t *testing.T) { | ||
flags := new(mockFlags) | ||
flags.On("String", "listen").Return("/ip4/0.0.0.0/tcp/8000/ws") | ||
flags.On("String", "public").Return("/dns4/p2p-1.c4dt.dela.org/tcp/443/wss") | ||
inj := node.NewInjector() | ||
inj.Inject(fake.NewInMemoryDB()) | ||
ctrl, stop := mustCreateController(t, inj) | ||
defer stop() | ||
|
||
err := ctrl.OnStart(flags, inj) | ||
require.NoError(t, err) | ||
var m *minows | ||
err = inj.Resolve(&m) | ||
require.NoError(t, 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.
And other tests - ChatGPT proposes the following change, what do you think?
func TestController_OnStart(t *testing.T) {
ctrl, flags, inj, stop := setupTest(t, "/ip4/0.0.0.0/tcp/8000/ws", "/dns4/p2p-1.c4dt.dela.org/tcp/443/wss")
defer stop()
err := ctrl.OnStart(flags, inj)
require.NoError(t, err)
var m *minows
err = inj.Resolve(&m)
require.NoError(t, err)
}
// ... Other tests using `setupTest` ...
func TestController_InvalidPublic(t *testing.T) {
ctrl, flags, inj, _ := setupTest(t, "/ip4/0.0.0.0/tcp/8000/ws", "invalid")
err := ctrl.OnStart(flags, inj)
require.Error(t, err)
}
func TestController_OnStop(t *testing.T) {
ctrl, flags, inj, _ := setupTest(t, "/ip4/0.0.0.0/tcp/8000/ws", "/dns4/p2p-1.c4dt.dela.org/tcp/443/wss")
err := ctrl.OnStart(flags, inj)
require.NoError(t, err)
err = ctrl.OnStop(inj)
require.NoError(t, err)
}
// ... Other tests ...
// `TestController_MissingDependency` doesn't need `stopFunc`, but common setup is still useful.
func TestController_MissingDependency(t *testing.T) {
ctrl, flags, inj, _ := setupTest(t, "/ip4/0.0.0.0/tcp/8000/ws", "/dns4/p2p-1.c4dt.dela.org/tcp/443/wss")
err := ctrl.OnStart(flags, inj)
require.NoError(t, err)
err = ctrl.OnStop(node.NewInjector())
require.Error(t, err)
}
type mockFlags struct {
mock.Mock
}
// Helper function for common setup
func setupTest(t *testing.T, listenValue string, publicValue string) (ctrl *controller.Controller, flags *mockFlags, inj *node.Injector, stopFunc func()) {
flags = new(mockFlags)
flags.On("String", "listen").Return(listenValue)
flags.On("String", "public").Return(publicValue)
inj = node.NewInjector()
inj.Inject(fake.NewInMemoryDB())
ctrl, stopFunc = mustCreateController(t, inj)
return ctrl, flags, inj, stopFunc
}
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.
i'm not a big fan of overengineering tests because 1) tests need to change accordingly when requirements change. if i need to modify 1 test setup that requires refactoring setupTest
, that affects all other tests that use it unnecessarily 2) having the setup code all in the test itself makes the test more readable (without scrolling). however, i can make this change if you prefer brevity.
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.
If it's two or three tests with the same setup, I do agree that it's not worth having a common setup. But here it is five tests, so I think it's worth it.
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.
changed