Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #184 from rawler/unblock-shutdown
Add unblock method for graceful shutdown
- Loading branch information
Showing
3 changed files
with
74 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
extern crate tiny_http; | ||
|
||
use std::sync::Arc; | ||
use std::thread; | ||
|
||
#[test] | ||
fn unblock_server() { | ||
let server = tiny_http::Server::http("0.0.0.0:0").unwrap(); | ||
let s = Arc::new(server); | ||
|
||
let s1 = s.clone(); | ||
thread::spawn(move || s1.unblock()); | ||
|
||
// Without unblock this would hang forever | ||
for _rq in s.incoming_requests() {} | ||
} | ||
|
||
#[test] | ||
fn unblock_threads() { | ||
let server = tiny_http::Server::http("0.0.0.0:0").unwrap(); | ||
let s = Arc::new(server); | ||
|
||
let s1 = s.clone(); | ||
let s2 = s.clone(); | ||
let h1 = thread::spawn(move || for _rq in s1.incoming_requests() {}); | ||
let h2 = thread::spawn(move || for _rq in s2.incoming_requests() {}); | ||
|
||
// Graceful shutdown; removing even one of the | ||
// unblock calls prevents termination | ||
s.unblock(); | ||
s.unblock(); | ||
h1.join().unwrap(); | ||
h2.join().unwrap(); | ||
} |