|
|
|
@ -70,3 +70,52 @@ func BenchmarkMetric(b *testing.B) {
|
|
|
|
|
testMetric(b) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestCOWMetric(t *testing.T) { |
|
|
|
|
testMetric := Metric{ |
|
|
|
|
"to_delete": "test1", |
|
|
|
|
"to_change": "test2", |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
scenarios := []struct { |
|
|
|
|
fn func(*COWMetric) |
|
|
|
|
out Metric |
|
|
|
|
}{ |
|
|
|
|
{ |
|
|
|
|
fn: func(cm *COWMetric) { |
|
|
|
|
cm.Delete("to_delete") |
|
|
|
|
}, |
|
|
|
|
out: Metric{ |
|
|
|
|
"to_change": "test2", |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
fn: func(cm *COWMetric) { |
|
|
|
|
cm.Set("to_change", "changed") |
|
|
|
|
}, |
|
|
|
|
out: Metric{ |
|
|
|
|
"to_delete": "test1", |
|
|
|
|
"to_change": "changed", |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for i, s := range scenarios { |
|
|
|
|
orig := testMetric.Clone() |
|
|
|
|
cm := &COWMetric{ |
|
|
|
|
Metric: orig, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
s.fn(cm) |
|
|
|
|
|
|
|
|
|
// Test that the original metric was not modified.
|
|
|
|
|
if !orig.Equal(testMetric) { |
|
|
|
|
t.Fatalf("%d. original metric changed; expected %v, got %v", i, testMetric, orig) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Test that the new metric has the right changes.
|
|
|
|
|
if !cm.Metric.Equal(s.out) { |
|
|
|
|
t.Fatalf("%d. copied metric doesn't contain expected changes; expected %v, got %v", i, s.out, cm.Metric) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|