Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove esutil ioutil import #877

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions esutil/bulk_indexer_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ package esutil_test
import (
"bytes"
"context"
"io/ioutil"
"io"
"net/http"
"strconv"
"strings"
Expand Down Expand Up @@ -55,7 +55,7 @@ var mockResponseBody = `{
type mockTransp struct{}

func (t *mockTransp) RoundTrip(req *http.Request) (*http.Response, error) {
return &http.Response{Body: ioutil.NopCloser(strings.NewReader(mockResponseBody))}, nil // 1x alloc
return &http.Response{Body: io.NopCloser(strings.NewReader(mockResponseBody))}, nil // 1x alloc
}

func BenchmarkBulkIndexer(b *testing.B) {
Expand Down
23 changes: 11 additions & 12 deletions esutil/bulk_indexer_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
Expand All @@ -46,7 +45,7 @@ import (
)

var defaultRoundTripFunc = func(*http.Request) (*http.Response, error) {
return &http.Response{Body: ioutil.NopCloser(strings.NewReader(`{}`))}, nil
return &http.Response{Body: io.NopCloser(strings.NewReader(`{}`))}, nil
}

type mockTransport struct {
Expand Down Expand Up @@ -81,9 +80,9 @@ func TestBulkIndexer(t *testing.T) {
case 3:
testfile = "testdata/bulk_response_1c.json"
}
bodyContent, _ := ioutil.ReadFile(testfile)
bodyContent, _ := os.ReadFile(testfile)
return &http.Response{
Body: ioutil.NopCloser(bytes.NewBuffer(bodyContent)),
Body: io.NopCloser(bytes.NewBuffer(bodyContent)),
Header: http.Header{"X-Elastic-Product": []string{"Elasticsearch"}},
}, nil
},
Expand Down Expand Up @@ -261,13 +260,13 @@ func TestBulkIndexer(t *testing.T) {

numItems = 4
numFailed = 2
bodyContent, _ = ioutil.ReadFile("testdata/bulk_response_2.json")
bodyContent, _ = os.ReadFile("testdata/bulk_response_2.json")
)

es, _ := elasticsearch.NewClient(elasticsearch.Config{Transport: &mockTransport{
RoundTripFunc: func(*http.Request) (*http.Response, error) {
return &http.Response{
Body: ioutil.NopCloser(bytes.NewBuffer(bodyContent)),
Body: io.NopCloser(bytes.NewBuffer(bodyContent)),
Header: http.Header{"X-Elastic-Product": []string{"Elasticsearch"}},
}, nil
},
Expand All @@ -283,7 +282,7 @@ func TestBulkIndexer(t *testing.T) {
successFunc := func(ctx context.Context, item BulkIndexerItem, res BulkIndexerResponseItem) {
atomic.AddUint64(&countSuccessful, 1)

buf, err := ioutil.ReadAll(item.Body)
buf, err := io.ReadAll(item.Body)
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
Expand All @@ -293,7 +292,7 @@ func TestBulkIndexer(t *testing.T) {
atomic.AddUint64(&countFailed, 1)
failedIDs = append(failedIDs, item.DocumentID)

buf, err := ioutil.ReadAll(item.Body)
buf, err := io.ReadAll(item.Body)
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
Expand Down Expand Up @@ -436,7 +435,7 @@ func TestBulkIndexer(t *testing.T) {
return &http.Response{
StatusCode: http.StatusOK,
Status: "200 OK",
Body: ioutil.NopCloser(strings.NewReader(`{"items":[{"index": {}}]}`)),
Body: io.NopCloser(strings.NewReader(`{"items":[{"index": {}}]}`)),
Header: http.Header{"X-Elastic-Product": []string{"Elasticsearch"}},
}, nil
},
Expand Down Expand Up @@ -499,14 +498,14 @@ func TestBulkIndexer(t *testing.T) {
return &http.Response{
StatusCode: http.StatusTooManyRequests,
Status: "429 TooManyRequests",
Body: ioutil.NopCloser(strings.NewReader(`{"took":1}`)),
Body: io.NopCloser(strings.NewReader(`{"took":1}`)),
}, nil
}
bodyContent, _ := ioutil.ReadFile("testdata/bulk_response_1c.json")
bodyContent, _ := os.ReadFile("testdata/bulk_response_1c.json")
return &http.Response{
StatusCode: http.StatusOK,
Status: "200 OK",
Body: ioutil.NopCloser(bytes.NewBuffer(bodyContent)),
Body: io.NopCloser(bytes.NewBuffer(bodyContent)),
Header: http.Header{"X-Elastic-Product": []string{"Elasticsearch"}},
}, nil
},
Expand Down
4 changes: 1 addition & 3 deletions esutil/json_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ func (r *JSONReader) WriteTo(w io.Writer) (int64, error) {
return int64(cw.n), err
}

func (r *JSONReader) encode(w io.Writer) error {
var err error

func (r *JSONReader) encode(w io.Writer) (err error) {
if e, ok := r.val.(JSONEncoder); ok {
err = e.EncodeJSON(w)
if err != nil {
Expand Down
5 changes: 2 additions & 3 deletions esutil/json_reader_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"strings"
"testing"

Expand Down Expand Up @@ -71,7 +70,7 @@ func BenchmarkJSONReader(b *testing.B) {
b.ResetTimer()

for i := 0; i < b.N; i++ {
out, _ := ioutil.ReadAll(esutil.NewJSONReader(map[string]string{"foo": "bar"}))
out, _ := io.ReadAll(esutil.NewJSONReader(map[string]string{"foo": "bar"}))
if string(out) != `{"foo":"bar"}`+"\n" {
b.Fatalf("Unexpected output: %q", out)
}
Expand All @@ -95,7 +94,7 @@ func BenchmarkJSONReader(b *testing.B) {
b.ResetTimer()

for i := 0; i < b.N; i++ {
out, _ := ioutil.ReadAll(esutil.NewJSONReader(Foo{Bar: "baz"}))
out, _ := io.ReadAll(esutil.NewJSONReader(Foo{Bar: "baz"}))
if string(out) != `{"bar":"BAZ"}`+"\n" {
b.Fatalf("Unexpected output: %q", out)
}
Expand Down
5 changes: 2 additions & 3 deletions esutil/json_reader_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"bytes"
"errors"
"io"
"io/ioutil"
"strings"
"testing"
)
Expand All @@ -49,14 +48,14 @@ func (f Foo) EncodeJSON(w io.Writer) error {

func TestJSONReader(t *testing.T) {
t.Run("Default", func(t *testing.T) {
out, _ := ioutil.ReadAll(NewJSONReader(map[string]string{"foo": "bar"}))
out, _ := io.ReadAll(NewJSONReader(map[string]string{"foo": "bar"}))
if string(out) != `{"foo":"bar"}`+"\n" {
t.Fatalf("Unexpected output: %s", out)
}
})

t.Run("Custom", func(t *testing.T) {
out, _ := ioutil.ReadAll(NewJSONReader(Foo{Bar: "baz"}))
out, _ := io.ReadAll(NewJSONReader(Foo{Bar: "baz"}))
if string(out) != `{"bar":"BAZ"}`+"\n" {
t.Fatalf("Unexpected output: %s", out)
}
Expand Down