Пример #1
0
func TestEpochBalance(t *testing.T) {
	trace := (*Tracer)(nil).NewTrace(traceID(9))
	trace.Epoch("never finalized")
	if !util.Panics(func() { trace.Finalize() }) {
		t.Fatalf("should panic when Finalize is called too early")
	}
}
Пример #2
0
func TestTracer(t *testing.T) {
	stopper := stop.NewStopper()
	defer stopper.Stop()

	feed := util.NewFeed(stopper)
	const origin = ":8081"

	expTraces := []Trace{
		{ID: "10", Content: []TraceItem{
			{
				depth:    1,
				Name:     "A1",
				Duration: 30 * time.Millisecond,
			},
			{
				depth:     2,
				Timestamp: time.Time{}.Add(10 * time.Millisecond),
				Name:      "A2",
				Duration:  10 * time.Millisecond,
			},
			{
				depth:     3,
				Timestamp: time.Time{}.Add(15 * time.Millisecond),
				Name:      "E3",
			},
			{
				depth:     3,
				Timestamp: time.Time{}.Add(17 * time.Millisecond),
				Name:      "E4",
			},
		}},
	}

	feed.Subscribe(func(event interface{}) {
		trace := event.(*Trace)

		if len(expTraces) == 0 {
			t.Fatalf("unexpected extra trace: %s", trace)
		}
		expTrace := expTraces[0]
		expTraces = expTraces[1:]
		if !reflect.DeepEqual(expTrace.ID, trace.ID) {
			t.Errorf("expected ID %s, got %s", expTrace.ID, trace.ID)
		}
		tc := trace.Content
		for i, v := range tc {
			if !strings.Contains(v.Func, "TestTracer") || !strings.Contains(v.File, "tracer_test.go") {
				t.Errorf("invalid callsite in trace: %s %s", v.Func, v.File)
			}
			if v.Origin != origin {
				t.Fatalf("unexpected origin %s", v.Origin)
			}
			tc[i].Func, tc[i].File, tc[i].Line, tc[i].Origin = "", "", 0, ""
		}
		if !reflect.DeepEqual(expTrace.Content, trace.Content) {
			t.Fatalf("unexpected content:\n%+v\nwanted:\n%+v", trace, expTrace)
		}
	})

	tracer := NewTracer(feed, origin)

	now := &time.Time{}

	add := func(t time.Duration) {
		then := now.Add(t)
		now = &then
	}

	tracer.now = func() time.Time {
		return *now
	}

	// TODO(tschottdorf): Test some more.
	req1 := traceID(10)
	t1 := tracer.NewTrace(req1)

	e1a := t1.Epoch("A1")
	add(10 * time.Millisecond)
	e1b := t1.Epoch("A2")
	add(5 * time.Millisecond)
	t1.Event("E3")
	add(2 * time.Millisecond)
	t1.Event("E4")
	add(3 * time.Millisecond)
	e1b() // 10ms elapsed
	add(10 * time.Millisecond)
	e1a() // 30ms elapsed
	t1.Finalize()

	if !util.Panics(e1a) || !util.Panics(e1b) {
		t.Fatalf("expected a panic when ending an epoch twice")
	}

	feed.Flush()

	if len(expTraces) > 0 {
		t.Fatalf("missing traces:\n%+v", expTraces)
	}
}