This repository has been archived by the owner on Nov 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 74
/
faulty.rs
181 lines (154 loc) · 4.77 KB
/
faulty.rs
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
use std::fmt::{self, Debug};
use crate::base::crypto::{Crypto, Key};
use crate::base::IntoRef;
use crate::error::Result;
use crate::trans::Eid;
use crate::volume::address::Span;
use crate::volume::storage::faulty_ctl::Controller;
use crate::volume::storage::mem::MemStorage;
use crate::volume::storage::Storable;
/// Faulty Storage
///
/// This storage is to simulate ramdon IO error, used for test only.
pub struct FaultyStorage {
inner: MemStorage,
ctlr: Controller,
}
impl FaultyStorage {
pub fn new(loc: &str) -> Self {
FaultyStorage {
inner: MemStorage::new(loc),
ctlr: Controller::new(),
}
}
}
impl Storable for FaultyStorage {
#[inline]
fn exists(&self) -> Result<bool> {
self.ctlr.make_random_error()?;
self.inner.exists()
}
#[inline]
fn connect(&mut self, force: bool) -> Result<()> {
self.inner.connect(force)
}
#[inline]
fn init(&mut self, crypto: Crypto, key: Key) -> Result<()> {
self.inner.init(crypto, key)
}
#[inline]
fn open(&mut self, crypto: Crypto, key: Key, force: bool) -> Result<()> {
self.ctlr.make_random_error()?;
self.inner.open(crypto, key, force)
}
#[inline]
fn get_super_block(&mut self, suffix: u64) -> Result<Vec<u8>> {
self.ctlr.make_random_error()?;
self.inner.get_super_block(suffix)
}
#[inline]
fn put_super_block(&mut self, super_blk: &[u8], suffix: u64) -> Result<()> {
self.ctlr.make_random_error()?;
self.inner.put_super_block(super_blk, suffix)
}
#[inline]
fn get_wal(&mut self, id: &Eid) -> Result<Vec<u8>> {
self.ctlr.make_random_error()?;
self.inner.get_wal(id)
}
#[inline]
fn put_wal(&mut self, id: &Eid, wal: &[u8]) -> Result<()> {
self.ctlr.make_random_error()?;
self.inner.put_wal(id, wal)
}
#[inline]
fn del_wal(&mut self, id: &Eid) -> Result<()> {
self.ctlr.make_random_error()?;
self.inner.del_wal(id)
}
#[inline]
fn get_address(&mut self, id: &Eid) -> Result<Vec<u8>> {
self.ctlr.make_random_error()?;
self.inner.get_address(id)
}
#[inline]
fn put_address(&mut self, id: &Eid, addr: &[u8]) -> Result<()> {
self.ctlr.make_random_error()?;
self.inner.put_address(id, addr)
}
#[inline]
fn del_address(&mut self, id: &Eid) -> Result<()> {
self.ctlr.make_random_error()?;
self.inner.del_address(id)
}
#[inline]
fn get_blocks(&mut self, dst: &mut [u8], span: Span) -> Result<()> {
self.ctlr.make_random_error()?;
self.inner.get_blocks(dst, span)
}
#[inline]
fn put_blocks(&mut self, span: Span, blks: &[u8]) -> Result<()> {
self.ctlr.make_random_error()?;
self.inner.put_blocks(span, blks)
}
#[inline]
fn del_blocks(&mut self, span: Span) -> Result<()> {
self.ctlr.make_random_error()?;
self.inner.del_blocks(span)
}
#[inline]
fn flush(&mut self) -> Result<()> {
self.ctlr.make_random_error()?;
self.inner.flush()
}
#[inline]
fn destroy(&mut self) -> Result<()> {
unimplemented!()
}
}
impl Debug for FaultyStorage {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("FaultyStorage")
.field("inner", &self.inner)
.finish()
}
}
impl IntoRef for FaultyStorage {}
#[cfg(test)]
mod tests {
use super::*;
use crate::base::init_env;
use crate::error::Error;
#[test]
fn static_storages() {
init_env();
let crypto = Crypto::default();
let key = Key::new_empty();
let loc = "foo";
let loc2 = "bar";
let id = Eid::new();
let id2 = Eid::new();
let buf = vec![1, 2, 3];
let buf2 = vec![4, 5, 6];
{
let mut fs = FaultyStorage::new(&loc);
let mut fs2 = FaultyStorage::new(&loc2);
fs.init(crypto.clone(), key.clone()).unwrap();
fs2.init(crypto.clone(), key.clone()).unwrap();
fs.put_address(&id, &buf).unwrap();
fs2.put_address(&id2, &buf2).unwrap();
}
{
let mut fs = FaultyStorage::new(&loc);
let mut fs2 = FaultyStorage::new(&loc2);
assert!(fs.exists().unwrap());
assert!(fs2.exists().unwrap());
fs.open(crypto.clone(), key.clone(), false).unwrap();
fs2.open(crypto.clone(), key.clone(), false).unwrap();
assert_eq!(fs.get_address(&id).unwrap(), buf);
assert_eq!(fs.get_address(&id2).unwrap_err(), Error::NotFound);
assert_eq!(fs2.get_address(&id2).unwrap(), buf2);
assert_eq!(fs2.get_address(&id).unwrap_err(), Error::NotFound);
}
}
}