Stop exporting Metrics.First() and Last()

They were exported for testing only, and the test isn't very useful.
This commit is contained in:
Bryan Boreham
2018-07-12 19:34:05 +00:00
parent 1cfc8f4581
commit fdfbedeea9
2 changed files with 10 additions and 28 deletions

View File

@@ -52,12 +52,8 @@ type Metric struct {
Min, Max float64
}
// Following two functions are exported for testing only: make sure there are samples before calling.
// First gives the timestamp of the first sample
func (m Metric) First() time.Time { return m.Samples[0].Timestamp }
// Last gives the timestamp of the last sample
func (m Metric) Last() time.Time { return m.Samples[len(m.Samples)-1].Timestamp }
func (m Metric) first() time.Time { return m.Samples[0].Timestamp }
func (m Metric) last() time.Time { return m.Samples[len(m.Samples)-1].Timestamp }
// Sample is a single datapoint of a metric.
type Sample struct {
@@ -127,7 +123,7 @@ func (m Metric) Merge(other Metric) Metric {
return other
case len(other.Samples) == 0:
return m
case other.First().After(m.Last()):
case other.first().After(m.last()):
samplesOut := make([]Sample, len(m.Samples)+len(other.Samples))
copy(samplesOut, m.Samples)
copy(samplesOut[len(m.Samples):], other.Samples)
@@ -136,7 +132,7 @@ func (m Metric) Merge(other Metric) Metric {
Max: math.Max(m.Max, other.Max),
Min: math.Min(m.Min, other.Min),
}
case m.First().After(other.Last()):
case m.first().After(other.last()):
samplesOut := make([]Sample, len(m.Samples)+len(other.Samples))
copy(samplesOut, other.Samples)
copy(samplesOut[len(other.Samples):], m.Samples)

View File

@@ -49,13 +49,7 @@ func TestMetricsCopy(t *testing.T) {
}
}
func checkMetric(t *testing.T, metric report.Metric, first, last time.Time, min, max float64) {
if !metric.First().Equal(first) {
t.Errorf("Expected metric.First == %q, but was: %q", first, metric.First())
}
if !metric.Last().Equal(last) {
t.Errorf("Expected metric.Last == %q, but was: %q", last, metric.Last())
}
func checkMetric(t *testing.T, metric report.Metric, min, max float64) {
if metric.Min != min {
t.Errorf("Expected metric.Min == %f, but was: %f", min, metric.Min)
}
@@ -70,16 +64,16 @@ func TestMetricFirstLastMinMax(t *testing.T) {
metric1 := report.MakeMetric([]report.Sample{{Timestamp: t1, Value: -0.1}, {Timestamp: t2, Value: 0.2}})
checkMetric(t, metric1, t1, t2, -0.1, 0.2)
checkMetric(t, metric1.Merge(metric1), t1, t2, -0.1, 0.2)
checkMetric(t, metric1, -0.1, 0.2)
checkMetric(t, metric1.Merge(metric1), -0.1, 0.2)
t3 := time.Now().Add(2 * time.Minute)
t4 := time.Now().Add(3 * time.Minute)
metric2 := report.MakeMetric([]report.Sample{{Timestamp: t3, Value: 0.31}, {Timestamp: t4, Value: 0.4}})
checkMetric(t, metric2, t3, t4, 0.31, 0.4)
checkMetric(t, metric1.Merge(metric2), t1, t4, -0.1, 0.4)
checkMetric(t, metric2.Merge(metric1), t1, t4, -0.1, 0.4)
checkMetric(t, metric2, 0.31, 0.4)
checkMetric(t, metric1.Merge(metric2), -0.1, 0.4)
checkMetric(t, metric2.Merge(metric1), -0.1, 0.4)
}
func TestMetricMerge(t *testing.T) {
@@ -98,14 +92,6 @@ func TestMetricMerge(t *testing.T) {
if !reflect.DeepEqual(want, have) {
t.Errorf("diff: %s", test.Diff(want, have))
}
// Check it didn't modify metric1
checkMetric(t, metric1, t2, t3, 0.2, 0.31)
// Check the result is not the same instance as metric1
if &metric1 == &have {
t.Errorf("Expected different pointers for metric1 and have, but both were: %p", &have)
}
}
func TestMetricMarshalling(t *testing.T) {