-
Notifications
You must be signed in to change notification settings - Fork 1
feat: add test-utils feature to allow connections to be force closed from the server side #63
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
base: main
Are you sure you want to change the base?
Conversation
…from the server side
✔️ 09cb1bb - Conventional commits check succeeded. |
WalkthroughA new feature flag named Changes
Sequence Diagram(s)sequenceDiagram
participant Test as Test Code
participant SbdServer
participant CSlot
Test->>SbdServer: drop_all_connections()
activate SbdServer
SbdServer->>CSlot: drop_all_connections()
activate CSlot
CSlot->>CSlot: Remove all active connections
deactivate CSlot
SbdServer-->>Test: (async complete)
deactivate SbdServer
Estimated code review effort🎯 2 (Simple) | ⏱️ ~7 minutes Note ⚡️ Unit Test Generation is now available in beta!Learn more here, or try it out under "Finishing Touches" below. ✨ Finishing Touches
🧪 Generate unit tests
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
rust/sbd-server/Cargo.toml
(1 hunks)rust/sbd-server/src/cslot.rs
(2 hunks)rust/sbd-server/src/lib.rs
(1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (2)
rust/sbd-server/src/lib.rs (1)
rust/sbd-server/src/cslot.rs (1)
drop_all_connections
(296-323)
rust/sbd-server/src/cslot.rs (1)
rust/sbd-server/src/lib.rs (1)
drop_all_connections
(644-646)
🔇 Additional comments (3)
rust/sbd-server/Cargo.toml (1)
60-60
: LGTM! Clean feature flag addition.The
test-utils
feature flag is properly positioned and follows Rust conventions for test-only functionality.rust/sbd-server/src/lib.rs (1)
640-646
: LGTM! Well-designed test utility method.The
drop_all_connections
method is properly implemented with:
- Appropriate feature gating with
test-utils
- Clear documentation explaining its fault simulation purpose
- Clean delegation to the underlying
CSlot
implementationrust/sbd-server/src/cslot.rs (1)
208-208
: LGTM! Useful logging addition.The logging statement provides helpful debugging information for connection management.
#[cfg(feature = "test-utils")] | ||
pub(crate) async fn drop_all_connections(&self) { | ||
let keys = { | ||
let guard = self.0.lock().expect("poisoned"); | ||
guard.pk_to_index.keys().cloned().collect::<Vec<_>>() | ||
}; | ||
|
||
for pk in keys { | ||
if let Ok((uniq, index, _ws)) = self.get_sender(&pk) { | ||
let mut lock = self.0.lock().unwrap(); | ||
|
||
match lock.slab.get(index) { | ||
None => return, | ||
Some(s) => { | ||
if s.uniq != uniq { | ||
return; | ||
} | ||
} | ||
} | ||
|
||
lock.slab.remove(index); | ||
lock.pk_to_index.retain(|_, i| *i != index); | ||
lock.ip_to_index.retain(|_, v| { | ||
v.retain(|i| *i != index); | ||
!v.is_empty() | ||
}); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix early returns that prevent processing all connections.
The method has logic issues that cause it to exit early instead of processing all connections:
Apply this diff to fix the early returns:
match lock.slab.get(index) {
- None => return,
+ None => continue,
Some(s) => {
if s.uniq != uniq {
- return;
+ continue;
}
}
}
The current implementation stops processing remaining connections if any single connection is not found or has a mismatched unique identifier, which defeats the purpose of dropping all connections.
Additionally, consider adding documentation about the intentional lack of close messages:
#[cfg(feature = "test-utils")]
+ /// Drop all connections without sending close messages.
+ /// This simulates a fault scenario for testing purposes.
pub(crate) async fn drop_all_connections(&self) {
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
#[cfg(feature = "test-utils")] | |
pub(crate) async fn drop_all_connections(&self) { | |
let keys = { | |
let guard = self.0.lock().expect("poisoned"); | |
guard.pk_to_index.keys().cloned().collect::<Vec<_>>() | |
}; | |
for pk in keys { | |
if let Ok((uniq, index, _ws)) = self.get_sender(&pk) { | |
let mut lock = self.0.lock().unwrap(); | |
match lock.slab.get(index) { | |
None => return, | |
Some(s) => { | |
if s.uniq != uniq { | |
return; | |
} | |
} | |
} | |
lock.slab.remove(index); | |
lock.pk_to_index.retain(|_, i| *i != index); | |
lock.ip_to_index.retain(|_, v| { | |
v.retain(|i| *i != index); | |
!v.is_empty() | |
}); | |
} | |
} | |
} | |
#[cfg(feature = "test-utils")] | |
/// Drop all connections without sending close messages. | |
/// This simulates a fault scenario for testing purposes. | |
pub(crate) async fn drop_all_connections(&self) { | |
let keys = { | |
let guard = self.0.lock().expect("poisoned"); | |
guard.pk_to_index.keys().cloned().collect::<Vec<_>>() | |
}; | |
for pk in keys { | |
if let Ok((uniq, index, _ws)) = self.get_sender(&pk) { | |
let mut lock = self.0.lock().unwrap(); | |
match lock.slab.get(index) { | |
None => continue, | |
Some(s) => { | |
if s.uniq != uniq { | |
continue; | |
} | |
} | |
} | |
lock.slab.remove(index); | |
lock.pk_to_index.retain(|_, i| *i != index); | |
lock.ip_to_index.retain(|_, v| { | |
v.retain(|i| *i != index); | |
!v.is_empty() | |
}); | |
} | |
} | |
} |
🤖 Prompt for AI Agents
In rust/sbd-server/src/cslot.rs between lines 295 and 323, the
drop_all_connections function uses early returns inside the loop that cause it
to stop processing all connections prematurely. To fix this, replace the return
statements with continue statements so the loop skips the current connection on
errors or mismatches but continues processing the rest. Also, add a comment
explaining that no close messages are sent intentionally when dropping
connections.
Summary by CodeRabbit