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

Tests cleanup #366

Merged
merged 2 commits into from Mar 27, 2024
Merged

Tests cleanup #366

merged 2 commits into from Mar 27, 2024

Conversation

wszaranski
Copy link
Contributor

Thanks for great project, here is a little cleanup - because less is more ✨ ✨ ✨


Change Run() to RunT(t) in tests

Goleak found unclosed goroutines in tests, for example
miniredis_test.go:TestDump never calls s.Close()

func TestDump(t *testing.T) {
s, err := Run()
ok(t, err)
s.Set("aap", "noot")
s.Set("vuur", "mies")
s.HSet("ahash", "aap", "noot")
s.HSet("ahash", "vuur", "mies")
if have, want := s.Dump(), `- aap
"noot"
- ahash
aap: "noot"
vuur: "mies"
- vuur
"mies"
`; have != want {
t.Errorf("have: %q, want: %q", have, want)
}
// Tricky whitespace
s.Select(1)
s.Set("whitespace", "foo\nbar\tbaz!")
if have, want := s.Dump(), `- whitespace
"foo\nbar\tbaz!"
`; have != want {
t.Errorf("have: %q, want: %q", have, want)
}
// Long key
s.Select(2)
s.Set("long", "This is a rather long key, with some fox jumping over a fence or something.")
s.Set("countonme", "0123456789012345678901234567890123456789012345678901234567890123456789")
s.HSet("hlong", "long", "This is another rather long key, with some fox jumping over a fence or something.")
if have, want := s.Dump(), `- countonme
"01234567890123456789012345678901234567890123456789012"...(70)
- hlong
long: "This is another rather long key, with some fox jumpin"...(81)
- long
"This is a rather long key, with some fox jumping over"...(75)
`; have != want {
t.Errorf("have: %q, want: %q", have, want)
}
}

I've changed Run() calls to RunT(t) calls to:

  • ensure all goroutines all closed after test is finished
  • we have shorter tests (no need to check for error and close server

There is no risk in using RunT even if test manually closes server
because calling Close multiple times is not an issue.


Move create of client in tests to helper function

Removed duplicated code in tests using helper function.

Goleak[1] found unclosed goroutines in tests, for example
`miniredis_test.go:TestDump`[2] never calls `s.Close()`
```go
func TestDump(t *testing.T) {
	s, err := Run()
	ok(t, err)
	s.Set("aap", "noot")
	s.Set("vuur", "mies")
// ...
}
```

I've changed `Run()` calls to `RunT(t)` calls to:
 - ensure all goroutines all closed after test is finished
 - we have shorter tests (no need to check for error and close server

There is no risk in using RunT even if test manually closes server
because calling Close multiple times is not an issue.

[1] http://go.uber.org/goleak
[2] https://github.com/alicebob/miniredis/blob/9ac631e686635ffc17cb1fb45027b8a67f125c80/miniredis_test.go#L88-L129

Signed-off-by: Wojciech Szarański <wojciech.szaranski@gmail.com>
Removed duplicated code in tests using helper function.

Signed-off-by: Wojciech Szarański <wojciech.szaranski@gmail.com>
@@ -84,20 +79,14 @@ func TestEval(t *testing.T) {
must0(t, c,
"EVAL", "return redis.call('expire','foo', 999999)", "0",
)
ok(t, err)
Copy link
Owner

Choose a reason for hiding this comment

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

:-)

good catch

@alicebob alicebob merged commit 889458a into alicebob:master Mar 27, 2024
4 checks passed
@alicebob
Copy link
Owner

Great idea, thanks. t.Cleanup didn't exist back then.

@wszaranski wszaranski deleted the tests-improvements branch April 9, 2024 09:05
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.

None yet

2 participants