Skip to content

Commit 3d451a4

Browse files
committed
telemetry/internal/counter: fix debugging Read
Read needs to expand the names of stack pointers, and return an error when a counter is not found. Test writers now need to be prepared for errors if they ask to Read counters that haven't been used. Change-Id: I8f3e10935289bff81cbb2751f91d097a82c03e73 Reviewed-on: https://go-review.googlesource.com/c/telemetry/+/564617 TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Peter Weinberger <[email protected]> Reviewed-by: Robert Findley <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 5d9906a commit 3d451a4

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

counter/countertest/countertest_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,12 @@ func TestReadCounter(t *testing.T) {
3232
testenv.SkipIfUnsupportedPlatform(t)
3333
c := counter.New("foobar")
3434

35-
if got, err := ReadCounter(c); err != nil || got != 0 {
36-
t.Errorf("ReadCounter = (%v, %v), want (%v, nil)", got, err, 0)
35+
got, err := ReadCounter(c)
36+
if got != 0 {
37+
t.Errorf("ReadCounter = %d, want 0", got)
38+
}
39+
if err == nil {
40+
t.Errorf("ReadCounter = (%v, nil), want (%v, error)", got, 0)
3741
}
3842

3943
var wg sync.WaitGroup

internal/counter/counter.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,8 +300,11 @@ func Read(c *Counter) (uint64, error) {
300300
if err != nil {
301301
return 0, err
302302
}
303-
// counter doesn't write the entry to file until the value becomes non-zero.
304-
return pf.Count[c.name], nil
303+
v, ok := pf.Count[DecodeStack(c.Name())]
304+
if !ok {
305+
return v, fmt.Errorf("not found:%q", DecodeStack(c.Name()))
306+
}
307+
return v, nil
305308
}
306309

307310
func readFile(f *file) (*File, error) {

internal/counter/counter_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,10 @@ func TestStack(t *testing.T) {
443443
if ct == nil {
444444
t.Fatal("nil counter")
445445
}
446+
_, err := Read(ct)
447+
if err != nil {
448+
t.Errorf("failed to read known counter %v", err)
449+
}
446450
if ct.ptr.count == nil {
447451
t.Errorf("%q has nil ptr.count", ct.Name())
448452
continue
@@ -491,7 +495,7 @@ func TestStack(t *testing.T) {
491495
}
492496
}
493497

494-
// fn calls itself n times recursively as incrementing the stack counter.
498+
// fn calls itself n times recursively while incrementing the stack counter.
495499
func fn(t *testing.T, n int, c *StackCounter) {
496500
c.Inc()
497501
if n > 0 {

0 commit comments

Comments
 (0)