Skip to content

Commit

Permalink
test: refactor to error_test.go and remove commented lines
Browse files Browse the repository at this point in the history
  • Loading branch information
karel-rehor committed Aug 8, 2024
1 parent 0da64b3 commit 4e3ff5f
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 46 deletions.
53 changes: 53 additions & 0 deletions api/http/error_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2020-2021 InfluxData, Inc. All rights reserved.
// Use of this source code is governed by MIT
// license that can be found in the LICENSE file.

package http

import (
"github.com/stretchr/testify/assert"
ihttp "net/http"

"testing"
)

func TestWriteErrorHeaderToString(t *testing.T) {
header := ihttp.Header{
"Date": []string{"2024-08-07T12:00:00.009"},
"Content-Length": []string{"12"},
"Content-Type": []string{"application/json", "encoding UTF-8"},
"X-Test-Value1": []string{"SaturnV"},
"X-Test-Value2": []string{"Apollo11"},
"Retry-After": []string{"2044"},
"Trace-Id": []string{"123456789ABCDEF0"},
}

err := Error{
StatusCode: ihttp.StatusBadRequest,
Code: "bad request",
Message: "this is just a test",
Err: nil,
RetryAfter: 2044,
Header: header,
}

fullString := err.HeaderToString([]string{})

// write order is not guaranteed
assert.Contains(t, fullString, "Date: 2024-08-07T12:00:00.009")
assert.Contains(t, fullString, "Content-Length: 12")
assert.Contains(t, fullString, "Content-Type: application/json")
assert.Contains(t, fullString, "X-Test-Value1: SaturnV")
assert.Contains(t, fullString, "X-Test-Value2: Apollo11")
assert.Contains(t, fullString, "Retry-After: 2044")
assert.Contains(t, fullString, "Trace-Id: 123456789ABCDEF0")

filterString := err.HeaderToString([]string{"date", "trace-id", "x-test-value1", "x-test-value2"})

// write order will follow filter arguments
assert.Equal(t, filterString,
"Date: 2024-08-07T12:00:00.009\nTrace-Id: 123456789ABCDEF0\nX-Test-Value1: SaturnV\nX-Test-Value2: Apollo11\n",
)
assert.NotContains(t, filterString, "Content-Type: application/json")
assert.NotContains(t, filterString, "Retry-After: 2044")
}
41 changes: 0 additions & 41 deletions api/write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,44 +312,3 @@ func TestWriteApiErrorHeaders(t *testing.T) {
wg.Wait()
assert.Equal(t, calls, 3)
}

func TestWriteErrorHeaderToString(t *testing.T) {
header := ihttp.Header{
"Date": []string{"2024-08-07T12:00:00.009"},
"Content-Length": []string{"12"},
"Content-Type": []string{"application/json", "encoding UTF-8"},
"X-Test-Value1": []string{"SaturnV"},
"X-Test-Value2": []string{"Apollo11"},
"Retry-After": []string{"2044"},
"Trace-Id": []string{"123456789ABCDEF0"},
}

err := http.Error{
StatusCode: ihttp.StatusBadRequest,
Code: "bad request",
Message: "this is just a test",
Err: nil,
RetryAfter: 2044,
Header: header,
}

fullString := err.HeaderToString([]string{})

// write order is not guaranteed
assert.Contains(t, fullString, "Date: 2024-08-07T12:00:00.009")
assert.Contains(t, fullString, "Content-Length: 12")
assert.Contains(t, fullString, "Content-Type: application/json")
assert.Contains(t, fullString, "X-Test-Value1: SaturnV")
assert.Contains(t, fullString, "X-Test-Value2: Apollo11")
assert.Contains(t, fullString, "Retry-After: 2044")
assert.Contains(t, fullString, "Trace-Id: 123456789ABCDEF0")

filterString := err.HeaderToString([]string{"date", "trace-id", "x-test-value1", "x-test-value2"})

// write order will follow filter arguments
assert.Equal(t, filterString,
"Date: 2024-08-07T12:00:00.009\nTrace-Id: 123456789ABCDEF0\nX-Test-Value1: SaturnV\nX-Test-Value2: Apollo11\n",
)
assert.NotContains(t, filterString, "Content-Type: application/json")
assert.NotContains(t, filterString, "Retry-After: 2044")
}
3 changes: 1 addition & 2 deletions internal/write/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ func (w *Service) HandleWrite(ctx context.Context, batch *Batch) error {
if batchToWrite != nil {
perror := w.WriteBatch(ctx, batchToWrite)
if perror != nil {
// fmt.Printf("DEBUG perror type %s\n", reflect.TypeOf(perror))
if isIgnorableError(perror) {
log.Warnf("Write error: %s", perror.Error())
} else {
Expand Down Expand Up @@ -207,7 +206,7 @@ func (w *Service) HandleWrite(ctx context.Context, batch *Batch) error {
"X-Influxdb-Version",
})
if len(logHeaders) > 0 {
logMessage += fmt.Sprintf("\nSelect Response Headers:\n%s", logHeaders)
logMessage += fmt.Sprintf("\nSelected Response Headers:\n%s", logHeaders)
}
log.Error(logMessage)
}
Expand Down
3 changes: 0 additions & 3 deletions internal/write/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,13 +337,10 @@ func TestMaxRetryTime(t *testing.T) {
b := NewBatch("2\n", exp)
// First batch will be checked against maxRetryTime and it will expire. New batch will fail and it will added to retry queue
err = srv.HandleWrite(ctx, b)
//fmt.Printf("DEBUG err %v\n", err)
//fmt.Printf("DEBUG err %v\n", err)
require.NotNil(t, err)
// 1st Batch expires and writing 2nd trows error
assert.Equal(t, "write failed (attempts 1): Unexpected status code 429", err.Error())
assert.Equal(t, 1, srv.retryQueue.list.Len())
// fmt.Printf("DEBUG Header len: %d\n", len(err.(*http.Error).Header))

//wait until remaining accumulated retryDelay has passed, because there hasn't been a successful write yet
<-time.After(time.Until(srv.lastWriteAttempt.Add(time.Millisecond * time.Duration(srv.retryDelay))))
Expand Down

0 comments on commit 4e3ff5f

Please sign in to comment.