Skip to content

Commit 36bc937

Browse files
committed
Move previous utils for unit tests to common
1 parent d002490 commit 36bc937

File tree

2 files changed

+155
-146
lines changed

2 files changed

+155
-146
lines changed

spdlog/src/test_utils/common.rs

Lines changed: 153 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,163 @@
66
// This file will be handled in `build.rs` for code generation to workaround the
77
// rustdoc bug https://github.com/rust-lang/rust/issues/67295
88

9-
use std::sync::Arc;
9+
use std::{
10+
env,
11+
fmt::Write,
12+
sync::{atomic::*, Arc, Mutex},
13+
thread::sleep,
14+
time::Duration,
15+
};
1016

17+
use atomic::Atomic;
1118
use spdlog::{
12-
formatter::{Formatter, Pattern, PatternFormatter},
13-
prelude::*,
14-
sink::WriteSink,
19+
formatter::{FmtExtraInfo, Formatter, Pattern, PatternFormatter},
20+
sink::{Sink, WriteSink},
21+
Error, ErrorHandler, LevelFilter, Logger, LoggerBuilder, Record, Result, StringBuf,
1522
};
1623

24+
//////////////////////////////////////////////////
25+
26+
pub struct CounterSink {
27+
level_filter: Atomic<LevelFilter>,
28+
log_counter: AtomicUsize,
29+
flush_counter: AtomicUsize,
30+
payloads: Mutex<Vec<String>>,
31+
delay_duration: Option<Duration>,
32+
}
33+
34+
impl CounterSink {
35+
#[must_use]
36+
pub fn new() -> Self {
37+
Self::with_delay(None)
38+
}
39+
40+
#[must_use]
41+
pub fn with_delay(duration: Option<Duration>) -> Self {
42+
Self {
43+
level_filter: Atomic::new(LevelFilter::All),
44+
log_counter: AtomicUsize::new(0),
45+
flush_counter: AtomicUsize::new(0),
46+
payloads: Mutex::new(vec![]),
47+
delay_duration: duration,
48+
}
49+
}
50+
51+
#[must_use]
52+
pub fn log_count(&self) -> usize {
53+
self.log_counter.load(Ordering::Relaxed)
54+
}
55+
56+
#[must_use]
57+
pub fn flush_count(&self) -> usize {
58+
self.flush_counter.load(Ordering::Relaxed)
59+
}
60+
61+
#[must_use]
62+
pub fn payloads(&self) -> Vec<String> {
63+
self.payloads.lock().unwrap().clone()
64+
}
65+
66+
pub fn reset(&self) {
67+
self.log_counter.store(0, Ordering::Relaxed);
68+
self.flush_counter.store(0, Ordering::Relaxed);
69+
self.payloads.lock().unwrap().clear();
70+
}
71+
}
72+
73+
impl Sink for CounterSink {
74+
fn log(&self, record: &Record) -> Result<()> {
75+
if let Some(delay) = self.delay_duration {
76+
sleep(delay);
77+
}
78+
79+
self.log_counter.fetch_add(1, Ordering::Relaxed);
80+
81+
self.payloads
82+
.lock()
83+
.unwrap()
84+
.push(record.payload().to_string());
85+
86+
Ok(())
87+
}
88+
89+
fn flush(&self) -> Result<()> {
90+
if let Some(delay) = self.delay_duration {
91+
sleep(delay);
92+
}
93+
94+
self.flush_counter.fetch_add(1, Ordering::Relaxed);
95+
Ok(())
96+
}
97+
98+
fn level_filter(&self) -> LevelFilter {
99+
self.level_filter.load(Ordering::Relaxed)
100+
}
101+
102+
fn set_level_filter(&self, level_filter: LevelFilter) {
103+
self.level_filter.store(level_filter, Ordering::Relaxed);
104+
}
105+
106+
fn set_formatter(&self, _formatter: Box<dyn Formatter>) {
107+
// no-op
108+
}
109+
110+
fn set_error_handler(&self, _handler: Option<ErrorHandler>) {
111+
// no-op
112+
}
113+
}
114+
115+
impl Default for CounterSink {
116+
fn default() -> Self {
117+
Self::new()
118+
}
119+
}
120+
121+
//////////////////////////////////////////////////
122+
123+
// no modifications formatter, it will write `record` to `dest` as is.
124+
#[derive(Clone)]
125+
pub struct NoModFormatter {}
126+
127+
impl NoModFormatter {
128+
#[must_use]
129+
pub fn new() -> Self {
130+
Self {}
131+
}
132+
}
133+
134+
impl Formatter for NoModFormatter {
135+
fn format(&self, record: &Record, dest: &mut StringBuf) -> Result<FmtExtraInfo> {
136+
dest.write_str(record.payload())
137+
.map_err(Error::FormatRecord)?;
138+
139+
Ok(FmtExtraInfo::new())
140+
}
141+
142+
fn clone_box(&self) -> Box<dyn Formatter> {
143+
Box::new(self.clone())
144+
}
145+
}
146+
147+
impl Default for NoModFormatter {
148+
fn default() -> Self {
149+
Self::new()
150+
}
151+
}
152+
153+
//////////////////////////////////////////////////
154+
155+
#[must_use]
156+
pub fn test_logger_builder() -> LoggerBuilder {
157+
let mut builder = Logger::builder();
158+
builder.error_handler(|err| panic!("{}", err));
159+
builder
160+
}
161+
162+
pub fn assert_send<T: Send>() {}
163+
164+
pub fn assert_sync<T: Sync>() {}
165+
17166
#[must_use]
18167
pub fn echo_logger_from_pattern(
19168
pattern: impl Pattern + Clone + 'static,

spdlog/src/test_utils/unit_test.rs

Lines changed: 2 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,9 @@
33
// In this file, you can use public or private items from spdlog-rs as you wish,
44
// as they will be used from unit tests only.
55

6-
use std::{env, fmt::Write, fs, path::PathBuf, thread::sleep, time::Duration};
6+
use std::{env, fs, path::PathBuf};
77

8-
use crate::{
9-
formatter::{FmtExtraInfo, Formatter},
10-
sink::Sink,
11-
sync::*,
12-
Error, ErrorHandler, LevelFilter, Logger, LoggerBuilder, Record, Result, StringBuf,
13-
};
8+
use crate::sync::*;
149

1510
pub static TEST_LOGS_PATH: Lazy<PathBuf> = Lazy::new(|| {
1611
let path = env::current_exe()
@@ -21,138 +16,3 @@ pub static TEST_LOGS_PATH: Lazy<PathBuf> = Lazy::new(|| {
2116
fs::create_dir_all(&path).unwrap();
2217
path
2318
});
24-
25-
pub struct CounterSink {
26-
level_filter: Atomic<LevelFilter>,
27-
log_counter: AtomicUsize,
28-
flush_counter: AtomicUsize,
29-
payloads: Mutex<Vec<String>>,
30-
delay_duration: Option<Duration>,
31-
}
32-
33-
// no modifications formatter, it will write `record` to `dest` as is.
34-
#[derive(Clone)]
35-
pub struct NoModFormatter {}
36-
37-
impl CounterSink {
38-
#[must_use]
39-
pub fn new() -> Self {
40-
Self::with_delay(None)
41-
}
42-
43-
#[must_use]
44-
pub fn with_delay(duration: Option<Duration>) -> Self {
45-
Self {
46-
level_filter: Atomic::new(LevelFilter::All),
47-
log_counter: AtomicUsize::new(0),
48-
flush_counter: AtomicUsize::new(0),
49-
payloads: Mutex::new(vec![]),
50-
delay_duration: duration,
51-
}
52-
}
53-
54-
#[must_use]
55-
pub fn log_count(&self) -> usize {
56-
self.log_counter.load(Ordering::Relaxed)
57-
}
58-
59-
#[must_use]
60-
pub fn flush_count(&self) -> usize {
61-
self.flush_counter.load(Ordering::Relaxed)
62-
}
63-
64-
#[must_use]
65-
pub fn payloads(&self) -> Vec<String> {
66-
self.payloads.lock_expect().clone()
67-
}
68-
69-
pub fn reset(&self) {
70-
self.log_counter.store(0, Ordering::Relaxed);
71-
self.flush_counter.store(0, Ordering::Relaxed);
72-
self.payloads.lock_expect().clear();
73-
}
74-
}
75-
76-
impl Sink for CounterSink {
77-
fn log(&self, record: &Record) -> Result<()> {
78-
if let Some(delay) = self.delay_duration {
79-
sleep(delay);
80-
}
81-
82-
self.log_counter.fetch_add(1, Ordering::Relaxed);
83-
84-
self.payloads
85-
.lock_expect()
86-
.push(record.payload().to_string());
87-
88-
Ok(())
89-
}
90-
91-
fn flush(&self) -> Result<()> {
92-
if let Some(delay) = self.delay_duration {
93-
sleep(delay);
94-
}
95-
96-
self.flush_counter.fetch_add(1, Ordering::Relaxed);
97-
Ok(())
98-
}
99-
100-
fn level_filter(&self) -> LevelFilter {
101-
self.level_filter.load(Ordering::Relaxed)
102-
}
103-
104-
fn set_level_filter(&self, level_filter: LevelFilter) {
105-
self.level_filter.store(level_filter, Ordering::Relaxed);
106-
}
107-
108-
fn set_formatter(&self, _formatter: Box<dyn Formatter>) {
109-
// no-op
110-
}
111-
112-
fn set_error_handler(&self, _handler: Option<ErrorHandler>) {
113-
// no-op
114-
}
115-
}
116-
117-
impl Default for CounterSink {
118-
fn default() -> Self {
119-
Self::new()
120-
}
121-
}
122-
123-
impl NoModFormatter {
124-
#[must_use]
125-
pub fn new() -> Self {
126-
Self {}
127-
}
128-
}
129-
130-
impl Formatter for NoModFormatter {
131-
fn format(&self, record: &Record, dest: &mut StringBuf) -> Result<FmtExtraInfo> {
132-
dest.write_str(record.payload())
133-
.map_err(Error::FormatRecord)?;
134-
135-
Ok(FmtExtraInfo::new())
136-
}
137-
138-
fn clone_box(&self) -> Box<dyn Formatter> {
139-
Box::new(self.clone())
140-
}
141-
}
142-
143-
impl Default for NoModFormatter {
144-
fn default() -> Self {
145-
Self::new()
146-
}
147-
}
148-
149-
#[must_use]
150-
pub fn test_logger_builder() -> LoggerBuilder {
151-
let mut builder = Logger::builder();
152-
builder.error_handler(|err| panic!("{}", err));
153-
builder
154-
}
155-
156-
pub fn assert_send<T: Send>() {}
157-
158-
pub fn assert_sync<T: Sync>() {}

0 commit comments

Comments
 (0)