etcd-io / raft

Raft library for maintaining a replicated state machine
Apache License 2.0
666 stars 164 forks source link

Revisit tests #146

Open pav-kv opened 9 months ago

pav-kv commented 9 months ago

This is an umbrella issue for all kinds of unit-test improvements. Unit-tests in this repository are significantly outdated, and need a revamp. For example:

Examples of things to fix/improve:

MrDXY commented 8 months ago

Hi @pav-kv, For the code you mentioned in "Eliminate Boilerplate", I am interested in improving it and have the time to do so. If I understand correctly, the following code

if sm.state != tt.wstate {
        t.Errorf("#%d.%d state = %v , want %v", i, j, sm.state, tt.wstate)
}

Should it be changed into something like this?

assert.Equal(t, tt.wstate, sm.state, "#%d.%d state = %v , want %v", i, j, sm.state, tt.wstate)

If you were expecting that, I would like to work on this as a starting point to contribute to etcd.

pav-kv commented 8 months ago

@MrDXY Yes, the idea is right. Feel free to send a PR.

In most cases we don't need the formatted text part. It's enough to do this:

assert.Equal(t, tt.wstate, sm.state)

There are some cases in which we need to add some information to the error, to identify the sub-test that failed. The example you quoted would then be like:

assert.Equal(t, tt.wstate, sm.state, "#%d.%d", i, j)

Also, t.Fatalf should correspond to require package calls, and t.Errorf should correspond to assert package calls.

MrDXY commented 8 months ago

Noted. Thanks! I'll give it a try.

MrDXY commented 8 months ago

Hi, @pav-kv I found multiple files that use t.Error/Fatal. While not all the files require a refactor, I am listing them for awareness. I plan to begin with raft_test.go, and will split this work into multiple PRs to make it easy to review. Would that be acceptable to you?

pav-kv commented 8 months ago

@MrDXY Please don't reference this issue from individual commits. This spams the history of this issue (see above: every time the commit is updated, it posts a new link to this issue). Instead, please reference the issue only from the PR description.

MrDXY commented 6 months ago

Hey @pav-kv , it's been a while since I was on this. IMHO, I think the node_util_test.go file doesn't need refactoring. If you agree, we can consider the refactor job for require/assert complete. What do you think? 😄

pav-kv commented 6 months ago

@MrDXY SGTM. Thanks a lot for this clean-up!