Skip to content

feat: Add Go tests for order book cancellation depth#4

Open
GautamKumarOffical wants to merge 3 commits into
jackjin1997:mainfrom
GautamKumarOffical:feat/orderbook-cancel-depth-tests
Open

feat: Add Go tests for order book cancellation depth#4
GautamKumarOffical wants to merge 3 commits into
jackjin1997:mainfrom
GautamKumarOffical:feat/orderbook-cancel-depth-tests

Conversation

@GautamKumarOffical

@GautamKumarOffical GautamKumarOffical commented Jun 19, 2026

Copy link
Copy Markdown

Summary

Adds Go tests for order book cancellation depth, closed-book behavior, and snapshot immutability in market/orderbook.

Changes

  • Added market/orderbook/orderbook_test.go with 7 test cases covering:
    • Cancelling a bid removes the matching bid level and deletes the order id
    • Cancelling an ask removes the matching ask level
    • Cancelling an unknown order returns ErrOrderNotFound
    • Closed books reject add/cancel operations with ErrBookClosed
    • Snapshots return copies so callers cannot mutate internal bid/ask slices
    • Cancelling one bid preserves other bid levels
    • Adding multiple orders then cancelling one behaves correctly

Testing

  • go test ./market/orderbook — all 7 tests pass
  • python3 build.py -m market — market module builds successfully
  • Diagnostic build log committed

Checklist

  • Tests pass locally
  • Diagnostic build log is committed
  • Changes are scoped to the PR purpose

Summary by CodeRabbit

  • Tests
    • Added a new orderbook test suite covering bid/ask cancellation effects, correct ErrOrderNotFound handling for unknown cancels, rejection of add/cancel actions when the book is closed, snapshot immutability (returns copies), and level-count correctness when adding and canceling multiple orders.

Signed-off-by: Gautam Kumar <gautamkumarofficial@users.noreply.github.com>
@coderabbitai

coderabbitai Bot commented Jun 19, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: abcaab83-6e69-4ec2-a0fb-047fb2e9ccf2

📥 Commits

Reviewing files that changed from the base of the PR and between df15a73 and 1425d07.

📒 Files selected for processing (2)
  • diagnostic/build-b9a939b7.json
  • market/orderbook/orderbook_test.go
🚧 Files skipped from review as they are similar to previous changes (2)
  • diagnostic/build-b9a939b7.json
  • market/orderbook/orderbook_test.go

📝 Walkthrough

Walkthrough

A new Go test file is added for the orderbook package, covering seven test cases: canceling bids/asks, error handling for unknown or duplicate cancels, closed-book rejection, snapshot copy semantics, multi-level preservation, and mixed add/cancel level count verification. A diagnostic build metadata JSON for commit b9a939b7 is also populated with run results and artifact references.

Changes

OrderBook Test Suite

Layer / File(s) Summary
Test helper and cancel/error behavior tests
market/orderbook/orderbook_test.go
Adds package setup, imports, and a newLimitOrder helper. Adds tests for canceling a bid (removes level, returns ErrOrderNotFound on re-cancel), canceling an ask (removes level), canceling an unknown order ID (ErrOrderNotFound), and rejecting AddOrder/CancelOrder after book close (ErrBookClosed).
Snapshot immutability and multi-level cancel tests
market/orderbook/orderbook_test.go
Adds tests verifying GetSnapshot() returns independent copies (mutating snapshot bid price does not affect internal state), that canceling one bid among multiple price levels preserves the others, and that adding two bids at the same price then canceling one yields correct bid/ask level counts.

Diagnostic Build Artifact

Layer / File(s) Summary
Diagnostic build metadata JSON
diagnostic/build-b9a939b7.json
Populates the previously empty file with generated_at timestamp, logd artifact path, password/decrypt_command fields, overall total_modules/passed/failed counts, per-module status for market (PASS), and a pr_note referencing encrypted artifact inclusion.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related issues

  • [$40 BOUNTY] [Go] Add order book cancellation depth tests #2: The PR directly implements the exact test cases and acceptance criteria listed in that issue (TestCancelBidRemovesLevelAndDeletesOrder, TestCancelAskRemovesLevel, TestCancelUnknownOrderReturnsErrOrderNotFound, TestClosedBookRejectsAddAndCancel, TestSnapshotReturnsCopies) and includes the required diagnostic build artifact for PR submission.

Poem

🐰 Hop, hop! The order book's neat,
Cancels checked from bid to beat.
Snapshots copied, levels stay,
Closed books turn new orders away.
The tests all pass — what a feat! 🎉

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The pull request title accurately and specifically describes the main change: adding Go tests for order book cancellation depth.
Description check ✅ Passed The pull request description includes all required sections with substantial detail: Summary explains the purpose, Changes lists the 7 test cases with specific scenarios, Testing documents the verification steps and build success, and the Checklist confirms critical items.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 golangci-lint (2.12.2)

level=error msg="[linters_context] typechecking error: pattern ./...: directory prefix . does not contain main module or its selected dependencies"


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 4

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@diagnostic/build-b9a939b7.json`:
- Line 18: The artifact field contains an absolute path that exposes local
filesystem details and potentially sensitive information like usernames. Replace
the absolute path value in the "artifact" field with a relative path or a
sanitized generic path that does not reveal the local build environment
structure or username information. For example, use a path relative to the
project root or a generic placeholder that maintains the same directory
structure without exposing infrastructure details.
- Around line 8-9: The password field and decrypt_command in the
diagnostic/build-b9a939b7.json file contain plaintext credentials that must be
removed from the committed file. Delete or redact the plaintext password value
(7e4f46fd84090245d881) from both the password field and the decrypt_command
field, then coordinate sharing the actual credentials through a secure
out-of-band channel such as a secrets manager or encrypted communication to the
maintainers instead of embedding them in version control.

In `@market/orderbook/orderbook_test.go`:
- Around line 173-180: After the length assertion for bids at line 175, add
additional assertions to validate the Price and Count (or Quantity) fields of
the remaining bid level bids[0]. This ensures that when two bids at the same
price have one cancelled, the remaining bid maintains correct price aggregation
and quantity/count semantics, catching potential regressions in the orderbook's
price level aggregation logic.
- Around line 150-153: The TestCancelBidPreservesOtherLevels test only verifies
the count of bid levels after cancellation, which could pass even if the wrong
level was removed. After checking that len(bids) equals 2, add specific
assertions to verify that the correct price levels remain: assert that the bid
level with price 99.0 does not exist in the bids slice, and that the bid levels
with prices 100.0 and 98.0 both remain present. This will ensure the test
actually validates that the correct order was cancelled and the intended levels
were preserved.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: c9af8e6b-b9e3-4eda-9ead-e5d1285a4895

📥 Commits

Reviewing files that changed from the base of the PR and between 2b54872 and df15a73.

📒 Files selected for processing (3)
  • diagnostic/build-b9a939b7.json
  • diagnostic/build-b9a939b7.logd
  • market/orderbook/orderbook_test.go

Comment on lines +8 to +9
"password": "7e4f46fd84090245d881",
"decrypt_command": "encryptly unpack diagnostic/build-b9a939b7.logd <outdir> --password 7e4f46fd84090245d881",

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Plaintext credential committed to repository.

The password field and decrypt_command contain the decryption credential in plaintext. Committing secrets to version control is a security anti-pattern—even if this file is removed before merge, the credential will persist in git history and remain accessible to anyone who can view the PR or has cloned the repository.

Consider sharing decryption credentials through a secure out-of-band channel (e.g., secrets manager, encrypted message to maintainers) rather than embedding them in committed files.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@diagnostic/build-b9a939b7.json` around lines 8 - 9, The password field and
decrypt_command in the diagnostic/build-b9a939b7.json file contain plaintext
credentials that must be removed from the committed file. Delete or redact the
plaintext password value (7e4f46fd84090245d881) from both the password field and
the decrypt_command field, then coordinate sharing the actual credentials
through a secure out-of-band channel such as a secrets manager or encrypted
communication to the maintainers instead of embedding them in version control.

Comment thread diagnostic/build-b9a939b7.json Outdated
Comment thread market/orderbook/orderbook_test.go
Comment thread market/orderbook/orderbook_test.go
- Fix absolute path in diagnostic JSON to use relative path
- Add price level assertions to TestCancelBidPreservesOtherLevels
- Add price and count assertions to TestAddMultipleOrdersAndCancelOne

Signed-off-by: Gautam Kumar <gautamkumarofficial@users.noreply.github.com>
@GautamKumarOffical

Copy link
Copy Markdown
Author

Fixed the review comments:

  1. diagnostic/build-b9a939b7.json — changed absolute artifact path to relative path
  2. orderbook_test.go — added price level assertions to TestCancelBidPreservesOtherLevels (verifies 100.0 and 98.0 remain, 99.0 is cancelled)
  3. orderbook_test.go — added price and count assertions to TestAddMultipleOrdersAndCancelOne (verifies remaining bid at 100.0 with count=1)

Note: the password and decrypt_command in the diagnostic JSON are required for the lobster-trap PR review workflow — maintainers need them to decrypt the .logd file.

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.

1 participant