forked from elodina/go-avro
-
Notifications
You must be signed in to change notification settings - Fork 4
/
register_test.go
140 lines (119 loc) · 3.29 KB
/
register_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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package avro
import (
"fmt"
"math/rand"
"sync"
"testing"
"time"
)
const subject = "this_is_a_test_schema"
const rawSchema = `{"type": "record", "name": "TestRecord", "fields": [
{"name": "longRecordField", "type": "long"},
{"name": "floatRecordField", "type": "float"}
]}`
const rawSchema1 = `{"type": "record", "name": "TestRecord", "fields": [
{"name": "stringRecordField", "type": "string"},
{"name": "floatRecordField", "type": "float"}
]}`
const rawSchema2 = `{"type": "record", "name": "TestRecord", "fields": [
{"name": "floatRecordField", "type": "float"}
]}`
const rawSchema3 = `{"type": "record", "name": "TestRecord", "fields": [
{"name": "intRecordField", "type": "int"},
{"name": "floatRecordField", "type": "float"}
]}`
const rawSchema4 = `{"type": "record", "name": "TestRecord", "fields": [
{"name": "floatRecordField", "type": "float"},
{"name": "floatRecordField", "type": "float"}
]}`
var regClient *CachedSchemaRegistryClient
var schema Schema
var id int32 = 735
var rawList = []string{rawSchema, rawSchema1, rawSchema2, rawSchema3, rawSchema4}
var schemaList []Schema
func init() {
regClient = NewCachedSchemaRegistryClient("")
_ = regClient
for i := range rawList {
sch, err := ParseSchema(rawList[i])
if err != nil {
panic(err)
}
schemaList = append(schemaList, sch)
}
_ = schema
}
// func TestRegister(t *testing.T) {
// if regClient == nil {
// t.Error("regclient is nil")
// }
// t.Errorf("regclient:%v", regClient)
// for i := 0; i < len(schemaList); i++ {
// id, err := regClient.Register(fmt.Sprintf("this_is_a_test_schema_%d", i), schemaList[i])
// assert(t, err, nil)
// t.Logf("this_is_a_test_schema_%v:id:%v", i, id)
// }
// }
func TestGetByID(t *testing.T) {
now := time.Now()
sch, err := regClient.GetByID(id)
assert(t, err, nil)
t.Logf("getByID scehma:%v", sch)
for i := 0; i < 100; i++ {
id = 700 + rand.Int31n(40)
t.Logf("getByID:%v", time.Since(now))
_, err := regClient.GetByID(id)
assert(t, err, nil)
now = time.Now()
}
}
func TestGetIDBySchema(t *testing.T) {
now := time.Now()
for i := 0; i < 20; i++ {
idx := rand.Int31n(4)
idtemp, err := regClient.GetIDBySchema(fmt.Sprintf("this_is_a_test_schema_%v", idx), schemaList[idx])
assert(t, err, nil)
t.Logf("getIDBySchema id:%v", idtemp)
t.Logf("duration:%v", time.Since(now))
now = time.Now()
}
}
func TestGetVersion(t *testing.T) {
vID, err := regClient.GetVersion("this_is_a_test_schema_0", schemaList[0])
assert(t, err, nil)
t.Logf("twice getVersion id:%v", vID)
now := time.Now()
for i := 0; i < 10; i++ {
vID, err := regClient.GetVersion("this_is_a_test_schema_0", schemaList[0])
assert(t, err, nil)
t.Logf("getVersion id:%v", vID)
t.Logf("duration:%v", time.Since(now))
now = time.Now()
}
}
func TestGetLatestSchemaMetadata(t *testing.T) {
meta, err := regClient.GetLatestSchemaMetadata(subject)
assert(t, err, nil)
t.Logf("meta:%v", meta)
}
func BenchmarkRegister(b *testing.B) {
var wg sync.WaitGroup
regClient := NewCachedSchemaRegistryClient("")
id := int32(735)
schema, err := regClient.GetByID(id)
if err != nil {
b.Fatal(err)
}
for i := 0; i < b.N; i++ {
wg.Add(1)
go func() {
defer wg.Done()
id, err := regClient.GetIDBySchema("this_is_a_test_schema", schema)
if err != nil {
b.Fatal(err)
}
_ = id
}()
}
wg.Wait()
}