Skip to content

Commit

Permalink
[chore] make tests faster and close http responses (#24925)
Browse files Browse the repository at this point in the history
These changes only touch tests.
  • Loading branch information
atoulme authored Aug 16, 2023
1 parent 5560819 commit d5ff759
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
19 changes: 18 additions & 1 deletion receiver/splunkhecreceiver/receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ func Test_splunkhecReceiver_handleReq(t *testing.T) {
r.handleReq(w, tt.req)

resp := w.Result()
defer resp.Body.Close()
respBytes, err := io.ReadAll(resp.Body)
assert.NoError(t, err)

Expand Down Expand Up @@ -371,6 +372,7 @@ func Test_consumer_err(t *testing.T) {
r.handleReq(w, req)

resp := w.Result()
defer resp.Body.Close()
respBytes, err := io.ReadAll(resp.Body)
assert.NoError(t, err)

Expand Down Expand Up @@ -398,6 +400,7 @@ func Test_consumer_err_metrics(t *testing.T) {
r.handleReq(w, req)

resp := w.Result()
defer resp.Body.Close()
respBytes, err := io.ReadAll(resp.Body)
assert.NoError(t, err)

Expand Down Expand Up @@ -455,7 +458,7 @@ func Test_splunkhecReceiver_TLS(t *testing.T) {

url := fmt.Sprintf("https://%s", addr)

req, err := http.NewRequest("POST", url, bytes.NewReader(body))
req, err := http.NewRequestWithContext(context.Background(), "POST", url, bytes.NewReader(body))
require.NoErrorf(t, err, "should have no errors with new request: %v", err)

tlscs := configtls.TLSClientSetting{
Expand All @@ -476,6 +479,7 @@ func Test_splunkhecReceiver_TLS(t *testing.T) {

resp, err := client.Do(req)
require.NoErrorf(t, err, "should not have failed when sending to splunk HEC receiver %v", err)
defer resp.Body.Close()
assert.Equal(t, http.StatusOK, resp.StatusCode)
t.Log("Splunk HEC Request Received")

Expand Down Expand Up @@ -600,6 +604,7 @@ func Test_splunkhecReceiver_AccessTokenPassthrough(t *testing.T) {
w := httptest.NewRecorder()
r.handleReq(w, req)
resp := w.Result()
defer resp.Body.Close()
_, err = io.ReadAll(resp.Body)
assert.NoError(t, err)
} else {
Expand All @@ -612,6 +617,7 @@ func Test_splunkhecReceiver_AccessTokenPassthrough(t *testing.T) {
w := httptest.NewRecorder()
r.handleReq(w, req)
resp := w.Result()
defer resp.Body.Close()
_, err = io.ReadAll(resp.Body)
assert.NoError(t, err)
}
Expand Down Expand Up @@ -706,6 +712,7 @@ func Test_Logs_splunkhecReceiver_IndexSourceTypePassthrough(t *testing.T) {
resp := w.Result()
respBytes, err := io.ReadAll(resp.Body)
assert.NoError(t, err)
defer resp.Body.Close()
var bodyStr string
assert.NoError(t, json.Unmarshal(respBytes, &bodyStr))
assert.Equal(t, http.StatusOK, resp.StatusCode)
Expand Down Expand Up @@ -799,6 +806,7 @@ func Test_Metrics_splunkhecReceiver_IndexSourceTypePassthrough(t *testing.T) {
r.handleReq(w, req)
resp := w.Result()
respBytes, err := io.ReadAll(resp.Body)
defer resp.Body.Close()
assert.NoError(t, err)
var bodyStr string
assert.NoError(t, json.Unmarshal(respBytes, &bodyStr))
Expand Down Expand Up @@ -875,6 +883,7 @@ func (aneh *assertNoErrorHost) ReportFatalError(err error) {
}

func Test_splunkhecReceiver_handleRawReq(t *testing.T) {
t.Parallel()
config := createDefaultConfig().(*Config)
config.Endpoint = "localhost:0" // Actually not creating the endpoint
config.RawPath = "/foo"
Expand Down Expand Up @@ -1006,6 +1015,7 @@ func Test_splunkhecReceiver_handleRawReq(t *testing.T) {
resp := w.Result()
respBytes, err := io.ReadAll(resp.Body)
assert.NoError(t, err)
defer resp.Body.Close()

var bodyStr string
if len(respBytes) > 0 {
Expand Down Expand Up @@ -1036,11 +1046,13 @@ func Test_splunkhecreceiver_handleHealthPath(t *testing.T) {
resp := w.Result()
respBytes, err := io.ReadAll(resp.Body)
assert.NoError(t, err)
defer resp.Body.Close()
assert.Equal(t, string(respBytes), responseHecHealthy)
assert.Equal(t, 200, resp.StatusCode)
}

func Test_splunkhecreceiver_handle_nested_fields(t *testing.T) {
t.Parallel()
tests := []struct {
name string
field interface{}
Expand Down Expand Up @@ -1112,6 +1124,7 @@ func Test_splunkhecreceiver_handle_nested_fields(t *testing.T) {
}

func Test_splunkhecReceiver_rawReqHasmetadataInResource(t *testing.T) {
t.Parallel()
config := createDefaultConfig().(*Config)
config.Endpoint = "localhost:0" // Actually not creating the endpoint
config.RawPath = "/foo"
Expand Down Expand Up @@ -1220,6 +1233,7 @@ func Test_splunkhecReceiver_rawReqHasmetadataInResource(t *testing.T) {

resp := w.Result()
assert.NoError(t, err)
defer resp.Body.Close()

assertResponse(t, resp.StatusCode, responseOK)
tt.assertResource(t, sink.AllLogs())
Expand Down Expand Up @@ -1254,11 +1268,13 @@ func BenchmarkHandleReq(b *testing.B) {

resp := w.Result()
_, err = io.ReadAll(resp.Body)
defer resp.Body.Close()
assert.NoError(b, err)
}
}

func Test_splunkhecReceiver_healthCheck_success(t *testing.T) {
t.Parallel()
config := createDefaultConfig().(*Config)
config.Endpoint = "localhost:0" // Actually not creating the endpoint

Expand Down Expand Up @@ -1317,6 +1333,7 @@ func Test_splunkhecReceiver_healthCheck_success(t *testing.T) {
w := httptest.NewRecorder()
r.server.Handler.ServeHTTP(w, tt.req)
resp := w.Result()
defer resp.Body.Close()
respBytes, err := io.ReadAll(resp.Body)
assert.NoError(t, err)
var bodyStr string
Expand Down
2 changes: 2 additions & 0 deletions receiver/splunkhecreceiver/splunkhec_to_metricdata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
)

func Test_splunkV2ToMetricsData(t *testing.T) {
t.Parallel()
// Timestamps for Splunk have a resolution to the millisecond, where the time is reported in seconds with a floating value to the millisecond.
now := time.Now()
msecInt64 := now.UnixNano() / 1e6
Expand Down Expand Up @@ -301,6 +302,7 @@ func Test_splunkV2ToMetricsData(t *testing.T) {
}

func TestGroupMetricsByResource(t *testing.T) {
t.Parallel()
// Timestamps for Splunk have a resolution to the millisecond, where the time is reported in seconds with a floating value to the millisecond.
now := time.Now()
msecInt64 := now.UnixNano() / 1e6
Expand Down

0 comments on commit d5ff759

Please sign in to comment.