Skip to content

Commit

Permalink
test: add unit tests for rolling file
Browse files Browse the repository at this point in the history
  • Loading branch information
1996fanrui committed Aug 11, 2024
1 parent 48ab6df commit e8b34f3
Show file tree
Hide file tree
Showing 5 changed files with 317 additions and 14 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ jobs:
- uses: actions/checkout@v4
- uses: Swatinem/rust-cache@v2
- name: Run unit tests
run: cargo test -- --nocapture
run: |
cargo test -- --nocapture
cargo test --all-features -- --nocapture
- name: Run examples
run: |
cargo run --example simple_stdio
Expand Down
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ time = { version = "0.3", features = [
"macros",
], optional = true }

[dev-dependencies]
rand = "0.8.5"
tempfile = "3.3"

## Fastrace dependencies
[dependencies.fastrace]
optional = true
Expand Down
79 changes: 79 additions & 0 deletions src/append/rolling_file/clock.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright 2024 tison <[email protected]>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use time::OffsetDateTime;

#[derive(Debug)]
pub enum Clock {
DefaultClock,
#[cfg(test)]
ManualClock(ManualClock),
}

impl Clock {
pub fn now(&self) -> OffsetDateTime {
match self {
Clock::DefaultClock => OffsetDateTime::now_utc(),
#[cfg(test)]
Clock::ManualClock(clock) => clock.now(),
}
}

#[cfg(test)]
pub fn set_now(&mut self, new_time: OffsetDateTime) {
if let Clock::ManualClock(clock) = self {
clock.set_now(new_time);
}
}
}

/// The time could be reset.
#[derive(Debug)]
#[cfg(test)]
pub struct ManualClock {
fixed_time: OffsetDateTime,
}

#[cfg(test)]
impl ManualClock {
pub fn new(fixed_time: OffsetDateTime) -> ManualClock {
ManualClock { fixed_time }
}

fn now(&self) -> OffsetDateTime {
self.fixed_time
}

pub fn set_now(&mut self, new_time: OffsetDateTime) {
self.fixed_time = new_time;
}
}

#[cfg(test)]
mod tests {
use time::macros::datetime;

use super::*;

#[test]
fn test_manual_clock_adjusting() {
let mut clock = ManualClock {
fixed_time: datetime!(2023-01-01 12:00:00 UTC),
};
assert_eq!(clock.now(), datetime!(2023-01-01 12:00:00 UTC));

clock.set_now(datetime!(2024-01-01 12:00:00 UTC));
assert_eq!(clock.now(), datetime!(2024-01-01 12:00:00 UTC));
}
}
1 change: 1 addition & 0 deletions src/append/rolling_file/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub use rolling::RollingFileWriterBuilder;
pub use rotation::Rotation;

mod append;
mod clock;
mod non_blocking;
mod rolling;
mod rotation;
Expand Down
Loading

0 comments on commit e8b34f3

Please sign in to comment.