-
Notifications
You must be signed in to change notification settings - Fork 721
Expand file tree
/
Copy pathvisit_test.go
More file actions
370 lines (344 loc) · 14.1 KB
/
Copy pathvisit_test.go
File metadata and controls
370 lines (344 loc) · 14.1 KB
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package cmd
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/vespa-engine/vespa/client/go/internal/mock"
"github.com/vespa-engine/vespa/client/go/internal/vespa"
)
const (
normalpre = `{"pathId":"/document/v1/","documents":[`
document1 = `{"id":"id:t:m::1","fields":{"title":"t"}}`
document2 = `{"id":"id:t:m::2","fields":{"title":"t2"}}`
document3 = `{"id":"id:t:m::3","fields":{"ar":"xyz","w":63,"title":"xyzzy","year":2000}}`
savedresponse = `{"pathId":"/document/v1/","documents":[{"id":"id:test:music::1921492307","fields":{"title":"song","year":2010}},{"id":"id:test:music::p_try-this-clean-bonus-dvd-_music_1922003403","fields":{"artist":"xyz","weight":600000,"song":"hate","title":"xyz","year":2000}}],"documentCount":2,"continuation":"AAAACAAAAAAAAAAJAAAAAAAAAAgAAAAAAAABAAAAAAEgAAAAAAAAEAAAAAAAAAAA"}`
saveddoc0 = `{"id":"id:test:music::1921492307","fields":{"title":"song","year":2010}}`
saveddoc1 = `{"id":"id:test:music::p_try-this-clean-bonus-dvd-_music_1922003403","fields":{"artist":"xyz","weight":600000,"song":"hate","title":"xyz","year":2000}}`
handlersResponse = `{
"handlers" : [ {
"id" : "com.yahoo.container.usability.BindingsOverviewHandler",
"class" : "com.yahoo.container.usability.BindingsOverviewHandler",
"bundle" : "container-disc:8.0.0",
"serverBindings" : [ "http://*/" ]
}, {
"id" : "com.yahoo.document.restapi.resource.DocumentV1ApiHandler",
"class" : "com.yahoo.document.restapi.resource.DocumentV1ApiHandler",
"bundle" : "vespaclient-container-plugin:8.0.0",
"serverBindings" : [ "http://*/document/v1/*", "http://*/document/v1/*/" ]
} ]
}`
clusterStarResponse = `{"pathId":"/document/v1/","message":"Your Vespa deployment has no content cluster '*', only 'fooCC'"}`
overloadResponse = `{"pathId":"/document/v1/","message":"Rejecting execution due to overload: 12345 requests already enqueued"}`
)
func TestQuoteFunc(t *testing.T) {
var buf []byte = make([]byte, 3)
buf[0] = 'a'
buf[2] = 'z'
for i := range 256 {
buf[1] = byte(i)
s := string(buf)
res := quoteArgForUrl(s)
if i < 32 || i > 127 {
assert.Equal(t, "a+z", res)
} else if testing.Verbose() { // go test -v
fmt.Printf("res %3d => '%s'\n", i, res)
}
}
}
// low-level (unit) test
func TestRunOneVisit(t *testing.T) {
withResponse := func(client *mock.HTTPClient) {
client.NextResponseString(200, savedresponse)
}
op := func(service *vespa.Service) {
vArgs := visitArgs{
contentCluster: "fooCC",
header: make(http.Header),
stream: true,
}
vArgs.header.Set("X-Foo", "Bar")
vvo, res := runOneVisit(&vArgs, service, "BBBB")
assert.Equal(t, true, res.Success)
assert.Equal(t, "visited fooCC", res.Message)
assert.Equal(t, "/document/v1/", vvo.PathId)
assert.Equal(t, "", vvo.ErrorMsg)
assert.Equal(t, "AAAACAAAAAAAAAAJAAAAAAAAAAgAAAAAAAABAAAAAAEgAAAAAAAAEAAAAAAAAAAA", vvo.Continuation)
assert.Equal(t, 2, vvo.DocumentCount)
assert.Equal(t, 2, len(vvo.Documents))
assert.Equal(t, saveddoc0, string(vvo.Documents[0].blob))
assert.Equal(t, saveddoc1, string(vvo.Documents[1].blob))
}
req := withMockClient(t, withResponse, op)
assert.Equal(t, "cluster=fooCC&continuation=BBBB&stream=true", req.URL.RawQuery)
assert.Equal(t, "Bar", req.Header.Get("X-Foo"))
op = func(service *vespa.Service) {
vArgs := visitArgs{
contentCluster: "search",
fieldSet: "[id]",
selection: "music.year>2000",
chunkCount: 123,
}
vvo, res := runOneVisit(&vArgs, service, "asdf")
assert.Equal(t, true, res.Success)
assert.Equal(t, 2, vvo.DocumentCount)
}
req = withMockClient(t, withResponse, op)
assert.Equal(t, "cluster=search&fieldSet=%5Bid%5D&selection=music%2Eyear%3E2000&continuation=asdf&wantedDocumentCount=123&stream=false", req.URL.RawQuery)
}
func withMockClient(t *testing.T, prepCli func(*mock.HTTPClient), runOp func(*vespa.Service)) *http.Request {
client := &mock.HTTPClient{}
mockServiceStatus(client, "container")
prepCli(client)
cli, _, _ := newTestCLI(t)
cli.httpClient = client
service, err := documentService(cli, &Waiter{cli: cli})
if err != nil {
t.Fatal(err)
}
runOp(service)
return client.LastRequest
}
type responseCodeAndPayload struct {
httpCode int
payload string
}
func TestVisitCommand(t *testing.T) {
assertVisitResults(
[]string{
"visit",
"--bucket-space", "default",
"--json-lines",
},
t,
[]responseCodeAndPayload{
{200, handlersResponse},
{400, clusterStarResponse},
// 429s shall be transparently retried with random exponential backoff
{429, overloadResponse},
{429, overloadResponse},
{429, overloadResponse},
{200, normalpre +
document1 +
`],"documentCount":1,"continuation":"CAFE"}`},
// Continuation token must be retained across 429 retries
{429, overloadResponse},
{200, normalpre +
document2 +
"," +
document3 +
`],"documentCount":2}`},
},
"cluster=fooCC&continuation=CAFE&wantedDocumentCount=1000&bucketSpace=default&stream=false",
document1+"\n"+
document2+"\n"+
document3+"\n")
}
const (
jsonlDoc1 = `{"put":"id:space:music::1","fields":{"title":"first"}}`
jsonlDoc2 = `{"put":"id:space:music::2","fields":{"title":"second"}}`
jsonlRemoveDoc = `{"remove":"id:space:music::3"}`
jsonlCont = `{"continuation":{"token":"JSONL_TOKEN","percentFinished":50.0}}`
jsonlDone = `{"continuation":{"percentFinished":100.0}}`
jsonlStats = `{"sessionStats":{"documentCount":2}}`
jsonlOutDoc1 = `{"id":"id:space:music::1","fields":{"title":"first"}}`
jsonlOutDoc2 = `{"id":"id:space:music::2","fields":{"title":"second"}}`
)
func jsonlResponse(body string) mock.HTTPResponse {
return mock.HTTPResponse{
Status: 200,
Body: []byte(body),
Header: http.Header{"Content-Type": []string{"application/jsonl; charset=UTF-8"}},
}
}
func TestRunOneVisitJSONL(t *testing.T) {
// Complete response: docs + stats + done signal
body := jsonlDoc1 + "\n" + jsonlDoc2 + "\n" + jsonlStats + "\n" + jsonlDone + "\n"
client := &mock.HTTPClient{}
mockServiceStatus(client, "container")
client.NextResponse(jsonlResponse(body))
cli, stdout, _ := newTestCLI(t)
cli.httpClient = client
service, err := documentService(cli, &Waiter{cli: cli})
assert.Nil(t, err)
vArgs := visitArgs{
contentCluster: "fooCC",
stream: true,
jsonLines: true,
chunkCount: 1000,
header: make(http.Header),
cli: cli,
}
vvo, res := runOneVisit(&vArgs, service, "")
assert.True(t, res.Success)
assert.Equal(t, "application/json, application/jsonl", client.LastRequest.Header.Get("Accept"))
assert.Equal(t, "", vvo.Continuation)
assert.False(t, vvo.Truncated)
assert.Equal(t, 2, vvo.DocumentCount)
assert.Equal(t, jsonlOutDoc1+"\n"+jsonlOutDoc2+"\n", stdout.String())
}
func TestVisitCommandJSONL(t *testing.T) {
// JSONL is a single streaming response — all docs in one body with done signal
body := jsonlDoc1 + "\n" + jsonlDoc2 + "\n" + jsonlDone + "\n"
client := &mock.HTTPClient{}
client.NextResponseString(200, handlersResponse)
client.NextResponse(jsonlResponse(body))
cli, stdout, stderr := newTestCLI(t)
cli.httpClient = client
cli.sleeper = func(d time.Duration) {}
assert.Nil(t, cli.Run("visit", "--stream", "--json-lines", "--bucket-space", "default", "--content-cluster", "fooCC", "-t", "http://127.0.0.1:8080"))
assert.Equal(t, jsonlOutDoc1+"\n"+jsonlOutDoc2+"\n", stdout.String())
assert.Equal(t, "", stderr.String())
assert.Equal(t, 2, len(client.Requests))
assert.Equal(t, "application/json, application/jsonl", client.LastRequest.Header.Get("Accept"))
assert.Equal(t, "cluster=fooCC&wantedDocumentCount=1000&bucketSpace=default&stream=true", client.LastRequest.URL.RawQuery)
}
func TestRunOneVisitJSONLUserAcceptHeader(t *testing.T) {
body := jsonlDoc1 + "\n" + jsonlStats + "\n" + jsonlDone + "\n"
client := &mock.HTTPClient{}
mockServiceStatus(client, "container")
client.NextResponse(jsonlResponse(body))
cli, _, _ := newTestCLI(t)
cli.httpClient = client
service, err := documentService(cli, &Waiter{cli: cli})
assert.Nil(t, err)
userHeader := make(http.Header)
userHeader.Set("Accept", "application/json")
vArgs := visitArgs{
contentCluster: "fooCC",
stream: true,
jsonLines: true,
chunkCount: 1000,
header: userHeader,
cli: cli,
}
_, res := runOneVisit(&vArgs, service, "")
assert.True(t, res.Success)
assert.Equal(t, "application/json", client.LastRequest.Header.Get("Accept"))
}
func SetUpCliAndRunVisit(t *testing.T, client *mock.HTTPClient) (stdout *bytes.Buffer, stderr *bytes.Buffer, err error) {
cli, stdout, stderr := newTestCLI(t)
cli.httpClient = client
cli.sleeper = func(d time.Duration) {}
err = cli.Run("visit", "--stream", "--json-lines", "--bucket-space", "default", "--content-cluster", "fooCC", "-t", "http://127.0.0.1:8080")
return stdout, stderr, err
}
func TestVisitCommandJSONLTruncatedRetry(t *testing.T) {
// First response has no trailing newline — truncated
truncatedBody := jsonlDoc1 + "\n" + jsonlCont
completeBody := jsonlDoc2 + "\n" + jsonlDone + "\n"
client := &mock.HTTPClient{}
client.NextResponseString(200, handlersResponse)
client.NextResponse(jsonlResponse(truncatedBody))
client.NextResponse(jsonlResponse(completeBody))
stdout, stderr, err := SetUpCliAndRunVisit(t, client)
assert.Nil(t, err)
assert.Contains(t, stdout.String(), jsonlOutDoc1+"\n")
assert.Contains(t, stdout.String(), jsonlOutDoc2+"\n")
assert.Contains(t, stderr.String(), "truncated")
// Retry request must use the continuation token extracted from the truncated response
assert.Equal(t, 3, len(client.Requests))
assert.Contains(t, client.Requests[2].URL.RawQuery, "continuation=JSONL_TOKEN")
}
func TestVisitCommandJSONLTruncatedAbort(t *testing.T) {
// Truncated 5 times in a row with no continuation token — should abort
truncatedNoToken := jsonlDoc1 + "\n" // no continuation line, no done signal
client := &mock.HTTPClient{}
client.NextResponseString(200, handlersResponse)
for range 5 {
client.NextResponse(jsonlResponse(truncatedNoToken))
}
_, _, err := SetUpCliAndRunVisit(t, client)
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "truncated")
assert.Equal(t, 6, len(client.Requests)) // probe + 5 visit attempts
}
func TestTruncatedMidJSONLMaxRetries(t *testing.T) {
client := &mock.HTTPClient{}
client.NextResponseString(200, handlersResponse)
// Creating responses that truncate at different lengths
for i := range 5 {
truncationIdx := (i * 5) + 1
client.NextResponse(jsonlResponse(jsonlDoc1[0:truncationIdx]))
}
_, _, err := SetUpCliAndRunVisit(t, client)
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "truncated")
assert.Equal(t, 6, len(client.Requests)) // probe + 5 visit attempts until it does not try retry any more
}
// Get truncated response 2 times and then the full response
func TestTruncatedMidJSONLSomeRetries(t *testing.T) {
client := &mock.HTTPClient{}
client.NextResponseString(200, handlersResponse)
// Creating responses that truncate at different lengths
for i := range 2 {
truncationIdx := (i * 5) + 1
client.NextResponse(jsonlResponse(jsonlDoc1[0:truncationIdx]))
}
jsonlComplete := jsonlDoc1 + "\n" + jsonlDone + "\n"
client.NextResponse(jsonlResponse(jsonlComplete))
_, _, err := SetUpCliAndRunVisit(t, client)
assert.Nil(t, err)
assert.Equal(t, 4, len(client.Requests)) // probe + 3 visit attempts where the last one is a valid response
}
func TestRemoveEntriesAreIgnored(t *testing.T) {
client := &mock.HTTPClient{}
client.NextResponseString(200, handlersResponse)
responseDoc := jsonlDoc1 + "\n" + jsonlDoc2 + "\n" + jsonlRemoveDoc + "\n" + jsonlDone + "\n"
client.NextResponse(jsonlResponse(responseDoc))
stdout, _, err := SetUpCliAndRunVisit(t, client)
assert.Nil(t, err)
// The remove content is ignored
assert.NotContains(t, stdout.String(), "remove")
assert.NotContains(t, stdout.String(), "id:space:music::3")
assert.Equal(t, 2, len(client.Requests)) // probe + 1 visit
}
func TestMakeFeedMultipleClusters(t *testing.T) {
clusterAResponse := normalpre + document1 + `],"documentCount":1}`
clusterBResponse := normalpre + document2 + `],"documentCount":1}`
client := &mock.HTTPClient{}
client.NextResponseString(200, handlersResponse)
client.NextResponseString(400, `{"pathId":"/document/v1/","message":"no content cluster '*', only 'clusterA', 'clusterB'"}`)
client.NextResponseString(200, clusterAResponse)
client.NextResponseString(200, clusterBResponse)
client.NextResponseString(200, clusterAResponse)
client.NextResponseString(200, clusterBResponse)
cli, stdout, _ := newTestCLI(t)
cli.httpClient = client
cli.sleeper = func(d time.Duration) {}
err := cli.Run("visit", "--make-feed", "-t", "http://127.0.0.1:8080")
assert.Nil(t, err)
var docs []json.RawMessage
assert.Nil(t, json.Unmarshal(stdout.Bytes(), &docs))
assert.Equal(t, 4, len(docs))
}
func inRangeMillis(v time.Duration, lo int64, hi int64) bool {
return v.Milliseconds() >= lo && v.Milliseconds() <= hi
}
func assertVisitResults(arguments []string, t *testing.T, responses []responseCodeAndPayload, queryPart, output string) {
t.Helper()
client := &mock.HTTPClient{}
for _, resp := range responses {
client.NextResponseString(resp.httpCode, resp.payload)
}
cli, stdout, stderr := newTestCLI(t)
cli.httpClient = client
var backoffs []time.Duration
cli.sleeper = func(d time.Duration) { backoffs = append(backoffs, d) }
arguments = append(arguments, "-t", "http://127.0.0.1:8080")
assert.Nil(t, cli.Run(arguments...))
assert.Equal(t, output, stdout.String())
assert.Equal(t, "", stderr.String())
assert.Equal(t, queryPart, client.LastRequest.URL.RawQuery)
assert.Equal(t, "/document/v1/", client.LastRequest.URL.Path)
assert.Equal(t, "GET", client.LastRequest.Method)
assert.Equal(t, 4, len(backoffs))
assert.True(t, inRangeMillis(backoffs[0], 100, 300)) // 200ms +- 100ms
assert.True(t, inRangeMillis(backoffs[1], 150, 450)) // 300ms +- 150ms
assert.True(t, inRangeMillis(backoffs[2], 225, 675)) // 450ms +- 225ms
assert.True(t, inRangeMillis(backoffs[3], 100, 300)) // reset back to 200ms +- 100ms
}