Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

config: simplify test code #6123

Merged
merged 3 commits into from
Sep 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 60 additions & 72 deletions config/metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{},
},
Expand Down Expand Up @@ -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",
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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)},
},
Expand Down Expand Up @@ -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"),
},
}
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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
}
Loading