diff --git a/src/expect_interface.rs b/src/expect_interface.rs index 5c209d6..0984333 100644 --- a/src/expect_interface.rs +++ b/src/expect_interface.rs @@ -13,6 +13,7 @@ // limitations under the License. use crate::tester::Tester; +use crate::types::Status; // As of now, the following expectations do not require "fn returning()" implementations and hence // no structure is provided for them. Setting of these expectations are built directly into tester.rs: @@ -182,7 +183,7 @@ impl<'a> ExpectGrpcCall<'a> { } } - pub fn returning(&mut self, token_id: Option) -> &mut Tester { + pub fn returning(&mut self, res: Result) -> &mut Tester { self.tester.get_expect_handle().staged.set_expect_grpc_call( self.service, self.service_name, @@ -190,7 +191,7 @@ impl<'a> ExpectGrpcCall<'a> { self.initial_metadata, self.request, self.timeout, - token_id, + res, ); self.tester } diff --git a/src/expectations.rs b/src/expectations.rs index 53a463b..180268b 100644 --- a/src/expectations.rs +++ b/src/expectations.rs @@ -96,7 +96,7 @@ pub struct Expect { Option, Option, Option, - Option, + Result, )>, get_property_value: Vec<(Option, Option)>, } @@ -593,7 +593,7 @@ impl Expect { initial_metadata: Option<&[u8]>, request: Option<&[u8]>, timeout: Option, - token_id: Option, + res: Result, ) { self.expect_count += 1; self.grpc_call.push(( @@ -603,7 +603,7 @@ impl Expect { initial_metadata.map(|s| s.to_vec()), request.map(|s| s.to_vec()), timeout.map(Duration::from_millis), - token_id, + res, )); } @@ -615,7 +615,7 @@ impl Expect { initial_metadata: &[u8], request: &[u8], timeout: i32, - ) -> Option { + ) -> Option> { match self.grpc_call.len() { 0 => { if !self.allow_unexpected { @@ -649,7 +649,7 @@ impl Expect { .map(|e| e.as_millis() as i32 == timeout) .unwrap_or(true); set_expect_status(expected); - return result; + return Some(result); } } } diff --git a/src/hostcalls.rs b/src/hostcalls.rs index 8147183..8e4e168 100644 --- a/src/hostcalls.rs +++ b/src/hostcalls.rs @@ -1485,9 +1485,9 @@ fn get_hostfunc( let mem = match caller.get_export("memory") { Some(Extern::Memory(mem)) => mem, _ => { - println!("Error: proxy_http_call cannot get export \"memory\""); + println!("Error: proxy_grpc_call cannot get export \"memory\""); println!( - "[vm<-host] proxy_http_call(...) -> (return_token) return: {:?}", + "[vm<-host] proxy_grpc_call(...) -> (return_token) return: {:?}", Status::InternalFailure ); return Status::InternalFailure as i32; @@ -1506,25 +1506,34 @@ fn get_hostfunc( println!( "[vm->host] proxy_grpc_call(service={service}, service_name={service_name}, method_name={method_name}, initial_metadata={initial_metadata:?}, request={request:?}, timeout={timeout_milliseconds}"); - let token_id = match EXPECT.lock().unwrap().staged.get_expect_grpc_call( - service, - service_name, - method_name, - initial_metadata, - request, - timeout_milliseconds, - ) { - Some(expect_token) => expect_token, - None => 0, - }; - - unsafe { - let return_token_add = mem.data_mut(&mut caller).get_unchecked_mut( - token_ptr as u32 as usize..token_ptr as u32 as usize + 4, - ); - return_token_add.copy_from_slice(&token_id.to_le_bytes()); + let call_result = EXPECT + .lock() + .unwrap() + .staged + .get_expect_grpc_call( + service, + service_name, + method_name, + initial_metadata, + request, + timeout_milliseconds, + ) + .unwrap_or(Ok(0)); + + if let Ok(token_id) = call_result { + unsafe { + let return_token_add = mem.data_mut(&mut caller).get_unchecked_mut( + token_ptr as u32 as usize..token_ptr as u32 as usize + 4, + ); + return_token_add.copy_from_slice(&token_id.to_le_bytes()); + } } + let call_status = match call_result { + Ok(_) => Status::Ok, + Err(s) => s, + }; + // Default Function: // Expectation: println!( @@ -1536,7 +1545,7 @@ fn get_hostfunc( Status::Ok ); assert_ne!(get_status(), ExpectStatus::Failed); - return Status::Ok as i32; + return call_status as i32; }, )) } diff --git a/src/types.rs b/src/types.rs index 72f0874..36ce810 100644 --- a/src/types.rs +++ b/src/types.rs @@ -59,6 +59,8 @@ pub enum Status { Ok = 0, NotFound = 1, BadArgument = 2, + SerializationFailure = 3, + ParseFailure = 4, Empty = 7, CasMismatch = 8, InternalFailure = 10,