Skip to content

Commit

Permalink
#101 Added Token::try_wait()
Browse files Browse the repository at this point in the history
  • Loading branch information
fpagliughi committed Dec 30, 2020
1 parent 71ccf89 commit e727e0f
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ pub enum Error {
#[error("{}", error_message(*.0))]
Paho(i32),
/// An error from the Paho C library with an additional description.
// TODO: Consider getting rid of this as it makes it more difficult to
// match to a Paho Error as sometimes the same error has a description
// and other times it doesn't.
#[error("[{0}] {1}")]
PahoDescr(i32, String),
/// A synchronous error when publishing creating or queuing the message.
Expand Down
59 changes: 58 additions & 1 deletion src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ impl TokenData {
complete: true,
ret_code: rc,
err_msg: if rc != 0 {
// TODO: Get rid of this? It seems to be redundant
// and confusing as the error message is just derived
// from the i32 error. Having it causes the error to
// fail the match to Error::Paho(_)
// Or, even better, get rid of Error::PahoDescr()
Some(String::from(errors::error_message(rc)))
}
else { None },
Expand Down Expand Up @@ -449,8 +454,19 @@ impl Token {
block_on(self)
}

/// Non-blocking check to see if the token is complete.
///
/// Returns `None` if the operation is still in progress, otherwise
/// returns the result of the operation which can be an error or,
/// on success, the response from the server.
pub fn try_wait(&mut self) -> Option<Result<ServerResponse>> {
self.now_or_never()
}

/// Blocks the caller a limited amount of time waiting for the
/// asynchronous operation to complete.
// TODO: We probably shouldn't consume the token if it's not complete.
// Maybe take '&mut self'?
pub fn wait_for(self, dur: Duration) -> Result<ServerResponse> {
block_on(async move {
let f = self.fuse();
Expand Down Expand Up @@ -571,7 +587,6 @@ impl Into<Token> for DeliveryToken {
}
}


impl Future for DeliveryToken {
type Output = Result<()>;

Expand Down Expand Up @@ -670,5 +685,47 @@ mod tests {
tok2.inner.on_complete(0, 0, None, ptr::null_mut());
let _ = thr.join().unwrap();
}

#[test]
fn test_try_wait() {
const ERR_CODE: i32 = -42;
let mut tok = Token::from_error(ERR_CODE);

match tok.try_wait() {
//Some(Err(Error::Paho(ERR_CODE))) => (),
Some(Err(Error::PahoDescr(ERR_CODE, _))) => (),
Some(Err(_)) => assert!(false),
Some(Ok(_)) => assert!(false),
None => assert!(false)
}

// An unsignaled token
let mut tok = Token::new();

// If it's not done, we should get None
match tok.try_wait() {
None => (),
Some(Err(_)) => assert!(false),
Some(Ok(_)) => assert!(false),
}

// Complete the token
{
let mut data = tok.inner.lock.lock().unwrap();
data.complete = true;
data.ret_code = ERR_CODE;
//data.err_msg = err_msg;
}

// Now it should resolve to Some(Err(...))
match tok.try_wait() {
Some(Err(Error::Paho(ERR_CODE))) => (),
//Some(Err(Error::PahoDescr(ERR_CODE, _))) => (),
Some(Err(_)) => assert!(false),
Some(Ok(_)) => assert!(false),
None => assert!(false)
}

}
}

0 comments on commit e727e0f

Please sign in to comment.