MCPcopy
hub / github.com/elastic/go-elasticsearch / TestBulkIndexerFlush

Function TestBulkIndexerFlush

esutil/bulk_indexer_internal_test.go:1922–2281  ·  view source on GitHub ↗

nolint:gocyclo

(t *testing.T)

Source from the content-addressed store, hash-verified

1920
1921//nolint:gocyclo
1922func TestBulkIndexerFlush(t *testing.T) {
1923 t.Run("Basic", func(t *testing.T) {
1924 responseBody := `{"took":1,"errors":false,"items":[{"index":{"_index":"test","_id":"1","_version":1,"result":"created","status":201}}]}`
1925
1926 es, err := elasticsearch.NewClient(elasticsearch.Config{Transport: &mockTransport{
1927 RoundTripFunc: func(_ *http.Request) (*http.Response, error) {
1928 return &http.Response{
1929 StatusCode: 200,
1930 Body: io.NopCloser(strings.NewReader(responseBody)),
1931 Header: http.Header{"X-Elastic-Product": []string{"Elasticsearch"}},
1932 }, nil
1933 },
1934 }})
1935 if err != nil {
1936 t.Fatalf("Unexpected error: %s", err)
1937 }
1938
1939 bi, err := NewBulkIndexer(BulkIndexerConfig{
1940 NumWorkers: 1,
1941 FlushInterval: time.Hour,
1942 Client: es,
1943 })
1944 if err != nil {
1945 t.Fatalf("Unexpected error: %s", err)
1946 }
1947
1948 if err := bi.Add(context.Background(), BulkIndexerItem{
1949 Action: "index",
1950 DocumentID: "1",
1951 Body: strings.NewReader(`{"title":"foo"}`),
1952 }); err != nil {
1953 t.Fatalf("Unexpected error: %s", err)
1954 }
1955
1956 if err := bi.Flush(context.Background()); err != nil {
1957 t.Fatalf("Unexpected error: %s", err)
1958 }
1959
1960 stats := bi.Stats()
1961 if stats.NumFlushed != 1 {
1962 t.Errorf("Unexpected NumFlushed after Flush: want=1, got=%d", stats.NumFlushed)
1963 }
1964
1965 // Indexer is still usable after Flush
1966 if err := bi.Add(context.Background(), BulkIndexerItem{
1967 Action: "index",
1968 DocumentID: "2",
1969 Body: strings.NewReader(`{"title":"bar"}`),
1970 }); err != nil {
1971 t.Fatalf("Unexpected error adding after Flush: %s", err)
1972 }
1973
1974 if err := bi.Close(context.Background()); err != nil {
1975 t.Fatalf("Unexpected error: %s", err)
1976 }
1977
1978 stats = bi.Stats()
1979 if stats.NumFlushed != 2 {

Callers

nothing calls this directly

Calls 8

AddMethod · 0.95
FlushMethod · 0.95
StatsMethod · 0.95
CloseMethod · 0.95
NewBulkIndexerFunction · 0.85
IsMethod · 0.80
RunMethod · 0.45
WithTimeoutMethod · 0.45

Tested by

no test coverage detected