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 67
|
package model
import (
"encoding/json"
"testing"
)
func TestTraceID(t *testing.T) {
traceID := TraceID{High: 1, Low: 2}
if len(traceID.String()) != 32 {
t.Errorf("Expected zero-padded TraceID to have 32 characters")
}
b, err := json.Marshal(traceID)
if err != nil {
t.Fatalf("Expected successful json serialization, got error: %+v", err)
}
if want, have := string(b), `"00000000000000010000000000000002"`; want != have {
t.Fatalf("Expected json serialization, want %q, have %q", want, have)
}
var traceID2 TraceID
if err = json.Unmarshal(b, &traceID2); err != nil {
t.Fatalf("Expected successful json deserialization, got error: %+v", err)
}
if traceID2.High != traceID.High || traceID2.Low != traceID.Low {
t.Fatalf("Unexpected traceID2, want: %#v, have %#v", traceID, traceID2)
}
have, err := TraceIDFromHex(traceID.String())
if err != nil {
t.Fatalf("Expected traceID got error: %+v", err)
}
if traceID.High != have.High || traceID.Low != have.Low {
t.Errorf("Expected %+v, got %+v", traceID, have)
}
traceID = TraceID{High: 0, Low: 2}
if len(traceID.String()) != 16 {
t.Errorf("Expected zero-padded TraceID to have 16 characters, got %d", len(traceID.String()))
}
have, err = TraceIDFromHex(traceID.String())
if err != nil {
t.Fatalf("Expected traceID got error: %+v", err)
}
if traceID.High != have.High || traceID.Low != have.Low {
t.Errorf("Expected %+v, got %+v", traceID, have)
}
traceID = TraceID{High: 0, Low: 0}
if !traceID.Empty() {
t.Errorf("Expected TraceID to be empty")
}
if _, err = TraceIDFromHex("12345678901234zz12345678901234zz"); err == nil {
t.Errorf("Expected error got nil")
}
if err = json.Unmarshal([]byte(`"12345678901234zz12345678901234zz"`), &traceID); err == nil {
t.Errorf("Expected error got nil")
}
}
|