From d5ff75962a5f147bb2cda7aab78c8991d9ec8f7f Mon Sep 17 00:00:00 2001 From: Antoine Toulme Date: Wed, 16 Aug 2023 12:33:35 -0700 Subject: [PATCH] [chore] make tests faster and close http responses (#24925) These changes only touch tests. --- receiver/splunkhecreceiver/receiver_test.go | 19 ++++++++++++++++++- .../splunkhec_to_metricdata_test.go | 2 ++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/receiver/splunkhecreceiver/receiver_test.go b/receiver/splunkhecreceiver/receiver_test.go index db2d7db92904..b716ab4ceb7b 100644 --- a/receiver/splunkhecreceiver/receiver_test.go +++ b/receiver/splunkhecreceiver/receiver_test.go @@ -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) @@ -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) @@ -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) @@ -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{ @@ -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") @@ -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 { @@ -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) } @@ -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) @@ -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)) @@ -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" @@ -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 { @@ -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{} @@ -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" @@ -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()) @@ -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 @@ -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 diff --git a/receiver/splunkhecreceiver/splunkhec_to_metricdata_test.go b/receiver/splunkhecreceiver/splunkhec_to_metricdata_test.go index 187c5b836944..6f0f820b6bb4 100644 --- a/receiver/splunkhecreceiver/splunkhec_to_metricdata_test.go +++ b/receiver/splunkhecreceiver/splunkhec_to_metricdata_test.go @@ -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 @@ -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