|
| 1 | +use std::net::SocketAddr; |
| 2 | + |
| 3 | +use tap_aggregator::grpc::{tap_aggregator_server::{TapAggregator, TapAggregatorServer}, RavRequest, RavResponse}; |
| 4 | +use tokio::sync::oneshot; |
| 5 | +use tonic::{transport::Server, Request, Response, Status}; |
| 6 | + |
| 7 | +#[derive(Default)] |
| 8 | +pub struct MockTapAggregatorService; |
| 9 | + |
| 10 | +#[tonic::async_trait] |
| 11 | +impl TapAggregator for MockTapAggregatorService { |
| 12 | + async fn aggregate_receipts( |
| 13 | + &self, |
| 14 | + _request: Request<RavRequest>, |
| 15 | + ) -> Result<Response<RavResponse>, Status> { |
| 16 | + // Do nothing, just return an unimplemented response |
| 17 | + Err(Status::unimplemented("This is a mock service")) |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | + |
| 22 | +const DUMMY_ADDR: &str = "127.0.0.1:1234"; // Correct format for SocketAddr |
| 23 | +/// Starts a mock TapAggregatorServer for testing purposes and returns a shutdown handle |
| 24 | +pub async fn mock_tap_aggregator_server() -> Result<oneshot::Sender<()>, Box<dyn std::error::Error>> { |
| 25 | + let address: SocketAddr = DUMMY_ADDR |
| 26 | + .parse() |
| 27 | + .expect("Failed to parse DUMMY_ADDR as SocketAddr"); |
| 28 | + |
| 29 | + println!("Starting TapAggregatorServer on {}", DUMMY_ADDR); |
| 30 | + |
| 31 | + // Create the blank mock service |
| 32 | + let service = MockTapAggregatorService::default(); |
| 33 | + |
| 34 | + // Create a shutdown channel |
| 35 | + let (shutdown_tx, shutdown_rx) = oneshot::channel::<()>(); |
| 36 | + |
| 37 | + // Spawn the server in a separate task |
| 38 | + tokio::spawn(async move { |
| 39 | + if let Err(e) = Server::builder() |
| 40 | + .add_service(TapAggregatorServer::new(service)) |
| 41 | + .serve_with_shutdown(address, async { |
| 42 | + shutdown_rx.await.ok(); // Wait for the shutdown signal |
| 43 | + }) |
| 44 | + .await |
| 45 | + { |
| 46 | + eprintln!("TapAggregatorServer failed: {}", e); |
| 47 | + } |
| 48 | + }); |
| 49 | + |
| 50 | + Ok(shutdown_tx) |
| 51 | +} |
| 52 | + |
| 53 | + |
| 54 | +#[tokio::main] |
| 55 | +async fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 56 | + // Start the mock server |
| 57 | + let shutdown_tx = mock_tap_aggregator_server().await?; |
| 58 | + |
| 59 | + println!("Mock server running. Press Ctrl+C to stop."); |
| 60 | + |
| 61 | + // Wait until the user decides to terminate the process |
| 62 | + tokio::signal::ctrl_c().await.expect("Failed to listen for Ctrl+C"); |
| 63 | + |
| 64 | + // Shut down the server gracefully |
| 65 | + shutdown_tx.send(()).ok(); |
| 66 | + println!("Mock server shutting down."); |
| 67 | + |
| 68 | + Ok(()) |
| 69 | +} |
0 commit comments