blob: 3600fd9fb3cdb47105485e29fb9ce77875dc218f [file] [log] [blame]
Matt Rosencrantzb30286b2014-11-10 14:52:17 -08001package impl_test
2
3import (
4 "io"
5 "testing"
6
7 "veyron.io/veyron/veyron2"
8 "veyron.io/veyron/veyron2/ipc"
9 "veyron.io/veyron/veyron2/naming"
10 "veyron.io/veyron/veyron2/rt"
11 service "veyron.io/veyron/veyron2/services/mgmt/vtrace"
12 "veyron.io/veyron/veyron2/vtrace"
13
14 "veyron.io/veyron/veyron/profiles"
15 "veyron.io/veyron/veyron/services/mgmt/vtrace/impl"
16)
17
18func setup(t *testing.T) (string, ipc.Server, veyron2.Runtime) {
19 runtime, err := rt.New()
20 if err != nil {
21 t.Fatalf("Could not create runtime: %s", err)
22 }
23
24 server, err := runtime.NewServer()
25 if err != nil {
26 t.Fatalf("Could not create server: %s", err)
27 }
28 endpoint, err := server.Listen(profiles.LocalListenSpec)
29 if err != nil {
30 t.Fatalf("Listen failed: %s", err)
31 }
32 if err := server.Serve("", impl.NewVtraceService(runtime.VtraceStore()), nil); err != nil {
33 t.Fatalf("Serve failed: %s", err)
34 }
35 return endpoint.String(), server, runtime
36}
37
Robin Thellende34761d2014-11-13 13:11:25 -080038func TestVtraceServer(t *testing.T) {
Matt Rosencrantzb30286b2014-11-10 14:52:17 -080039 endpoint, server, runtime := setup(t)
40 defer server.Stop()
41
42 sctx := runtime.NewContext()
43 sctx, span := runtime.WithNewSpan(sctx, "The Span")
44 span.Trace().ForceCollect()
45 span.Finish()
46 id := span.Trace().ID()
47
48 client := service.StoreClient(naming.JoinAddressName(endpoint, ""))
49
50 trace, err := client.Trace(runtime.NewContext(), id)
51 if err != nil {
52 t.Fatalf("Unexpected error getting trace: %s", err)
53 }
54 if len(trace.Spans) != 1 {
55 t.Errorf("Returned trace should have 1 span, found %#v", trace)
56 }
57 if trace.Spans[0].Name != "The Span" {
58 t.Errorf("Returned span has wrong name: %#v", trace)
59 }
60
61 call, err := client.AllTraces(runtime.NewContext())
62 if err != nil {
63 t.Fatalf("Unexpected error getting traces: %s", err)
64 }
65 ntraces := 0
66 stream := call.RecvStream()
67 var tr *vtrace.TraceRecord
68 for stream.Advance() {
69 trace := stream.Value()
70 if trace.ID == id {
71 tr = &trace
72 }
73 ntraces++
74 }
75 if err = stream.Err(); err != nil && err != io.EOF {
76 t.Fatalf("Unexpected error reading trace stream: %s", err)
77 }
Matt Rosencrantzbe1a8b52014-11-21 15:14:06 -080078 if ntraces != 1 {
79 t.Fatalf("Expected 1 trace, got %#v", ntraces)
Matt Rosencrantzb30286b2014-11-10 14:52:17 -080080 }
81 if tr == nil {
82 t.Fatalf("Desired trace %x not found.", id)
83 }
84 if len(tr.Spans) != 1 {
85 t.Errorf("Returned trace should have 1 span, found %#v", tr)
86 }
87 if tr.Spans[0].Name != "The Span" {
88 t.Fatalf("Returned span has wrong name: %#v", tr)
89 }
90}