Skip to content

Commit bee7705

Browse files
committed
Fixes merge conflicts
Refactors message handling in mock network tests for better readability and clarity. This ensures messages are processed as expected in concurrent scenarios.
2 parents f854757 + ffb93bf commit bee7705

3 files changed

Lines changed: 24 additions & 8 deletions

File tree

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,8 @@ install-tools: install-lint install-rustfmt
2121
.PHONEY test:
2222
test:
2323
@echo "Running tests"
24-
@cargo test
24+
@cargo test
25+
.PHONY format:
26+
format:
27+
@echo "Formatting code"
28+
@cargo fmt --all -- --check

src/network/mock/hub.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,13 @@ impl NetworkHub {
2121
}
2222

2323
/// Creates a new mock network with the given identifier and registers it in the hub.
24-
pub fn new_mock_network(hub: Arc<Mutex<Self>>, identifier: Identifier) -> anyhow::Result<Arc<Mutex<MockNetwork>>> {
25-
let inner_hub = hub.lock().map_err(|_| anyhow!("Failed to acquire lock on hub"))?;
24+
pub fn new_mock_network(
25+
hub: Arc<Mutex<Self>>,
26+
identifier: Identifier,
27+
) -> anyhow::Result<Arc<Mutex<MockNetwork>>> {
28+
let inner_hub = hub
29+
.lock()
30+
.map_err(|_| anyhow!("Failed to acquire lock on hub"))?;
2631
let mut inner_networks = inner_hub
2732
.networks
2833
.write()

src/network/mock/network_test.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ use std::sync::{Arc, Mutex, Barrier};
44
use std::thread;
55
use crate::core::testutil::fixtures::random_identifier;
66
use crate::network::mock::hub::NetworkHub;
7+
use crate::network::Payload::TestMessage;
8+
use crate::network::{Message, MessageProcessor, Network};
9+
use std::collections::HashSet;
10+
use std::sync::{Arc, Barrier, Mutex};
11+
use std::thread;
712

813
#[derive(Debug)]
914
struct MockMessageProcessor {
@@ -125,9 +130,8 @@ fn test_concurrent_message_sending() {
125130
let mock_net_2 = NetworkHub::new_mock_network(hub, id_2).unwrap();
126131

127132
// Create 10 different message contents
128-
let message_contents: Vec<String> = (0..10)
129-
.map(|i| format!("Concurrent message {i}"))
130-
.collect();
133+
let message_contents: Vec<String> =
134+
(0..10).map(|i| format!("Concurrent message {i}")).collect();
131135

132136
// Set up a barrier to synchronize all threads
133137
let barrier = Arc::new(Barrier::new(10));
@@ -142,7 +146,7 @@ fn test_concurrent_message_sending() {
142146

143147
let handle = thread::spawn(move || {
144148
let message = Message {
145-
payload: Payload::TestMessage(content),
149+
payload: TestMessage(content),
146150
target_node_id: id_1_copy,
147151
};
148152

@@ -165,7 +169,10 @@ fn test_concurrent_message_sending() {
165169
// Verify that all messages were received
166170
let processor = msg_proc_1.lock().unwrap();
167171
for content in message_contents {
168-
assert!(processor.has_seen(&content), "Message '{content}' was not received");
172+
assert!(
173+
processor.has_seen(&content),
174+
"Message '{content}' was not received"
175+
);
169176
println!("Message '{content}' was successfully processed");
170177
}
171178
}

0 commit comments

Comments
 (0)