Skip to content
This repository has been archived by the owner on Oct 2, 2020. It is now read-only.

Commit

Permalink
Improve fib tracer example (#17)
Browse files Browse the repository at this point in the history
* Fix incorrectly hardcoded number in tracer fib example

* Change ordering in tracer example to provoke activation problem

* Care about results in the tracer example test
  • Loading branch information
jcorbin authored and Joshua T Corbin committed Jun 29, 2016
1 parent eeaa00b commit 4e187cb
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions source/tap/tracer_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,20 @@ func tracedFib(n int, scope *tap.TraceScope) (r int) {
func fib(n int) int {
if scope := fibTracer.MaybeScope("wrapper"); scope != nil {
scope.Open(n)
r := tracedFib(5, scope)
r := tracedFib(n, scope)
scope.CloseCall(r)
return r
}
return untracedFib(n)
}

func ExampleTracer() {
// this one won't be traced since there is no watcher yet
fib(4)

// this just makes trace ids stable for the test
tap.ResetTraceID()

// this one won't be traced since there is no watcher yet
fmt.Printf("the 4th fib is %v\n", fib(4))

// this causes fibTracer's output to get printed to stdout; the use here is
// more complicated than you'd have in a real program to get stable test
// output.
Expand All @@ -101,17 +101,19 @@ func ExampleTracer() {
defer rep.Stop()

// this one will be traced since there's now a watcher
fib(5)
fmt.Printf("the 5th fib is %v\n", fib(5))

// this flushes and stops all watchers on the reported source; otherwise
// this function returns too quickly for even one of the emitted trace
// items to have been printed.
rep.Source().(source.DrainableSource).Drain()

// this one won't be traced since Drain deactivated the tracer
fib(6)
fmt.Printf("the 6th fib is %v\n", fib(6))

// Output:
// the 4th fib is 5
// the 5th fib is 8
// /tap/trace/fib: --> DATE TIME_0 [1::1] wrapper: 5
// /tap/trace/fib: --> DATE TIME_1 [1:1:2] fib(5)
// /tap/trace/fib: --> DATE TIME_2 [1:2:3] fib(4)
Expand Down Expand Up @@ -144,6 +146,7 @@ func ExampleTracer() {
// /tap/trace/fib: <-- DATE TIME_29 [1:2:12] return 3
// /tap/trace/fib: <-- DATE TIME_30 [1:1:2] return 8
// /tap/trace/fib: <-- DATE TIME_31 [1::1] return 8
// the 6th fib is 13
}

// timeElider is here just to normalize output for the test
Expand Down

0 comments on commit 4e187cb

Please sign in to comment.