Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add unit tests for rolling file #36

Merged
merged 3 commits into from
Aug 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ jobs:
- uses: actions/checkout@v4
- uses: Swatinem/rust-cache@v2
- name: Run unit tests
run: cargo test -- --nocapture
run: cargo test --all-features -- --nocapture
- name: Run examples
run: |
cargo run --example simple_stdio
Expand Down
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,13 @@ serde_json = { version = "1.0", optional = true }
time = { version = "0.3", features = [
"formatting",
"parsing",
"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));
}
}
4 changes: 3 additions & 1 deletion src/append/rolling_file/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ pub use non_blocking::NonBlockingBuilder;
pub use non_blocking::WorkerGuard;
pub use rolling::RollingFileWriter;
pub use rolling::RollingFileWriterBuilder;
pub use rolling::Rotation;
pub use rotation::Rotation;

mod append;
mod clock;
mod non_blocking;
mod rolling;
mod rotation;
mod worker;

#[derive(Debug)]
Expand Down
Loading