From d3836d6b30c7c018438edd314bd576a409ad5f88 Mon Sep 17 00:00:00 2001 From: Alex Boten <223565+codeboten@users.noreply.github.com> Date: Thu, 26 Sep 2024 04:52:29 -0700 Subject: [PATCH] config: simplify test code (#6123) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Removing unnecessary additional Ptr funcs. This is test code only, I don't think it requires a changelog entry Signed-off-by: Alex Boten <223565+codeboten@users.noreply.github.com> Co-authored-by: Robert PajÄ…k --- config/metric_test.go | 132 +++++++++++++++++++----------------------- 1 file changed, 60 insertions(+), 72 deletions(-) diff --git a/config/metric_test.go b/config/metric_test.go index 677fb3704b5..ad45fbdf35f 100644 --- a/config/metric_test.go +++ b/config/metric_test.go @@ -464,8 +464,8 @@ func TestReader(t *testing.T) { name: "periodic/console-exporter-with-extra-options", reader: MetricReader{ Periodic: &PeriodicMetricReader{ - Interval: newIntPtr(30_000), - Timeout: newIntPtr(5_000), + Interval: ptr(30_000), + Timeout: ptr(5_000), Exporter: MetricExporter{ Console: Console{}, }, @@ -522,7 +522,7 @@ func TestView(t *testing.T) { name: "selector/invalid_type", view: View{ Selector: &ViewSelector{ - InstrumentType: (*ViewSelectorInstrumentType)(newStringPtr("invalid_type")), + InstrumentType: (*ViewSelectorInstrumentType)(ptr("invalid_type")), }, }, wantErr: "view_selector: instrument_type: invalid value", @@ -538,12 +538,12 @@ func TestView(t *testing.T) { name: "all selectors match", view: View{ Selector: &ViewSelector{ - InstrumentName: newStringPtr("test_name"), - InstrumentType: (*ViewSelectorInstrumentType)(newStringPtr("counter")), - Unit: newStringPtr("test_unit"), - MeterName: newStringPtr("test_meter_name"), - MeterVersion: newStringPtr("test_meter_version"), - MeterSchemaUrl: newStringPtr("test_schema_url"), + InstrumentName: ptr("test_name"), + InstrumentType: (*ViewSelectorInstrumentType)(ptr("counter")), + Unit: ptr("test_unit"), + MeterName: ptr("test_meter_name"), + MeterVersion: ptr("test_meter_version"), + MeterSchemaUrl: ptr("test_schema_url"), }, }, matchInstrument: &sdkmetric.Instrument{ @@ -563,12 +563,12 @@ func TestView(t *testing.T) { name: "all selectors no match name", view: View{ Selector: &ViewSelector{ - InstrumentName: newStringPtr("test_name"), - InstrumentType: (*ViewSelectorInstrumentType)(newStringPtr("counter")), - Unit: newStringPtr("test_unit"), - MeterName: newStringPtr("test_meter_name"), - MeterVersion: newStringPtr("test_meter_version"), - MeterSchemaUrl: newStringPtr("test_schema_url"), + InstrumentName: ptr("test_name"), + InstrumentType: (*ViewSelectorInstrumentType)(ptr("counter")), + Unit: ptr("test_unit"), + MeterName: ptr("test_meter_name"), + MeterVersion: ptr("test_meter_version"), + MeterSchemaUrl: ptr("test_schema_url"), }, }, matchInstrument: &sdkmetric.Instrument{ @@ -588,12 +588,12 @@ func TestView(t *testing.T) { name: "all selectors no match unit", view: View{ Selector: &ViewSelector{ - InstrumentName: newStringPtr("test_name"), - InstrumentType: (*ViewSelectorInstrumentType)(newStringPtr("counter")), - Unit: newStringPtr("test_unit"), - MeterName: newStringPtr("test_meter_name"), - MeterVersion: newStringPtr("test_meter_version"), - MeterSchemaUrl: newStringPtr("test_schema_url"), + InstrumentName: ptr("test_name"), + InstrumentType: (*ViewSelectorInstrumentType)(ptr("counter")), + Unit: ptr("test_unit"), + MeterName: ptr("test_meter_name"), + MeterVersion: ptr("test_meter_version"), + MeterSchemaUrl: ptr("test_schema_url"), }, }, matchInstrument: &sdkmetric.Instrument{ @@ -613,12 +613,12 @@ func TestView(t *testing.T) { name: "all selectors no match kind", view: View{ Selector: &ViewSelector{ - InstrumentName: newStringPtr("test_name"), - InstrumentType: (*ViewSelectorInstrumentType)(newStringPtr("histogram")), - Unit: newStringPtr("test_unit"), - MeterName: newStringPtr("test_meter_name"), - MeterVersion: newStringPtr("test_meter_version"), - MeterSchemaUrl: newStringPtr("test_schema_url"), + InstrumentName: ptr("test_name"), + InstrumentType: (*ViewSelectorInstrumentType)(ptr("histogram")), + Unit: ptr("test_unit"), + MeterName: ptr("test_meter_name"), + MeterVersion: ptr("test_meter_version"), + MeterSchemaUrl: ptr("test_schema_url"), }, }, matchInstrument: &sdkmetric.Instrument{ @@ -638,12 +638,12 @@ func TestView(t *testing.T) { name: "all selectors no match meter name", view: View{ Selector: &ViewSelector{ - InstrumentName: newStringPtr("test_name"), - InstrumentType: (*ViewSelectorInstrumentType)(newStringPtr("counter")), - Unit: newStringPtr("test_unit"), - MeterName: newStringPtr("test_meter_name"), - MeterVersion: newStringPtr("test_meter_version"), - MeterSchemaUrl: newStringPtr("test_schema_url"), + InstrumentName: ptr("test_name"), + InstrumentType: (*ViewSelectorInstrumentType)(ptr("counter")), + Unit: ptr("test_unit"), + MeterName: ptr("test_meter_name"), + MeterVersion: ptr("test_meter_version"), + MeterSchemaUrl: ptr("test_schema_url"), }, }, matchInstrument: &sdkmetric.Instrument{ @@ -663,12 +663,12 @@ func TestView(t *testing.T) { name: "all selectors no match meter version", view: View{ Selector: &ViewSelector{ - InstrumentName: newStringPtr("test_name"), - InstrumentType: (*ViewSelectorInstrumentType)(newStringPtr("counter")), - Unit: newStringPtr("test_unit"), - MeterName: newStringPtr("test_meter_name"), - MeterVersion: newStringPtr("test_meter_version"), - MeterSchemaUrl: newStringPtr("test_schema_url"), + InstrumentName: ptr("test_name"), + InstrumentType: (*ViewSelectorInstrumentType)(ptr("counter")), + Unit: ptr("test_unit"), + MeterName: ptr("test_meter_name"), + MeterVersion: ptr("test_meter_version"), + MeterSchemaUrl: ptr("test_schema_url"), }, }, matchInstrument: &sdkmetric.Instrument{ @@ -688,12 +688,12 @@ func TestView(t *testing.T) { name: "all selectors no match meter schema url", view: View{ Selector: &ViewSelector{ - InstrumentName: newStringPtr("test_name"), - InstrumentType: (*ViewSelectorInstrumentType)(newStringPtr("counter")), - Unit: newStringPtr("test_unit"), - MeterName: newStringPtr("test_meter_name"), - MeterVersion: newStringPtr("test_meter_version"), - MeterSchemaUrl: newStringPtr("test_schema_url"), + InstrumentName: ptr("test_name"), + InstrumentType: (*ViewSelectorInstrumentType)(ptr("counter")), + Unit: ptr("test_unit"), + MeterName: ptr("test_meter_name"), + MeterVersion: ptr("test_meter_version"), + MeterSchemaUrl: ptr("test_schema_url"), }, }, matchInstrument: &sdkmetric.Instrument{ @@ -713,12 +713,12 @@ func TestView(t *testing.T) { name: "with stream", view: View{ Selector: &ViewSelector{ - InstrumentName: newStringPtr("test_name"), - Unit: newStringPtr("test_unit"), + InstrumentName: ptr("test_name"), + Unit: ptr("test_unit"), }, Stream: &ViewStream{ - Name: newStringPtr("new_name"), - Description: newStringPtr("new_description"), + Name: ptr("new_name"), + Description: ptr("new_description"), AttributeKeys: []string{"foo", "bar"}, Aggregation: &ViewStreamAggregation{Sum: make(ViewStreamAggregationSum)}, }, @@ -768,37 +768,37 @@ func TestInstrumentType(t *testing.T) { }, { name: "counter", - instType: (*ViewSelectorInstrumentType)(newStringPtr("counter")), + instType: (*ViewSelectorInstrumentType)(ptr("counter")), wantKind: sdkmetric.InstrumentKindCounter, }, { name: "up_down_counter", - instType: (*ViewSelectorInstrumentType)(newStringPtr("up_down_counter")), + instType: (*ViewSelectorInstrumentType)(ptr("up_down_counter")), wantKind: sdkmetric.InstrumentKindUpDownCounter, }, { name: "histogram", - instType: (*ViewSelectorInstrumentType)(newStringPtr("histogram")), + instType: (*ViewSelectorInstrumentType)(ptr("histogram")), wantKind: sdkmetric.InstrumentKindHistogram, }, { name: "observable_counter", - instType: (*ViewSelectorInstrumentType)(newStringPtr("observable_counter")), + instType: (*ViewSelectorInstrumentType)(ptr("observable_counter")), wantKind: sdkmetric.InstrumentKindObservableCounter, }, { name: "observable_up_down_counter", - instType: (*ViewSelectorInstrumentType)(newStringPtr("observable_up_down_counter")), + instType: (*ViewSelectorInstrumentType)(ptr("observable_up_down_counter")), wantKind: sdkmetric.InstrumentKindObservableUpDownCounter, }, { name: "observable_gauge", - instType: (*ViewSelectorInstrumentType)(newStringPtr("observable_gauge")), + instType: (*ViewSelectorInstrumentType)(ptr("observable_gauge")), wantKind: sdkmetric.InstrumentKindObservableGauge, }, { name: "invalid", - instType: (*ViewSelectorInstrumentType)(newStringPtr("invalid")), + instType: (*ViewSelectorInstrumentType)(ptr("invalid")), wantErr: errors.New("instrument_type: invalid value"), }, } @@ -846,9 +846,9 @@ func TestAggregation(t *testing.T) { name: "Base2ExponentialBucketHistogram", aggregation: &ViewStreamAggregation{ Base2ExponentialBucketHistogram: &ViewStreamAggregationBase2ExponentialBucketHistogram{ - MaxSize: newIntPtr(2), - MaxScale: newIntPtr(3), - RecordMinMax: newBoolPtr(true), + MaxSize: ptr(2), + MaxScale: ptr(3), + RecordMinMax: ptr(true), }, }, wantAggregation: sdkmetric.AggregationBase2ExponentialHistogram{ @@ -886,7 +886,7 @@ func TestAggregation(t *testing.T) { aggregation: &ViewStreamAggregation{ ExplicitBucketHistogram: &ViewStreamAggregationExplicitBucketHistogram{ Boundaries: []float64{1, 2, 3}, - RecordMinMax: newBoolPtr(true), + RecordMinMax: ptr(true), }, }, wantAggregation: sdkmetric.AggregationExplicitBucketHistogram{ @@ -949,15 +949,3 @@ func TestAttributeFilter(t *testing.T) { }) } } - -func newBoolPtr(b bool) *bool { - return &b -} - -func newIntPtr(i int) *int { - return &i -} - -func newStringPtr(str string) *string { - return &str -}