Skip to content

Commit 4e3ff5f

Browse files
committed
test: refactor to error_test.go and remove commented lines
1 parent 0da64b3 commit 4e3ff5f

File tree

4 files changed

+54
-46
lines changed

4 files changed

+54
-46
lines changed

api/http/error_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright 2020-2021 InfluxData, Inc. All rights reserved.
2+
// Use of this source code is governed by MIT
3+
// license that can be found in the LICENSE file.
4+
5+
package http
6+
7+
import (
8+
"github.com/stretchr/testify/assert"
9+
ihttp "net/http"
10+
11+
"testing"
12+
)
13+
14+
func TestWriteErrorHeaderToString(t *testing.T) {
15+
header := ihttp.Header{
16+
"Date": []string{"2024-08-07T12:00:00.009"},
17+
"Content-Length": []string{"12"},
18+
"Content-Type": []string{"application/json", "encoding UTF-8"},
19+
"X-Test-Value1": []string{"SaturnV"},
20+
"X-Test-Value2": []string{"Apollo11"},
21+
"Retry-After": []string{"2044"},
22+
"Trace-Id": []string{"123456789ABCDEF0"},
23+
}
24+
25+
err := Error{
26+
StatusCode: ihttp.StatusBadRequest,
27+
Code: "bad request",
28+
Message: "this is just a test",
29+
Err: nil,
30+
RetryAfter: 2044,
31+
Header: header,
32+
}
33+
34+
fullString := err.HeaderToString([]string{})
35+
36+
// write order is not guaranteed
37+
assert.Contains(t, fullString, "Date: 2024-08-07T12:00:00.009")
38+
assert.Contains(t, fullString, "Content-Length: 12")
39+
assert.Contains(t, fullString, "Content-Type: application/json")
40+
assert.Contains(t, fullString, "X-Test-Value1: SaturnV")
41+
assert.Contains(t, fullString, "X-Test-Value2: Apollo11")
42+
assert.Contains(t, fullString, "Retry-After: 2044")
43+
assert.Contains(t, fullString, "Trace-Id: 123456789ABCDEF0")
44+
45+
filterString := err.HeaderToString([]string{"date", "trace-id", "x-test-value1", "x-test-value2"})
46+
47+
// write order will follow filter arguments
48+
assert.Equal(t, filterString,
49+
"Date: 2024-08-07T12:00:00.009\nTrace-Id: 123456789ABCDEF0\nX-Test-Value1: SaturnV\nX-Test-Value2: Apollo11\n",
50+
)
51+
assert.NotContains(t, filterString, "Content-Type: application/json")
52+
assert.NotContains(t, filterString, "Retry-After: 2044")
53+
}

api/write_test.go

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -312,44 +312,3 @@ func TestWriteApiErrorHeaders(t *testing.T) {
312312
wg.Wait()
313313
assert.Equal(t, calls, 3)
314314
}
315-
316-
func TestWriteErrorHeaderToString(t *testing.T) {
317-
header := ihttp.Header{
318-
"Date": []string{"2024-08-07T12:00:00.009"},
319-
"Content-Length": []string{"12"},
320-
"Content-Type": []string{"application/json", "encoding UTF-8"},
321-
"X-Test-Value1": []string{"SaturnV"},
322-
"X-Test-Value2": []string{"Apollo11"},
323-
"Retry-After": []string{"2044"},
324-
"Trace-Id": []string{"123456789ABCDEF0"},
325-
}
326-
327-
err := http.Error{
328-
StatusCode: ihttp.StatusBadRequest,
329-
Code: "bad request",
330-
Message: "this is just a test",
331-
Err: nil,
332-
RetryAfter: 2044,
333-
Header: header,
334-
}
335-
336-
fullString := err.HeaderToString([]string{})
337-
338-
// write order is not guaranteed
339-
assert.Contains(t, fullString, "Date: 2024-08-07T12:00:00.009")
340-
assert.Contains(t, fullString, "Content-Length: 12")
341-
assert.Contains(t, fullString, "Content-Type: application/json")
342-
assert.Contains(t, fullString, "X-Test-Value1: SaturnV")
343-
assert.Contains(t, fullString, "X-Test-Value2: Apollo11")
344-
assert.Contains(t, fullString, "Retry-After: 2044")
345-
assert.Contains(t, fullString, "Trace-Id: 123456789ABCDEF0")
346-
347-
filterString := err.HeaderToString([]string{"date", "trace-id", "x-test-value1", "x-test-value2"})
348-
349-
// write order will follow filter arguments
350-
assert.Equal(t, filterString,
351-
"Date: 2024-08-07T12:00:00.009\nTrace-Id: 123456789ABCDEF0\nX-Test-Value1: SaturnV\nX-Test-Value2: Apollo11\n",
352-
)
353-
assert.NotContains(t, filterString, "Content-Type: application/json")
354-
assert.NotContains(t, filterString, "Retry-After: 2044")
355-
}

internal/write/service.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,6 @@ func (w *Service) HandleWrite(ctx context.Context, batch *Batch) error {
164164
if batchToWrite != nil {
165165
perror := w.WriteBatch(ctx, batchToWrite)
166166
if perror != nil {
167-
// fmt.Printf("DEBUG perror type %s\n", reflect.TypeOf(perror))
168167
if isIgnorableError(perror) {
169168
log.Warnf("Write error: %s", perror.Error())
170169
} else {
@@ -207,7 +206,7 @@ func (w *Service) HandleWrite(ctx context.Context, batch *Batch) error {
207206
"X-Influxdb-Version",
208207
})
209208
if len(logHeaders) > 0 {
210-
logMessage += fmt.Sprintf("\nSelect Response Headers:\n%s", logHeaders)
209+
logMessage += fmt.Sprintf("\nSelected Response Headers:\n%s", logHeaders)
211210
}
212211
log.Error(logMessage)
213212
}

internal/write/service_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -337,13 +337,10 @@ func TestMaxRetryTime(t *testing.T) {
337337
b := NewBatch("2\n", exp)
338338
// First batch will be checked against maxRetryTime and it will expire. New batch will fail and it will added to retry queue
339339
err = srv.HandleWrite(ctx, b)
340-
//fmt.Printf("DEBUG err %v\n", err)
341-
//fmt.Printf("DEBUG err %v\n", err)
342340
require.NotNil(t, err)
343341
// 1st Batch expires and writing 2nd trows error
344342
assert.Equal(t, "write failed (attempts 1): Unexpected status code 429", err.Error())
345343
assert.Equal(t, 1, srv.retryQueue.list.Len())
346-
// fmt.Printf("DEBUG Header len: %d\n", len(err.(*http.Error).Header))
347344

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

0 commit comments

Comments
 (0)