Matt Rosencrantz | 3e76f28 | 2014-11-10 09:38:57 -0800 | [diff] [blame] | 1 | package vtrace |
| 2 | |
| 3 | import ( |
| 4 | "encoding/binary" |
| 5 | "reflect" |
| 6 | "testing" |
| 7 | |
| 8 | "veyron.io/veyron/veyron2/uniqueid" |
| 9 | "veyron.io/veyron/veyron2/vtrace" |
| 10 | ) |
| 11 | |
| 12 | var nextid = uint64(1) |
| 13 | |
| 14 | func id() uniqueid.ID { |
| 15 | var out uniqueid.ID |
| 16 | binary.BigEndian.PutUint64(out[8:], nextid) |
| 17 | nextid++ |
| 18 | return out |
| 19 | } |
| 20 | |
| 21 | func makeTraces(n int, st *Store) []vtrace.Trace { |
| 22 | traces := make([]vtrace.Trace, n) |
| 23 | for i := range traces { |
| 24 | traces[i] = newCollector(id(), st) |
| 25 | traces[i].ForceCollect() |
| 26 | } |
| 27 | return traces |
| 28 | } |
| 29 | |
Matt Rosencrantz | b30286b | 2014-11-10 14:52:17 -0800 | [diff] [blame] | 30 | func recordids(records ...vtrace.TraceRecord) map[uniqueid.ID]bool { |
Matt Rosencrantz | 3e76f28 | 2014-11-10 09:38:57 -0800 | [diff] [blame] | 31 | out := make(map[uniqueid.ID]bool) |
| 32 | for _, trace := range records { |
| 33 | out[trace.ID] = true |
| 34 | } |
| 35 | return out |
| 36 | } |
| 37 | |
| 38 | func traceids(traces ...vtrace.Trace) map[uniqueid.ID]bool { |
| 39 | out := make(map[uniqueid.ID]bool) |
| 40 | for _, trace := range traces { |
| 41 | out[trace.ID()] = true |
| 42 | } |
| 43 | return out |
| 44 | } |
| 45 | |
| 46 | func TestConsiderAndTrim(t *testing.T) { |
| 47 | st := NewStore(5) |
| 48 | traces := makeTraces(10, st) |
| 49 | records := st.TraceRecords() |
| 50 | |
| 51 | if want, got := traceids(traces[5:]...), recordids(records...); !reflect.DeepEqual(want, got) { |
| 52 | t.Errorf("Got wrong traces. Want %#v, got %#v", want, got) |
| 53 | } |
| 54 | |
| 55 | // Starting a new span on one of the traces should bring it back into the stored set. |
| 56 | traces[2].(*collector).start(&span{id: id()}) |
| 57 | records = st.TraceRecords() |
| 58 | if want, got := traceids(traces[2], traces[6], traces[7], traces[8], traces[9]), recordids(records...); !reflect.DeepEqual(want, got) { |
| 59 | t.Errorf("Got wrong traces. Want %#v, got %#v", want, got) |
| 60 | } |
| 61 | |
| 62 | // Starting a new span on one of the traces should bring it back into the stored set. |
| 63 | traces[2].(*collector).start(&span{id: id()}) |
| 64 | records = st.TraceRecords() |
| 65 | if want, got := traceids(traces[2], traces[6], traces[7], traces[8], traces[9]), recordids(records...); !reflect.DeepEqual(want, got) { |
| 66 | t.Errorf("Got wrong traces. Want %#v, got %#v", want, got) |
| 67 | } |
| 68 | |
| 69 | // Finishing a span on one of the traces should bring it back into the stored set. |
| 70 | traces[3].(*collector).finish(&span{id: id()}) |
| 71 | records = st.TraceRecords() |
| 72 | if want, got := traceids(traces[3], traces[2], traces[7], traces[8], traces[9]), recordids(records...); !reflect.DeepEqual(want, got) { |
| 73 | t.Errorf("Got wrong traces. Want %#v, got %#v", want, got) |
| 74 | } |
| 75 | |
| 76 | // Annotating a span on one of the traces should bring it back into the stored set. |
| 77 | traces[4].(*collector).annotate(&span{id: id()}, "hello") |
| 78 | records = st.TraceRecords() |
| 79 | if want, got := traceids(traces[4], traces[3], traces[2], traces[8], traces[9]), recordids(records...); !reflect.DeepEqual(want, got) { |
| 80 | t.Errorf("Got wrong traces. Want %#v, got %#v", want, got) |
| 81 | } |
| 82 | } |