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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
// These are full integration tests that use the BITS service.
// TODO
// It may make sense to restrict how many tests can run at once. BITS is only supposed to support
// four simultaneous notifications per user, it is not impossible that this test suite could
// exceed that.
#![cfg(test)]
extern crate bits;
extern crate lazy_static;
extern crate rand;
extern crate regex;
extern crate tempfile;
use std::ffi::{OsStr, OsString};
use std::fs::{self, File};
use std::io::{Read, Write};
use std::net::{SocketAddr, TcpListener, TcpStream};
use std::panic;
use std::sync::{Arc, Condvar, Mutex};
use std::thread::{self, JoinHandle};
use std::time::{Duration, Instant};
use self::{
bits::BackgroundCopyManager,
lazy_static::lazy_static,
rand::{thread_rng, Rng},
regex::bytes::Regex,
tempfile::{Builder, TempDir},
};
use super::{
super::{BitsJobState, Error},
BitsProxyUsage, InProcessClient, StartJobSuccess,
};
static SERVER_ADDRESS: [u8; 4] = [127, 0, 0, 1];
lazy_static! {
static ref TEST_MUTEX: Mutex<()> = Mutex::new(());
}
fn format_job_name(name: &str) -> OsString {
format!("InProcessClient Test {}", name).into()
}
fn format_dir_prefix(tmp_dir: &TempDir) -> OsString {
let mut dir = tmp_dir.path().to_path_buf().into_os_string();
dir.push("\\");
dir
}
fn cancel_jobs(name: &OsStr) {
BackgroundCopyManager::connect()
.unwrap()
.cancel_jobs_by_name(name)
.unwrap();
}
struct HttpServerResponses {
body: Box<[u8]>,
delay: u64,
//error: Box<[u8]>,
}
struct MockHttpServerHandle {
port: u16,
join: Option<JoinHandle<Result<(), ()>>>,
shutdown: Arc<(Mutex<bool>, Condvar)>,
}
impl MockHttpServerHandle {
fn shutdown(&mut self) {
if self.join.is_none() {
return;
}
{
let &(ref lock, ref cvar) = &*self.shutdown;
let mut shutdown = lock.lock().unwrap();
if !*shutdown {
*shutdown = true;
cvar.notify_all();
}
}
// Wake up the server from `accept()`. Will fail if the server wasn't listening.
let _ = TcpStream::connect_timeout(
&(SERVER_ADDRESS, self.port).into(),
Duration::from_millis(10_000),
);
match self.join.take().unwrap().join() {
Ok(_) => {}
Err(p) => panic::resume_unwind(p),
}
}
fn format_url(&self, name: &str) -> OsString {
format!(
"http://{}/{}",
SocketAddr::from((SERVER_ADDRESS, self.port)),
name
)
.into()
}
}
fn mock_http_server(name: &'static str, responses: HttpServerResponses) -> MockHttpServerHandle {
let mut bind_retries = 10;
let shutdown = Arc::new((Mutex::new(false), Condvar::new()));
let caller_shutdown = shutdown.clone();
let (listener, port) = loop {
let port = thread_rng().gen_range(1024..0x1_0000u32) as u16;
match TcpListener::bind(SocketAddr::from((SERVER_ADDRESS, port))) {
Ok(listener) => {
break (listener, port);
}
r @ Err(_) => {
if bind_retries == 0 {
r.unwrap();
}
bind_retries -= 1;
continue;
}
}
};
let join = thread::Builder::new()
.name(format!("mock_http_server {}", name))
.spawn(move || {
// returns Err(()) if server should shut down immediately
fn check_shutdown(shutdown: &Arc<(Mutex<bool>, Condvar)>) -> Result<(), ()> {
if *shutdown.0.lock().unwrap() {
Err(())
} else {
Ok(())
}
}
fn sleep(shutdown: &Arc<(Mutex<bool>, Condvar)>, delay_millis: u64) -> Result<(), ()> {
let sleep_start = Instant::now();
let sleep_end = sleep_start + Duration::from_millis(delay_millis);
let (ref lock, ref cvar) = **shutdown;
let mut shutdown_requested = lock.lock().unwrap();
loop {
if *shutdown_requested {
return Err(());
}
let before_wait = Instant::now();
if before_wait >= sleep_end {
return Ok(());
}
let wait_dur = sleep_end - before_wait;
shutdown_requested = cvar.wait_timeout(shutdown_requested, wait_dur).unwrap().0;
}
}
let error_404 = Regex::new(r"^((GET)|(HEAD)) [[:print:]]*/error_404 ").unwrap();
let error_500 = Regex::new(r"^((GET)|(HEAD)) [[:print:]]*/error_500 ").unwrap();
loop {
let (mut socket, _addr) = listener.accept().expect("accept should succeed");
socket
.set_read_timeout(Some(Duration::from_millis(10_000)))
.unwrap();
let mut s = Vec::new();
for b in Read::by_ref(&mut socket).bytes() {
if b.is_err() {
eprintln!("read error {:?}", b);
break;
}
let b = b.unwrap();
s.push(b);
if s.ends_with(b"\r\n\r\n") {
break;
}
check_shutdown(&shutdown)?;
}
// request received
check_shutdown(&shutdown)?;
// Special error URIs
if error_404.is_match(&s) {
sleep(&shutdown, responses.delay)?;
let result = socket
.write(b"HTTP/1.1 404 Not Found\r\n\r\n")
.and_then(|_| socket.flush());
if let Err(e) = result {
eprintln!("error writing 404 header {:?}", e);
}
continue;
}
if error_500.is_match(&s) {
sleep(&shutdown, responses.delay)?;
let result = socket
.write(b"HTTP/1.1 500 Internal Server Error\r\n\r\n")
.and_then(|_| socket.flush());
if let Err(e) = result {
eprintln!("error writing 500 header {:?}", e);
}
continue;
}
// Response with a body.
if s.starts_with(b"HEAD") || s.starts_with(b"GET") {
let result = socket
.write(
format!(
"HTTP/1.1 200 OK\r\nContent-Length: {}\r\n\r\n",
responses.body.len()
)
.as_bytes(),
)
.and_then(|_| socket.flush());
if let Err(e) = result {
eprintln!("error writing header {:?}", e);
continue;
}
}
if s.starts_with(b"GET") {
sleep(&shutdown, responses.delay)?;
let result = socket.write(&responses.body).and_then(|_| socket.flush());
if let Err(e) = result {
eprintln!("error writing content {:?}", e);
continue;
}
}
}
});
MockHttpServerHandle {
port,
join: Some(join.unwrap()),
shutdown: caller_shutdown,
}
}
// Test wrapper to ensure jobs are canceled, set up name strings
macro_rules! test {
(fn $name:ident($param:ident : &str, $tmpdir:ident : &TempDir) $body:block) => {
#[test]
fn $name() {
let $param = stringify!($name);
let $tmpdir = &Builder::new().prefix($param).tempdir().unwrap();
let result = panic::catch_unwind(|| $body);
cancel_jobs(&format_job_name($param));
if let Err(e) = result {
panic::resume_unwind(e);
}
}
};
}
test! {
fn start_monitor_and_cancel(name: &str, tmp_dir: &TempDir) {
let mut server = mock_http_server(name, HttpServerResponses {
body: name.to_owned().into_boxed_str().into_boxed_bytes(),
delay: 10_000,
});
let mut client = InProcessClient::new(format_job_name(name), tmp_dir.path().into()).unwrap();
let no_progress_timeout_secs = 60;
let interval = 10_000;
let timeout = 10_000;
let (StartJobSuccess {guid}, mut monitor) =
client.start_job(
server.format_url(name),
name.into(),
BitsProxyUsage::Preconfig,
no_progress_timeout_secs,
interval,
).unwrap();
// cancel in ~250ms
let _join = thread::Builder::new()
.spawn(move || {
thread::sleep(Duration::from_millis(250));
client.cancel_job(guid).unwrap();
});
let start = Instant::now();
// First immediate report
monitor.get_status(timeout).expect("should initially be ok").unwrap();
// ~250ms the cancel should cause an immediate disconnect (otherwise we wouldn't get
// an update until 10s when the transfer completes or the interval expires)
match monitor.get_status(timeout) {
Err(Error::NotConnected) => {},
Ok(r) => panic!("unexpected success from get_status() {:?}", r),
Err(e) => panic!("unexpected failure from get_status() {:?}", e),
}
assert!(start.elapsed() < Duration::from_millis(9_000));
server.shutdown();
}
}
test! {
fn start_monitor_and_complete(name: &str, tmp_dir: &TempDir) {
let file_path = tmp_dir.path().join(name);
let mut server = mock_http_server(name, HttpServerResponses {
body: name.to_owned().into_boxed_str().into_boxed_bytes(),
delay: 500,
});
let mut client = InProcessClient::new(format_job_name(name), format_dir_prefix(tmp_dir)).unwrap();
let no_progress_timeout_secs = 60;
let interval = 100;
let timeout = 10_000;
let (StartJobSuccess {guid}, mut monitor) =
client.start_job(
server.format_url(name),
name.into(),
BitsProxyUsage::Preconfig,
no_progress_timeout_secs,
interval,
).unwrap();
let start = Instant::now();
// Get status reports until transfer finishes (~500ms)
let mut completed = false;
loop {
match monitor.get_status(timeout) {
Err(e) => {
if completed {
break;
} else {
panic!("monitor failed before completion {:?}", e);
}
}
Ok(Ok(status)) => match BitsJobState::from(status.state) {
BitsJobState::Queued | BitsJobState::Connecting
| BitsJobState::Transferring => {
//eprintln!("{:?}", BitsJobState::from(status.state));
//eprintln!("{:?}", status);
// As long as there is no error, setting the timeout to 0 will not
// fail an active transfer.
client.set_no_progress_timeout(guid.clone(), 0).unwrap();
}
BitsJobState::Transferred => {
client.complete_job(guid.clone()).unwrap();
completed = true;
}
_ => {
panic!("{:?}", status);
}
}
Ok(Err(e)) => panic!("{:?}", e),
}
// Timeout to prevent waiting forever
assert!(start.elapsed() < Duration::from_millis(60_000));
}
// Verify file contents
let result = panic::catch_unwind(|| {
let mut file = File::open(file_path.clone()).unwrap();
let mut v = Vec::new();
file.read_to_end(&mut v).unwrap();
assert_eq!(v, name.as_bytes());
});
let _ = fs::remove_file(file_path);
if let Err(e) = result {
panic::resume_unwind(e);
}
// Save this for last to ensure the file is removed.
server.shutdown();
}
}
test! {
fn async_transferred_notification(name: &str, tmp_dir: &TempDir) {
let mut server = mock_http_server(name, HttpServerResponses {
body: name.to_owned().into_boxed_str().into_boxed_bytes(),
delay: 250,
});
let mut client = InProcessClient::new(format_job_name(name), format_dir_prefix(tmp_dir)).unwrap();
let no_progress_timeout_secs = 60;
let interval = 60_000;
let timeout = 10_000;
let (_, mut monitor) =
client.start_job(
server.format_url(name),
name.into(),
BitsProxyUsage::Preconfig,
no_progress_timeout_secs,
interval,
).unwrap();
// Start the timer now, the initial job creation may be delayed by BITS service startup.
let start = Instant::now();
// First report, immediate
let report_1 = monitor.get_status(timeout).expect("should initially be ok").unwrap();
let elapsed_to_report_1 = start.elapsed();
// Transferred notification should come when the job completes in ~250 ms, otherwise we
// will be stuck until timeout.
let report_2 = monitor.get_status(timeout).expect("should get status update").unwrap();
let elapsed_to_report_2 = start.elapsed();
assert!(elapsed_to_report_2 < Duration::from_millis(9_000));
assert_eq!(report_2.state, BitsJobState::Transferred);
let short_timeout = 500;
let report_3 = monitor.get_status(short_timeout);
let elapsed_to_report_3 = start.elapsed();
if let Ok(report_3) = report_3 {
panic!("should be disconnected\n\
report_1 ({}.{:03}): {:?}\n\
report_2 ({}.{:03}): {:?}\n\
report_3 ({}.{:03}): {:?}",
elapsed_to_report_1.as_secs(), elapsed_to_report_1.subsec_millis(), report_1,
elapsed_to_report_2.as_secs(), elapsed_to_report_2.subsec_millis(), report_2,
elapsed_to_report_3.as_secs(), elapsed_to_report_3.subsec_millis(), report_3,
);
}
server.shutdown();
// job will be cancelled by macro
}
}
test! {
fn change_interval(name: &str, tmp_dir: &TempDir) {
let mut server = mock_http_server(name, HttpServerResponses {
body: name.to_owned().into_boxed_str().into_boxed_bytes(),
delay: 1000,
});
let mut client = InProcessClient::new(format_job_name(name), format_dir_prefix(tmp_dir)).unwrap();
let no_progress_timeout_secs = 0;
let interval = 60_000;
let timeout = 10_000;
let (StartJobSuccess { guid }, mut monitor) =
client.start_job(
server.format_url(name),
name.into(),
BitsProxyUsage::Preconfig,
no_progress_timeout_secs,
interval,
).unwrap();
let start = Instant::now();
// reduce monitor interval in ~250ms to 500ms
let _join = thread::Builder::new()
.spawn(move || {
thread::sleep(Duration::from_millis(250));
client.set_update_interval(guid, 500).unwrap();
});
// First immediate report
monitor.get_status(timeout).expect("should initially be ok").unwrap();
// Next report should be rescheduled to 500ms by the spawned thread, otherwise no status
// until the original 10s interval.
monitor.get_status(timeout).expect("expected second status").unwrap();
assert!(start.elapsed() < Duration::from_millis(9_000));
assert!(start.elapsed() > Duration::from_millis(400));
server.shutdown();
// job will be cancelled by macro
}
}
test! {
fn async_error_notification(name: &str, tmp_dir: &TempDir) {
let mut server = mock_http_server(name, HttpServerResponses {
body: name.to_owned().into_boxed_str().into_boxed_bytes(),
delay: 100,
});
let mut client = InProcessClient::new(format_job_name(name), format_dir_prefix(tmp_dir)).unwrap();
let no_progress_timeout_secs = 60;
let interval = 60_000;
let timeout = 10_000;
let (_, mut monitor) =
client.start_job(
server.format_url("error_404"),
name.into(),
BitsProxyUsage::Preconfig,
no_progress_timeout_secs,
interval,
).unwrap();
// Start the timer now, the initial job creation may be delayed by BITS service startup.
let start = Instant::now();
// First immediate report
monitor.get_status(timeout).expect("should initially be ok").unwrap();
// Error notification should come with HEAD response in 100ms, otherwise no status until
// 10s interval or timeout.
let status = monitor.get_status(timeout).expect("should get status update").unwrap();
assert!(start.elapsed() < Duration::from_millis(9_000));
assert_eq!(status.state, BitsJobState::Error);
server.shutdown();
// job will be cancelled by macro
}
}
test! {
fn transient_error(name: &str, tmp_dir: &TempDir) {
let mut server = mock_http_server(name, HttpServerResponses {
body: name.to_owned().into_boxed_str().into_boxed_bytes(),
delay: 100,
});
let mut client = InProcessClient::new(format_job_name(name), format_dir_prefix(tmp_dir)).unwrap();
let no_progress_timeout_secs = 60;
let interval = 1_000;
let timeout = 10_000;
let (StartJobSuccess { guid }, mut monitor) =
client.start_job(
server.format_url("error_500"),
name.into(),
BitsProxyUsage::Preconfig,
no_progress_timeout_secs,
interval,
).unwrap();
// Start the timer now, the initial job creation may be delayed by BITS service startup.
let start = Instant::now();
// First immediate report
monitor.get_status(timeout).expect("should initially be ok").unwrap();
// Transient error notification should come when the interval expires in ~1s.
let status = monitor.get_status(timeout).expect("should get status update").unwrap();
assert!(start.elapsed() > Duration::from_millis(900));
assert!(start.elapsed() < Duration::from_millis(9_000));
assert_eq!(status.state, BitsJobState::TransientError);
// Lower no progress timeout to 0
let set_timeout_at = Instant::now();
client.set_no_progress_timeout(guid, 0).unwrap();
// Should convert the transient error to a permanent error immediately.
let status = monitor.get_status(timeout).expect("should get status update").unwrap();
assert!(set_timeout_at.elapsed() < Duration::from_millis(500));
assert_eq!(status.state, BitsJobState::Error);
server.shutdown();
// job will be cancelled by macro
}
}
test! {
fn transient_to_permanent_error(name: &str, tmp_dir: &TempDir) {
let mut server = mock_http_server(name, HttpServerResponses {
body: name.to_owned().into_boxed_str().into_boxed_bytes(),
delay: 100,
});
let mut client = InProcessClient::new(format_job_name(name), format_dir_prefix(tmp_dir)).unwrap();
let no_progress_timeout_secs = 0;
let interval = 1_000;
let timeout = 10_000;
let (_, mut monitor) =
client.start_job(
server.format_url("error_500"),
name.into(),
BitsProxyUsage::Preconfig,
no_progress_timeout_secs,
interval,
).unwrap();
// Start the timer now, the initial job creation may be delayed by BITS service startup.
let start = Instant::now();
// First immediate report
monitor.get_status(timeout).expect("should initially be ok").unwrap();
// 500 is a transient error, but with no_progress_timeout_secs = 0 it should immediately
// produce an error notification with the HEAD response in 100ms. Otherwise no status
// until 10s interval or timeout.
let status = monitor.get_status(timeout).expect("should get status update").unwrap();
assert!(start.elapsed() < Duration::from_millis(500));
assert_eq!(status.state, BitsJobState::Error);
server.shutdown();
// job will be cancelled by macro
}
}
|