Skip to content
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

Merged
merged 30 commits into from
May 24, 2024
Merged

#5 add cli #12

merged 30 commits into from
May 24, 2024

Conversation

XioZ
Copy link
Collaborator

@XioZ XioZ commented May 3, 2024

@XioZ XioZ requested a review from ineiti May 6, 2024 11:34
Copy link
Member

@ineiti ineiti left a 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.

mino/minows/controller_test.go Outdated Show resolved Hide resolved
mino/minows/controller_test.go Outdated Show resolved Hide resolved
Base automatically changed from feature/#4-test-integration to main May 8, 2024 12:51
XioZ added 2 commits May 8, 2024 17:55
# Conflicts:
#	mino/minows/rpc.go
#	mino/minows/session.go
mino/minows/controller_test.go Outdated Show resolved Hide resolved
mino/minows/key/disk.go Outdated Show resolved Hide resolved
mino/minows/key/disk.go Outdated Show resolved Hide resolved
mino/minows/key/disk.go Outdated Show resolved Hide resolved
mino/minows/key/disk.go Outdated Show resolved Hide resolved
mino/minows/key/memory.go Outdated Show resolved Hide resolved
Copy link
Member

@ineiti ineiti left a 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/key/mod.go Show resolved Hide resolved
Comment on lines 12 to 26
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)
}
Copy link
Member

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
}

Copy link
Collaborator Author

@XioZ XioZ May 16, 2024

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.

Copy link
Member

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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

changed

@XioZ XioZ merged commit ae4c18e into main May 24, 2024
4 checks passed
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