@@ -164,91 +164,68 @@ constexpr const char *kOneItemRejected =
164164TEST (ElasticsearchBulkResponseTests, ReportsSuccessWhenErrorsIsFalse)
165165{
166166 std::string reason;
167- EXPECT_TRUE (logs_exporter::detail::IsBulkResponseSuccessful (200 , kPrettySuccess , 1 , reason));
168- EXPECT_TRUE (logs_exporter::detail::IsBulkResponseSuccessful (200 , kCompactSuccess , 1 , reason));
167+ EXPECT_TRUE (logs_exporter::detail::IsBulkResponseSuccessful (200 , kPrettySuccess , reason));
168+ EXPECT_TRUE (logs_exporter::detail::IsBulkResponseSuccessful (200 , kCompactSuccess , reason));
169+ }
170+
171+ // A successful check must not leave a stale failure reason from a previous call behind.
172+ TEST (ElasticsearchBulkResponseTests, ClearsFailureReasonOnSuccess)
173+ {
174+ std::string reason = " stale" ;
175+ EXPECT_TRUE (logs_exporter::detail::IsBulkResponseSuccessful (200 , kCompactSuccess , reason));
176+ EXPECT_TRUE (reason.empty ());
169177}
170178
171179TEST (ElasticsearchBulkResponseTests, ReportsFailureWhenAnyItemWasRejected)
172180{
173181 std::string reason;
174- EXPECT_FALSE (logs_exporter::detail::IsBulkResponseSuccessful (200 , kOneItemRejected , 2 , reason));
182+ EXPECT_FALSE (logs_exporter::detail::IsBulkResponseSuccessful (200 , kOneItemRejected , reason));
175183 EXPECT_NE (reason.find (" mapper_parsing_exception" ), std::string::npos);
176184}
177185
178186TEST (ElasticsearchBulkResponseTests, ReportsFailureOnUnusableBody)
179187{
180188 std::string reason;
181- EXPECT_FALSE (logs_exporter::detail::IsBulkResponseSuccessful (200 , " not json at all" , 1 , reason));
189+ EXPECT_FALSE (logs_exporter::detail::IsBulkResponseSuccessful (200 , " not json at all" , reason));
182190 EXPECT_FALSE (reason.empty ());
183191
184- EXPECT_FALSE (logs_exporter::detail::IsBulkResponseSuccessful (200 , " " , 1 , reason));
192+ EXPECT_FALSE (logs_exporter::detail::IsBulkResponseSuccessful (200 , " " , reason));
185193 EXPECT_FALSE (reason.empty ());
186194
187195 // An array rather than the expected object.
188- EXPECT_FALSE (logs_exporter::detail::IsBulkResponseSuccessful (200 , " [1,2,3]" , 1 , reason));
196+ EXPECT_FALSE (logs_exporter::detail::IsBulkResponseSuccessful (200 , " [1,2,3]" , reason));
189197 EXPECT_FALSE (reason.empty ());
190198}
191199
192200TEST (ElasticsearchBulkResponseTests, ReportsFailureWhenErrorsFieldIsMissingOrNotBoolean)
193201{
194202 std::string reason;
195203 EXPECT_FALSE (
196- logs_exporter::detail::IsBulkResponseSuccessful (200 , R"( {"took":30,"items":[]})" , 0 , reason));
204+ logs_exporter::detail::IsBulkResponseSuccessful (200 , R"( {"took":30,"items":[]})" , reason));
197205 EXPECT_FALSE (logs_exporter::detail::IsBulkResponseSuccessful (
198- 200 , R"( {"errors":"false","items":[]})" , 0 , reason));
206+ 200 , R"( {"errors":"false","items":[]})" , reason));
199207}
200208
201209// A non-2xx status is a failure even when the body reports errors:false. This is the invariant
202210// the substring check and the body-only check both missed, on both the sync and async paths.
203211TEST (ElasticsearchBulkResponseTests, ReportsFailureOnNon2xxStatus)
204212{
205213 std::string reason;
206- // Empty batch, so zero item results are expected; the status is what varies here.
207214 const char *ok_body = R"( {"errors":false,"items":[]})" ;
208- EXPECT_FALSE (logs_exporter::detail::IsBulkResponseSuccessful (500 , ok_body, 0 , reason));
209- EXPECT_FALSE (logs_exporter::detail::IsBulkResponseSuccessful (429 , ok_body, 0 , reason));
210- EXPECT_FALSE (logs_exporter::detail::IsBulkResponseSuccessful (400 , ok_body, 0 , reason));
211- EXPECT_TRUE (logs_exporter::detail::IsBulkResponseSuccessful (200 , ok_body, 0 , reason));
215+ EXPECT_FALSE (logs_exporter::detail::IsBulkResponseSuccessful (500 , ok_body, reason));
216+ EXPECT_FALSE (logs_exporter::detail::IsBulkResponseSuccessful (429 , ok_body, reason));
217+ EXPECT_FALSE (logs_exporter::detail::IsBulkResponseSuccessful (400 , ok_body, reason));
218+ EXPECT_TRUE (logs_exporter::detail::IsBulkResponseSuccessful (200 , ok_body, reason));
212219}
213220
214221// errors:true with no extractable item error still fails, with the generic reason.
215222TEST (ElasticsearchBulkResponseTests, ReportsFailureWhenErrorsTrueWithoutItemError)
216223{
217224 std::string reason;
218225 EXPECT_FALSE (logs_exporter::detail::IsBulkResponseSuccessful (200 , R"( {"errors":true,"items":[]})" ,
219- 0 , reason));
226+ reason));
220227 EXPECT_FALSE (logs_exporter::detail::IsBulkResponseSuccessful (
221- 200 , R"( {"errors":true,"items":[null]})" , 1 , reason));
228+ 200 , R"( {"errors":true,"items":[null]})" , reason));
222229 EXPECT_FALSE (logs_exporter::detail::IsBulkResponseSuccessful (
223- 200 , R"( {"errors":true,"items":[{"index":42}]})" , 1 , reason));
224- }
225-
226- // A 2xx errors:false body must still carry one item result per exported record. A missing or short
227- // "items" array cannot confirm the batch was written, so a dropped or truncated response that
228- // happens to be valid JSON is a failure rather than a silent success.
229- TEST (ElasticsearchBulkResponseTests, ReportsFailureWhenItemCountDoesNotMatch)
230- {
231- std::string reason;
232-
233- // errors:false but no "items" field at all.
234- EXPECT_FALSE (
235- logs_exporter::detail::IsBulkResponseSuccessful (200 , R"( {"errors":false})" , 1 , reason));
236- EXPECT_NE (reason.find (" items" ), std::string::npos);
237-
238- // errors:false with an empty "items" array for a non-empty batch.
239- EXPECT_FALSE (logs_exporter::detail::IsBulkResponseSuccessful (
240- 200 , R"( {"errors":false,"items":[]})" , 1 , reason));
241-
242- // Fewer item results than records exported.
243- EXPECT_FALSE (logs_exporter::detail::IsBulkResponseSuccessful (
244- 200 , R"( {"errors":false,"items":[{"index":{"status":201}}]})" , 2 , reason));
245-
246- // One result per record is a success.
247- EXPECT_TRUE (logs_exporter::detail::IsBulkResponseSuccessful (
248- 200 , R"( {"errors":false,"items":[{"index":{"status":201}},{"index":{"status":201}}]})" , 2 ,
249- reason));
250-
251- // An empty batch with an empty "items" array is a success.
252- EXPECT_TRUE (logs_exporter::detail::IsBulkResponseSuccessful (200 , R"( {"errors":false,"items":[]})" ,
253- 0 , reason));
230+ 200 , R"( {"errors":true,"items":[{"index":42}]})" , reason));
254231}
0 commit comments