add a first working test with actixweb

This commit is contained in:
Tamo 2023-11-28 14:47:07 +01:00 committed by Clément Renault
parent 391eb72137
commit fbea721378
No known key found for this signature in database
GPG Key ID: F250A4C4E3AE5F5F
2 changed files with 20 additions and 29 deletions

View File

@ -1286,7 +1286,8 @@ impl IndexScheduler {
let _ = serde_json::to_writer(&mut buffer, &task); let _ = serde_json::to_writer(&mut buffer, &task);
} }
let _ = ureq::post(url).send_bytes(&buffer); println!("Sending request to {url}");
let _ = ureq::post(url).send_bytes(&buffer).unwrap();
} }
Ok(()) Ok(())

View File

@ -12,10 +12,10 @@ use crate::common::{default_settings, Server};
use crate::json; use crate::json;
#[post("/")] #[post("/")]
async fn forward_body(sender: Data<mpsc::Sender<Vec<u8>>>, body: Bytes) -> HttpResponse { async fn forward_body(sender: Data<mpsc::UnboundedSender<Vec<u8>>>, body: Bytes) -> HttpResponse {
println!("Received something"); println!("Received something");
let body = body.to_vec(); let body = body.to_vec();
sender.send(body).await.unwrap(); sender.send(body).unwrap();
HttpResponse::Ok().into() HttpResponse::Ok().into()
} }
@ -40,13 +40,18 @@ struct WebhookHandle {
} }
async fn create_webhook_server() -> WebhookHandle { async fn create_webhook_server() -> WebhookHandle {
let mut log_builder = env_logger::Builder::new();
log_builder.parse_filters("debug");
log_builder.init();
let (sender, receiver) = mpsc::unbounded_channel(); let (sender, receiver) = mpsc::unbounded_channel();
let sender = Arc::new(sender); let sender = Arc::new(sender);
let server = let server =
HttpServer::new(move || create_app(sender.clone())).bind(("localhost", 0)).unwrap(); HttpServer::new(move || create_app(sender.clone())).bind(("127.0.0.1", 0)).unwrap();
let (ip, scheme) = server.addrs_with_scheme()[0]; let (ip, scheme) = server.addrs_with_scheme()[0];
let url = format!("{scheme}://{ip}/"); let url = format!("{scheme}://{ip}/");
println!("url is {url}");
// TODO: Is it cleaned once the test is over // TODO: Is it cleaned once the test is over
let server_handle = tokio::spawn(server.run()); let server_handle = tokio::spawn(server.run());
@ -60,7 +65,6 @@ async fn test_basic_webhook() {
let mut handle = create_webhook_server().await; let mut handle = create_webhook_server().await;
let db_path = tempfile::tempdir().unwrap(); let db_path = tempfile::tempdir().unwrap();
// let (_handle, mut webhook) = create_webhook_server().await;
let server = Server::new_with_options(Opt { let server = Server::new_with_options(Opt {
task_webhook_url: Some(handle.url.clone()), task_webhook_url: Some(handle.url.clone()),
..default_settings(db_path.path()) ..default_settings(db_path.path())
@ -68,34 +72,20 @@ async fn test_basic_webhook() {
.await .await
.unwrap(); .unwrap();
println!("Sending something"); let index = server.index("tamo");
reqwest::Client::new().post(&handle.url).body("hello").send().await.unwrap(); for i in 0..3 {
let (_, _status) = index.add_documents(json!({ "id": i, "doggo": "bone" }), None).await;
// let (_, status) = server.create_index(json!({ "uid": "tamo" })).await; }
// snapshot!(status, @"202 Accepted");
let payload = handle.receiver.recv().await.unwrap(); let payload = handle.receiver.recv().await.unwrap();
let jsonl = String::from_utf8(payload).unwrap(); let jsonl = String::from_utf8(payload).unwrap();
// TODO: kill the server snapshot!(jsonl,
// handle.server_handle.; @r###"{"uid":0,"enqueuedAt":"2023-11-28T13:43:24.754587Z","startedAt":"2023-11-28T13:43:24.756445Z","finishedAt":"2023-11-28T13:43:24.791527Z","error":null,"canceledBy":null,"details":{"DocumentAdditionOrUpdate":{"received_documents":1,"indexed_documents":1}},"status":"succeeded","kind":{"documentAdditionOrUpdate":{"index_uid":"tamo","primary_key":null,"method":"ReplaceDocuments","content_file":"ca77ac82-4504-4c85-81a5-1a8d68f1a386","documents_count":1,"allow_index_creation":true}}}"###);
let payload = handle.receiver.recv().await.unwrap();
let jsonl = String::from_utf8(payload).unwrap();
snapshot!(jsonl, snapshot!(jsonl,
@r###" @r###"{"uid":1,"enqueuedAt":"2023-11-28T13:43:24.761498Z","startedAt":"2023-11-28T13:43:24.793989Z","finishedAt":"2023-11-28T13:43:24.814623Z","error":null,"canceledBy":null,"details":{"DocumentAdditionOrUpdate":{"received_documents":1,"indexed_documents":1}},"status":"succeeded","kind":{"documentAdditionOrUpdate":{"index_uid":"tamo","primary_key":null,"method":"ReplaceDocuments","content_file":"c947aefa-7f98-433d-8ce4-5926d8d2ce10","documents_count":1,"allow_index_creation":true}}}{"uid":2,"enqueuedAt":"2023-11-28T13:43:24.76776Z","startedAt":"2023-11-28T13:43:24.793989Z","finishedAt":"2023-11-28T13:43:24.814623Z","error":null,"canceledBy":null,"details":{"DocumentAdditionOrUpdate":{"received_documents":1,"indexed_documents":1}},"status":"succeeded","kind":{"documentAdditionOrUpdate":{"index_uid":"tamo","primary_key":null,"method":"ReplaceDocuments","content_file":"a21d6da6-9322-4827-8c08-f33d2e1b6cae","documents_count":1,"allow_index_creation":true}}}"###);
{
"uid": 0,
"indexUid": null,
"status": "succeeded",
"type": "dumpCreation",
"canceledBy": null,
"details": {
"dumpUid": "[dumpUid]"
},
"error": null,
"duration": "[duration]",
"enqueuedAt": "[date]",
"startedAt": "[date]",
"finishedAt": "[date]"
}
"###);
} }