1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
package nettrace
import (
"context"
"testing"
)
func TestContext(t *testing.T) {
tr := New("TestContext", "trace")
defer tr.Finish()
// Attach the trace to a new context.
ctx := NewContext(context.Background(), tr)
// Get the trace back from the context.
{
tr2, ok := FromContext(ctx)
if !ok {
t.Errorf("Context with trace returned not found")
}
if tr != tr2 {
t.Errorf("Trace from context is different: %v != %v", tr, tr2)
}
}
// Create a child trace from the context.
{
tr3 := ChildFromContext(ctx, "TestContext", "child")
if p := tr3.(*trace).Parent; p != tr {
t.Errorf("Child doesn't have the right parent: %v != %v", p, tr)
}
tr3.Finish()
}
// FromContextOrNew returns the one from the context.
{
tr4, ctx4 := FromContextOrNew(ctx, "TestContext", "from-ctx")
if ctx4 != ctx {
t.Errorf("Got new context: %v != %v", ctx4, ctx)
}
if tr4 != tr {
t.Errorf("Context with trace returned new trace: %v != %v", tr4, tr)
}
}
// FromContextOrNew needs to create a new one.
{
tr5, ctx5 := FromContextOrNew(
context.Background(), "TestContext", "tr5")
if tr, _ := FromContext(ctx5); tr != tr5 {
t.Errorf("Context with trace returned the wrong trace: %v != %v",
tr, tr5)
}
tr5.Finish()
}
// Child from a context that has no trace attached.
{
tr6 := ChildFromContext(
context.Background(), "TestContext", "child")
tr6.Finish()
if p := tr6.(*trace).Parent; p != nil {
t.Errorf("Expected orphan trace, it has a parent: %v", p)
}
}
}
|