forked from heetch/avro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
singleencoder_test.go
119 lines (103 loc) · 2.99 KB
/
singleencoder_test.go
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package avro_test
import (
"context"
"sync"
"testing"
qt "github.com/frankban/quicktest"
"github.com/heetch/avro"
)
func TestSingleEncoder(t *testing.T) {
c := qt.New(t)
avroType := mustTypeOf(TestRecord{})
registry := memRegistry{
1: avroType,
}
enc := avro.NewSingleEncoder(registry, nil)
data, err := enc.Marshal(context.Background(), TestRecord{A: 20, B: 34})
c.Assert(err, qt.Equals, nil)
c.Assert(data, qt.DeepEquals, []byte{1, 40, 68})
// Check that we can decode it again.
var x TestRecord
dec := avro.NewSingleDecoder(registry, nil)
_, err = dec.Unmarshal(context.Background(), data, &x)
c.Assert(err, qt.Equals, nil)
c.Assert(x, qt.DeepEquals, TestRecord{A: 20, B: 34})
}
func TestSingleEncoderCheckMarshalTypeBadType(t *testing.T) {
c := qt.New(t)
enc := avro.NewSingleEncoder(memRegistry{}, nil)
err := enc.CheckMarshalType(context.Background(), struct{ C chan int }{})
c.Assert(err, qt.ErrorMatches, `cannot use unnamed type struct .*`)
}
func TestSingleEncoderCheckMarshalTypeNotFound(t *testing.T) {
c := qt.New(t)
enc := avro.NewSingleEncoder(memRegistry{}, nil)
err := enc.CheckMarshalType(context.Background(), TestRecord{})
c.Assert(err, qt.ErrorMatches, `schema not found`)
}
func TestSingleEncoderCachesTypes(t *testing.T) {
c := qt.New(t)
registry := &statsRegistry{
memRegistry: memRegistry{
1: mustTypeOf(TestRecord{}),
},
}
enc := avro.NewSingleEncoder(registry, nil)
data, err := enc.Marshal(context.Background(), TestRecord{A: 20, B: 34})
c.Assert(err, qt.Equals, nil)
c.Assert(data, qt.DeepEquals, []byte{1, 40, 68})
// Check that when we marshal it again that we don't get another
// call to the registry.
data, err = enc.Marshal(context.Background(), TestRecord{A: 22, B: 35})
c.Assert(err, qt.Equals, nil)
c.Assert(data, qt.DeepEquals, []byte{1, 44, 70})
c.Assert(registry.idForSchemaCount, qt.Equals, 1)
}
func TestSingleEncoderRace(t *testing.T) {
// Note: this test is designed to be run with the
// race detector enabled.
c := qt.New(t)
type T1 struct {
A int
}
type T2 struct {
B int
}
registry := memRegistry{
1: mustTypeOf(T1{}),
2: mustTypeOf(T2{}),
}
enc := avro.NewSingleEncoder(registry, nil)
var wg sync.WaitGroup
marshal := func(x interface{}) {
defer wg.Done()
_, err := enc.Marshal(context.Background(), x)
c.Check(err, qt.Equals, nil)
}
wg.Add(3)
go marshal(T1{10})
go marshal(T1{11})
go marshal(T2{12})
wg.Wait()
}
// statsRegistry wraps a memRegistry instance and counts calls to some calls.
type statsRegistry struct {
idForSchemaCount int
schemaForIDCount int
memRegistry
}
func (r *statsRegistry) IDForSchema(ctx context.Context, schema *avro.Type) (int64, error) {
r.idForSchemaCount++
return r.memRegistry.IDForSchema(ctx, schema)
}
func (r *statsRegistry) SchemaForID(ctx context.Context, id int64) (*avro.Type, error) {
r.schemaForIDCount++
return r.memRegistry.SchemaForID(ctx, id)
}
func mustTypeOf(x interface{}) *avro.Type {
t, err := avro.TypeOf(x)
if err != nil {
panic(err)
}
return t
}