@@ -163,7 +163,7 @@ impl Args {
163163
164164async fn consume ( args : & Args , routes : & cache:: RouteCache , health : & Health ) -> Result < ( ) , Status > {
165165 let client = args. client ( ) . await ?;
166- let mut grpc = client. raw_grpc ( ) ;
166+ let mut grpc = client. raw_service_discovery ( ) ;
167167 let request = GetServiceRouteSnapshotRequest {
168168 workspace_scope : args. selector ( ) ,
169169 workspaces : args. selected_workspaces ( ) ,
@@ -304,3 +304,187 @@ mod tests {
304304 assert ! ( too_many. validate( ) . is_err( ) ) ;
305305 }
306306}
307+
308+ #[ cfg( test) ]
309+ mod service_tests {
310+ use super :: * ;
311+ use openshell_core:: proto:: {
312+ DiscoveredServiceRoute , GetServiceRouteSnapshotResponse , ServiceRouteEndpoint ,
313+ ServiceRouteSnapshot , WatchServiceRoutesResponse , WorkspaceSelector ,
314+ service_discovery_server:: { ServiceDiscovery , ServiceDiscoveryServer } ,
315+ } ;
316+ use tonic:: codegen:: tokio_stream:: wrappers:: ReceiverStream ;
317+ use tonic:: { Request , Response } ;
318+
319+ type Updates = Arc <
320+ tokio:: sync:: Mutex <
321+ Option < tokio:: sync:: mpsc:: Sender < Result < WatchServiceRoutesResponse , Status > > > ,
322+ > ,
323+ > ;
324+
325+ struct Discovery {
326+ scope : Option < WorkspaceSelector > ,
327+ workspaces : Vec < String > ,
328+ snapshot : ServiceRouteSnapshot ,
329+ stream : Updates ,
330+ }
331+
332+ #[ tonic:: async_trait]
333+ impl ServiceDiscovery for Discovery {
334+ async fn get_service_route_snapshot (
335+ & self ,
336+ request : Request < GetServiceRouteSnapshotRequest > ,
337+ ) -> Result < Response < GetServiceRouteSnapshotResponse > , Status > {
338+ assert_eq ! ( request. get_ref( ) . workspace_scope, self . scope) ;
339+ assert_eq ! ( request. get_ref( ) . workspaces, self . workspaces) ;
340+ Ok ( Response :: new ( GetServiceRouteSnapshotResponse {
341+ snapshot : Some ( self . snapshot . clone ( ) ) ,
342+ } ) )
343+ }
344+ type WatchServiceRoutesStream = ReceiverStream < Result < WatchServiceRoutesResponse , Status > > ;
345+ async fn watch_service_routes (
346+ & self ,
347+ request : Request < WatchServiceRoutesRequest > ,
348+ ) -> Result < Response < Self :: WatchServiceRoutesStream > , Status > {
349+ assert_eq ! ( request. get_ref( ) . workspace_scope, self . scope) ;
350+ assert_eq ! ( request. get_ref( ) . workspaces, self . workspaces) ;
351+ let ( tx, rx) = tokio:: sync:: mpsc:: channel ( 1 ) ;
352+ tx. send ( Ok ( WatchServiceRoutesResponse {
353+ snapshot : Some ( self . snapshot . clone ( ) ) ,
354+ } ) )
355+ . await
356+ . unwrap ( ) ;
357+ * self . stream . lock ( ) . await = Some ( tx) ;
358+ Ok ( Response :: new ( ReceiverStream :: new ( rx) ) )
359+ }
360+ }
361+
362+ #[ tokio:: test]
363+ async fn discovery_only_service_drives_proxy_routes_for_single_finite_and_all_workspaces ( ) {
364+ for mode in 0 ..3 {
365+ let listener = tokio:: net:: TcpListener :: bind ( "127.0.0.1:0" ) . await . unwrap ( ) ;
366+ let addr = listener. local_addr ( ) . unwrap ( ) ;
367+ let mut flags = vec ! [
368+ "openshell-sandbox-proxy" . to_string( ) ,
369+ "--gateway-endpoint" . into( ) ,
370+ format!( "http://{addr}" ) ,
371+ "--allow-insecure-gateway" . into( ) ,
372+ ] ;
373+ if mode == 2 {
374+ flags. push ( "--all-workspaces" . into ( ) ) ;
375+ } else {
376+ flags. extend ( [ "--workspace" . into ( ) , "default" . into ( ) ] ) ;
377+ if mode == 1 {
378+ flags. extend ( [ "--workspace" . into ( ) , "team-b" . into ( ) ] ) ;
379+ }
380+ }
381+ let args = Args :: try_parse_from ( flags) . unwrap ( ) ;
382+ args. validate ( ) . unwrap ( ) ;
383+ let workspaces = if mode == 0 {
384+ vec ! [ "default" ]
385+ } else {
386+ vec ! [ "default" , "team-b" ]
387+ } ;
388+ let snapshot = ServiceRouteSnapshot {
389+ revision : "initial" . into ( ) ,
390+ routes : workspaces
391+ . iter ( )
392+ . map ( |workspace| DiscoveredServiceRoute {
393+ workspace : ( * workspace) . into ( ) ,
394+ sandbox : "demo" . into ( ) ,
395+ name : "web" . into ( ) ,
396+ hostname : format ! ( "{workspace}--demo--web.example.com" ) ,
397+ endpoint : Some ( ServiceRouteEndpoint {
398+ host : "127.0.0.1" . into ( ) ,
399+ port : 9443 ,
400+ } ) ,
401+ installation_version : "installation-1" . into ( ) ,
402+ expires_at : Some (
403+ openshell_core:: time:: timestamp_from_millis ( crate :: now_ms ( ) + 12_000 )
404+ . unwrap ( ) ,
405+ ) ,
406+ ..Default :: default ( )
407+ } )
408+ . collect ( ) ,
409+ } ;
410+ let stream = Arc :: new ( tokio:: sync:: Mutex :: new ( None ) ) ;
411+ let service = ServiceDiscoveryServer :: new ( Discovery {
412+ scope : args. selector ( ) ,
413+ workspaces : args. selected_workspaces ( ) ,
414+ snapshot,
415+ stream : stream. clone ( ) ,
416+ } ) ;
417+ let ( sockets, rx) = tokio:: sync:: mpsc:: channel ( 4 ) ;
418+ let accept = tokio:: spawn ( async move {
419+ loop {
420+ let ( socket, _) = listener. accept ( ) . await . unwrap ( ) ;
421+ if sockets. send ( Ok :: < _ , std:: io:: Error > ( socket) ) . await . is_err ( ) {
422+ break ;
423+ }
424+ }
425+ } ) ;
426+ let ( stop, stopped) = tokio:: sync:: oneshot:: channel ( ) ;
427+ // No OpenShell management service is registered: old RPC paths fail.
428+ let server = tokio:: spawn ( async move {
429+ tonic:: transport:: Server :: builder ( )
430+ . add_service ( service)
431+ . serve_with_incoming_shutdown ( ReceiverStream :: new ( rx) , async {
432+ let _ = stopped. await ;
433+ } )
434+ . await
435+ . unwrap ( ) ;
436+ } ) ;
437+ let ( routes, mut changes) = tokio:: sync:: watch:: channel ( cache:: Routes :: new ( ) ) ;
438+ let health = Arc :: new ( Health :: default ( ) ) ;
439+ let task_routes = routes. clone ( ) ;
440+ let task_health = health. clone ( ) ;
441+ let consumer =
442+ tokio:: spawn ( async move { consume ( & args, & task_routes, & task_health) . await } ) ;
443+ tokio:: time:: timeout ( Duration :: from_secs ( 3 ) , async {
444+ while stream. lock ( ) . await . is_none ( ) {
445+ tokio:: task:: yield_now ( ) . await ;
446+ }
447+ } )
448+ . await
449+ . unwrap ( ) ;
450+ assert_eq ! ( routes. borrow( ) . len( ) , workspaces. len( ) ) ;
451+ assert ! ( health. discovery_ready. load( Ordering :: Acquire ) ) ;
452+ for workspace in workspaces {
453+ assert ! (
454+ routes
455+ . borrow( )
456+ . contains_key( & format!( "{workspace}--demo--web.example.com" ) )
457+ ) ;
458+ }
459+ stream
460+ . lock ( )
461+ . await
462+ . as_ref ( )
463+ . unwrap ( )
464+ . send ( Ok ( WatchServiceRoutesResponse {
465+ snapshot : Some ( ServiceRouteSnapshot {
466+ routes : Vec :: new ( ) ,
467+ revision : "withdrawn" . into ( ) ,
468+ } ) ,
469+ } ) )
470+ . await
471+ . unwrap ( ) ;
472+ tokio:: time:: timeout ( Duration :: from_secs ( 3 ) , async {
473+ while !changes. borrow_and_update ( ) . is_empty ( ) {
474+ changes. changed ( ) . await . unwrap ( ) ;
475+ }
476+ } )
477+ . await
478+ . unwrap ( ) ;
479+ drop ( stream. lock ( ) . await . take ( ) ) ;
480+ tokio:: time:: timeout ( Duration :: from_secs ( 3 ) , consumer)
481+ . await
482+ . unwrap ( )
483+ . unwrap ( )
484+ . unwrap ( ) ;
485+ stop. send ( ( ) ) . unwrap ( ) ;
486+ accept. abort ( ) ;
487+ server. await . unwrap ( ) ;
488+ }
489+ }
490+ }
0 commit comments