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

testing(dot): create dot unit tests #2112

Merged
merged 12 commits into from
Apr 4, 2022
Merged

Conversation

edwardmack
Copy link
Member

Changes

  • Create unit tests dot package: build_spec_test.go, config_test.go, import_test.go, node_test.go, service_test.go and utils_test.go

Tests

go test ./dot/ -v

Issues

Primary Reviewer

@codecov
Copy link

codecov bot commented Dec 10, 2021

Codecov Report

Merging #2112 (d894db6) into development (ded3a0b) will increase coverage by 3.22%.
The diff coverage is 48.55%.

@@               Coverage Diff               @@
##           development    #2112      +/-   ##
===============================================
+ Coverage        58.50%   61.73%   +3.22%     
===============================================
  Files              214      216       +2     
  Lines            28182    28179       -3     
===============================================
+ Hits             16489    17395     +906     
+ Misses           10037     9039     -998     
- Partials          1656     1745      +89     
Flag Coverage Δ
unit-tests 61.73% <48.55%> (+3.22%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
dot/state/service.go 49.55% <0.00%> (-0.68%) ⬇️
dot/telemetry/telemetry.go 0.00% <0.00%> (ø)
lib/babe/babe.go 3.00% <0.00%> (-0.58%) ⬇️
lib/services/services.go 81.39% <ø> (ø)
dot/utils.go 33.33% <23.07%> (+33.33%) ⬆️
internal/trie/node/decode.go 96.07% <55.55%> (-3.93%) ⬇️
dot/services.go 73.59% <60.00%> (+73.59%) ⬆️
dot/node.go 56.21% <70.76%> (+56.21%) ⬆️
lib/trie/database.go 52.13% <82.35%> (+1.17%) ⬆️
dot/build_spec.go 80.58% <100.00%> (+80.58%) ⬆️
... and 12 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9efde47...d894db6. Read the comment docs.

dot/build_spec_test.go Outdated Show resolved Hide resolved
dot/build_spec_test.go Outdated Show resolved Hide resolved
Comment on lines 147 to 152
if tt.want != nil {
assert.Equal(t, tt.want.genesis.Name, got.genesis.Name)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you assert the entire BuildSpec here 🤔 ?

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't check the entire BuildSpec because it would require comparing the code entry which is very large.

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe just force set the code field to empty for the actual value and assert the rest?

Like

actual := ...
actual.code = nil
expected := &BuildSpec{...}
assert.Equal(t, expected, actual)

Copy link
Contributor

Choose a reason for hiding this comment

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

can we resolve this or does this need to updated?

Copy link
Member Author

Choose a reason for hiding this comment

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

Updated to force set genesis Raw and Runtime fields before comparing.

dot/build_spec_test.go Outdated Show resolved Hide resolved
Comment on lines 196 to 195
if tt.want != nil {
assert.Equal(t, tt.want.genesis.Name, got.genesis.Name)
}
Copy link
Contributor

@qdm12 qdm12 Dec 13, 2021

Choose a reason for hiding this comment

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

Can you assert the entire buildSpec? Or is there some random aspect to it? Or set unexported fields?

Copy link
Contributor

Choose a reason for hiding this comment

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

can we resolve this or does this need to updated?

Copy link
Member Author

Choose a reason for hiding this comment

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

Updated to test buildSpec.

dot/build_spec_test.go Outdated Show resolved Hide resolved
dot/config_test.go Outdated Show resolved Hide resolved
dot/config_test.go Outdated Show resolved Hide resolved
dot/config_test.go Outdated Show resolved Hide resolved
dot/core/service.go Outdated Show resolved Hide resolved
dot/build_spec_test.go Outdated Show resolved Hide resolved
dot/core/service.go Outdated Show resolved Hide resolved
dot/import_test.go Outdated Show resolved Hide resolved
dot/import_test.go Outdated Show resolved Hide resolved
dot/node.go Outdated Show resolved Hide resolved
dot/node.go Outdated Show resolved Hide resolved
@timwu20
Copy link
Contributor

timwu20 commented Jan 18, 2022

@edwardmack this branch is failing to compile.

@edwardmack edwardmack force-pushed the ed/create_dot_unit_tests_b branch 2 times, most recently from 46dd3ab to a2bebe1 Compare January 25, 2022 21:46
@edwardmack
Copy link
Member Author

I've cleaned up conflicts with this, it should be ready for re-review.

@ChainSafe ChainSafe deleted a comment from edwardmack Jan 26, 2022
lib/blocktree/blocktree.go Outdated Show resolved Hide resolved
Copy link
Contributor

@noot noot left a comment

Choose a reason for hiding this comment

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

basically looks good just a few comments!

Copy link
Contributor

@timwu20 timwu20 left a comment

Choose a reason for hiding this comment

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

TestNewTestGenesisAndRuntime is failing when I run this locally.

@edwardmack
Copy link
Member Author

@timwu20 TestNewTestGenesisAndRuntime is working for me locally, please let me know what error you are seeing and I'll try to fix it.

@timwu20
Copy link
Contributor

timwu20 commented Feb 3, 2022

@timwu20 TestNewTestGenesisAndRuntime is working for me locally, please let me know what error you are seeing and I'll try to fix it.

=== RUN   TestNewTestGenesisAndRuntime
=== RUN   TestNewTestGenesisAndRuntime/working_example
    /Users/timothywu/dev/gossamer/dot/utils_test.go:328:
        	Error Trace:	utils_test.go:328
        	Error:      	Should be true
        	Test:       	TestNewTestGenesisAndRuntime/working_example
--- FAIL: TestNewTestGenesisAndRuntime (1.90s)
    --- FAIL: TestNewTestGenesisAndRuntime/working_example (1.81s)
FAIL
FAIL	github.com/ChainSafe/gossamer/dot	2.514s

Looks like on MacOS it writes to a different folder than /tmp. My path is /var/folders/16/fwrrm5ss4gb6f5d7j6przgf80000gn/T/TestNewTestGenesisAndRuntime3015430054/004/genesis.json

Copy link
Contributor

@timwu20 timwu20 left a comment

Choose a reason for hiding this comment

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

Can you move all the NewTestXXX functions into a test file. All the references to these functions are only in test files. Then you don't need to write unit tests for them 😉

@edwardmack
Copy link
Member Author

@timwu20 TestNewTestGenesisAndRuntime is working for me locally, please let me know what error you are seeing and I'll try to fix it.

=== RUN   TestNewTestGenesisAndRuntime
=== RUN   TestNewTestGenesisAndRuntime/working_example
    /Users/timothywu/dev/gossamer/dot/utils_test.go:328:
        	Error Trace:	utils_test.go:328
        	Error:      	Should be true
        	Test:       	TestNewTestGenesisAndRuntime/working_example
--- FAIL: TestNewTestGenesisAndRuntime (1.90s)
    --- FAIL: TestNewTestGenesisAndRuntime/working_example (1.81s)
FAIL
FAIL	github.com/ChainSafe/gossamer/dot	2.514s

Looks like on MacOS it writes to a different folder than /tmp. My path is /var/folders/16/fwrrm5ss4gb6f5d7j6przgf80000gn/T/TestNewTestGenesisAndRuntime3015430054/004/genesis.json

I've updated this test.

@edwardmack
Copy link
Member Author

Can you move all the NewTestXXX functions into a test file. All the references to these functions are only in test files. Then you don't need to write unit tests for them wink

Good point, I've moved these.

cmd/gossamer/config_test.go Outdated Show resolved Hide resolved
@edwardmack
Copy link
Member Author

I've created a new PR #2446, to replace this PR. The new PR has been cleaned-up to show the files that were actually updated for this PR. I believe all the comments for this have been addressed, please see review the new PR.

@qdm12 qdm12 force-pushed the ed/create_dot_unit_tests_b branch from 15993cc to c145175 Compare March 29, 2022 08:45
Copy link
Contributor

@qdm12 qdm12 left a comment

Choose a reason for hiding this comment

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

@edwardmack there are still a few unresolved comments I read through above.

I also took the liberty to force push the squashed commits from your new branch from #2112 to this PR's branch so Github keeps track of already reviewed changes, to avoid re-reviewing 4k+ lines from scratch for me and other reviewers.

FYI Github changes web view works by comparing squashed changes vs the first commit the branch has branched out off. That means that rewriting history (like force pushing squashed commits here) is fine and won't affect reviewers changes viewed.

Comment on lines +163 to -160
go func() {
<-node.started
node.Stop()
}()
err = node.Start()
require.NoError(t, err)
<-node.started
node.Stop()
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe it was better before? Start launches a goroutine to handle the node I think, so it should be fine to call all this synchronously. Plus if the start fails and returns a start error, node.started will never get closed

Copy link
Member Author

Choose a reason for hiding this comment

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

It seems that node.Start() blocks until node.Stop is called. There is a WaitGroup in start that waits until Done is called in Stop, so I needed to put the <-node.started in a routine before calling node.Start because this test was getting stuck never moving past start.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ooof yeah weird piece of code there. It's a bit out of scope, but maybe rename it to Run() instead of Start()? Since it doesn't really start and return, it 'runs' all the services in a blocking way.

dot/state/service.go Outdated Show resolved Hide resolved
go.mod Show resolved Hide resolved
@edwardmack
Copy link
Member Author

I've commented and/or addressed remaining comments, let me know if there are remaining issues.

Copy link
Contributor

@timwu20 timwu20 left a comment

Choose a reason for hiding this comment

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

good work Ed!

lib/babe/babe.go Outdated
NewServiceIFace(cfg *ServiceConfig) (ServiceIFace, error)
}

var _ ServiceBuilder = (*Builder)(nil)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are you using ServiceBuilder?

Why is using ServiceBuilder better than using NewServiceIFace directly?

I think you should be able to using NewServiceIFace directly

Copy link
Member Author

Choose a reason for hiding this comment

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

The ServiceBuilder interface was created so that we have an interface for generating mocks for that, mocks for this interface are used in service_test.go Test_nodeBuilder_createBABEService tests. I'm not sure how to generate mocks without an interface.

Copy link
Contributor

@timwu20 timwu20 Mar 30, 2022

Choose a reason for hiding this comment

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

You could have used an anonymous function signature instead of an interface. But you would have to manually write a dummy function that implemented that function signature for testing.

I don't think this interface needs to be defined in the babe package. You could move this interface to be defined in dot to localise it to nodeBuilder.createBABEServiceWithBuilder. It doesn't really make sense for the babe package to have to have an interface for one if its types constructors. Can we change this?

Copy link
Contributor

Choose a reason for hiding this comment

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

@edwardmack Have you addressed this?

lib/babe/babe.go Show resolved Hide resolved
lib/babe/babe.go Outdated Show resolved Hide resolved
dot/utils_test.go Outdated Show resolved Hide resolved
dot/utils_test.go Outdated Show resolved Hide resolved
dot/utils_integration_test.go Outdated Show resolved Hide resolved
dot/services.go Outdated Show resolved Hide resolved
dot/build_spec_test.go Show resolved Hide resolved
dot/build_spec_test.go Outdated Show resolved Hide resolved
dot/errors.go Outdated Show resolved Hide resolved
Copy link
Contributor

@timwu20 timwu20 left a comment

Choose a reason for hiding this comment

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

just the moving of the ServiceBuilder interface to where it's required.

lib/babe/babe.go Outdated
NewServiceIFace(cfg *ServiceConfig) (ServiceIFace, error)
}

var _ ServiceBuilder = (*Builder)(nil)
Copy link
Contributor

@timwu20 timwu20 Mar 30, 2022

Choose a reason for hiding this comment

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

You could have used an anonymous function signature instead of an interface. But you would have to manually write a dummy function that implemented that function signature for testing.

I don't think this interface needs to be defined in the babe package. You could move this interface to be defined in dot to localise it to nodeBuilder.createBABEServiceWithBuilder. It doesn't really make sense for the babe package to have to have an interface for one if its types constructors. Can we change this?

lib/babe/babe.go Show resolved Hide resolved
@edwardmack
Copy link
Member Author

I've updated to address comments, and moved ServiceBuilder interface to dot package. Should be ready for re-re-review.

got, err := builder.createSystemService(tt.args.cfg, tt.args.service)
assert.ErrorIs(t, err, tt.err)

// TODO: change this check to assert.Equal after state.Service interface is implemented.
Copy link
Contributor

Choose a reason for hiding this comment

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

There are five TODO statements in this file, remove when applicable

@edwardmack edwardmack merged commit 620535d into development Apr 4, 2022
@edwardmack edwardmack deleted the ed/create_dot_unit_tests_b branch April 4, 2022 16:50
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.

Create dot unit tests
5 participants