forked from fgeller/kt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
offsetresolve_test.go
180 lines (167 loc) · 4.07 KB
/
offsetresolve_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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package main
import (
"bufio"
"strconv"
"strings"
"testing"
"time"
qt "github.com/frankban/quicktest"
"github.com/google/go-cmp/cmp"
)
var epoch = mustParseTime("2010-01-02T12:00:00+02:00")
type resolveTestGroup struct {
times map[int32][]time.Time
offsets map[int32]int64
tests []resolveTest
}
type resolveTest struct {
offsets string
expect map[int32]resolvedInterval
}
func parseResolveTests(c *qt.C, testStr string) []resolveTestGroup {
blocks := strings.Split(testStr, "\n----\n")
groups := make([]resolveTestGroup, len(blocks))
for i, b := range blocks {
groups[i] = parseResolveGroup(c, b)
}
return groups
}
func TestParseResolveTests(t *testing.T) {
c := qt.New(t)
// Sanity-check the parseResolveGroup code.
gs := parseResolveTests(c, `
0 100 +0 +2s +3s +59m55s +2s
1 50 2001-10-23T01:03:00Z +1h
all=0:newest
0 100 104
1 50 52
all=0:
2 3 5
3 50 52
`[1:])
c.Assert(gs, qt.CmpEquals(cmp.AllowUnexported(
resolveTestGroup{},
resolveTest{},
resolvedInterval{},
)), []resolveTestGroup{{
times: map[int32][]time.Time{
0: {
epoch,
epoch.Add(2 * time.Second),
epoch.Add(5 * time.Second),
epoch.Add(time.Hour),
epoch.Add(time.Hour + 2*time.Second),
},
1: {
mustParseTime("2001-10-23T01:03:00Z"),
mustParseTime("2001-10-23T02:03:00Z"),
},
},
offsets: map[int32]int64{0: 100, 1: 50},
tests: []resolveTest{{
offsets: "all=0:newest",
expect: map[int32]resolvedInterval{
0: {100, 104},
1: {50, 52},
},
}, {
offsets: "all=0:",
expect: map[int32]resolvedInterval{
2: {3, 5},
3: {50, 52},
},
}},
}})
}
func parseResolveGroup(c *qt.C, block string) resolveTestGroup {
g := resolveTestGroup{
times: make(map[int32][]time.Time),
offsets: make(map[int32]int64),
}
scan := bufio.NewScanner(strings.NewReader(block))
for {
if !scan.Scan() {
c.Fatalf("unexpected end of resolve test block")
}
pfields := strings.Fields(scan.Text())
if len(pfields) == 0 {
break
}
if len(pfields) < 2 {
c.Fatalf("too few fields in line %q", scan.Text())
}
partition, err := strconv.Atoi(pfields[0])
c.Assert(err, qt.Equals, nil)
startOffset, err := strconv.ParseInt(pfields[1], 10, 64)
c.Assert(err, qt.Equals, nil)
g.offsets[int32(partition)] = startOffset
msgs := pfields[2:]
times := make([]time.Time, len(msgs))
t := epoch
for i, m := range msgs {
if strings.HasPrefix(m, "+") {
d, err := time.ParseDuration(m[1:])
c.Assert(err, qt.Equals, nil)
t = t.Add(d)
times[i] = t
continue
}
msgTime, err := time.Parse(time.RFC3339, m)
c.Assert(err, qt.Equals, nil, qt.Commentf("line %q; field %d of %q", scan.Text(), i, msgs))
if msgTime.Before(t) && i > 0 {
c.Fatalf("out of order test messages")
}
times[i] = msgTime
t = msgTime
}
g.times[int32(partition)] = times
}
for scan.Scan() {
test := resolveTest{
offsets: scan.Text(),
expect: make(map[int32]resolvedInterval),
}
for scan.Scan() {
fields := strings.Fields(scan.Text())
if len(fields) == 0 {
break
}
if len(fields) != 3 {
c.Fatalf("expected three fields in a resolved offset for a partition, got %q", scan.Text())
}
partition, err := strconv.Atoi(fields[0])
c.Assert(err, qt.Equals, nil)
start, err := strconv.ParseInt(fields[1], 10, 64)
c.Assert(err, qt.Equals, nil)
end, err := strconv.ParseInt(fields[2], 10, 64)
c.Assert(err, qt.Equals, nil)
test.expect[int32(partition)] = resolvedInterval{
start: start,
end: end,
}
}
g.tests = append(g.tests, test)
}
return g
}
//// TestResolver tests the resolver.ResolveOffsets method
//// independently of Kafka itself.
//func TestResolverResolveOffsets(t *testing.T) {
// c := qt.New(t)
// testGroups := parseResolveTests(c, resolveTestData)
// for i, g := range testGroups {
// c.Run(fmt.Sprintf("group%d", i), func(c *qt.C) {
// c.Parallel()
// topic := makeTopic(c)
// for partition, startOff := range g.offsets {
// for _, m :=
// })
// }
//}
func mustParseTime(s string) time.Time {
t, err := time.Parse(time.RFC3339, s)
if err != nil {
panic(err)
}
return t
}