-
Notifications
You must be signed in to change notification settings - Fork 3
/
bytes_test.go
98 lines (79 loc) · 2.15 KB
/
bytes_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package dmetering
import (
"context"
"testing"
)
func TestMeter_BytesWrittenDelta(t *testing.T) {
meter := NewBytesMeter()
meter.AddBytesWritten(10)
meter.AddBytesWritten(10)
if meter.BytesWrittenDelta() != 20 {
t.Errorf("expected 20, got %d", meter.BytesWrittenDelta())
}
meter.AddBytesWritten(10)
if meter.BytesWrittenDelta() != 10 {
t.Errorf("expected 10, got %d", meter.BytesWrittenDelta())
}
}
func TestMeter_BytesReadDelta(t *testing.T) {
meter := NewBytesMeter()
meter.AddBytesRead(10)
meter.AddBytesRead(10)
if meter.BytesReadDelta() != 20 {
t.Errorf("expected 20, got %d", meter.BytesReadDelta())
}
meter.AddBytesRead(10)
if meter.BytesReadDelta() != 10 {
t.Errorf("expected 10, got %d", meter.BytesReadDelta())
}
}
func TestWithCounter(t *testing.T) {
ctx := WithBytesMeter(context.Background())
ctx = WithCounter(ctx, "test")
GetBytesMeter(ctx).CountInc("test", 10)
c := GetBytesMeter(ctx).GetCount("test")
if c != 10 {
t.Errorf("expected 10, got %d", c)
}
}
func TestWithBytesMeter(t *testing.T) {
ctx := WithBytesMeter(context.Background())
if GetBytesMeter(ctx) == nil {
t.Error("expected a meter")
}
}
func TestWithExistingBytesMeter(t *testing.T) {
meter := NewBytesMeter()
ctx := WithExistingBytesMeter(context.Background(), meter)
if GetBytesMeter(ctx) != meter {
t.Error("expected a meter")
}
}
func TestWithBytesMeter_existing_noop(t *testing.T) {
ctx := WithExistingBytesMeter(context.Background(), NoopBytesMeter)
ctx = WithBytesMeter(ctx)
bm := GetBytesMeter(ctx)
if bm == NoopBytesMeter {
t.Error("expected a meter")
}
}
func TestWithBytesMeter_exists(t *testing.T) {
meter := NewBytesMeter()
ctx := WithExistingBytesMeter(context.Background(), meter)
meter.AddBytesRead(10)
ctx = WithBytesMeter(ctx)
bm := GetBytesMeter(ctx)
if bm == NoopBytesMeter {
t.Error("expected a meter")
}
if bm.BytesRead() != 10 {
t.Errorf("expected 10, got %d. was a different meter added?", bm.BytesRead())
}
}
func TestWithExistingBytesMeter_nil(t *testing.T) {
ctx := WithExistingBytesMeter(context.Background(), nil)
bm := GetBytesMeter(ctx)
if bm != NoopBytesMeter {
t.Error("expected a noop meter")
}
}