Skip to content

Commit

Permalink
Move metric.equal to test file
Browse files Browse the repository at this point in the history
  • Loading branch information
danielnelson committed Mar 27, 2018
1 parent ed5b6b0 commit f046a29
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 29 deletions.
26 changes: 0 additions & 26 deletions metric/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,32 +229,6 @@ func (m *metric) IsAggregate() bool {
return m.aggregate
}

func (m *metric) equal(o *metric) bool {
if m.name != o.name || m.tm != o.tm {
return false
}

if len(m.tags) != len(o.tags) || len(m.fields) != len(o.fields) {
return false
}

for i, tag := range m.tags {
if tag.Key != o.tags[i].Key || tag.Value != o.tags[i].Value {
return false
}
}

sort.Slice(m.fields, func(i, j int) bool { return m.fields[i].Key < m.fields[j].Key })
sort.Slice(o.fields, func(i, j int) bool { return o.fields[i].Key < o.fields[j].Key })

for i, field := range m.fields {
if field.Key != o.fields[i].Key || field.Value != o.fields[i].Value {
return false
}
}
return true
}

func (m *metric) HashID() uint64 {
h := fnv.New64a()
h.Write([]byte(m.name))
Expand Down
33 changes: 30 additions & 3 deletions metric/metric_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package metric

import (
"sort"
"testing"
"time"

Expand All @@ -9,6 +10,32 @@ import (
"github.com/stretchr/testify/require"
)

func MetricEqual(lhs *metric, rhs *metric) bool {
if lhs.name != rhs.name || lhs.tm != rhs.tm {
return false
}

if len(lhs.tags) != len(rhs.tags) || len(lhs.fields) != len(rhs.fields) {
return false
}

for i, tag := range lhs.tags {
if tag.Key != rhs.tags[i].Key || tag.Value != rhs.tags[i].Value {
return false
}
}

sort.Slice(lhs.fields, func(i, j int) bool { return lhs.fields[i].Key < lhs.fields[j].Key })
sort.Slice(rhs.fields, func(i, j int) bool { return rhs.fields[i].Key < rhs.fields[j].Key })

for i, field := range lhs.fields {
if field.Key != rhs.fields[i].Key || field.Value != rhs.fields[i].Value {
return false
}
}
return true
}

func TestNewMetric(t *testing.T) {
now := time.Now()

Expand Down Expand Up @@ -193,12 +220,12 @@ func TestEquals(t *testing.T) {
require.NoError(t, err)

lhs := m1.(*metric)
require.True(t, lhs.equal(m2.(*metric)))
require.True(t, MetricEqual(lhs, m2.(*metric)))

m3 := m2.Copy()
require.True(t, lhs.equal(m3.(*metric)))
require.True(t, MetricEqual(lhs, m3.(*metric)))
m3.AddTag("a", "x")
require.False(t, lhs.equal(m3.(*metric)))
require.False(t, MetricEqual(lhs, m3.(*metric)))
}

func TestHashID(t *testing.T) {
Expand Down

0 comments on commit f046a29

Please sign in to comment.