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

Fix TestPublishIdentityUpdate test #607

Merged
merged 1 commit into from
Mar 10, 2025
Merged

Fix TestPublishIdentityUpdate test #607

merged 1 commit into from
Mar 10, 2025

Conversation

mkysel
Copy link
Collaborator

@mkysel mkysel commented Mar 10, 2025

The test nonce manager (which is not backed by a DB) was buggy.

Summary by CodeRabbit

  • Tests

    • Updated test scenarios to accurately validate behaviors under both normal and cancelled contexts.
  • New Features

    • Introduced a new approach for managing and recycling identifiers, improving efficiency through a heap-based structure and enhanced context error handling.

@mkysel mkysel requested a review from a team as a code owner March 10, 2025 20:22
Copy link

coderabbitai bot commented Mar 10, 2025

Walkthrough

The changes update test scenarios and nonce management logic. In the blockchain publisher tests, the names, context usage, and parameters of two test cases are swapped. In the nonce manager tests, a new heap type is introduced to manage and recycle nonces. The test manager gains an abandoned heap field, and the nonce generation process now checks for context cancellation, retrieves a nonce from the heap if available, or uses the current value. The cancelation function is updated to return used nonces back to the heap.

Changes

File(s) Change Summary
pkg/blockchain/blockchainPublisher_test.go In TestPublishIdentityUpdate, swapped test scenarios: the "happy path" case now uses a 104-byte identity update with a background context (expecting no error), while the "cancelled context" case uses a 100-byte update with a cancelled context (expecting an error).
pkg/blockchain/nonceManager_test.go Introduced a new type Int64Heap implementing heap methods for int64 values. Modified TestNonceManager by adding an abandoned field to manage recycled nonces. Updated GetNonce to check for cancelled context and use the heap, and modified the cancel function to push nonces back onto the heap.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant NonceManager
    participant Heap

    Client->>NonceManager: GetNonce(ctx)
    alt Context Cancelled?
        NonceManager-->>Client: Return error
    else
        alt Abandoned nonce available?
            NonceManager->>Heap: Pop smallest nonce
            Heap-->>NonceManager: Return nonce
            NonceManager-->>Client: Return recycled nonce
        else
            NonceManager->>NonceManager: Use current nonce and increment counter
            NonceManager-->>Client: Return new nonce
        end
    end
    Client->>NonceManager: Cancel() after use
    NonceManager->>Heap: Push nonce back for reuse
Loading

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 (1.62.2)

Error: can't load config: the Go language version (go1.23) used to build golangci-lint is lower than the targeted Go version (1.24)
Failed executing command with error: can't load config: the Go language version (go1.23) used to build golangci-lint is lower than the targeted Go version (1.24)

✨ Finishing Touches
  • 📝 Generate Docstrings

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (2)
pkg/blockchain/nonceManager_test.go (2)

15-38: Well-implemented heap interface for nonce management

The Int64Heap implementation properly implements all the required heap interface methods. This provides an efficient way to store and retrieve abandoned nonces for reuse.

The Peek method is a useful addition that allows checking the smallest element without removing it, though it returns -1 for empty heaps which could be confusing as -1 could be mistaken for a valid value in some contexts.

Consider using a more explicit approach for the empty heap case in Peek():

func (h *Int64Heap) Peek() int64 {
	if len(*h) == 0 {
-		return -1 // Return an invalid value if empty
+		panic("cannot peek an empty heap") // Or return (0, false) if you change the return type
	}
	return (*h)[0]
}

70-75: Updated nonce cancel function to recycle nonces

The nonce cancellation now properly pushes the nonce back to the abandoned heap, allowing it to be reused later. This completes the nonce recycling mechanism.

The comment "// No-op" at line 75 is no longer accurate since the Cancel function now performs an action.

Remove the outdated comment:

Cancel: func() {
	tm.mu.Lock()
	defer tm.mu.Unlock()
	tm.abandoned.Push(nonce)
-}, // No-op
+},
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d3536e7 and 6848be8.

📒 Files selected for processing (2)
  • pkg/blockchain/blockchainPublisher_test.go (2 hunks)
  • pkg/blockchain/nonceManager_test.go (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Build pre-baked anvil-xmtpd
🔇 Additional comments (6)
pkg/blockchain/blockchainPublisher_test.go (2)

59-64: Test case renamed with correct expectations

The test case has been properly renamed from "happy path" to "cancelled context" with appropriate changes:

  • Using a cancelled context
  • Expecting an error to occur
  • Reduced size of identityUpdate to 100 bytes

This change aligns the test case name with its actual behavior and expectations.


73-78: Test case renamed with correct expectations

The test case has been properly renamed from "cancelled context" to "happy path" with appropriate changes:

  • Using a background context
  • Expecting successful execution (no error)
  • Increased size of identityUpdate to 104 bytes

This change correctly represents a successful execution path for the test.

pkg/blockchain/nonceManager_test.go (4)

4-4: Added heap import for nonce recycling mechanism

The container/heap package is correctly imported to support the new nonce recycling functionality.


44-44: Added abandoned nonce tracking

The TestNonceManager now properly tracks abandoned nonces with the Int64Heap field, which enables efficient nonce recycling.


52-54: Added context cancellation check

This change properly checks if the context is already cancelled before attempting to get a nonce, which prevents unnecessary operations and returns the appropriate error.


59-65: Implemented nonce recycling mechanism

The logic now efficiently reuses nonces from the abandoned heap before generating new ones. This should improve resource usage by preventing nonce values from growing unnecessarily.

The implementation correctly:

  1. Checks if abandoned nonces are available
  2. Pops the smallest one if available (maintaining order)
  3. Falls back to incrementing the nonce counter if none are available

@mkysel mkysel merged commit 0be95dc into main Mar 10, 2025
6 of 7 checks passed
@mkysel mkysel deleted the mkysel/fix-test branch March 10, 2025 22:32
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.

2 participants