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

chore: modernize tests #15244

Merged
merged 18 commits into from
Feb 22, 2024
Merged

chore: modernize tests #15244

merged 18 commits into from
Feb 22, 2024

Conversation

Maniktherana
Copy link
Contributor

@Maniktherana Maniktherana commented Feb 15, 2024

Description

Used test writer to convert t.errorf to assert

Related Issue(s)

addresses #15183
partially covers #15182

Checklist

  • "Backport to:" labels have been added if this change should be back-ported to release branches
  • If this change is to be back-ported to previous releases, a justification is included in the PR description
  • Tests were added or are not required
  • Did the new or modified tests pass consistently locally and on CI?
  • Documentation was added or is not required

Deployment Notes

Signed-off-by: Manik Rana <manikrana54@gmail.com>
Copy link
Contributor

vitess-bot bot commented Feb 15, 2024

Review Checklist

Hello reviewers! 👋 Please follow this checklist when reviewing this Pull Request.

General

  • Ensure that the Pull Request has a descriptive title.
  • Ensure there is a link to an issue (except for internal cleanup and flaky test fixes), new features should have an RFC that documents use cases and test cases.

Tests

  • Bug fixes should have at least one unit or end-to-end test, enhancement and new features should have a sufficient number of tests.

Documentation

  • Apply the release notes (needs details) label if users need to know about this change.
  • New features should be documented.
  • There should be some code comments as to why things are implemented the way they are.
  • There should be a comment at the top of each new or modified test to explain what the test does.

New flags

  • Is this flag really necessary?
  • Flag names must be clear and intuitive, use dashes (-), and have a clear help text.

If a workflow is added or modified:

  • Each item in Jobs should be named in order to mark it as required.
  • If the workflow needs to be marked as required, the maintainer team must be notified.

Backward compatibility

  • Protobuf changes should be wire-compatible.
  • Changes to _vt tables and RPCs need to be backward compatible.
  • RPC changes should be compatible with vitess-operator
  • If a flag is removed, then it should also be removed from vitess-operator and arewefastyet, if used there.
  • vtctl command output order should be stable and awk-able.

@vitess-bot vitess-bot bot added NeedsBackportReason If backport labels have been applied to a PR, a justification is required NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsIssue A linked issue is missing for this Pull Request NeedsWebsiteDocsUpdate What it says labels Feb 15, 2024
@Maniktherana Maniktherana marked this pull request as draft February 15, 2024 08:28
@github-actions github-actions bot added this to the v20.0.0 milestone Feb 15, 2024
Copy link

codecov bot commented Feb 15, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (de3c9c5) 67.46% compared to head (faadfce) 67.54%.
Report is 19 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #15244      +/-   ##
==========================================
+ Coverage   67.46%   67.54%   +0.07%     
==========================================
  Files        1560     1561       +1     
  Lines      193186   193387     +201     
==========================================
+ Hits       130333   130618     +285     
+ Misses      62853    62769      -84     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Signed-off-by: Manik Rana <manikrana54@gmail.com>
@Maniktherana Maniktherana changed the title chore: remove loopclosure captures from tests chore: modernize tests Feb 15, 2024
@Maniktherana
Copy link
Contributor Author

Maniktherana commented Feb 15, 2024

hey @ajm188 I tried to add the new range in some of the testfiles but on commiting, the linter's complaining with this output:

git commit -m "chore: use new range for loops"
2024/02/15 17:04:03 5 files OK
Linting go/bucketpool go/cache/theine go/mysql go/mysql/endtoend go/pools go/pools/smartconnpool
go/bucketpool/bucketpool_test.go:181:12: cannot range over 20000 (untyped int constant) (typecheck)
        for range 20000 {
                  ^

not sure if the issue's with CI or with my machine as I've been having some issues setting up go1.22

@ajm188
Copy link
Contributor

ajm188 commented Feb 15, 2024

not sure if the issue's with CI or with my machine as I've been having some issues setting up go1.22

if you aren't on at least 1.22 this syntax is invalid, so that is probably the issue. what is the issue you are having setting up on your machine? i'm happy to help!

@Maniktherana
Copy link
Contributor Author

I did install go1.22 and it's now the default go version on my machine. These errors don't show up in vscode. Only shows up when I try to commit via my terminal 🤔

As for my setup I'm having issues with the Go lsp on vscode but I think that's unrelated.

@ajm188
Copy link
Contributor

ajm188 commented Feb 15, 2024

I just tried applying that change and didn't have a problem:

$ git commit -sS -m 'test commit'
2024/02/15 07:58:42 5 files OK
Linting go/bucketpool
[andrew/delete-vtctl-commands 12a6beabaa] test commit
 1 file changed, 1 insertion(+), 1 deletion(-)
$ git show HEAD -p
commit 12a6beabaa1dfdff4e2470871d4725fcea8c9203
Author: Andrew Mason <andrew@planetscale.com>
Date:   Thu Feb 15 07:58:39 2024 -0500

    test commit
    
    Signed-off-by: Andrew Mason <andrew@planetscale.com>

diff --git a/go/bucketpool/bucketpool_test.go b/go/bucketpool/bucketpool_test.go
index cc8bcff0ee..6f088caddc 100644
--- a/go/bucketpool/bucketpool_test.go
+++ b/go/bucketpool/bucketpool_test.go
@@ -178,7 +178,7 @@ func TestPoolWeirdMaxSize(t *testing.T) {
 
 func TestFuzz(t *testing.T) {
 	maxTestSize := 16384
-	for i := 0; i < 20000; i++ {
+	for range 20000 {
 		minSize := rand.Intn(maxTestSize)
 		if minSize == 0 {
 			minSize = 1

maybe you need to update golangci-lint after the go upgrade?

@ajm188
Copy link
Contributor

ajm188 commented Feb 15, 2024

@Maniktherana i think you've overlapped with some work i did here: #15194 you might want to rebase on that

Signed-off-by: Manik Rana <manikrana54@gmail.com>
Signed-off-by: Manik Rana <manikrana54@gmail.com>
Signed-off-by: Manik Rana <manikrana54@gmail.com>
@Maniktherana
Copy link
Contributor Author

@Maniktherana i think you've overlapped with some work i did here: #15194 you might want to rebase on that

done

Signed-off-by: Manik Rana <Manikrana54@gmail.com>
@Maniktherana Maniktherana marked this pull request as ready for review February 15, 2024 14:31
Comment on lines 32 to 34
assert.Equal(t, 0, l, "length = %v, want 0", l)
assert.Equal(t, int64(0), sz, "size = %v, want 0", sz)
assert.Equal(t, int64(5), c, "capacity = %v, want 5", c)
Copy link
Member

Choose a reason for hiding this comment

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

When using the asset I don't think it is useful to add the expectations in the message field. The method already prints the wanted value and the received value:

Error:      	Not equal: 
        	            	expected: 1
        	            	actual  : 0
        	Test:       	TestInitialState
        	Messages:   	size = 0, want 1

This apply to all the files you modified in this PR

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done, I'll wait on the the CI checks tho

@frouioui frouioui removed the NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work label Feb 15, 2024
@frouioui frouioui added Type: Enhancement Logical improvement (somewhere between a bug and feature) Type: Testing Component: General Changes throughout the code base and removed NeedsWebsiteDocsUpdate What it says NeedsIssue A linked issue is missing for this Pull Request NeedsBackportReason If backport labels have been applied to a PR, a justification is required labels Feb 15, 2024
Maniktherana and others added 2 commits February 15, 2024 23:09
Signed-off-by: Manik Rana <Manikrana54@gmail.com>
Signed-off-by: Manik Rana <manikrana54@gmail.com>
Copy link
Contributor

@ajm188 ajm188 left a comment

Choose a reason for hiding this comment

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

looking good, pending previous review

@@ -45,6 +45,7 @@ func TestDo(t *testing.T) {

assert.Equal(t, "bar (string)", fmt.Sprintf("%v (%T)", v, v), "incorrect Do value")
assert.NoError(t, err, "got Do error")

Copy link
Contributor

Choose a reason for hiding this comment

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

on your next pass you should ditch this extra line

Signed-off-by: Manik Rana <manikrana54@gmail.com>
Signed-off-by: Manik Rana <manikrana54@gmail.com>
Signed-off-by: Manik Rana <manikrana54@gmail.com>
@@ -54,8 +54,8 @@ func TestDoErr(t *testing.T) {
return "", someErr
})

assert.ErrorIs(t, err, someErr, "incorrect Do error")
assert.Empty(t, v, "unexpected non-empty value")
assert.Equal(t, someErr, err)
Copy link
Contributor

Choose a reason for hiding this comment

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

why did we change from ErrorIs to Equal here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this may have been due to me using the test-rewriter and then resolving merge conflicts when rebasing. I'll revert this

assert.ErrorIs(t, err, someErr, "incorrect Do error")
assert.Empty(t, v, "unexpected non-empty value")
assert.Equal(t, someErr, err)
assert.Equal(t, "", v)
Copy link
Contributor

Choose a reason for hiding this comment

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

why not assert.Empty?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll revert this one too

@@ -98,7 +98,7 @@ func TestDoDupSuppress(t *testing.T) {
c <- "bar"
wg2.Wait()
got := atomic.LoadInt32(&calls)
assert.True(t, got > 0 && got < n, "number of calls not between 0 and %d", n)
assert.True(t, got > 0 && got < n)
Copy link
Contributor

Choose a reason for hiding this comment

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

assert.Greater and assert.Lesser might produce more useful error messages

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ah, these are mostly from using test writer tool you mentioned. I'll go back and fix the tests

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah that tool isn't perfect but it's definitely a start over doing this all by hand! just needs some touching up after

if !triggered {
t.Errorf("static listener failed to trigger")
}
assert.True(t, triggered)
Copy link
Contributor

Choose a reason for hiding this comment

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

this will read just as expected true, got false, so we might want a more descriptive message here (i.e. "expected static listener to trigger")

Copy link
Contributor

Choose a reason for hiding this comment

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

same applies to other assert.True and assert.False instances

Copy link
Member

@deepthi deepthi Feb 20, 2024

Choose a reason for hiding this comment

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

Agree with @ajm188 on this. If you are up to doing a PR against the test rewriter tool, it will be good to try and preserve the custom messages.

Copy link
Member

@deepthi deepthi left a comment

Choose a reason for hiding this comment

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

Nice work. I can approve once the current set of comments are addressed. I didn't see anything else that needs to change.

if !triggered {
t.Errorf("static listener failed to trigger")
}
assert.True(t, triggered)
Copy link
Member

@deepthi deepthi Feb 20, 2024

Choose a reason for hiding this comment

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

Agree with @ajm188 on this. If you are up to doing a PR against the test rewriter tool, it will be good to try and preserve the custom messages.

@@ -315,7 +315,6 @@ func TestShouldEmitLog(t *testing.T) {
}

for _, tt := range tests {
tt := tt
Copy link
Member

Choose a reason for hiding this comment

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

👍

Signed-off-by: Manik Rana <manikrana54@gmail.com>
Signed-off-by: Manik Rana <manikrana54@gmail.com>
@Maniktherana
Copy link
Contributor Author

I've refactored the tests @deepthi @ajm188
this should cover it

Copy link
Member

@deepthi deepthi left a comment

Choose a reason for hiding this comment

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

I found a couple of small things that still need to fixed. Will give final approval after that.

@@ -245,10 +227,8 @@ func TestDispatchUpdate(t *testing.T) {

ev := &testUpdateEvent{}
DispatchUpdate(ev, "hello")
assert.True(t, triggered)
Copy link
Member

Choose a reason for hiding this comment

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

You missed this one, it should have a message passed in.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Comment on lines 785 to 787
assert.
// rule 1: d = d2*q +r
True(t, d.Equal(d2.mul(q).Add(r)))
Copy link
Member

Choose a reason for hiding this comment

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

Should fix the formatting. Comment should go on the line above the assertion.

Signed-off-by: Manik Rana <manikrana54@gmail.com>
t.Errorf("misses = %v, want 0", c)
}
assert.Equal(t, 0, l)
assert.Equal(t, int64(0), sz)
Copy link
Collaborator

Choose a reason for hiding this comment

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

nitpick: you can use assert.EqualValue to avoid casting the literals to int64

Copy link
Contributor Author

@Maniktherana Maniktherana Feb 21, 2024

Choose a reason for hiding this comment

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

done

if Zero.Sign() != 0 {
t.Errorf("%q should have sign 0", Zero)
}
assert.Equal(t, 0, Zero.Sign())
Copy link
Collaborator

Choose a reason for hiding this comment

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

nitpick: assert.Zero

if c.Cmp(a) == 0 {
t.Errorf("error")
}
assert.NotEqual(t, 0, c.Cmp(a))
Copy link
Collaborator

Choose a reason for hiding this comment

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

nitpick: assert.NotZero

Signed-off-by: Manik Rana <manikrana54@gmail.com>
Signed-off-by: Manik Rana <manikrana54@gmail.com>
Signed-off-by: Manik Rana <manikrana54@gmail.com>
Copy link
Contributor

@ajm188 ajm188 left a comment

Choose a reason for hiding this comment

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

i'm on-board with the assert.True messages and the Greater/Less, but i have questions about the Zero and EqualValues

Comment on lines +32 to +37
assert.Zero(t, l)
assert.EqualValues(t, 0, sz)
assert.EqualValues(t, 5, c)
assert.EqualValues(t, 0, e)
assert.EqualValues(t, 0, h)
assert.EqualValues(t, 0, m)
Copy link
Contributor

Choose a reason for hiding this comment

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

why did we make these changes?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I received a review earlier to use EqualValues to avoid typecasting and Zero/NotZero. I thought it would apply to other portions of the PR such as here

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry @Maniktherana I completely missed those review comments. I'm good to merge!

@ajm188 ajm188 merged commit 6fec119 into vitessio:main Feb 22, 2024
102 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: General Changes throughout the code base Type: Enhancement Logical improvement (somewhere between a bug and feature) Type: Testing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants