...
1
16
17 package typeurl
18
19 import (
20 "fmt"
21 "reflect"
22 "testing"
23 )
24
25 type TestType struct {
26 ID string
27 }
28
29 func init() {
30 Register(&TestType{}, "typeurl.Type")
31 }
32
33 func TestMarshalEvent(t *testing.T) {
34 for _, testcase := range []struct {
35 event interface{}
36 url string
37 }{
38 {
39 event: &TestType{ID: "Test"},
40 url: "typeurl.Type",
41 },
42 } {
43 t.Run(fmt.Sprintf("%T", testcase.event), func(t *testing.T) {
44 a, err := MarshalAny(testcase.event)
45 if err != nil {
46 t.Fatal(err)
47 }
48 if a.GetTypeUrl() != testcase.url {
49 t.Fatalf("unexpected url: %q != %q", a.GetTypeUrl(), testcase.url)
50 }
51
52 v, err := UnmarshalAny(a)
53 if err != nil {
54 t.Fatal(err)
55 }
56 if !reflect.DeepEqual(v, testcase.event) {
57 t.Fatalf("round trip failed %v != %v", v, testcase.event)
58 }
59 })
60 }
61 }
62
63 func BenchmarkMarshalEvent(b *testing.B) {
64 ev := &TestType{}
65 expected, err := MarshalAny(ev)
66 if err != nil {
67 b.Fatal(err)
68 }
69 for i := 0; i < b.N; i++ {
70 a, err := MarshalAny(ev)
71 if err != nil {
72 b.Fatal(err)
73 }
74 if a.GetTypeUrl() != expected.GetTypeUrl() {
75 b.Fatalf("incorrect type url: %v != %v", a, expected)
76 }
77 }
78 }
79
View as plain text