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(tests): refactor tests/utils/request_utils.go #2385

Merged
merged 7 commits into from
Mar 31, 2022

Conversation

qdm12
Copy link
Contributor

@qdm12 qdm12 commented Mar 17, 2022

Changes

  • Inject context to PostRPC and PostRPCWithRetry
  • Swap method and url arguments for PostRPC and PostRPCWithRetry
  • Remove some global variables
  • Wrap all possible errors
  • PostRPCWithRetry retries until context is canceled
  • Keep 1s timeout for RPC calls

Tests

Existing tests suites passing

Issues

Primary Reviewer

@codecov
Copy link

codecov bot commented Mar 17, 2022

Codecov Report

Merging #2385 (13fcf3e) into development (11f6c1c) will decrease coverage by 0.02%.
The diff coverage is n/a.

@@               Coverage Diff               @@
##           development    #2385      +/-   ##
===============================================
- Coverage        58.50%   58.47%   -0.03%     
===============================================
  Files              214      214              
  Lines            28004    28004              
===============================================
- Hits             16383    16375       -8     
- Misses            9963     9971       +8     
  Partials          1658     1658              
Flag Coverage Δ
unit-tests 58.47% <ø> (-0.03%) ⬇️

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

Impacted Files Coverage Δ
dot/network/inbound.go 85.96% <0.00%> (-14.04%) ⬇️
dot/state/block_notify.go 81.90% <0.00%> (-8.58%) ⬇️
dot/network/block_announce.go 59.32% <0.00%> (-5.09%) ⬇️
dot/network/notifications.go 66.07% <0.00%> (-1.43%) ⬇️
dot/network/connmgr.go 89.04% <0.00%> (-1.37%) ⬇️
dot/network/service.go 56.84% <0.00%> (-0.47%) ⬇️
dot/sync/chain_sync.go 53.42% <0.00%> (-0.31%) ⬇️
lib/runtime/wasmer/imports.go 48.40% <0.00%> (-0.05%) ⬇️
dot/network/host.go 65.37% <0.00%> (+1.06%) ⬆️
lib/blocktree/blocktree.go 54.71% <0.00%> (+1.08%) ⬆️
... and 4 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 11f6c1c...13fcf3e. Read the comment docs.

@@ -38,13 +39,14 @@ type testCase struct {
skip bool
}

func getResponse(t *testing.T, test *testCase) interface{} {
func getResponse(ctx context.Context, t *testing.T, test *testCase) interface{} {
Copy link
Contributor

Choose a reason for hiding this comment

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

could this function not just accept a time.Duration for timeout and handle context creation and cancellation within this function? Doesn't look like the contexts are used for anything other than timeout functionality.

Copy link
Contributor Author

@qdm12 qdm12 Mar 23, 2022

Choose a reason for hiding this comment

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

Yes for now, but the idea I have at the back of my head is to use it with cancel() in the future once it's more event-driven.

Context offer the flexibility of timeouts + event driven cancellation, and inherit from each other, so we should really be using them in e2e tests instead of timers/signal channels which would make a mess of a code especially regarding 'cascaded cancelation'.

Also contexts are part of the std library, and most standard library use them like http.NewRequestWithContext or exec.CommandContext

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Further down the line in my branches, this context is actually used to cancel operations, for example if a node crashes unexpectedly.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd argue that we shouldn't be using context for cancellation. Dave Cheney wrote about it in his blog.

The documentation for the context package strongly recommends that context.Context is only for request scoped values:

With regards to cascading cancellation, you can just have a cancel channel that is closed and can be listened on by multiple goroutines.

I know using contexts for cancellation is a common pattern. It's something to keep an eye on for Go 2. They may remove the cancellation from the context package.

Copy link
Contributor Author

@qdm12 qdm12 Mar 31, 2022

Choose a reason for hiding this comment

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

Interesting blog post, and I 100% agree context values are the fruit of demoniac developers 👿 (except they're 100% needed for i.e. a server metrics given restrictive signatures of handlers).

It's also sad they didn't figure out how to wait for a goroutine to finish on context cancellation, forcing to still have another signal/error channel to wait.

Now for the closing of a signal channel, there are 3 limitations I can think of:

  • Handling timers to close a channel is painful (always need to check to empty the timer channel as well), and even more when nesting them.
  • You cannot close a channel multiple times, so you would have to pretty much re-invent contexts for this (check if the channel is closed etc.)
  • The standard library accepts contexts to cancel IO operations, but not close channels and often have no easy Stop method. One could use a signal channel to cancel a context down the callers, but it feels wrong to me.

tests/utils/chain.go Show resolved Hide resolved
tests/rpc/rpc_01-system_test.go Outdated Show resolved Hide resolved
tests/rpc/system_integration_test.go Outdated Show resolved Hide resolved
@qdm12 qdm12 force-pushed the qdm12/tests-refactor-2 branch 3 times, most recently from fd9d785 to dc9ee5f Compare March 28, 2022 17:07
qdm12 and others added 4 commits March 29, 2022 15:17
- Inject context to `PostRPC` and `PostRPCWithRetry`
- Swap method and url arguments for `PostRPC` and `PostRPCWithRetry`
- Remove some global variables
- Wrap all possible errors
- `PostRPCWithRetry` retries until context is canceled
- Keep 1s timeout for RPC calls
Co-authored-by: Eclésio Junior <eclesiomelo.1@gmail.com>
Copy link
Member

@EclesioMeloJunior EclesioMeloJunior left a comment

Choose a reason for hiding this comment

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

lgtm! just a small change

tests/utils/gossamer_utils.go Outdated Show resolved Hide resolved
Co-authored-by: Eclésio Junior <eclesiomelo.1@gmail.com>
@qdm12 qdm12 merged commit e700d5e into development Mar 31, 2022
@qdm12 qdm12 deleted the qdm12/tests-refactor-2 branch March 31, 2022 10:05
rrtti pushed a commit to polkadot-fellows/seeding that referenced this pull request Sep 29, 2022
I am Quentin Mc Gaw, a software engineer working the Go Polkadot host **Gossamer**.
I have been working full time on Gossamer since October 2021, mostly on the state trie and storage.
I have also made a [few minor pull requests](https://github.com/w3f/polkadot-spec/pulls?q=is%3Apr+is%3Aclosed+author%3Aqdm12) to the Polkadot specification repository.

I am requesting to join the Fellowship at rank 1.

## Main contributions

### Gossamer

- Fix memory leaks
  - Trie encoding buffer pools usage fixed [#2009](ChainSafe/gossamer#2009)
  - Fix state map of tries memory leak [#2286](ChainSafe/gossamer#2286)
  - Fix sync benchmark [#2234](ChainSafe/gossamer#2234)
- Trie proof fixes ([#2604](ChainSafe/gossamer#2604), [#2661](ChainSafe/gossamer#2661))
- Fix end to end tests orchestration ([#2470](ChainSafe/gossamer#2470), [#2452](ChainSafe/gossamer#2452), [#2385](ChainSafe/gossamer#2385), [#2370](ChainSafe/gossamer#2370))
- State trie statistics ([#2378](ChainSafe/gossamer#2378), [#2310](ChainSafe/gossamer#2310), [#2272](ChainSafe/gossamer#2272))
- State trie fixes and improvements
  - Only deep copy nodes when mutation is certain [#2352](ChainSafe/gossamer#2352) and [#2223](ChainSafe/gossamer#2223)
  - Only deep copy necessary fields of a node [#2384](ChainSafe/gossamer#2384)
  - Use Merkle values for database keys instead of always hash [#2725](ChainSafe/gossamer#2725)
  - Opportunistic parallel Merkle value commputing [#2081](ChainSafe/gossamer#2081)
- Grandpa capped number of tracked messages ([#2490](ChainSafe/gossamer#2490), [#2485](ChainSafe/gossamer#2485))
- Add pprof HTTP service for profiling [#1991](ChainSafe/gossamer#1991)

Ongoing work:

- State trie lazy loading and caching
- State trie v1 support ([#2736](ChainSafe/gossamer#2736), [#2747](ChainSafe/gossamer#2747), [#2687](ChainSafe/gossamer#2687), [#2686](ChainSafe/gossamer#2686), [#2685](ChainSafe/gossamer#2685), [#2673](ChainSafe/gossamer#2673), [#2611](ChainSafe/gossamer#2611), [#2530](ChainSafe/gossamer#2530))

### Polkadot specification

➡️ [Pull requests from qdm12](https://github.com/w3f/polkadot-spec/pulls?q=is%3Apr+is%3Aclosed+author%3Aqdm12)
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.

4 participants