|
8 | 8 | "context"
|
9 | 9 | "fmt"
|
10 | 10 | "math/rand"
|
| 11 | + "net/http" |
| 12 | + "net/http/httptest" |
11 | 13 | "reflect"
|
12 | 14 | "sync/atomic"
|
13 | 15 | "testing"
|
@@ -335,6 +337,60 @@ func TestBulkProcessorFlush(t *testing.T) {
|
335 | 337 | }
|
336 | 338 | }
|
337 | 339 |
|
| 340 | +func TestBulkWorker_commit_clearFailedRequests(t *testing.T) { |
| 341 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})) |
| 342 | + defer server.Close() |
| 343 | + |
| 344 | + client, err := NewClient( |
| 345 | + SetURL(server.URL), |
| 346 | + SetSniff(false), |
| 347 | + SetHealthcheck(false), |
| 348 | + SetHttpClient(tooManyHTTPClient{}), |
| 349 | + ) |
| 350 | + if err != nil { |
| 351 | + t.Fatal(err) |
| 352 | + } |
| 353 | + |
| 354 | + var calls int |
| 355 | + |
| 356 | + bulkProcessor, err := NewBulkProcessorService(client). |
| 357 | + BulkActions(-1). |
| 358 | + BulkSize(-1). |
| 359 | + FlushInterval(0). |
| 360 | + RetryItemStatusCodes(). |
| 361 | + Backoff(StopBackoff{}). |
| 362 | + After(BulkAfterFunc(func(executionId int64, requests []BulkableRequest, response *BulkResponse, err error) { |
| 363 | + calls++ |
| 364 | + if calls == 1 { |
| 365 | + if len(requests) != 10 { |
| 366 | + t.Errorf("expected 10 requests; got: %d", len(requests)) |
| 367 | + } |
| 368 | + } else if len(requests) > 0 { |
| 369 | + t.Errorf("expected 0 requests; got: %d", len(requests)) |
| 370 | + } |
| 371 | + })).Do(context.Background()) |
| 372 | + |
| 373 | + if err != nil { |
| 374 | + t.Fatal(err) |
| 375 | + } |
| 376 | + |
| 377 | + for i := 0; i < 10; i++ { |
| 378 | + bulkProcessor.Add(NewBulkIndexRequest()) |
| 379 | + } |
| 380 | + |
| 381 | + // first flush should process 10 items |
| 382 | + if err := bulkProcessor.Flush(); err != nil { |
| 383 | + t.Fatal(err) |
| 384 | + } |
| 385 | + // second flush should process none (even if the first flush failed) |
| 386 | + if err := bulkProcessor.Flush(); err != nil { |
| 387 | + t.Fatal(err) |
| 388 | + } |
| 389 | + if err := bulkProcessor.Close(); err != nil { |
| 390 | + t.Fatal(err) |
| 391 | + } |
| 392 | +} |
| 393 | + |
338 | 394 | // -- Helper --
|
339 | 395 |
|
340 | 396 | func testBulkProcessor(t *testing.T, numDocs int, svc *BulkProcessorService) {
|
@@ -427,3 +483,12 @@ func testBulkProcessor(t *testing.T, numDocs int, svc *BulkProcessorService) {
|
427 | 483 | t.Fatalf("expected %d documents; got: %d", numDocs, count)
|
428 | 484 | }
|
429 | 485 | }
|
| 486 | + |
| 487 | +type tooManyHTTPClient struct { |
| 488 | +} |
| 489 | + |
| 490 | +func (t tooManyHTTPClient) Do(r *http.Request) (*http.Response, error) { |
| 491 | + recorder := httptest.NewRecorder() |
| 492 | + recorder.WriteHeader(http.StatusTooManyRequests) |
| 493 | + return recorder.Result(), nil |
| 494 | +} |
0 commit comments