-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathsearch_test.go
258 lines (214 loc) · 5.61 KB
/
search_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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package replication
import (
"context"
"math/rand"
"net/http"
"testing"
"time"
)
var baseTime = time.Date(2016, 1, 1, 0, 0, 0, 0, time.UTC)
// buildState is helper to build "valid" state files.
func buildState(n int) *State {
d := time.Duration(n)
return &State{SeqNum: uint64(n), Timestamp: baseTime.Add(d * time.Hour)}
}
func TestSearchTimestamp(t *testing.T) {
t.Skip()
ctx := context.Background()
ts := &stater{
Min: 1,
Current: func(ctx context.Context) (*State, error) { return buildState(50), nil },
State: func(ctx context.Context, n uint64) (*State, error) {
states := map[uint64]*State{
5: buildState(5),
10: buildState(10),
15: buildState(15),
20: buildState(20),
25: buildState(25),
30: buildState(30),
}
s := states[n]
if s == nil {
return nil, &UnexpectedStatusCodeError{Code: http.StatusNotFound}
}
return s, nil
},
}
cases := []struct {
name string
time time.Time
expected uint64
}{
{
name: "before",
time: baseTime,
expected: 5,
},
{
name: "after",
time: baseTime.Add(100 * time.Hour),
expected: 50,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
s, err := searchTimestamp(ctx, ts, tc.time)
if err != nil {
t.Fatalf("error getting timestamp: %v", err)
}
if s.SeqNum != tc.expected {
t.Errorf("incorrect seq number: %v != %v", s.SeqNum, tc.expected)
}
})
}
}
func TestFindBound(t *testing.T) {
ctx := context.Background()
ts := &stater{
Min: 1,
Current: func(ctx context.Context) (*State, error) { return buildState(9), nil },
State: func(ctx context.Context, n uint64) (*State, error) {
states := map[uint64]*State{
3: buildState(3),
4: buildState(4),
5: buildState(5),
6: buildState(6),
7: buildState(7),
8: buildState(8),
}
s := states[n]
if s == nil {
return nil, &UnexpectedStatusCodeError{Code: http.StatusNotFound}
}
return s, nil
},
}
cases := []struct {
name string
time time.Time
lower uint64
upper uint64
}{
{
name: "before",
time: baseTime,
lower: 3,
upper: 3,
},
{
name: "middle before",
time: baseTime.Add(6 * time.Hour).Add(30 * time.Minute),
lower: 5,
upper: 9,
},
{
name: "middle after",
time: baseTime.Add(4 * time.Hour).Add(30 * time.Minute),
lower: 3,
upper: 5,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
upper, _ := ts.Current(ctx)
lower, upper, err := findBound(ctx, ts, upper, tc.time)
if err != nil {
t.Fatalf("error getting timestamp: %v", err)
}
if lower.SeqNum != tc.lower {
t.Errorf("incorrect lower seq number: %v != %v", lower.SeqNum, tc.lower)
}
if upper.SeqNum != tc.upper {
t.Errorf("incorrect upper seq number: %v != %v", upper.SeqNum, tc.upper)
}
})
}
}
func TestMinuteStateAt(t *testing.T) {
liveOnly(t)
ctx := context.Background()
base := time.Date(2012, 9, 1, 0, 0, 0, 0, time.UTC)
now := time.Date(2022, 9, 1, 0, 0, 0, 0, time.UTC)
diff := int64(now.Sub(base)/time.Second + 10)
r := rand.New(rand.NewSource(42))
for i := 0; i < 10; i++ {
secs := r.Int63n(diff)
timestamp := base.Add(time.Duration(secs) * time.Second)
t.Logf("n: %d timestamp: %v", i, timestamp)
_, state, err := MinuteStateAt(ctx, timestamp)
if err != nil {
t.Fatalf("failed to get state: %v", err)
}
if timestamp.After(state.Timestamp) {
_, current, err := CurrentMinuteState(ctx)
if err != nil {
t.Fatalf("could not get current state: %v", err)
}
if current.SeqNum != state.SeqNum {
t.Logf("state: %+v", state)
t.Fatalf("if timstamp is before, it must be the current timestamp")
}
continue
}
if state.SeqNum == minMinute {
// timestamp was before the first state
continue
}
// state's timestamp is after what we want
// get previous to make sure it before what we want
previous, err := MinuteState(ctx, MinuteSeqNum(state.SeqNum-1))
if err != nil {
t.Fatalf("could not get previous state: %v", err)
}
if !previous.Timestamp.Before(timestamp) {
t.Logf("prev: %+v", previous)
t.Logf("state: %+v", state)
t.Fatalf("previus state not before timestamp")
}
t.Logf("found: %+v", state)
}
}
func TestChangesetStateAt(t *testing.T) {
liveOnly(t)
ctx := context.Background()
base := time.Date(2016, 9, 15, 0, 0, 0, 0, time.UTC)
now := time.Date(2022, 9, 1, 0, 0, 0, 0, time.UTC)
diff := int64(now.Sub(base)/time.Second + 10)
r := rand.New(rand.NewSource(42))
for i := 0; i < 10; i++ {
secs := r.Int63n(diff)
timestamp := base.Add(time.Duration(secs) * time.Second)
t.Logf("n: %d timestamp: %v", i, timestamp)
_, state, err := ChangesetStateAt(ctx, timestamp)
if err != nil {
t.Fatalf("failed to get state: %v", err)
}
if timestamp.After(state.Timestamp) {
_, current, err := CurrentChangesetState(ctx)
if err != nil {
t.Fatalf("could not get current state: %v", err)
}
if current.SeqNum != state.SeqNum {
t.Logf("state: %+v", state)
t.Fatalf("if timstamp is before, it must be the current timestamp")
}
continue
}
if state.SeqNum == minChangeset {
// timestamp was before the first state
continue
}
// state's timestamp is after what we want
// get previous to make sure it before what we want
previous, err := ChangesetState(ctx, ChangesetSeqNum(state.SeqNum-1))
if err != nil {
t.Fatalf("could not get previous state: %v", err)
}
if !previous.Timestamp.Before(timestamp) {
t.Logf("prev: %+v", previous)
t.Logf("state: %+v", state)
t.Fatalf("previus state not before timestamp")
}
t.Logf("found: %+v", state)
}
}