Skip to content

Commit

Permalink
testifylint: enable float-compare (#5989)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmorel-35 authored Sep 15, 2024
1 parent 0824fc2 commit da039e8
Show file tree
Hide file tree
Showing 21 changed files with 74 additions and 85 deletions.
13 changes: 1 addition & 12 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -222,19 +222,8 @@ linters-settings:
disabled: true
testifylint:
disable:
- float-compare
- go-require
enable:
- bool-compare
- compares
- empty
- error-is-as
- error-nil
- expected-actual
- len
- require-error
- suite-dont-use-pkg
- suite-extra-assert-call
enable-all: true

run:
go: "1.22"
Expand Down
2 changes: 1 addition & 1 deletion cmd/all-in-one/all_in_one_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func getSamplingStrategy(t *testing.T) {
require.NoError(t, jsonpb.Unmarshal(bytes.NewReader(body), &queryResponse))

assert.NotNil(t, queryResponse.ProbabilisticSampling)
assert.EqualValues(t, 1.0, queryResponse.ProbabilisticSampling.SamplingRate)
assert.InDelta(t, 1.0, queryResponse.ProbabilisticSampling.SamplingRate, 0.01)
}

func getServicesAPIV3(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func TestStartGRPC(t *testing.T) {
c := api_v2.NewSamplingManagerClient(conn)
response, err := c.GetSamplingStrategy(context.Background(), &api_v2.SamplingStrategyParameters{ServiceName: "foo"})
require.NoError(t, err)
require.Equal(t, 0.8, response.ProbabilisticSampling.SamplingRate)
require.InDelta(t, 0.8, response.ProbabilisticSampling.SamplingRate, 0.01)

require.NoError(t, ext.Shutdown(context.Background()))
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/query/app/handler_deps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ func TestGetDependenciesSuccess(t *testing.T) {
actual := data.(map[string]any)
assert.Equal(t, "killer", actual["parent"])
assert.Equal(t, "queen", actual["child"])
assert.Equal(t, 12.00, actual["callCount"]) // recovered type is float
assert.InDelta(t, 12.00, actual["callCount"], 0.01) // recovered type is float
require.NoError(t, err)
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/query/app/http_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -817,7 +817,7 @@ func TestGetMinStep(t *testing.T) {

// Verify
require.NoError(t, err)
assert.Equal(t, float64(5), response.Data)
assert.InDelta(t, float64(5), response.Data, 0.01)
}

// getJSON fetches a JSON document from a server via HTTP GET
Expand Down
4 changes: 2 additions & 2 deletions crossdock/services/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestGetSamplingRateInternal(t *testing.T) {
if test.shouldErr {
require.EqualError(t, err, errSamplingRateMissing.Error())
}
assert.EqualValues(t, test.rate, rate)
assert.InDelta(t, test.rate, rate, 0.01)
}
}

Expand Down Expand Up @@ -89,7 +89,7 @@ func TestGetSamplingRate(t *testing.T) {
agent = NewAgentService(server.URL, zap.NewNop())
rate, err := agent.GetSamplingRate("svc", "op")
require.NoError(t, err)
assert.EqualValues(t, 1, rate)
assert.InDelta(t, 1.0, rate, 0.01)

_, err = agent.GetSamplingRate("bad_svc", "op")
require.Error(t, err)
Expand Down
12 changes: 6 additions & 6 deletions internal/metrics/otelmetrics/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func TestCounter(t *testing.T) {

testCounter := findMetric(t, registry, "test_counter_total")
metrics := testCounter.GetMetric()
assert.Equal(t, float64(2), metrics[0].GetCounter().GetValue())
assert.InDelta(t, float64(2), metrics[0].GetCounter().GetValue(), 0.01)
expectedLabels := map[string]string{
"tag1": "value1",
}
Expand Down Expand Up @@ -128,7 +128,7 @@ func TestGauge(t *testing.T) {
testGauge := findMetric(t, registry, "test_gauge")

metrics := testGauge.GetMetric()
assert.Equal(t, float64(2), metrics[0].GetGauge().GetValue())
assert.InDelta(t, float64(2), metrics[0].GetGauge().GetValue(), 0.01)
expectedLabels := map[string]string{
"tag1": "value1",
}
Expand All @@ -148,7 +148,7 @@ func TestHistogram(t *testing.T) {
testHistogram := findMetric(t, registry, "test_histogram")

metrics := testHistogram.GetMetric()
assert.Equal(t, float64(1), metrics[0].GetHistogram().GetSampleSum())
assert.InDelta(t, float64(1), metrics[0].GetHistogram().GetSampleSum(), 0.01)
expectedLabels := map[string]string{
"tag1": "value1",
}
Expand All @@ -168,7 +168,7 @@ func TestTimer(t *testing.T) {
testTimer := findMetric(t, registry, "test_timer_seconds")

metrics := testTimer.GetMetric()
assert.Equal(t, float64(0.1), metrics[0].GetHistogram().GetSampleSum())
assert.InDelta(t, float64(0.1), metrics[0].GetHistogram().GetSampleSum(), 0.01)
expectedLabels := map[string]string{
"tag1": "value1",
}
Expand Down Expand Up @@ -241,7 +241,7 @@ func TestNamespace(t *testing.T) {
testCounter := findMetric(t, registry, tc.expectedName)

metrics := testCounter.GetMetric()
assert.Equal(t, float64(1), metrics[0].GetCounter().GetValue())
assert.InDelta(t, float64(1), metrics[0].GetCounter().GetValue(), 0.01)
assert.Equal(t, tc.expectedLabels, promLabelsToMap(metrics[0].GetLabel()))
})
}
Expand All @@ -263,5 +263,5 @@ func TestNormalization(t *testing.T) {
testGauge := findMetric(t, registry, "My_Namespace_My_Gauge")

metrics := testGauge.GetMetric()
assert.Equal(t, float64(1), metrics[0].GetGauge().GetValue())
assert.InDelta(t, float64(1), metrics[0].GetGauge().GetValue(), 0.01)
}
26 changes: 13 additions & 13 deletions internal/metrics/prometheus/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestSeparator(t *testing.T) {
snapshot, err := registry.Gather()
require.NoError(t, err)
m1 := findMetric(t, snapshot, "bender:rodriguez_total", map[string]string{"a": "b"})
assert.EqualValues(t, 1, m1.GetCounter().GetValue(), "%+v", m1)
assert.InDelta(t, 1.0, m1.GetCounter().GetValue(), 0.01, "%+v", m1)
}

func TestCounter(t *testing.T) {
Expand Down Expand Up @@ -75,10 +75,10 @@ func TestCounter(t *testing.T) {
assert.EqualValues(t, "Help message", snapshot[0].GetHelp())

m1 := findMetric(t, snapshot, "bender_rodriguez_total", map[string]string{"a": "b", "x": "y"})
assert.EqualValues(t, 3, m1.GetCounter().GetValue(), "%+v", m1)
assert.InDelta(t, 3.0, m1.GetCounter().GetValue(), 0.01, "%+v", m1)

m2 := findMetric(t, snapshot, "bender_rodriguez_total", map[string]string{"a": "b", "x": "z"})
assert.EqualValues(t, 7, m2.GetCounter().GetValue(), "%+v", m2)
assert.InDelta(t, 7.0, m2.GetCounter().GetValue(), 0.01, "%+v", m2)
}

func TestCounterDefaultHelp(t *testing.T) {
Expand Down Expand Up @@ -132,10 +132,10 @@ func TestGauge(t *testing.T) {
assert.EqualValues(t, "Help message", snapshot[0].GetHelp())

m1 := findMetric(t, snapshot, "bender_rodriguez", map[string]string{"a": "b", "x": "y"})
assert.EqualValues(t, 2, m1.GetGauge().GetValue(), "%+v", m1)
assert.InDelta(t, 2.0, m1.GetGauge().GetValue(), 0.01, "%+v", m1)

m2 := findMetric(t, snapshot, "bender_rodriguez", map[string]string{"a": "b", "x": "z"})
assert.EqualValues(t, 4, m2.GetGauge().GetValue(), "%+v", m2)
assert.InDelta(t, 4.0, m2.GetGauge().GetValue(), 0.01, "%+v", m2)
}

func TestGaugeDefaultHelp(t *testing.T) {
Expand Down Expand Up @@ -190,7 +190,7 @@ func TestTimer(t *testing.T) {

m1 := findMetric(t, snapshot, "bender_rodriguez", map[string]string{"a": "b", "x": "y"})
assert.EqualValues(t, 2, m1.GetHistogram().GetSampleCount(), "%+v", m1)
assert.EqualValues(t, 3, m1.GetHistogram().GetSampleSum(), "%+v", m1)
assert.InDelta(t, 3.0, m1.GetHistogram().GetSampleSum(), 0.01, "%+v", m1)
for _, bucket := range m1.GetHistogram().GetBucket() {
switch {
case bucket.GetUpperBound() < 1:
Expand All @@ -204,7 +204,7 @@ func TestTimer(t *testing.T) {

m2 := findMetric(t, snapshot, "bender_rodriguez", map[string]string{"a": "b", "x": "z"})
assert.EqualValues(t, 2, m2.GetHistogram().GetSampleCount(), "%+v", m2)
assert.EqualValues(t, 7, m2.GetHistogram().GetSampleSum(), "%+v", m2)
assert.InDelta(t, 7.0, m2.GetHistogram().GetSampleSum(), 0.01, "%+v", m2)
for _, bucket := range m2.GetHistogram().GetBucket() {
switch {
case bucket.GetUpperBound() < 3:
Expand Down Expand Up @@ -249,7 +249,7 @@ func TestTimerCustomBuckets(t *testing.T) {

m1 := findMetric(t, snapshot, "bender_bending_rodriguez", map[string]string{"x": "y"})
assert.EqualValues(t, 2, m1.GetHistogram().GetSampleCount(), "%+v", m1)
assert.EqualValues(t, 3, m1.GetHistogram().GetSampleSum(), "%+v", m1)
assert.InDelta(t, 3.0, m1.GetHistogram().GetSampleSum(), 0.01, "%+v", m1)
assert.Len(t, m1.GetHistogram().GetBucket(), 2)
}

Expand All @@ -270,7 +270,7 @@ func TestTimerDefaultBuckets(t *testing.T) {

m1 := findMetric(t, snapshot, "bender_bending_rodriguez", map[string]string{"x": "y"})
assert.EqualValues(t, 2, m1.GetHistogram().GetSampleCount(), "%+v", m1)
assert.EqualValues(t, 3, m1.GetHistogram().GetSampleSum(), "%+v", m1)
assert.InDelta(t, 3.0, m1.GetHistogram().GetSampleSum(), 0.01, "%+v", m1)
assert.Len(t, m1.GetHistogram().GetBucket(), 2)
}

Expand Down Expand Up @@ -311,7 +311,7 @@ func TestHistogram(t *testing.T) {

m1 := findMetric(t, snapshot, "bender_rodriguez", map[string]string{"a": "b", "x": "y"})
assert.EqualValues(t, 2, m1.GetHistogram().GetSampleCount(), "%+v", m1)
assert.EqualValues(t, 3, m1.GetHistogram().GetSampleSum(), "%+v", m1)
assert.InDelta(t, 3.0, m1.GetHistogram().GetSampleSum(), 0.01, "%+v", m1)
for _, bucket := range m1.GetHistogram().GetBucket() {
switch {
case bucket.GetUpperBound() < 1:
Expand All @@ -325,7 +325,7 @@ func TestHistogram(t *testing.T) {

m2 := findMetric(t, snapshot, "bender_rodriguez", map[string]string{"a": "b", "x": "z"})
assert.EqualValues(t, 2, m2.GetHistogram().GetSampleCount(), "%+v", m2)
assert.EqualValues(t, 7, m2.GetHistogram().GetSampleSum(), "%+v", m2)
assert.InDelta(t, 7.0, m2.GetHistogram().GetSampleSum(), 0.01, "%+v", m2)
for _, bucket := range m2.GetHistogram().GetBucket() {
switch {
case bucket.GetUpperBound() < 3:
Expand Down Expand Up @@ -370,7 +370,7 @@ func TestHistogramCustomBuckets(t *testing.T) {

m1 := findMetric(t, snapshot, "bender_bending_rodriguez", map[string]string{"x": "y"})
assert.EqualValues(t, 2, m1.GetHistogram().GetSampleCount(), "%+v", m1)
assert.EqualValues(t, 3, m1.GetHistogram().GetSampleSum(), "%+v", m1)
assert.InDelta(t, 3.0, m1.GetHistogram().GetSampleSum(), 0.01, "%+v", m1)
assert.Len(t, m1.GetHistogram().GetBucket(), 1)
}

Expand All @@ -391,7 +391,7 @@ func TestHistogramDefaultBuckets(t *testing.T) {

m1 := findMetric(t, snapshot, "bender_bending_rodriguez", map[string]string{"x": "y"})
assert.EqualValues(t, 2, m1.GetHistogram().GetSampleCount(), "%+v", m1)
assert.EqualValues(t, 3, m1.GetHistogram().GetSampleSum(), "%+v", m1)
assert.InDelta(t, 3.0, m1.GetHistogram().GetSampleSum(), 0.01, "%+v", m1)
assert.Len(t, m1.GetHistogram().GetBucket(), 1)
}

Expand Down
4 changes: 2 additions & 2 deletions model/keyvalue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestKeyValueString(t *testing.T) {
assert.Equal(t, "y", kv.VStr)
assert.False(t, kv.Bool())
assert.Equal(t, int64(0), kv.Int64())
assert.Equal(t, float64(0), kv.Float64())
assert.InDelta(t, float64(0), kv.Float64(), 0.01)
assert.Nil(t, kv.Binary())
}

Expand All @@ -43,7 +43,7 @@ func TestKeyValueFloat64(t *testing.T) {
kv := model.Float64("x", 123.345)
assert.Equal(t, "x", kv.Key)
assert.Equal(t, model.Float64Type, kv.VType)
assert.Equal(t, 123.345, kv.Float64())
assert.InDelta(t, 123.345, kv.Float64(), 1e-4)
}

func TestKeyValueBinary(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion model/span_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ func TestGetSamplerParams(t *testing.T) {
span.Tags = tt.tags
actualType, actualParam := span.GetSamplerParams(logger)
assert.Equal(t, tt.expectedType, actualType)
assert.Equal(t, tt.expectedParam, actualParam)
assert.InDelta(t, tt.expectedParam, actualParam, 0.01)
})
}
}
2 changes: 1 addition & 1 deletion plugin/metrics/prometheus/metricsstore/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -961,7 +961,7 @@ func assertMetrics(t *testing.T, gotMetrics *metrics.MetricFamily, wantLabels ma
assert.Equal(t, int64(1620351786), mps[0].Timestamp.GetSeconds())

actualVal := mps[0].Value.(*metrics.MetricPoint_GaugeValue).GaugeValue.Value.(*metrics.GaugeValue_DoubleValue).DoubleValue
assert.Equal(t, float64(9223372036854), actualVal)
assert.InDelta(t, float64(9223372036854), actualVal, 0.01)
}

func TestMain(m *testing.M) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ func TestCalculateFunc(t *testing.T) {
return targetQPS
})
val := 1.0
assert.Equal(t, val, c.Calculate(val, 0, 0))
assert.InDelta(t, val, c.Calculate(val, 0, 0), 0.01)
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ func TestPercentageIncreaseCappedCalculator(t *testing.T) {
}
for _, tt := range tests {
probability := calculator.Calculate(tt.targetQPS, tt.curQPS, tt.oldProbability)
assert.InDelta(t, tt.expectedProbability, probability, 0.0001, tt.testName)
assert.InDelta(t, tt.expectedProbability, probability, 1e-4, tt.testName)
}
}
10 changes: 5 additions & 5 deletions plugin/sampling/strategyprovider/adaptive/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ func TestFactory(t *testing.T) {

f.InitFromViper(v, zap.NewNop())

assert.Equal(t, 5.0, f.options.TargetSamplesPerSecond)
assert.Equal(t, 0.25, f.options.DeltaTolerance)
assert.InDelta(t, 5.0, f.options.TargetSamplesPerSecond, 0.01)
assert.InDelta(t, 0.25, f.options.DeltaTolerance, 1e-3)
assert.Equal(t, int(2), f.options.BucketsForCalculation)
assert.Equal(t, time.Minute*15, f.options.CalculationInterval)
assert.Equal(t, int(3), f.options.AggregationBuckets)
assert.Equal(t, time.Minute*3, f.options.Delay)
assert.Equal(t, 0.02, f.options.InitialSamplingProbability)
assert.Equal(t, 0.01, f.options.MinSamplingProbability)
assert.Equal(t, 1.0, f.options.MinSamplesPerSecond)
assert.InDelta(t, 0.02, f.options.InitialSamplingProbability, 1e-3)
assert.InDelta(t, 0.01, f.options.MinSamplingProbability, 1e-3)
assert.InDelta(t, 1.0, f.options.MinSamplesPerSecond, 0.01)
assert.Equal(t, time.Second, f.options.LeaderLeaseRefreshInterval)
assert.Equal(t, time.Second*2, f.options.FollowerLeaseRefreshInterval)

Expand Down
20 changes: 10 additions & 10 deletions plugin/sampling/strategyprovider/adaptive/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,30 @@ func TestOptionsWithFlags(t *testing.T) {

opts.InitFromViper(v)

assert.Equal(t, 2.0, opts.TargetSamplesPerSecond)
assert.Equal(t, 0.6, opts.DeltaTolerance)
assert.InDelta(t, 2.0, opts.TargetSamplesPerSecond, 0.01)
assert.InDelta(t, 0.6, opts.DeltaTolerance, 0.01)
assert.Equal(t, 2, opts.BucketsForCalculation)
assert.Equal(t, time.Duration(120000000000), opts.CalculationInterval)
assert.Equal(t, 20, opts.AggregationBuckets)
assert.Equal(t, time.Duration(360000000000), opts.Delay)
assert.Equal(t, 0.002, opts.InitialSamplingProbability)
assert.Equal(t, 1e-4, opts.MinSamplingProbability)
assert.Equal(t, 0.016666666666666666, opts.MinSamplesPerSecond)
assert.InDelta(t, 0.002, opts.InitialSamplingProbability, 1e-4)
assert.InDelta(t, 1e-4, opts.MinSamplingProbability, 1e-5)
assert.InDelta(t, 0.016666666666666666, opts.MinSamplesPerSecond, 1e-3)
assert.Equal(t, time.Duration(5000000000), opts.LeaderLeaseRefreshInterval)
assert.Equal(t, time.Duration(60000000000), opts.FollowerLeaseRefreshInterval)
}

func TestDefaultOptions(t *testing.T) {
options := DefaultOptions()
assert.Equal(t, float64(defaultTargetSamplesPerSecond), options.TargetSamplesPerSecond)
assert.Equal(t, defaultDeltaTolerance, options.DeltaTolerance)
assert.InDelta(t, float64(defaultTargetSamplesPerSecond), options.TargetSamplesPerSecond, 1e-4)
assert.InDelta(t, defaultDeltaTolerance, options.DeltaTolerance, 1e-3)
assert.Equal(t, defaultBucketsForCalculation, options.BucketsForCalculation)
assert.Equal(t, defaultCalculationInterval, options.CalculationInterval)
assert.Equal(t, defaultAggregationBuckets, options.AggregationBuckets)
assert.Equal(t, defaultDelay, options.Delay)
assert.Equal(t, defaultInitialSamplingProbability, options.InitialSamplingProbability)
assert.Equal(t, defaultMinSamplingProbability, options.MinSamplingProbability)
assert.Equal(t, defaultMinSamplesPerSecond, options.MinSamplesPerSecond)
assert.InDelta(t, defaultInitialSamplingProbability, options.InitialSamplingProbability, 1e-4)
assert.InDelta(t, defaultMinSamplingProbability, options.MinSamplingProbability, 1e-4)
assert.InDelta(t, defaultMinSamplesPerSecond, options.MinSamplesPerSecond, 1e-4)
assert.Equal(t, defaultLeaderLeaseRefreshInterval, options.LeaderLeaseRefreshInterval)
assert.Equal(t, defaultFollowerLeaseRefreshInterval, options.FollowerLeaseRefreshInterval)
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,10 @@ func TestInitializeThroughputFailure(t *testing.T) {

func TestCalculateQPS(t *testing.T) {
qps := calculateQPS(int64(90), 60*time.Second)
assert.Equal(t, 1.5, qps)
assert.InDelta(t, 1.5, qps, 0.01)

qps = calculateQPS(int64(45), 60*time.Second)
assert.Equal(t, 0.75, qps)
assert.InDelta(t, 0.75, qps, 0.01)
}

func TestGenerateOperationQPS(t *testing.T) {
Expand Down Expand Up @@ -231,7 +231,7 @@ func TestCalculateWeightedQPS(t *testing.T) {
p := PostAggregator{weightVectorCache: NewWeightVectorCache()}
assert.InDelta(t, 0.86735, p.calculateWeightedQPS([]float64{0.8, 1.2, 1.0}), 0.001)
assert.InDelta(t, 0.95197, p.calculateWeightedQPS([]float64{1.0, 1.0, 0.0, 0.0}), 0.001)
assert.Equal(t, 0.0, p.calculateWeightedQPS([]float64{}))
assert.InDelta(t, 0.0, p.calculateWeightedQPS([]float64{}), 0.01)
}

func TestCalculateProbability(t *testing.T) {
Expand Down Expand Up @@ -278,7 +278,7 @@ func TestCalculateProbability(t *testing.T) {
}
for _, test := range tests {
probability := p.calculateProbability(test.service, test.operation, test.qps)
assert.Equal(t, test.expectedProbability, probability, test.errMsg)
assert.InDelta(t, test.expectedProbability, probability, 1e-6, test.errMsg)
}
}

Expand Down
4 changes: 2 additions & 2 deletions plugin/sampling/strategyprovider/adaptive/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ func TestProviderRealisticRunCalculationLoop(t *testing.T) {
for _, s := range strategies {
switch s.Operation {
case "GET":
assert.Equal(t, 0.001, s.ProbabilisticSampling.SamplingRate,
assert.InDelta(t, 0.001, s.ProbabilisticSampling.SamplingRate, 1e-4,
"Already at 1QPS, no probability change")
case "POST":
assert.Equal(t, 0.001, s.ProbabilisticSampling.SamplingRate,
assert.InDelta(t, 0.001, s.ProbabilisticSampling.SamplingRate, 1e-4,
"Within epsilon of 1QPS, no probability change")
case "PUT":
assert.InEpsilon(t, 0.002, s.ProbabilisticSampling.SamplingRate, 0.025,
Expand Down
Loading

0 comments on commit da039e8

Please sign in to comment.