Skip to content

Commit

Permalink
Fix handling of commit parameters in rest client (#3197)
Browse files Browse the repository at this point in the history
Fixes handling of CommitType::Force and CommitType::WaitFor in the rest
client.

Fixes #3196
  • Loading branch information
imotov authored Apr 19, 2023
1 parent d69bec9 commit f501157
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
6 changes: 3 additions & 3 deletions quickwit/quickwit-ingest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,11 @@ impl From<u32> for CommitType {
}

impl CommitType {
pub fn to_query_parameter(&self) -> Option<&'static str> {
pub fn to_query_parameter(&self) -> Option<&'static [(&'static str, &'static str)]> {
match self {
CommitType::Auto => None,
CommitType::WaitFor => Some("commit=wait_for"),
CommitType::Force => Some("commit=force"),
CommitType::WaitFor => Some(&[("commit", "wait_for")]),
CommitType::Force => Some(&[("commit", "force")]),
}
}
}
Expand Down
62 changes: 61 additions & 1 deletion quickwit/quickwit-rest-client/src/rest_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,9 @@ mod test {
use serde_json::json;
use tokio::fs::File;
use tokio::io::AsyncReadExt;
use wiremock::matchers::{body_bytes, body_json, header, method, path, query_param};
use wiremock::matchers::{
body_bytes, body_json, header, method, path, query_param, query_param_is_missing,
};
use wiremock::{Mock, MockServer, ResponseTemplate};

use super::{QuickwitClient, Transport};
Expand Down Expand Up @@ -529,6 +531,7 @@ mod test {
.unwrap();
Mock::given(method("POST"))
.and(path("/api/v1/my-index/ingest"))
.and(query_param_is_missing("commit"))
.and(body_bytes(buffer.clone()))
.respond_with(ResponseTemplate::new(StatusCode::TOO_MANY_REQUESTS))
.up_to_n_times(2)
Expand All @@ -537,6 +540,7 @@ mod test {
.await;
Mock::given(method("POST"))
.and(path("/api/v1/my-index/ingest"))
.and(query_param_is_missing("commit"))
.and(body_bytes(buffer))
.respond_with(ResponseTemplate::new(StatusCode::OK))
.up_to_n_times(1)
Expand All @@ -549,6 +553,62 @@ mod test {
.unwrap();
}

#[tokio::test]
async fn test_ingest_endpoint_with_force_commit() {
let mock_server = MockServer::start().await;
let server_url = Url::parse(&mock_server.uri()).unwrap();
let qw_client = QuickwitClient::new(Transport::new(server_url));
let ndjson_filepath = get_ndjson_filepath("documents_to_ingest.json");
let mut buffer = Vec::new();
File::open(&ndjson_filepath)
.await
.unwrap()
.read_to_end(&mut buffer)
.await
.unwrap();
Mock::given(method("POST"))
.and(path("/api/v1/my-index/ingest"))
.and(query_param("commit", "force"))
.and(body_bytes(buffer))
.respond_with(ResponseTemplate::new(StatusCode::OK))
.up_to_n_times(1)
.mount(&mock_server)
.await;
let ingest_source = IngestSource::File(PathBuf::from_str(&ndjson_filepath).unwrap());
qw_client
.ingest("my-index", ingest_source, None, CommitType::Force)
.await
.unwrap();
}

#[tokio::test]
async fn test_ingest_endpoint_with_wait_for_commit() {
let mock_server = MockServer::start().await;
let server_url = Url::parse(&mock_server.uri()).unwrap();
let qw_client = QuickwitClient::new(Transport::new(server_url));
let ndjson_filepath = get_ndjson_filepath("documents_to_ingest.json");
let mut buffer = Vec::new();
File::open(&ndjson_filepath)
.await
.unwrap()
.read_to_end(&mut buffer)
.await
.unwrap();
Mock::given(method("POST"))
.and(path("/api/v1/my-index/ingest"))
.and(query_param("commit", "wait_for"))
.and(body_bytes(buffer))
.respond_with(ResponseTemplate::new(StatusCode::OK))
.up_to_n_times(1)
.mount(&mock_server)
.await;
let ingest_source = IngestSource::File(PathBuf::from_str(&ndjson_filepath).unwrap());
qw_client
.ingest("my-index", ingest_source, None, CommitType::WaitFor)
.await
.unwrap();
}

#[tokio::test]
async fn test_ingest_endpoint_should_return_api_error() {
let mock_server = MockServer::start().await;
Expand Down

0 comments on commit f501157

Please sign in to comment.