forked from databricks/databricks-sql-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver_e2e_test.go
590 lines (499 loc) · 22.8 KB
/
driver_e2e_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
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
package dbsql
import (
"context"
"database/sql"
"encoding/json"
"fmt"
"net/http/httptest"
"net/url"
"os"
"strconv"
"testing"
"time"
"github.com/databricks/databricks-sql-go/driverctx"
dbsqlerr "github.com/databricks/databricks-sql-go/errors"
"github.com/databricks/databricks-sql-go/internal/cli_service"
"github.com/databricks/databricks-sql-go/internal/client"
"github.com/databricks/databricks-sql-go/logger"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestWorkflowExample(t *testing.T) {
// these are global variables that get changed depending on the the expected response
var (
catalog = "hive_metastore"
schema = "default"
)
state := &callState{}
// load basic responses
loadTestData(t, "GetResultSetMetadataNotStaging.json", &state.getResultSetMetadataResp)
loadTestData(t, "OpenSessionSuccess.json", &state.openSessionResp)
loadTestData(t, "CloseSessionSuccess.json", &state.closeSessionResp)
loadTestData(t, "CloseOperationSuccess.json", &state.closeOperationResp)
ts := getServer(state)
defer ts.Close()
r, err := url.Parse(ts.URL)
require.NoError(t, err)
port, err := strconv.Atoi(r.Port())
require.NoError(t, err)
connector, err := NewConnector(
WithServerHostname("localhost"),
WithPort(port),
WithHTTPPath(""),
WithAccessToken(""),
WithSessionParams(map[string]string{"timezone": "America/Sao_Paulo", "ansi_mode": "true"}),
WithUserAgentEntry("workflow-example"),
WithInitialNamespace(catalog, schema),
WithTimeout(time.Minute),
WithMaxRows(10),
)
require.NoError(t, err)
db := sql.OpenDB(connector)
defer db.Close()
ogCtx := driverctx.NewContextWithCorrelationId(context.Background(), "workflow-example")
ctx1, cancel := context.WithTimeout(ogCtx, 30*time.Second)
defer cancel()
// set up basic responses for ping
state.executeStatementResp = cli_service.TExecuteStatementResp{}
loadTestData(t, "ExecuteStatement1.json", &state.executeStatementResp)
if err := db.PingContext(ctx1); err != nil {
require.NoError(t, err)
}
state.executeStatementResp = cli_service.TExecuteStatementResp{}
loadTestData(t, "ExecuteStatement4.json", &state.executeStatementResp)
if _, err := db.ExecContext(ogCtx, `CREATE TABLE IF NOT EXISTS diamonds USING CSV LOCATION '/databricks-datasets/Rdatasets/data-001/csv/ggplot2/diamonds.csv' options (header = true, inferSchema = true)`); err != nil {
require.NoError(t, err)
}
state.executeStatementResp = cli_service.TExecuteStatementResp{}
loadTestData(t, "ExecuteStatement5.json", &state.executeStatementResp)
var max float64
if err := db.QueryRowContext(ogCtx, `select max(carat) from diamonds`).Scan(&max); err != nil {
require.NoError(t, err)
} else {
require.Equal(t, 5.01, max)
}
state.executeStatementResp = cli_service.TExecuteStatementResp{}
loadTestData(t, "ExecuteStatement7.json", &state.executeStatementResp)
state.fetchResultsResp = cli_service.TFetchResultsResp{}
loadTestData(t, "FetchResults8.json", &state.fetchResultsResp)
ctx2, cancel := context.WithTimeout(ogCtx, 30*time.Second)
defer cancel()
if rows, err := db.QueryContext(ctx2, "select * from diamonds limit 19"); err != nil {
require.NoError(t, err)
} else {
expectedColumnNames := []string{"_c0", "carat", "cut", "color", "clarity", "depth", "table", "price", "x", "y", "z"}
expectedDatabaseType := []string{"INT", "DOUBLE", "STRING", "STRING", "STRING", "DOUBLE", "DOUBLE", "INT", "DOUBLE", "DOUBLE", "DOUBLE"}
expectedNullable := []bool{false, false, false, false, false, false, false, false, false, false, false}
cols, err := rows.Columns()
require.NoError(t, err)
require.Equal(t, expectedColumnNames, cols)
types, err := rows.ColumnTypes()
require.NoError(t, err)
for i, v := range types {
assert.Equal(t, expectedColumnNames[i], v.Name())
assert.Equal(t, expectedDatabaseType[i], v.DatabaseTypeName())
nullable, ok := v.Nullable()
assert.False(t, ok)
assert.Equal(t, expectedNullable[i], nullable)
}
type row struct {
_c0 int
carat float64
cut string
color string
clarity string
depth sql.NullFloat64
table sql.NullInt64
price int
x float64
y float64
z float64
}
expectedRows := []row{
{_c0: 1, carat: 0.23, cut: "Ideal", color: "E", clarity: "SI2", depth: sql.NullFloat64{Float64: 61.5, Valid: true}, table: sql.NullInt64{Int64: 55, Valid: true}, price: 326, x: 3.95, y: 3.98, z: 2.43},
{_c0: 2, carat: 0.21, cut: "Premium", color: "E", clarity: "SI1", depth: sql.NullFloat64{Float64: 59.8, Valid: true}, table: sql.NullInt64{Int64: 61, Valid: true}, price: 326, x: 3.89, y: 3.84, z: 2.31},
{_c0: 3, carat: 0.23, cut: "Good", color: "E", clarity: "VS1", depth: sql.NullFloat64{Float64: 56.9, Valid: true}, table: sql.NullInt64{Int64: 65, Valid: true}, price: 327, x: 4.05, y: 4.07, z: 2.31},
{_c0: 4, carat: 0.29, cut: "Premium", color: "I", clarity: "VS2", depth: sql.NullFloat64{Float64: 62.4, Valid: true}, table: sql.NullInt64{Int64: 58, Valid: true}, price: 334, x: 4.2, y: 4.23, z: 2.63},
{_c0: 5, carat: 0.31, cut: "Good", color: "J", clarity: "SI2", depth: sql.NullFloat64{Float64: 63.3, Valid: true}, table: sql.NullInt64{Int64: 58, Valid: true}, price: 335, x: 4.34, y: 4.35, z: 2.75},
{_c0: 6, carat: 0.24, cut: "Very Good", color: "J", clarity: "VVS2", depth: sql.NullFloat64{Float64: 62.8, Valid: true}, table: sql.NullInt64{Int64: 57, Valid: true}, price: 336, x: 3.94, y: 3.96, z: 2.48},
{_c0: 7, carat: 0.24, cut: "Very Good", color: "I", clarity: "VVS1", depth: sql.NullFloat64{Float64: 62.3, Valid: true}, table: sql.NullInt64{Int64: 57, Valid: true}, price: 336, x: 3.95, y: 3.98, z: 2.47},
{_c0: 8, carat: 0.26, cut: "Very Good", color: "H", clarity: "SI1", depth: sql.NullFloat64{Float64: 61.9, Valid: true}, table: sql.NullInt64{Int64: 55, Valid: true}, price: 337, x: 4.07, y: 4.11, z: 2.53},
{_c0: 9, carat: 0.22, cut: "Fair", color: "E", clarity: "VS2", depth: sql.NullFloat64{Float64: 65.1, Valid: true}, table: sql.NullInt64{Int64: 61, Valid: true}, price: 337, x: 3.87, y: 3.78, z: 2.49},
{_c0: 10, carat: 0.23, cut: "Very Good", color: "H", clarity: "VS1", depth: sql.NullFloat64{Float64: 59.4, Valid: true}, table: sql.NullInt64{Int64: 61, Valid: true}, price: 338, x: 4, y: 4.05, z: 2.39},
{_c0: 11, carat: 0.3, cut: "Good", color: "J", clarity: "SI1", depth: sql.NullFloat64{Float64: 64, Valid: true}, table: sql.NullInt64{Int64: 55, Valid: true}, price: 339, x: 4.25, y: 4.28, z: 2.73},
{_c0: 12, carat: 0.23, cut: "Ideal", color: "J", clarity: "VS1", depth: sql.NullFloat64{Float64: 62.8, Valid: true}, table: sql.NullInt64{Int64: 56, Valid: true}, price: 340, x: 3.93, y: 3.9, z: 2.46},
{_c0: 13, carat: 0.22, cut: "Premium", color: "F", clarity: "SI1", depth: sql.NullFloat64{Float64: 60.4, Valid: true}, table: sql.NullInt64{Int64: 61, Valid: true}, price: 342, x: 3.88, y: 3.84, z: 2.33},
{_c0: 14, carat: 0.31, cut: "Ideal", color: "J", clarity: "SI2", depth: sql.NullFloat64{Float64: 62.2, Valid: true}, table: sql.NullInt64{Int64: 54, Valid: true}, price: 344, x: 4.35, y: 4.37, z: 2.71},
{_c0: 15, carat: 0.2, cut: "Premium", color: "E", clarity: "SI2", depth: sql.NullFloat64{Float64: 60.2, Valid: true}, table: sql.NullInt64{Int64: 62, Valid: true}, price: 345, x: 3.79, y: 3.75, z: 2.27},
{_c0: 16, carat: 0.32, cut: "Premium", color: "E", clarity: "I1", depth: sql.NullFloat64{Float64: 60.9, Valid: true}, table: sql.NullInt64{Int64: 58, Valid: true}, price: 345, x: 4.38, y: 4.42, z: 2.68},
{_c0: 17, carat: 0.3, cut: "Ideal", color: "I", clarity: "SI2", depth: sql.NullFloat64{Float64: 62, Valid: true}, table: sql.NullInt64{Int64: 54, Valid: true}, price: 348, x: 4.31, y: 4.34, z: 2.68},
{_c0: 18, carat: 0.3, cut: "Good", color: "J", clarity: "SI1", depth: sql.NullFloat64{Float64: 63.4, Valid: true}, table: sql.NullInt64{Int64: 54, Valid: true}, price: 351, x: 4.23, y: 4.29, z: 2.7},
{_c0: 19, carat: 0.3, cut: "Good", color: "J", clarity: "SI1", depth: sql.NullFloat64{Float64: 63.8, Valid: true}, table: sql.NullInt64{Int64: 56, Valid: true}, price: 351, x: 4.23, y: 4.26, z: 2.71},
}
rowIdx := 0
for rows.Next() {
// After row 10 this will cause one fetch call, as 10 rows (maxRows config) will come from the first execute statement call.
r := row{}
if err := rows.Scan(&r._c0, &r.carat, &r.cut, &r.color, &r.clarity, &r.depth, &r.table, &r.price, &r.x, &r.y, &r.z); err != nil {
require.NoError(t, err)
} else {
assert.Equal(t, expectedRows[rowIdx], r)
}
rowIdx++
}
}
state.executeStatementResp = cli_service.TExecuteStatementResp{}
loadTestData(t, "ExecuteStatement10.json", &state.executeStatementResp)
// // timezones are also supported
var curTimestamp time.Time
var curDate time.Time
var curTimezone string
loc, err := time.LoadLocation("America/Sao_Paulo")
require.NoError(t, err)
if err := db.QueryRowContext(ogCtx, `select current_date(), current_timestamp(), current_timezone()`).Scan(&curDate, &curTimestamp, &curTimezone); err != nil {
require.NoError(t, err)
} else {
assert.Equal(t, "America/Sao_Paulo", curTimezone)
assert.Equal(t, time.Date(2022, time.Month(11), 17, 14, 33, 15, 771000000, loc), curTimestamp)
assert.Equal(t, time.Date(2022, time.Month(11), 17, 0, 0, 0, 0, loc), curDate)
}
state.executeStatementResp = cli_service.TExecuteStatementResp{}
loadTestData(t, "ExecuteStatement14.json", &state.executeStatementResp)
state.getOperationStatusResp = cli_service.TGetOperationStatusResp{}
loadTestData(t, "GetOperationStatusFinished.json", &state.getOperationStatusResp)
// // we can also create and query a table with complex data types
if _, err := db.ExecContext(
ogCtx,
`create table if not exists array_map_struct (
array_col array < int >,
map_col map < string, int >,
struct_col struct < string_field string, array_field array < int > >)`); err != nil {
require.NoError(t, err)
}
state.executeStatementResp = cli_service.TExecuteStatementResp{}
loadTestData(t, "ExecuteStatement19.json", &state.executeStatementResp)
if res, err := db.ExecContext(
context.Background(),
`insert into table array_map_struct (array_col, map_col, struct_col) VALUES (
array(1, 2, 3),
map('key1', 1),
struct('string_val', array(4, 5, 6)))`); err != nil {
require.NoError(t, err)
} else {
iRows, err := res.RowsAffected()
if err != nil {
require.NoError(t, err)
}
assert.Equal(t, int64(1), iRows)
}
state.executeStatementResp = cli_service.TExecuteStatementResp{}
loadTestData(t, "ExecuteStatement20.json", &state.executeStatementResp)
if rows, err := db.QueryContext(ogCtx, "select * from array_map_struct"); err != nil {
require.NoError(t, err)
} else {
// complex data types are returned as string
type row struct {
arrayVal string
mapVal string
structVal string
}
expectedColumnNames := []string{"array_col", "map_col", "struct_col"}
expectedDatabaseType := []string{"ARRAY", "MAP", "STRUCT"}
expectedNullable := []bool{false, false, false}
cols, err := rows.Columns()
require.NoError(t, err)
require.Equal(t, expectedColumnNames, cols)
types, err := rows.ColumnTypes()
require.NoError(t, err)
for i, v := range types {
assert.Equal(t, expectedColumnNames[i], v.Name())
assert.Equal(t, expectedDatabaseType[i], v.DatabaseTypeName())
nullable, ok := v.Nullable()
assert.False(t, ok)
assert.Equal(t, expectedNullable[i], nullable)
}
expectedRows := []row{{arrayVal: "[1,2,3]", mapVal: "{\"key1\":1}", structVal: "{\"string_field\":\"string_val\",\"array_field\":[4,5,6]}"}}
rowIdx := 0
for rows.Next() {
r := row{}
if err := rows.Scan(&r.arrayVal, &r.mapVal, &r.structVal); err != nil {
require.NoError(t, err)
} else {
assert.Equal(t, expectedRows[rowIdx], r)
}
rowIdx++
}
}
}
func TestContextTimeoutExample(t *testing.T) {
_ = logger.SetLogLevel("debug")
state := &callState{}
// load basic responses
loadTestData(t, "GetResultSetMetadataNotStaging.json", &state.getResultSetMetadataResp)
loadTestData(t, "OpenSessionSuccess.json", &state.openSessionResp)
loadTestData(t, "CloseSessionSuccess.json", &state.closeSessionResp)
loadTestData(t, "CloseOperationSuccess.json", &state.closeOperationResp)
ts := getServer(state)
defer ts.Close()
db, err := sql.Open("databricks", ts.URL+"/path")
require.NoError(t, err)
defer db.Close()
ogCtx := driverctx.NewContextWithCorrelationId(context.Background(), "context-timeout-example")
state.executeStatementResp = cli_service.TExecuteStatementResp{}
loadTestData(t, "ExecuteStatement21.json", &state.executeStatementResp)
state.getOperationStatusResp = cli_service.TGetOperationStatusResp{}
loadTestData(t, "GetOperationStatusRunning.json", &state.getOperationStatusResp)
loadTestData(t, "CancelOperationSuccess.json", &state.cancelOperationResp)
ctx1, cancel := context.WithTimeout(ogCtx, 5*time.Second)
defer cancel()
rows, err := db.QueryContext(ctx1, `SELECT id FROM RANGE(100000000) ORDER BY RANDOM() + 2 asc`)
if err, ok := err.(interface{ StackTrace() errors.StackTrace }); ok {
fmt.Printf("Stack trace: %v", err.StackTrace())
}
require.True(t, errors.Is(err, context.DeadlineExceeded))
require.True(t, errors.Is(err, dbsqlerr.ExecutionError))
var ee dbsqlerr.DBExecutionError
require.True(t, errors.As(err, &ee))
require.Equal(t, "context-timeout-example", ee.CorrelationId())
require.Nil(t, rows)
_, ok := err.(interface{ Cause() error })
assert.True(t, ok)
_, ok = err.(interface{ StackTrace() errors.StackTrace })
assert.True(t, ok)
assert.Equal(t, 1, state.executeStatementCalls)
assert.GreaterOrEqual(t, state.getOperationStatusCalls, 1)
assert.Equal(t, 1, state.cancelOperationCalls)
}
func TestRetries(t *testing.T) {
t.Run("it should retry on 503 and respect retry-after", func(t *testing.T) {
_ = logger.SetLogLevel("debug")
state := &callState{}
// load basic responses
loadTestData(t, "GetResultSetMetadataNotStaging.json", &state.getResultSetMetadataResp)
loadTestData(t, "OpenSessionSuccess.json", &state.openSessionResp)
loadTestData(t, "CloseSessionSuccess.json", &state.closeSessionResp)
loadTestData(t, "CloseOperationSuccess.json", &state.closeOperationResp)
ts := getServer(state)
defer ts.Close()
db, err := sql.Open("databricks", fmt.Sprintf("%s/503-2-retries", ts.URL))
require.NoError(t, err)
defer db.Close()
state.executeStatementResp = cli_service.TExecuteStatementResp{}
loadTestData(t, "ExecuteStatement1.json", &state.executeStatementResp)
err = db.Ping()
require.NoError(t, err)
assert.Equal(t, 1, state.executeStatementCalls)
})
t.Run("it should retry on 429 and respect retry-after", func(t *testing.T) {
_ = logger.SetLogLevel("debug")
state := &callState{}
// load basic responses
loadTestData(t, "GetResultSetMetadataNotStaging.json", &state.getResultSetMetadataResp)
loadTestData(t, "OpenSessionSuccess.json", &state.openSessionResp)
loadTestData(t, "CloseSessionSuccess.json", &state.closeSessionResp)
loadTestData(t, "CloseOperationSuccess.json", &state.closeOperationResp)
ts := getServer(state)
defer ts.Close()
db, err := sql.Open("databricks", fmt.Sprintf("%s/429-2-retries", ts.URL))
require.NoError(t, err)
defer db.Close()
state.executeStatementResp = cli_service.TExecuteStatementResp{}
loadTestData(t, "ExecuteStatement1.json", &state.executeStatementResp)
err = db.Ping()
require.NoError(t, err)
assert.Equal(t, 1, state.executeStatementCalls)
})
t.Run("it should fail after maxRetries", func(t *testing.T) {
_ = logger.SetLogLevel("debug")
state := &callState{}
// load basic responses
loadTestData(t, "GetResultSetMetadataNotStaging.json", &state.getResultSetMetadataResp)
loadTestData(t, "OpenSessionSuccess.json", &state.openSessionResp)
loadTestData(t, "CloseSessionSuccess.json", &state.closeSessionResp)
loadTestData(t, "CloseOperationSuccess.json", &state.closeOperationResp)
ts := getServer(state)
defer ts.Close()
r, err := url.Parse(ts.URL)
require.NoError(t, err)
port, err := strconv.Atoi(r.Port())
require.NoError(t, err)
connector, err := NewConnector(
WithServerHostname("localhost"),
WithHTTPPath("/503-5-retries"),
WithPort(port),
WithRetries(2, 10*time.Millisecond, 1*time.Second),
)
require.NoError(t, err)
db := sql.OpenDB(connector)
defer db.Close()
state.executeStatementResp = cli_service.TExecuteStatementResp{}
loadTestData(t, "ExecuteStatement1.json", &state.executeStatementResp)
err = db.Ping()
require.ErrorContains(t, err, "after 3 attempt(s)")
})
t.Run("it should be able to turn off retries", func(t *testing.T) {
_ = logger.SetLogLevel("debug")
state := &callState{}
// load basic responses
loadTestData(t, "GetResultSetMetadataNotStaging.json", &state.getResultSetMetadataResp)
loadTestData(t, "OpenSessionSuccess.json", &state.openSessionResp)
loadTestData(t, "CloseSessionSuccess.json", &state.closeSessionResp)
loadTestData(t, "CloseOperationSuccess.json", &state.closeOperationResp)
ts := getServer(state)
defer ts.Close()
r, err := url.Parse(ts.URL)
require.NoError(t, err)
port, err := strconv.Atoi(r.Port())
require.NoError(t, err)
connector, err := NewConnector(
WithServerHostname("localhost"),
WithHTTPPath("/429-2-retries"),
WithPort(port),
WithRetries(-1, 0, 0),
)
require.NoError(t, err)
db := sql.OpenDB(connector)
defer db.Close()
state.executeStatementResp = cli_service.TExecuteStatementResp{}
loadTestData(t, "ExecuteStatement1.json", &state.executeStatementResp)
err = db.Ping()
require.ErrorContains(t, err, "after 1 attempt(s)")
})
t.Run("a 429 or 503 should result in a retryable error", func(t *testing.T) {
_ = logger.SetLogLevel("debug")
state := &callState{}
// load basic responses
loadTestData(t, "GetResultSetMetadataNotStaging.json", &state.getResultSetMetadataResp)
loadTestData(t, "OpenSessionSuccess.json", &state.openSessionResp)
loadTestData(t, "CloseSessionSuccess.json", &state.closeSessionResp)
loadTestData(t, "CloseOperationSuccess.json", &state.closeOperationResp)
ts := getServer(state)
defer ts.Close()
r, err := url.Parse(ts.URL)
require.NoError(t, err)
port, err := strconv.Atoi(r.Port())
require.NoError(t, err)
connector, err := NewConnector(
WithServerHostname("localhost"),
WithHTTPPath("/429-5-retries"),
WithPort(port),
WithRetries(2, 10*time.Millisecond, 1*time.Second),
)
require.NoError(t, err)
db := sql.OpenDB(connector)
defer db.Close()
state.executeStatementResp = cli_service.TExecuteStatementResp{}
loadTestData(t, "ExecuteStatement1.json", &state.executeStatementResp)
err = db.Ping()
require.ErrorContains(t, err, "after 3 attempt(s)")
// The error chain should contain a databricks request error
b := errors.Is(err, dbsqlerr.RequestError)
require.True(t, b)
var re dbsqlerr.DBRequestError
b = errors.As(err, &re)
require.True(t, b)
require.NotNil(t, re)
require.True(t, re.IsRetryable())
require.Equal(t, 12*time.Second, re.RetryAfter())
connector2, err := NewConnector(
WithServerHostname("localhost"),
WithHTTPPath("/503-5-retries"),
WithPort(port),
WithRetries(2, 10*time.Millisecond, 1*time.Second),
)
require.NoError(t, err)
db2 := sql.OpenDB(connector2)
defer db.Close()
state.executeStatementResp = cli_service.TExecuteStatementResp{}
loadTestData(t, "ExecuteStatement1.json", &state.executeStatementResp)
err = db2.Ping()
require.ErrorContains(t, err, "after 3 attempt(s)")
// The error chain should contain a databricks request error
b = errors.Is(err, dbsqlerr.RequestError)
require.True(t, b)
b = errors.As(err, &re)
require.True(t, b)
require.NotNil(t, re)
require.True(t, re.IsRetryable())
})
}
// TODO: add tests for x-databricks headers
func strPtr(s string) *string {
return &s
}
func loadTestData(t *testing.T, name string, v any) {
if f, err := os.ReadFile(fmt.Sprintf("testdata/%s", name)); err != nil {
t.Errorf("could not read data from: %s", name)
} else {
if err := json.Unmarshal(f, v); err != nil {
t.Errorf("could not load data from: %s", name)
}
}
}
type callState struct {
openSessionCalls int
openSessionResp cli_service.TOpenSessionResp
openSessionError error
closeSessionCalls int
closeSessionResp cli_service.TCloseSessionResp
closeSessionError error
executeStatementCalls int
executeStatementSleep time.Duration
executeStatementResp cli_service.TExecuteStatementResp
executeStatementError error
cancelOperationCalls int
cancelOperationResp cli_service.TCancelOperationResp
cancelOperationError error
closeOperationCalls int
closeOperationResp cli_service.TCloseOperationResp
closeOperationError error
fetchResultsCalls int
fetchResultsResp cli_service.TFetchResultsResp
fetchResultsError error
getResultSetMetadataCalls int
getResultSetMetadataResp cli_service.TGetResultSetMetadataResp
getResultSetMetadataError error
getOperationStatusCalls int
getOperationStatusResp cli_service.TGetOperationStatusResp
getOperationStatusError error
}
func getServer(state *callState) *httptest.Server {
return initThriftTestServer(&client.TestClient{
FnOpenSession: func(ctx context.Context, req *cli_service.TOpenSessionReq) (*cli_service.TOpenSessionResp, error) {
state.openSessionCalls++
return &state.openSessionResp, state.openSessionError
},
FnCloseSession: func(ctx context.Context, req *cli_service.TCloseSessionReq) (*cli_service.TCloseSessionResp, error) {
state.closeSessionCalls++
return &state.closeSessionResp, state.closeSessionError
},
FnExecuteStatement: func(ctx context.Context, req *cli_service.TExecuteStatementReq) (*cli_service.TExecuteStatementResp, error) {
state.executeStatementCalls++
if state.executeStatementSleep != 0 {
time.Sleep(state.executeStatementSleep)
}
return &state.executeStatementResp, state.executeStatementError
},
FnGetOperationStatus: func(ctx context.Context, req *cli_service.TGetOperationStatusReq) (*cli_service.TGetOperationStatusResp, error) {
state.getOperationStatusCalls++
return &state.getOperationStatusResp, state.getOperationStatusError
},
FnCloseOperation: func(ctx context.Context, req *cli_service.TCloseOperationReq) (*cli_service.TCloseOperationResp, error) {
state.closeOperationCalls++
return &state.closeOperationResp, state.closeOperationError
},
FnCancelOperation: func(ctx context.Context, req *cli_service.TCancelOperationReq) (*cli_service.TCancelOperationResp, error) {
state.cancelOperationCalls++
return &state.cancelOperationResp, state.cancelOperationError
},
FnFetchResults: func(ctx context.Context, req *cli_service.TFetchResultsReq) (*cli_service.TFetchResultsResp, error) {
state.fetchResultsCalls++
return &state.fetchResultsResp, state.fetchResultsError
},
FnGetResultSetMetadata: func(ctx context.Context, req *cli_service.TGetResultSetMetadataReq) (*cli_service.TGetResultSetMetadataResp, error) {
state.getResultSetMetadataCalls++
return &state.getResultSetMetadataResp, state.getResultSetMetadataError
},
})
}