@@ -193,12 +193,24 @@ impl Client {
193193 let line = read_line ( & mut stream, MAX_RESPONSE ) ?;
194194 let response: Value = serde_json:: from_slice ( & line) ?;
195195 ensure ! (
196- response[ "jsonrpc" ] == "2.0" && response[ "id " ] == 1 ,
196+ response. is_object ( ) && response[ "jsonrpc " ] == "2.0" ,
197197 "invalid shared RPC response"
198198 ) ;
199199 if let Some ( error) = response. get ( "error" ) {
200- bail ! ( "shared daemon: {}" , error[ "message" ] ) ;
200+ ensure ! (
201+ response. get( "id" ) . is_some_and( |id| id == 1 || id. is_null( ) )
202+ && response. get( "result" ) . is_none( )
203+ && error. is_object( )
204+ && error[ "code" ] . is_i64( )
205+ && error[ "message" ] . is_string( ) ,
206+ "invalid shared RPC error response"
207+ ) ;
208+ bail ! (
209+ "shared daemon: {}" ,
210+ error[ "message" ] . as_str( ) . expect( "validated message" )
211+ ) ;
201212 }
213+ ensure ! ( response[ "id" ] == 1 , "invalid shared RPC response" ) ;
202214 let result = response
203215 . get ( "result" )
204216 . context ( "missing shared RPC result" ) ?;
@@ -304,3 +316,103 @@ pub fn run_lifecycle(command: Lifecycle) -> Result<()> {
304316 println ! ( "{}" , client. request( method, params) ?) ;
305317 Ok ( ( ) )
306318}
319+
320+ #[ cfg( test) ]
321+ mod tests {
322+ use super :: * ;
323+ use std:: net:: TcpListener ;
324+ use std:: thread;
325+ use std:: time:: Instant ;
326+
327+ fn exchange ( response : Value ) -> Result < Value > {
328+ let listener = TcpListener :: bind ( ( Ipv4Addr :: LOCALHOST , 0 ) ) . unwrap ( ) ;
329+ listener. set_nonblocking ( true ) . unwrap ( ) ;
330+ let client = Client {
331+ registration : Registration {
332+ protocol : PROTOCOL ,
333+ instance : "test-instance" . into ( ) ,
334+ repository : "test-repository" . into ( ) ,
335+ pid : std:: process:: id ( ) ,
336+ port : listener. local_addr ( ) . unwrap ( ) . port ( ) ,
337+ storage : PathBuf :: new ( ) ,
338+ } ,
339+ root : PathBuf :: new ( ) ,
340+ } ;
341+ let worker = thread:: spawn ( move || {
342+ let started = Instant :: now ( ) ;
343+ let mut stream = loop {
344+ match listener. accept ( ) {
345+ Ok ( ( stream, _) ) => break stream,
346+ Err ( error) if error. kind ( ) == std:: io:: ErrorKind :: WouldBlock => {
347+ assert ! ( started. elapsed( ) < Duration :: from_secs( 5 ) ) ;
348+ thread:: sleep ( Duration :: from_millis ( 10 ) ) ;
349+ }
350+ Err ( error) => panic ! ( "{error}" ) ,
351+ }
352+ } ;
353+ stream
354+ . set_read_timeout ( Some ( Duration :: from_secs ( 5 ) ) )
355+ . unwrap ( ) ;
356+ stream
357+ . set_write_timeout ( Some ( Duration :: from_secs ( 5 ) ) )
358+ . unwrap ( ) ;
359+ let request: Value =
360+ serde_json:: from_slice ( & read_line ( & mut stream, MAX_REQUEST ) . unwrap ( ) ) . unwrap ( ) ;
361+ assert_eq ! ( request[ "id" ] , 1 ) ;
362+ assert_eq ! ( request[ "method" ] , "hello" ) ;
363+ writeln ! ( stream, "{response}" ) . unwrap ( ) ;
364+ } ) ;
365+ let result = client. request ( "hello" , json ! ( { } ) ) ;
366+ worker. join ( ) . unwrap ( ) ;
367+ result
368+ }
369+
370+ #[ test]
371+ fn rpc_response_envelopes_preserve_queue_diagnostics ( ) {
372+ let success = json ! ( {
373+ "protocol" : PROTOCOL , "instance" : "test-instance" , "repository" : "test-repository"
374+ } ) ;
375+ assert_eq ! (
376+ exchange( json!( { "jsonrpc" : "2.0" , "id" : 1 , "result" : success} ) ) . unwrap( ) ,
377+ success
378+ ) ;
379+ let error = json ! ( { "code" : -32001 , "message" : "shared connection queue full" } ) ;
380+ for id in [ json ! ( null) , json ! ( 1 ) ] {
381+ let failure = exchange ( json ! ( { "jsonrpc" : "2.0" , "id" : id, "error" : error} ) ) . unwrap_err ( ) ;
382+ assert ! (
383+ failure. to_string( ) . contains( "shared connection queue full" ) ,
384+ "{failure:#}"
385+ ) ;
386+ }
387+ for response in [
388+ json ! ( null) ,
389+ json ! ( [ ] ) ,
390+ json ! ( "not an envelope" ) ,
391+ json ! ( { "jsonrpc" : "1.0" , "id" : null, "error" : error} ) ,
392+ json ! ( { "id" : null, "error" : error} ) ,
393+ json ! ( { "jsonrpc" : "2.0" , "error" : error} ) ,
394+ json ! ( { "jsonrpc" : "2.0" , "id" : 2 , "error" : error} ) ,
395+ json ! ( { "jsonrpc" : "2.0" , "id" : "1" , "error" : error} ) ,
396+ json ! ( { "jsonrpc" : "2.0" , "id" : null, "error" : null} ) ,
397+ json ! ( { "jsonrpc" : "2.0" , "id" : null, "error" : [ ] } ) ,
398+ json ! ( { "jsonrpc" : "2.0" , "id" : null, "error" : { "message" : "shared connection queue full" } } ) ,
399+ json ! ( { "jsonrpc" : "2.0" , "id" : null, "error" : { "code" : "-32001" , "message" : "shared connection queue full" } } ) ,
400+ json ! ( { "jsonrpc" : "2.0" , "id" : null, "error" : { "code" : -1.5 , "message" : "shared connection queue full" } } ) ,
401+ json ! ( { "jsonrpc" : "2.0" , "id" : null, "error" : { "code" : -32001 } } ) ,
402+ json ! ( { "jsonrpc" : "2.0" , "id" : null, "error" : { "code" : -32001 , "message" : { } } } ) ,
403+ json ! ( { "jsonrpc" : "2.0" , "id" : 1 , "error" : error, "result" : success} ) ,
404+ json ! ( { "jsonrpc" : "2.0" , "id" : null, "error" : error, "result" : null} ) ,
405+ json ! ( { "jsonrpc" : "2.0" , "id" : null, "result" : success} ) ,
406+ json ! ( { "jsonrpc" : "2.0" , "id" : 2 , "result" : success} ) ,
407+ json ! ( { "jsonrpc" : "2.0" , "id" : "1" , "result" : success} ) ,
408+ json ! ( { "jsonrpc" : "2.0" , "result" : success} ) ,
409+ json ! ( { "jsonrpc" : "2.0" , "id" : 1 } ) ,
410+ ] {
411+ let failure = exchange ( response. clone ( ) ) . unwrap_err ( ) ;
412+ assert ! (
413+ !failure. to_string( ) . contains( "shared connection queue full" ) ,
414+ "{response}: {failure:#}"
415+ ) ;
416+ }
417+ }
418+ }
0 commit comments