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

chore(*): enable revive.flag-parameter rules #416

Merged
merged 1 commit into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ linters-settings:
- 4
- name: flag-parameter
severity: warning
disabled: true
disabled: false
- name: deep-exit
severity: warning
disabled: true
Expand Down
25 changes: 11 additions & 14 deletions cmd/client/put/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,13 @@ type Query struct {
}

func (query Query) Perform(client oxia.AsyncClient) common.Call {
binary := false
if query.Binary != nil {
binary = *query.Binary
value := []byte(query.Value)
var err error
if query.Binary != nil && *query.Binary {
value, err = convertFromBinaryValue(query.Value)
}
value, err := convertValue(binary, query.Value)
call := Call{}

if err != nil {
errChan := make(chan oxia.PutResult, 1)
errChan <- oxia.PutResult{Err: err}
Expand All @@ -138,17 +139,13 @@ func (Query) Unmarshal(b []byte) (common.Query, error) {
return q, err
}

func convertValue(binary bool, value string) ([]byte, error) {
if binary {
decoded := make([]byte, int64(float64(len(value))*0.8))
_, err := base64.StdEncoding.Decode(decoded, []byte(value))
if err != nil {
return nil, ErrBase64ValueInvalid
}
return decoded, nil
func convertFromBinaryValue(value string) ([]byte, error) {
decoded := make([]byte, int64(float64(len(value))*0.8))
_, err := base64.StdEncoding.Decode(decoded, []byte(value))
if err != nil {
return nil, ErrBase64ValueInvalid
}

return []byte(value), nil
return decoded, nil
}

type Call struct {
Expand Down
12 changes: 2 additions & 10 deletions cmd/client/put/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,34 +289,26 @@ func TestOutputMarshal(t *testing.T) {
}
}

func TestConvertValue(t *testing.T) {
func TestConvertBinaryValue(t *testing.T) {
for _, test := range []struct {
name string
binary bool
value string
expectedErr error
expected string
}{
{
name: "text",
value: "hello",
expected: "hello",
},
{
name: "binary",
value: "aGVsbG8y",
binary: true,
expected: "hello2",
},
{
name: "invalid-binary",
value: "hello",
binary: true,
expectedErr: ErrBase64ValueInvalid,
},
} {
t.Run(test.name, func(t *testing.T) {
result, err := convertValue(test.binary, test.value)
result, err := convertFromBinaryValue(test.value)
assert.Equal(t, test.expected, string(result))
assert.ErrorIs(t, err, test.expectedErr)
})
Expand Down
37 changes: 22 additions & 15 deletions oxia/internal/metrics/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,11 @@ func TestMetricsDecorate(t *testing.T) {
assert.NoError(t, err)

assertTimer(t, rm, "oxia_client_op", item.expectedType, condition.expectedResult)
assertHistogram(t, rm, "oxia_client_op_value", item.hasHistogram, 5, item.expectedType, condition.expectedResult)
if item.hasHistogram {
assertWithHistogram(t, rm, "oxia_client_op_value", 5, item.expectedType, condition.expectedResult)
} else {
assertWithoutHistogram(t, rm, "oxia_client_op_value")
}
})
}
}
Expand Down Expand Up @@ -127,8 +131,8 @@ func TestMetricsCallback(t *testing.T) {

assertTimer(t, rm, "oxia_client_batch_total", item.expectedType, condition.expectedResult)
assertTimer(t, rm, "oxia_client_batch_exec", item.expectedType, condition.expectedResult)
assertHistogram(t, rm, "oxia_client_batch_value", true, 5, item.expectedType, condition.expectedResult)
assertHistogram(t, rm, "oxia_client_batch_request", true, 1, item.expectedType, condition.expectedResult)
assertWithHistogram(t, rm, "oxia_client_batch_value", 5, item.expectedType, condition.expectedResult)
assertWithHistogram(t, rm, "oxia_client_batch_request", 1, item.expectedType, condition.expectedResult)
})
}
}
Expand All @@ -153,22 +157,25 @@ func assertTimer(t *testing.T, rm metricdata.ResourceMetrics, name string, expec
assertDataPoints(t, counts, int64(1), expectedType, expectedResult)
}

func assertHistogram(t *testing.T, rm metricdata.ResourceMetrics, name string, hasHistogram bool, expectedSum int64, expectedType string, expectedResult string) {
func assertWithHistogram(t *testing.T, rm metricdata.ResourceMetrics, name string, expectedSum int64, expectedType string, expectedResult string) {
t.Helper()

datapoints, err := histogram(rm, name)

if hasHistogram {
assert.NoError(t, err)
assert.Equal(t, 1, len(datapoints))
histogram := datapoints[0]
assert.Equal(t, expectedSum, histogram.Sum)
assert.Equal(t, uint64(1), histogram.Count)
assertAttributes(t, histogram.Attributes, expectedType, expectedResult)
} else {
assert.Error(t, err)
assert.Equal(t, 0, len(datapoints))
}
assert.NoError(t, err)
assert.Equal(t, 1, len(datapoints))
histogram := datapoints[0]
assert.Equal(t, expectedSum, histogram.Sum)
assert.Equal(t, uint64(1), histogram.Count)
assertAttributes(t, histogram.Attributes, expectedType, expectedResult)
}

func assertWithoutHistogram(t *testing.T, rm metricdata.ResourceMetrics, name string) {
t.Helper()

datapoints, err := histogram(rm, name)
assert.Error(t, err)
assert.Equal(t, 0, len(datapoints))
}

func counter[N int64 | float64](rm metricdata.ResourceMetrics, name string) ([]metricdata.DataPoint[N], error) {
Expand Down
Loading