move future test code into extra file
This commit is contained in:
parent
0f52af5b23
commit
948c74f4ca
|
@ -12,6 +12,11 @@ pub mod api {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub mod storage {
|
||||||
|
|
||||||
|
pub mod futures;
|
||||||
|
}
|
||||||
|
|
||||||
pub mod getopts;
|
pub mod getopts;
|
||||||
|
|
||||||
pub mod api3;
|
pub mod api3;
|
||||||
|
|
63
src/main.rs
63
src/main.rs
|
@ -12,65 +12,10 @@ use apitest::getopts;
|
||||||
//use failure::*;
|
//use failure::*;
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
|
|
||||||
|
|
||||||
use futures::future::Future;
|
use futures::future::Future;
|
||||||
|
|
||||||
use hyper;
|
use hyper;
|
||||||
|
|
||||||
use std::thread;
|
|
||||||
use std::sync::{Arc, Mutex};
|
|
||||||
use failure::*;
|
|
||||||
use tokio::prelude::*;
|
|
||||||
|
|
||||||
struct StorageOperation {
|
|
||||||
state: Arc<Mutex<bool>>,
|
|
||||||
running: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl StorageOperation {
|
|
||||||
|
|
||||||
fn new() -> Self {
|
|
||||||
StorageOperation { state: Arc::new(Mutex::new(false)), running: false }
|
|
||||||
}
|
|
||||||
|
|
||||||
fn run(&mut self, task: task::Task) {
|
|
||||||
|
|
||||||
let state = self.state.clone();
|
|
||||||
|
|
||||||
thread::spawn(move || {
|
|
||||||
println!("state {}", state.lock().unwrap());
|
|
||||||
println!("Starting Asnyc worker thread");
|
|
||||||
thread::sleep(::std::time::Duration::from_secs(5));
|
|
||||||
println!("End Asnyc worker thread");
|
|
||||||
*state.lock().unwrap() = true;
|
|
||||||
task.notify();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Future for StorageOperation {
|
|
||||||
type Item = ();
|
|
||||||
type Error = Error;
|
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
||||||
if *self.state.lock().unwrap() != true {
|
|
||||||
println!("not ready - parking the task.");
|
|
||||||
|
|
||||||
if !self.running {
|
|
||||||
println!("starting storage thread");
|
|
||||||
self.run(task::current());
|
|
||||||
self.running = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(Async::NotReady)
|
|
||||||
} else {
|
|
||||||
println!("storage thread ready - task will complete.");
|
|
||||||
Ok(Async::Ready(()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("Proxmox REST Server example.");
|
println!("Proxmox REST Server example.");
|
||||||
|
|
||||||
|
@ -116,14 +61,6 @@ fn main() {
|
||||||
.map_err(|e| eprintln!("server error: {}", e));
|
.map_err(|e| eprintln!("server error: {}", e));
|
||||||
|
|
||||||
|
|
||||||
if false {
|
|
||||||
let op = StorageOperation::new();
|
|
||||||
hyper::rt::run(op.map_err(|e| {
|
|
||||||
println!("Got Error: {}", e);
|
|
||||||
()
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Run this server for... forever!
|
// Run this server for... forever!
|
||||||
hyper::rt::run(server);
|
hyper::rt::run(server);
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,64 @@
|
||||||
|
use std::thread;
|
||||||
|
use std::sync::{Arc, Mutex};
|
||||||
|
use failure::*;
|
||||||
|
use tokio::prelude::*;
|
||||||
|
|
||||||
|
struct StorageOperation {
|
||||||
|
state: Arc<Mutex<bool>>,
|
||||||
|
running: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl StorageOperation {
|
||||||
|
|
||||||
|
fn new() -> Self {
|
||||||
|
StorageOperation { state: Arc::new(Mutex::new(false)), running: false }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run(&mut self, task: task::Task) {
|
||||||
|
|
||||||
|
let state = self.state.clone();
|
||||||
|
|
||||||
|
thread::spawn(move || {
|
||||||
|
println!("state {}", state.lock().unwrap());
|
||||||
|
println!("Starting Asnyc worker thread (delay 1 second)");
|
||||||
|
thread::sleep(::std::time::Duration::from_secs(1));
|
||||||
|
println!("End Asnyc worker thread");
|
||||||
|
*state.lock().unwrap() = true;
|
||||||
|
task.notify();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Future for StorageOperation {
|
||||||
|
type Item = ();
|
||||||
|
type Error = Error;
|
||||||
|
|
||||||
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
||||||
|
if *self.state.lock().unwrap() != true {
|
||||||
|
println!("not ready - parking the task.");
|
||||||
|
|
||||||
|
if !self.running {
|
||||||
|
println!("starting storage thread");
|
||||||
|
self.run(task::current());
|
||||||
|
self.running = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(Async::NotReady)
|
||||||
|
} else {
|
||||||
|
println!("storage thread ready - task will complete.");
|
||||||
|
Ok(Async::Ready(()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_storage_future()
|
||||||
|
{
|
||||||
|
|
||||||
|
let op = StorageOperation::new();
|
||||||
|
hyper::rt::run(op.map_err(|e| {
|
||||||
|
println!("Got Error: {}", e);
|
||||||
|
()
|
||||||
|
}));
|
||||||
|
}
|
Loading…
Reference in New Issue