-
Notifications
You must be signed in to change notification settings - Fork 3
/
client_test.go
533 lines (455 loc) · 14 KB
/
client_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
package exasol
import (
"bytes"
"crypto/tls"
"fmt"
"net/url"
"os"
"regexp"
"strconv"
"strings"
"time"
"github.com/sirupsen/logrus"
)
// Test various connection options:
func (s *testSuite) TestConnClientName() {
conf := s.connConf()
conf.ClientName = "MyTester"
conf.ClientVersion = "123"
c, err := Connect(conf)
s.Nil(err, "No connection errors")
got, _ := c.FetchSlice(`
SELECT client
FROM exa_user_sessions
WHERE session_id = CURRENT_SESSION
`)
s.Equal("MyTester 123", got[0][0].(string), "Correctly set client name/version")
c.Disconnect()
}
func (s *testSuite) TestQueryTimeout() {
conf := s.connConf()
conf.SuppressError = true
conf.QueryTimeout = 5 * time.Second
c, err := Connect(conf)
s.Nil(err, "No connection errors")
c.Execute("OPEN SCHEMA " + s.qschema)
// First create a function that sleeps for 3 sec
// to simulate a long running query
c.Execute(`
CREATE OR REPLACE PYTHON3 SCALAR SCRIPT sleep("d" INTEGER) RETURNS INTEGER AS
import subprocess
def run(ctx):
subprocess.check_output("sleep " + str(ctx.d), shell=True)
return 123
`)
// Now run the script and verify that it didn't timeout
got, err := c.FetchSlice(`SELECT sleep(1)`)
s.Nil(err, "No query errors")
s.Equal(123.0, got[0][0].(float64), "Did not time out")
// Now run the script longer and verify that it aborted
_, err = c.FetchSlice(`SELECT sleep(10)`)
if s.Error(err) {
s.Contains(err.Error(), "timeout", "Got error")
}
// No need to disconnect because the server killed the connection
}
func (s *testSuite) TestConnectTimeout() {
conf := s.connConf()
conf.SuppressError = true
conf.ConnectTimeout = 5 * time.Second
// To test this properly you need to set the EXA_TIMEOUT_HOST ENV
// to a host+port that will result in a hanging connection.
env := os.Getenv("EXA_TIMEOUT_HOST")
if env == "" {
s.T().Skip("EXA_TIMEOUT_HOST must be set to 'host:port' in order for TestConnectTimeout to run.")
}
parts := strings.Split(env, ":")
conf.Host = parts[0]
port, _ := strconv.ParseUint(parts[1], 10, 64)
conf.Port = uint16(port)
timeIn := time.Now()
_, err := Connect(conf)
if s.Error(err) {
s.Contains(err.Error(), "Unable to connect", "Got error")
}
s.Less(time.Since(timeIn).Seconds(), conf.ConnectTimeout.Seconds()+1, "It timed out correctly")
s.Greater(time.Since(timeIn).Seconds(), conf.ConnectTimeout.Seconds()-1, "It did hang")
}
func (s *testSuite) TestConnSuppressError() {
conf := s.connConf()
output := &bytes.Buffer{}
logger := customTestLogger("error")
logger.SetOutput(output)
conf.Logger = logger
// First allow error logging
conf.SuppressError = false
c, _ := Connect(conf)
c.Execute("SELECT 1")
s.Equal(output.String(), "", "No error output")
c.Execute("ASDF")
s.Contains(output.String(), "syntax error", "Got error output")
c.Disconnect()
// Now suppress the error logging
output.Reset()
conf.SuppressError = true
c, _ = Connect(conf)
c.Execute("SELECT 1")
s.Equal(output.String(), "", "No error output")
c.Execute("ASDF")
s.Equal(output.String(), "", "Still no error output")
c.Disconnect()
}
func (s *testSuite) TestConnLogger() {
conf := s.connConf()
// First test that it works with no logger at all
conf.Logger = nil
c, err := Connect(conf)
s.Nil(err, "No error")
got, err := c.FetchSlice("SELECT 123")
s.Nil(err, "Still no error")
s.Equal(got[0][0].(float64), float64(123), "Everything OK")
got, err = c.FetchSlice("ASDF")
s.NotNil(err, "Got error")
s.Nil(got, "No results")
c.Disconnect()
// Now test with our own logger
output := &bytes.Buffer{}
logger := customTestLogger("error")
logger.SetOutput(output)
conf.Logger = logger
c, err = Connect(conf)
s.Nil(err, "No error")
got, err = c.FetchSlice("SELECT 123")
s.Nil(err, "Still no error")
s.Equal(got[0][0].(float64), float64(123), "Everything OK")
s.Equal(output.String(), "", "No error output")
got, err = c.FetchSlice("ASDF")
s.NotNil(err, "Got error")
s.Nil(got, "No results")
s.Contains(output.String(), "syntax error", "Got error output")
output.Reset()
// Turn on debugging
l, _ := logrus.ParseLevel("debug")
logger.SetLevel(l)
got, err = c.FetchSlice("SELECT 123")
s.Nil(err, "Still no error")
s.Equal(got[0][0].(float64), float64(123), "Everything OK")
s.Contains(output.String(), "Execute: SELECT 123", "Got debug output")
c.Disconnect()
}
func (s *testSuite) TestConnCachePrepStmt() {
conf := s.connConf()
// First try with cache disabled
conf.CachePrepStmts = false
c, _ := Connect(conf)
got, _ := c.FetchSlice("SELECT 123 FROM dual WHERE true = ?", []interface{}{true})
s.Equal(got[0][0].(float64), float64(123), "Everything OK")
s.Equal(c.Stats["StmtCacheLen"], 0, "Cache is empty")
s.Equal(c.Stats["StmtCacheMiss"], 0, "Cache miss not recorded")
c.Disconnect()
// Then try with cache enabled
conf.CachePrepStmts = true
c, _ = Connect(conf)
got, _ = c.FetchSlice("SELECT 123 FROM dual WHERE true = ?", []interface{}{true})
s.Equal(got[0][0].(float64), float64(123), "Everything OK")
s.Equal(c.Stats["StmtCacheLen"], 1, "Cache is not empty")
s.Equal(c.Stats["StmtCacheMiss"], 1, "Cache miss recorded")
got, _ = c.FetchSlice("SELECT 123 FROM dual WHERE true = ?", []interface{}{true})
s.Equal(got[0][0].(float64), float64(123), "Everything OK")
s.Equal(c.Stats["StmtCacheLen"], 1, "Cache is not empty")
s.Equal(c.Stats["StmtCacheMiss"], 1, "Cache miss not recorded")
c.Disconnect()
}
func (s *testSuite) TestHostRanges() {
conf := s.connConf()
conf.SuppressError = true // Set to false to see the random output
conf.Host = "127.0.0.1..3"
conf.Port = 1
for i := 0; i < 10; i++ {
c, err := Connect(conf)
s.Nil(c)
if s.Error(err) {
s.Regexp(regexp.MustCompile(`\b127\.0\.0\.(1|2|3)\b`), err.Error())
}
}
}
func (s *testSuite) TestConnErrors() {
// Connection error
conf := s.connConf()
conf.SuppressError = true
conf.Host = "-1"
c, err := Connect(conf)
s.Nil(c)
if s.Error(err) {
s.Contains(err.Error(), "Unable to connect")
}
// Authentication error
conf = s.connConf()
conf.SuppressError = true
conf.Username = ""
c, err = Connect(conf)
s.Nil(c)
if s.Error(err) {
s.Contains(err.Error(), "Unable to login")
}
}
// This also tests GetSessionAttr
func (s *testSuite) TestAutoCommit() {
exa := s.exaConn
got, _ := exa.GetSessionAttr()
s.Equal(true, got.Autocommit, "Autocommit defaults to true")
exa.DisableAutoCommit()
got, _ = exa.GetSessionAttr()
s.Equal(false, got.Autocommit, "Autocommit is disabled")
exa.FetchSlice("SELECT 1")
got, _ = exa.GetSessionAttr()
s.Equal(false, got.Autocommit, "Autocommit still disabled")
exa.EnableAutoCommit()
got, _ = exa.GetSessionAttr()
s.Equal(true, got.Autocommit, "Autocommit is enabled")
exa.FetchSlice("SELECT 1")
got, _ = exa.GetSessionAttr()
s.Equal(true, got.Autocommit, "Autocommit still enabled")
}
func (s *testSuite) TestCommitAndRollback() {
exa := s.exaConn
exa.DisableAutoCommit()
exa.Execute("CREATE TABLE foo ( id INT )")
exa.Commit()
exa.Execute("INSERT INTO foo VALUES (123)")
got, _ := exa.FetchSlice("SELECT id FROM foo")
s.Len(got, 1, "Data is there")
exa.Rollback()
got, _ = exa.FetchSlice("SELECT id FROM foo")
s.Len(got, 0, "The data is gone")
exa.Execute("INSERT INTO foo VALUES (123)")
got, _ = exa.FetchSlice("SELECT id FROM foo")
s.Len(got, 1, "The data is back again")
exa.Commit()
exa.Rollback()
got, _ = exa.FetchSlice("SELECT id FROM foo")
s.Len(got, 1, "Still there after rollback because of prior commit")
}
func (s *testSuite) TestSessionID() {
exa := s.exaConn
sesh, _ := exa.FetchSlice("SELECT CURRENT_SESSION")
s.Equal(sesh[0][0].(string), fmt.Sprintf("%d", exa.SessionID), "SessionID is correct")
s.Equal(sesh[0][0].(string), fmt.Sprintf("%d", exa.Metadata.SessionID), "SessionID in metadata is correct")
}
func (s *testSuite) TestExecute() {
exa := s.exaConn
exa.Conf.SuppressError = true
exa.Execute("CREATE TABLE foo ( id INT, val CHAR(1) )")
exa.Commit()
// Generate an error
got, err := exa.Execute("ASDF")
if s.Error(err) {
s.Contains(err.Error(), "syntax error")
}
s.Equal(int64(0), got)
// Successful, no binds
got, err = exa.Execute("INSERT INTO foo VALUES (1,'a'),(2,'b'),(3,'c')")
s.Nil(err)
s.Equal(int64(3), got)
// With []interface{} binds
got, err = exa.Execute("INSERT INTO foo VALUES (?,?)", []interface{}{1, "a"})
s.Nil(err)
s.Equal(int64(1), got)
// With [][]interface{} binds
got, err = exa.Execute("INSERT INTO foo VALUES (?,?)", [][]interface{}{{1, "a"}, {2, "b"}})
s.Nil(err)
s.Equal(int64(2), got)
// With default schema
exa.Execute("OPEN SCHEMA sys")
got, err = exa.Execute("INSERT INTO foo VALUES (1,'a')") // This should fail
if s.Error(err) {
s.Contains(err.Error(), "not found")
}
got, err = exa.Execute("INSERT INTO foo VALUES (1,'a')", nil, s.schema) // This should work
s.Nil(err)
s.Equal(int64(1), got)
// With column data types
got, err = exa.Execute(
"INSERT INTO foo VALUES (?,?)",
[]interface{}{1, "a"},
nil,
[]DataType{
{Type: "DECIMAL", Precision: 10},
{Type: "CHAR", Size: 1},
},
)
s.Nil(err)
s.Equal(int64(1), got)
// With isColumnar flag
got, err = exa.Execute(
"INSERT INTO foo VALUES (?,?)",
[][]interface{}{{1, 2, 3}, {"a", "b", "c"}},
nil, nil, false,
) // This should fail because the data is columnar and the flag is false
if s.Error(err) {
s.Contains(err.Error(), "number of column metadata objects is not the same")
}
got, err = exa.Execute(
"INSERT INTO foo VALUES (?,?)",
[][]interface{}{{1, 2, 3}, {"a", "b", "c"}},
nil, nil, true,
) // This should work
s.Nil(err)
s.Equal(int64(3), got)
}
func (s *testSuite) TestFetchChan() {
exa := s.exaConn
exa.Conf.SuppressError = true
exa.Execute("CREATE TABLE foo ( id INT, val CHAR(1) )")
exa.Execute(
"INSERT INTO foo VALUES (?,?)",
[][]interface{}{{1, 2, 3}, {"a", "b", "c"}},
nil, nil, true,
)
// First an error
got, err := exa.FetchChan("ASDF")
if s.Error(err) {
s.Contains(err.Error(), "syntax error")
}
s.Nil(got)
// Successful, no binds
got, err = exa.FetchChan("SELECT * FROM foo WHERE id < 3 ORDER BY id")
if s.NoError(err) {
var res [][]interface{}
for row := range got {
res = append(res, row)
}
expect := [][]interface{}{
{float64(1), "a"},
{float64(2), "b"},
}
s.Equal(expect, res)
}
// Successful, with binds
got, err = exa.FetchChan("SELECT * FROM foo WHERE id < ? ORDER BY id", []interface{}{3})
if s.NoError(err) {
var res [][]interface{}
for row := range got {
res = append(res, row)
}
expect := [][]interface{}{
{float64(1), "a"},
{float64(2), "b"},
}
s.Equal(expect, res)
}
// Successful, with schema
_, err = exa.Execute("OPEN SCHEMA sys")
s.Nil(err)
got, err = exa.FetchChan("SELECT * FROM foo WHERE id < ? ORDER BY id", []interface{}{3}, s.schema)
if s.NoError(err) {
var res [][]interface{}
for row := range got {
res = append(res, row)
}
expect := [][]interface{}{
{float64(1), "a"},
{float64(2), "b"},
}
s.Equal(expect, res)
}
}
func (s *testSuite) TestFetchSlice() {
exa := s.exaConn
exa.Execute("CREATE TABLE foo ( id INT, val CHAR(1) )")
exa.Execute(
"INSERT INTO foo VALUES (?,?)",
[][]interface{}{{1, 2, 3}, {"a", "b", "c"}},
nil, nil, true,
)
// First an error
exa.Conf.SuppressError = true
got, err := exa.FetchSlice("ASDF")
if s.Error(err) {
s.Contains(err.Error(), "syntax error")
}
s.Nil(got)
got, err = exa.FetchSlice("SELECT * FROM foo WHERE id < 3 ORDER BY id")
if s.NoError(err) {
expect := [][]interface{}{
{float64(1), "a"},
{float64(2), "b"},
}
s.Equal(expect, got)
}
// Test No results at all
got, err = exa.FetchSlice("SELECT * FROM foo WHERE FALSE")
if s.NoError(err) {
var exp [][]interface{}
s.Equal(exp, got)
}
}
func (s *testSuite) TestLargeFetch() {
// This results in a payload > 64MB but < 1000 rows which triggers
// result handles but still has data in the initial response
val := strings.Repeat("x", 2000000)
payload := [][]interface{}{{}, {}}
for i := 0; i < 100; i++ {
payload[0] = append(payload[0], float64(i))
payload[1] = append(payload[1], val)
}
exa := s.exaConn
exa.Execute("CREATE TABLE foo ( id INT, val VARCHAR(2000000) )")
exa.Execute("INSERT INTO foo VALUES (?,?)", payload, nil, nil, true)
got, err := exa.FetchSlice("SELECT * FROM foo ORDER BY id")
if s.NoError(err) {
s.Equal(Transpose(payload), got)
}
// This results in a payload < 64MB but > 1000 rows which triggers
// result handles but and no data in the initial response
payload = [][]interface{}{{}, {}}
for i := 0; i < 2500; i++ {
payload[0] = append(payload[0], float64(i))
payload[1] = append(payload[1], "a")
}
exa.Execute("CREATE OR REPLACE TABLE foo ( id INT, val CHAR(1) )")
exa.Execute("INSERT INTO foo VALUES (?,?)", payload, nil, nil, true)
got, err = exa.FetchSlice("SELECT * FROM foo ORDER BY id")
if s.NoError(err) {
s.Equal(Transpose(payload), got)
}
}
func (s *testSuite) TestSetTimeout() {
conf := s.connConf()
conf.QueryTimeout = 5 * time.Second
c, err := Connect(conf)
s.Nil(err)
attr, err := c.GetSessionAttr()
s.Nil(err)
s.Equal(uint32(5), attr.QueryTimeout)
err = c.SetTimeout(10)
s.Nil(err)
attr, err = c.GetSessionAttr()
s.Nil(err)
s.Equal(uint32(10), attr.QueryTimeout)
}
func (s *testSuite) TestHashTypeInsert() {
// This insert fails with Exasol v8 + websocket API v1
exa := s.exaConn
exa.Execute("CREATE TABLE foo (ht HASHTYPE)")
got, err := exa.Execute("INSERT INTO foo VALUES (?)", []interface{}{"00000000000000000000000000000000"})
s.Nil(err)
s.Equal(int64(1), got)
}
type testWSHandler struct{}
func (wsh *testWSHandler) Connect(u url.URL, s *tls.Config, t time.Duration) error {
return fmt.Errorf("Connecting in test handler")
}
func (wsh *testWSHandler) WriteJSON(req interface{}) error { return nil }
func (wsh *testWSHandler) ReadJSON(resp interface{}) error { return nil }
func (wsh *testWSHandler) EnableCompression(e bool) {}
func (wsh *testWSHandler) Close() {}
func (s *testSuite) TestWSHandler() {
conf := s.connConf()
conf.SuppressError = true
conf.WSHandler = &testWSHandler{}
_, err := Connect(conf)
if s.Error(err) {
s.Contains(err.Error(), "Connecting in test handler", "Got error")
}
}