Skip to content

Commit e6d2fa2

Browse files
committed
update testcase
1 parent b2b21e1 commit e6d2fa2

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

common_types/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ test = []
1616

1717
[dependencies]
1818
# In alphabetical order
19-
ahash = "0.8.3"
19+
ahash = { version = "0.8.2", default-features = false, features = ["runtime-rng"] }
2020
arrow = { workspace = true, optional = true }
2121
arrow_ext = { workspace = true }
2222
byteorder = "1.2"

common_util/src/partitioned_lock.rs

+16-10
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::{
66
hash::{Hash, Hasher},
77
sync::{Mutex, MutexGuard, RwLock, RwLockReadGuard, RwLockWriteGuard},
88
};
9-
9+
/// Warning, do not use this hash in non-memory scenarios.
1010
use common_types::hash::AHasher;
1111
/// Simple partitioned `RwLock`
1212
pub struct PartitionedRwLock<T> {
@@ -47,6 +47,11 @@ where
4747

4848
&self.partitions[(hasher.finish() as usize) & self.partition_mask]
4949
}
50+
51+
#[cfg(test)]
52+
fn get_partition_by_index(&self, index: usize) -> &RwLock<T>{
53+
&self.partitions[index]
54+
}
5055
}
5156

5257
/// Simple partitioned `Mutex`
@@ -79,9 +84,13 @@ where
7984
fn get_partition<K: Eq + Hash>(&self, key: &K) -> &Mutex<T> {
8085
let mut hasher = AHasher::default();
8186
key.hash(&mut hasher);
82-
8387
&self.partitions[(hasher.finish() as usize) & self.partition_mask]
8488
}
89+
90+
#[cfg(test)]
91+
fn get_partition_by_index(&self, index: usize) -> &Mutex<T>{
92+
&self.partitions[index]
93+
}
8594
}
8695

8796
#[cfg(test)]
@@ -128,13 +137,12 @@ mod tests {
128137
fn test_partitioned_mutex_vis_different_partition() {
129138
let tmp_vec: Vec<f32> = Vec::new();
130139
let test_locked_map = PartitionedMutex::new(tmp_vec, 4);
131-
let test_key_first = "test_key_first".to_string();
132-
let mutex_first = test_locked_map.get_partition(&test_key_first);
140+
let mutex_first = test_locked_map.get_partition_by_index(0);
141+
133142
let mut _tmp_data = mutex_first.lock().unwrap();
134143
assert!(mutex_first.try_lock().is_err());
135144

136-
let test_key_second = "test_key_second".to_string();
137-
let mutex_second = test_locked_map.get_partition(&test_key_second);
145+
let mutex_second = test_locked_map.get_partition_by_index(1);
138146
assert!(mutex_second.try_lock().is_ok());
139147
assert!(mutex_first.try_lock().is_err());
140148
}
@@ -143,13 +151,11 @@ mod tests {
143151
fn test_partitioned_rwmutex_vis_different_partition() {
144152
let tmp_vec: Vec<f32> = Vec::new();
145153
let test_locked_map = PartitionedRwLock::new(tmp_vec, 4);
146-
let test_key_first = "test_key_first".to_string();
147-
let mutex_first = test_locked_map.get_partition(&test_key_first);
154+
let mutex_first = test_locked_map.get_partition_by_index(0);
148155
let mut _tmp = mutex_first.write().unwrap();
149156
assert!(mutex_first.try_write().is_err());
150157

151-
let test_key_second = "test_key_second".to_string();
152-
let mutex_second_try_lock = test_locked_map.get_partition(&test_key_second);
158+
let mutex_second_try_lock = test_locked_map.get_partition_by_index(1);
153159
assert!(mutex_second_try_lock.try_write().is_ok());
154160
assert!(mutex_first.try_write().is_err());
155161
}

0 commit comments

Comments
 (0)