test: add unit tests for stratumserver module#3886
Conversation
Expand stratumserver coverage beyond RPC serde helpers to exercise WorkersList lifecycle, parse_params, RpcError constructors, and Handler RPC routing (login, keepalive, status, getjobtemplate, submit stale). Addresses mimblewimble#3210.
- Use JSON-RPC -32603 for RpcError::internal_error (match api/json_rpc) - Apply cargo fmt on the login invalid-params assertion - Store test chain DBs under target/tmp/ to avoid repo-root clutter
…ors, re-login test - Share a single lazily-initialized test chain across RPC routing tests instead of opening one LMDB env per test; avoids the intermittent "Invalid argument" failures seen under default test parallelism. - test_last_seen_updates: seed last_seen to UNIX_EPOCH and assert strict '>' instead of '>=' after a sleep, so a no-op update would fail the test. - Add cheap submit error coverage: missing params (-32600) and an edge_bits value that is neither primary nor secondary PoW (-32502), neither requiring a mined proof. - Add a re-login test documenting that logging in again replaces the previous login/agent rather than being rejected. - Add a WorkersList-level test for get_stats on an unknown worker id (-32603), in place of exercising it through handle_rpc_requests, which panics on an out-of-range worker_id before reaching handle_status (see PR comment).
|
Pushed a follow-up commit addressing the remaining open comments:
One thing I could not implement as suggested — found a real bug while trying: the request to add a I confirmed this with a throwaway test ( Since fixing that unchecked indexing is a production-code behavior change beyond this test-only PR's scope, I left it alone here and instead added Happy to open a separate PR/issue for the panic if that's useful — a connected worker's |
wiesche89
left a comment
There was a problem hiding this comment.
The behavioral coverage is useful and the earlier review points are addressed. The test module still feels broader and more heavily documented than the surrounding code, though. Could we trim tests that only mirror constructor field assignments, inline single-use helpers, and keep comments for non obvious behavior and setup constraints?
| let _ = fs::remove_dir_all(dir_name); | ||
| } | ||
|
|
||
| /// Path under `target/tmp/` so interrupted tests do not leave dirs in the repo root. |
There was a problem hiding this comment.
These helpers are only used by shared_test_chain. Could we inline them there to keep the test setup smaller?
| tx | ||
| } | ||
|
|
||
| /// A single chain instance shared by every RPC routing test below. |
There was a problem hiding this comment.
This comment is quite long. Could we shorten it and just note that write-path tests need their own chain?
| } | ||
|
|
||
| // ---------------------------------------- | ||
| // RpcRequest / RpcResponse serde (existing) |
There was a problem hiding this comment.
“Existing” is review context rather than useful source documentation. Could we drop it?
| workers | ||
| .login(id0, "alice".to_string(), "agent-a".to_string()) | ||
| .unwrap(); | ||
| // Logging in again (e.g. after a reconnect) replaces the previous |
There was a problem hiding this comment.
This is a second login for the same worker, not a reconnect, which would create a new worker ID. Could we drop the reconnect example?
Drop constructor-mirror tests for State and Worker, inline the single-use chain dir helpers into shared_test_chain, shorten its doc comment, and remove review-context wording from comments.
Summary
stratumserverunit tests from 8 (RPC serde only) to 34, covering the main testable units without consensus or full PoW mining.RpcErrorconstructors and JSON conversionparse_paramssuccess/failure paths (LoginParams,SubmitParams)WorkersListadd/login/remove/re-login, stats updates, broadcast vs unicast, network stats, unknown-worker error pathsHandlerRPC routing:keepalive, unknown method, login,status,getjobtemplate(synced + syncing), stale/invalid/missing-params/invalid-edge_bitssubmit,build_block_template,last_seenAddresses #3210.
Behavior fix (found via these tests, not just a test change):
RpcError::internal_error()used a positive JSON-RPC error code (32603). Per the JSON-RPC 2.0 spec (andapi/src/json_rpc.rs, which already uses the correct value), this is now-32603. This changes the wire response a stratum client receives on an internal error from{"code": 32603, ...}to{"code": -32603, ...}.RPC routing tests share a single lazily-initialized test chain (via
OnceLock) instead of opening one LMDB env per test, to avoid intermittentInvalid argumentfailures under default test-runner parallelism.Test plan
cargo test -p grin_servers --lib mining::stratumserver::tests(34 passed, default parallelism; also verified with--test-threads=16)Not covered in this PR (possible follow-ups): valid share/block submit with real PoW, TCP accept-loop integration tests. See also the review comment about a pre-existing panic on out-of-range
worker_idinhandle_rpc_requests.