Skip to content

Commit b255938

Browse files
committed
Improve tests. Reduce clone() usages.
1 parent ba7070e commit b255938

File tree

5 files changed

+20
-29
lines changed

5 files changed

+20
-29
lines changed

src/cor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -710,5 +710,5 @@ fn test_cor_new() {
710710
cor_start!(_cor1);
711711
cor_start!(_cor2);
712712

713-
thread::sleep(time::Duration::from_millis(100));
713+
thread::sleep(time::Duration::from_millis(10));
714714
}

src/handler.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,13 +275,13 @@ fn test_handler_new() {
275275
}));
276276
println!("Test");
277277

278-
thread::sleep(time::Duration::from_millis(50));
278+
thread::sleep(time::Duration::from_millis(10));
279279

280280
assert_eq!(true, h.is_alive());
281281
assert_eq!(true, h.is_started());
282282

283283
h.stop();
284-
thread::sleep(time::Duration::from_millis(50));
284+
thread::sleep(time::Duration::from_millis(10));
285285

286286
assert_eq!(false, h.is_alive());
287287
assert_eq!(true, h.is_started());

src/monadio.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ fn test_monadio_new() {
236236
let latch = CountDownLatch::new(1);
237237
let latch2 = latch.clone();
238238

239-
thread::sleep(time::Duration::from_millis(100));
239+
thread::sleep(time::Duration::from_millis(10));
240240

241241
let subscription = Arc::new(SubscriptionFunc::new(move |x: Arc<String>| {
242242
println!("monadio_async {:?}", x); // monadio_async ok
@@ -262,7 +262,7 @@ fn test_monadio_new() {
262262
handler_observe_on.post(RawFunc::new(move || {}));
263263
handler_observe_on.post(RawFunc::new(move || {}));
264264
}
265-
thread::sleep(time::Duration::from_millis(100));
265+
thread::sleep(time::Duration::from_millis(10));
266266

267267
// Waiting for being unlcoked
268268
latch.clone().wait();

src/publisher.rs

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,7 @@ where
8383
) -> Arc<SubscriptionFunc<X>> {
8484
let _func = Arc::new(Mutex::new(func));
8585
self.subscribe_fn(move |x: Arc<X>| {
86-
let _func = _func.clone();
87-
let func = &mut *_func.lock().unwrap();
88-
(func)(x);
86+
(_func.lock().unwrap())(x);
8987
})
9088
}
9189
pub fn unsubscribe(&mut self, s: Arc<SubscriptionFunc<X>>) {
@@ -136,7 +134,7 @@ impl<X: Send + Sync + 'static> Observable<X, SubscriptionFunc<X>> for Publisher<
136134
observer.as_ref().clone().close_stream();
137135
}
138136

139-
for (index, obs) in self.observers.clone().iter().enumerate() {
137+
for (index, obs) in self.observers.iter().enumerate() {
140138
if obs.get_id() == id {
141139
// println!("delete_observer({});", observer);
142140
self.observers.remove(index);
@@ -145,34 +143,25 @@ impl<X: Send + Sync + 'static> Observable<X, SubscriptionFunc<X>> for Publisher<
145143
}
146144
}
147145
fn notify_observers(&mut self, val: Arc<X>) {
148-
let _observers = self.observers.clone();
149-
let observers = Arc::new(_observers);
146+
let observers = self.observers.clone();
150147
let mut _do_sub = Arc::new(move || {
151-
let mut _observers = observers.clone();
152-
let observers = Arc::make_mut(&mut _observers);
153-
154148
for (_, observer) in observers.iter().enumerate() {
155149
{
156150
observer.on_next(val.clone());
157151
}
158152
}
159153
});
160154

161-
let sub_handler_thread = &mut self.sub_handler;
162-
let mut do_sub_thread_ob = _do_sub.clone();
163-
164-
match sub_handler_thread {
155+
match &mut self.sub_handler {
165156
Some(ref mut sub_handler) => {
166-
let mut do_sub_thread_sub = _do_sub.clone();
167-
168157
sub_handler.lock().unwrap().post(RawFunc::new(move || {
169-
let sub = Arc::make_mut(&mut do_sub_thread_sub);
158+
let sub = Arc::make_mut(&mut _do_sub);
170159

171160
(sub)();
172161
}));
173162
}
174163
None => {
175-
let sub = Arc::make_mut(&mut do_sub_thread_ob);
164+
let sub = Arc::make_mut(&mut _do_sub);
176165
(sub)();
177166
}
178167
};
@@ -195,15 +184,15 @@ async fn test_publisher_stream() {
195184
let mut pub1 = Publisher::new_with_handlers(Some(_h.clone()));
196185
//*
197186
let s = pub1.subscribe_as_stream(Arc::new(SubscriptionFunc::new(move |x| {
198-
println!("{:?}: {:?}", "SS", x);
187+
println!("test_publisher_stream {:?}: {:?}", "SS", x);
199188
})));
200189
// */
201190
{
202191
let h = &mut _h.lock().unwrap();
203192

204-
println!("hh2");
193+
println!("test_publisher_stream hh2");
205194
h.start();
206-
println!("hh2 running");
195+
println!("test_publisher_stream hh2 running");
207196
}
208197
pub1.publish(1);
209198
pub1.publish(2);
@@ -213,12 +202,12 @@ async fn test_publisher_stream() {
213202
{
214203
let mut result = s.clone();
215204
for n in 1..5 {
216-
println!("{:?}: {:?}", n, "Before");
205+
println!("test_publisher_stream {:?}: {:?}", n, "Before");
217206
let item = result.next().await;
218207
if let Some(result) = item.clone() {
219208
(&mut got_list).push(result.clone());
220209
}
221-
println!("{:?}: {:?}", n, item);
210+
println!("test_publisher_stream {:?}: {:?}", n, item);
222211
}
223212
// got_list = s.collect::<Vec<_>>().await;
224213
}
@@ -252,6 +241,8 @@ async fn test_publisher_stream() {
252241
}));
253242
}
254243

244+
std::thread::sleep(std::time::Duration::from_millis(10));
245+
255246
got_list = s.clone().collect::<Vec<_>>().await;
256247
assert_eq!(
257248
vec![Arc::new(5), Arc::new(6), Arc::new(7), Arc::new(8),],

src/sync.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -587,13 +587,13 @@ fn test_will_sync_new() {
587587
h.stop();
588588
assert_eq!(false, h.is_alive());
589589
assert_eq!(false, h.is_started());
590-
h.start();
591590
h.add_callback(Arc::new(SubscriptionFunc::new(move |_v: Arc<i16>| {
592591
assert_eq!(1, *Arc::make_mut(&mut _v.clone()));
593592
latch2.countdown();
594593
})));
594+
h.start();
595595
latch.clone().wait();
596-
thread::sleep(time::Duration::from_millis(50));
596+
thread::sleep(time::Duration::from_millis(10));
597597
assert_eq!(false, h.is_alive());
598598
assert_eq!(true, h.is_started());
599599
assert_eq!(1, h.result().unwrap());

0 commit comments

Comments
 (0)