Skip to content

Commit 728f9b7

Browse files
committed
ci: add TapAggregator server mock to minimize changes to tests
1 parent 7825d01 commit 728f9b7

File tree

2 files changed

+73
-2
lines changed

2 files changed

+73
-2
lines changed

.github/workflows/tests.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,10 @@ jobs:
5858
run: cargo sqlx migrate run
5959
- name: Check that the sqlx prepared query metadata is up-to-date
6060
run: cargo sqlx prepare --workspace --check -- --all-targets --all-features
61-
62-
clippy:
61+
- name: Start mock server
62+
run: |
63+
cargo run --bin mock_tap_aggregator_server_runner &
64+
clippy:
6365
name: cargo clippy
6466
runs-on: ubuntu-latest
6567
container:
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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

Comments
 (0)