-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathflushes.rs
More file actions
105 lines (85 loc) · 2.66 KB
/
flushes.rs
File metadata and controls
105 lines (85 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright 2024 FastLabs Developers
//
// 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 std::sync::Arc;
use std::sync::Barrier;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering;
use logforth_append_async::AsyncBuilder;
use logforth_core::Append;
use logforth_core::Diagnostic;
use logforth_core::Error;
use logforth_core::Trap;
use logforth_core::record::Record;
#[derive(Debug)]
struct BarrierAppend {
started: Arc<AtomicBool>,
barrier: Arc<Barrier>,
}
impl Append for BarrierAppend {
fn append(&self, _: &Record, _: &[Box<dyn Diagnostic>]) -> Result<(), Error> {
Ok(())
}
fn flush(&self) -> Result<(), Error> {
self.started.store(true, Ordering::SeqCst);
self.barrier.wait();
Ok(())
}
}
#[derive(Debug)]
struct FailingFlush;
impl Append for FailingFlush {
fn append(&self, _: &Record, _: &[Box<dyn Diagnostic>]) -> Result<(), Error> {
Ok(())
}
fn flush(&self) -> Result<(), Error> {
Err(Error::new("flush failed"))
}
}
#[derive(Debug)]
struct NoopTrap;
impl Trap for NoopTrap {
fn trap(&self, _: &Error) {}
}
#[test]
fn flush_waits_for_worker_completion() {
let started = Arc::new(AtomicBool::new(false));
let barrier = Arc::new(Barrier::new(2));
let append = BarrierAppend {
started: started.clone(),
barrier: barrier.clone(),
};
let async_append = AsyncBuilder::new("async-flush-wait").append(append).build();
let barrier_for_main = barrier.clone();
let flush_handle = std::thread::spawn(move || async_append.flush());
while !started.load(Ordering::SeqCst) {
std::thread::yield_now();
}
assert!(!flush_handle.is_finished());
barrier_for_main.wait();
flush_handle
.join()
.expect("flush thread panicked")
.expect("flush should succeed");
}
#[test]
fn flush_propagates_errors() {
let async_append = AsyncBuilder::new("async-flush-error")
.trap(NoopTrap)
.append(FailingFlush)
.build();
let err = async_append.flush().unwrap_err();
let err = err.to_string();
assert!(err.contains("failed to flush"));
assert!(err.contains("flush failed"));
}