-
Notifications
You must be signed in to change notification settings - Fork 0
/
e2e.rs
201 lines (178 loc) · 6.91 KB
/
e2e.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
use std::{
fs::File,
io::{BufReader, BufWriter},
sync::Arc,
};
use assert_matches::assert_matches;
use csv::WriterBuilder;
use dashmap::DashMap;
use jouet_paiement::{
account::SimpleAccountTransactor,
model::{AccountSummary, AccountSummaryCsvWriter},
transaction_processor::SimpleTransactionProcessor,
transaction_stream_processor::{
async_csv_stream_processor::AsyncCsvStreamProcessor,
csv_stream_processor::CsvStreamProcessor, TransactionRecord,
TransactionRecordType::Deposit, TransactionStreamProcessError, TransactionStreamProcessor,
},
};
#[tokio::test]
async fn e2e_small_input_using_async_processor() {
let accounts = Arc::new(DashMap::new());
let processor = AsyncCsvStreamProcessor::new(
Arc::new(SimpleTransactionProcessor::new(
accounts.clone(),
Box::new(SimpleAccountTransactor::new()),
)),
DashMap::new(),
);
let file = File::open("tests/small_input.txt").unwrap();
let reader = BufReader::new(file);
processor.process(reader).await.unwrap();
processor.shutdown().await.unwrap();
let mut summaries: Vec<AccountSummary> =
accounts.iter().map(|entry| entry.value().into()).collect();
summaries.sort_by(|a, b| {
a.client_id
.partial_cmp(&b.client_id)
.expect("ClientId is not a float so there is no way this could return a `None`.")
});
assert_eq!(
String::from_utf8(AccountSummaryCsvWriter::write(summaries).unwrap()).unwrap(),
"\
client,available,held,total,locked\n\
1,9.0000,0.0000,9.0000,false\n\
2,6.0000,0.0000,6.0000,false\n"
);
}
#[tokio::test]
async fn e2e_small_input_with_transaction_process_error_using_async_processor() {
let accounts = Arc::new(DashMap::new());
let processor = AsyncCsvStreamProcessor::new(
Arc::new(SimpleTransactionProcessor::new(
accounts.clone(),
Box::new(SimpleAccountTransactor::new()),
)),
DashMap::new(),
);
let file = File::open("tests/small_input_with_transaction_process_error.txt").unwrap();
let reader = BufReader::new(file);
processor.process(reader).await.unwrap();
assert_matches!(
processor.shutdown().await,
Err(TransactionStreamProcessError::ProcessError(_))
);
let mut summaries: Vec<AccountSummary> =
accounts.iter().map(|entry| entry.value().into()).collect();
summaries.sort_by(|a, b| {
a.client_id
.partial_cmp(&b.client_id)
.expect("ClientId is not a float so there is no way this could return a `None`.")
});
assert_eq!(
String::from_utf8(AccountSummaryCsvWriter::write(summaries).unwrap()).unwrap(),
"\
client,available,held,total,locked\n\
1,7.0000,0.0000,7.0000,false\n\
2,3.0000,0.0000,3.0000,true\n"
);
}
#[tokio::test(flavor = "multi_thread", worker_threads = 16)]
#[ignore = "this test takes time to run and should be enabled ondemand"]
async fn e2e_large_input_using_async_processor() {
let accounts = Arc::new(DashMap::new());
let processor = AsyncCsvStreamProcessor::new(
Arc::new(SimpleTransactionProcessor::new(
accounts.clone(),
Box::new(SimpleAccountTransactor::new()),
)),
DashMap::new(),
);
create_test_file("/tmp/large_input.txt", create_test_records(10, 1_000_000));
let file = File::open("/tmp/large_input.txt").unwrap();
let reader = BufReader::new(file);
processor.process(reader).await.unwrap();
processor.shutdown().await.unwrap();
let mut summaries: Vec<AccountSummary> =
accounts.iter().map(|entry| entry.value().into()).collect();
summaries.sort_by(|a, b| {
a.client_id
.partial_cmp(&b.client_id)
.expect("ClientId is not a float so there is no way this could return a `None`.")
});
assert_eq!(
String::from_utf8(AccountSummaryCsvWriter::write(summaries).unwrap()).unwrap(),
"\
client,available,held,total,locked\n\
1,1000000.0000,0.0000,1000000.0000,false\n\
2,1000000.0000,0.0000,1000000.0000,false\n\
3,1000000.0000,0.0000,1000000.0000,false\n\
4,1000000.0000,0.0000,1000000.0000,false\n\
5,1000000.0000,0.0000,1000000.0000,false\n\
6,1000000.0000,0.0000,1000000.0000,false\n\
7,1000000.0000,0.0000,1000000.0000,false\n\
8,1000000.0000,0.0000,1000000.0000,false\n\
9,1000000.0000,0.0000,1000000.0000,false\n\
10,1000000.0000,0.0000,1000000.0000,false\n"
);
}
#[tokio::test]
#[ignore = "this test takes time to run and should be enabled ondemand"]
async fn e2e_large_input_using_blocking_processor() {
let accounts = Arc::new(DashMap::new());
let processor = CsvStreamProcessor::new(Box::new(SimpleTransactionProcessor::new(
accounts.clone(),
Box::new(SimpleAccountTransactor::new()),
)));
create_test_file("/tmp/large_input.txt", create_test_records(10, 1_000_000));
let file = File::open("/tmp/large_input.txt").unwrap();
let reader = BufReader::new(file);
processor.process(reader).await.unwrap();
let mut summaries: Vec<AccountSummary> =
accounts.iter().map(|entry| entry.value().into()).collect();
summaries.sort_by(|a, b| {
a.client_id
.partial_cmp(&b.client_id)
.expect("ClientId is not a float so there is no way this could return a `None`.")
});
assert_eq!(
String::from_utf8(AccountSummaryCsvWriter::write(summaries).unwrap()).unwrap(),
"\
client,available,held,total,locked\n\
1,1000000.0000,0.0000,1000000.0000,false\n\
2,1000000.0000,0.0000,1000000.0000,false\n\
3,1000000.0000,0.0000,1000000.0000,false\n\
4,1000000.0000,0.0000,1000000.0000,false\n\
5,1000000.0000,0.0000,1000000.0000,false\n\
6,1000000.0000,0.0000,1000000.0000,false\n\
7,1000000.0000,0.0000,1000000.0000,false\n\
8,1000000.0000,0.0000,1000000.0000,false\n\
9,1000000.0000,0.0000,1000000.0000,false\n\
10,1000000.0000,0.0000,1000000.0000,false\n"
);
}
fn create_test_records(client_count: u16, transaction_count: u32) -> Vec<TransactionRecord> {
let mut records = Vec::new();
let mut transaction_id = 1u32;
for _ in 1..=transaction_count {
for client_id in 1..=client_count {
records.push(TransactionRecord {
txn_type: Deposit,
client_id,
transaction_id,
optional_amount: Some("1".to_string()),
});
transaction_id += 1;
}
}
records
}
fn create_test_file(filename: &str, records: Vec<TransactionRecord>) {
let file = File::create(filename).unwrap();
let buf_writer = BufWriter::new(file);
let mut wtr = WriterBuilder::new().from_writer(buf_writer);
for record in records {
wtr.serialize(record).unwrap();
}
wtr.flush().unwrap();
}