|
1 | 1 | package connection_pool_test
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "fmt" |
4 | 5 | "log"
|
5 | 6 | "os"
|
| 7 | + "reflect" |
6 | 8 | "strings"
|
7 | 9 | "testing"
|
8 | 10 | "time"
|
@@ -1276,6 +1278,97 @@ func TestDo(t *testing.T) {
|
1276 | 1278 | require.NotNilf(t, resp, "response is nil after Ping")
|
1277 | 1279 | }
|
1278 | 1280 |
|
| 1281 | +func TestNewPrepared(t *testing.T) { |
| 1282 | + test_helpers.SkipIfSQLUnsupported(t) |
| 1283 | + |
| 1284 | + roles := []bool{true, true, false, true, false} |
| 1285 | + |
| 1286 | + err := test_helpers.SetClusterRO(servers, connOpts, roles) |
| 1287 | + require.Nilf(t, err, "fail to set roles for cluster") |
| 1288 | + |
| 1289 | + connPool, err := connection_pool.Connect(servers, connOpts) |
| 1290 | + require.Nilf(t, err, "failed to connect") |
| 1291 | + require.NotNilf(t, connPool, "conn is nil after Connect") |
| 1292 | + |
| 1293 | + defer connPool.Close() |
| 1294 | + |
| 1295 | + stmt, err := connPool.NewPrepared("SELECT NAME0, NAME1 FROM SQL_TEST WHERE NAME0=:id AND NAME1=:name;", connection_pool.RO) |
| 1296 | + require.Nilf(t, err, "fail to prepare statement: %v", err) |
| 1297 | + |
| 1298 | + if connPool.GetPoolInfo()[stmt.Conn.Addr()].ConnRole != connection_pool.RO { |
| 1299 | + t.Errorf("wrong role for the statement's connection") |
| 1300 | + } |
| 1301 | + |
| 1302 | + executeReq := tarantool.NewExecutePreparedRequest(stmt) |
| 1303 | + unprepareReq := tarantool.NewUnprepareRequest(stmt) |
| 1304 | + |
| 1305 | + resp, err := connPool.Do(executeReq.Args([]interface{}{1, "test"}), connection_pool.ANY).Get() |
| 1306 | + if err != nil { |
| 1307 | + t.Fatalf("failed to execute prepared: %v", err) |
| 1308 | + } |
| 1309 | + if resp == nil { |
| 1310 | + t.Fatalf("nil response") |
| 1311 | + } |
| 1312 | + if resp.Code != tarantool.OkCode { |
| 1313 | + t.Fatalf("failed to execute prepared: code %d", resp.Code) |
| 1314 | + } |
| 1315 | + if reflect.DeepEqual(resp.Data[0], []interface{}{1, "test"}) { |
| 1316 | + t.Error("Select with named arguments failed") |
| 1317 | + } |
| 1318 | + if resp.MetaData[0].FieldType != "unsigned" || |
| 1319 | + resp.MetaData[0].FieldName != "NAME0" || |
| 1320 | + resp.MetaData[1].FieldType != "string" || |
| 1321 | + resp.MetaData[1].FieldName != "NAME1" { |
| 1322 | + t.Error("Wrong metadata") |
| 1323 | + } |
| 1324 | + |
| 1325 | + // the second argument for unprepare request is unused - it already belongs to some connection |
| 1326 | + resp, err = connPool.Do(unprepareReq, connection_pool.ANY).Get() |
| 1327 | + if err != nil { |
| 1328 | + t.Errorf("failed to unprepare prepared statement: %v", err) |
| 1329 | + } |
| 1330 | + if resp.Code != tarantool.OkCode { |
| 1331 | + t.Errorf("failed to unprepare prepared statement: code %d", resp.Code) |
| 1332 | + } |
| 1333 | + |
| 1334 | + _, err = connPool.Do(unprepareReq, connection_pool.ANY).Get() |
| 1335 | + if err == nil { |
| 1336 | + t.Errorf("the statement must be already unprepared") |
| 1337 | + } |
| 1338 | + require.Contains(t, err.Error(), "Prepared statement with id") |
| 1339 | + |
| 1340 | + _, err = connPool.Do(executeReq, connection_pool.ANY).Get() |
| 1341 | + if err == nil { |
| 1342 | + t.Errorf("the statement must be already unprepared") |
| 1343 | + } |
| 1344 | + require.Contains(t, err.Error(), "Prepared statement with id") |
| 1345 | +} |
| 1346 | + |
| 1347 | +func TestDoWithStrangerConn(t *testing.T) { |
| 1348 | + expectedErr := fmt.Errorf("the passed connected request doesn't belong to the current connection or connection pool") |
| 1349 | + |
| 1350 | + roles := []bool{true, true, false, true, false} |
| 1351 | + |
| 1352 | + err := test_helpers.SetClusterRO(servers, connOpts, roles) |
| 1353 | + require.Nilf(t, err, "fail to set roles for cluster") |
| 1354 | + |
| 1355 | + connPool, err := connection_pool.Connect(servers, connOpts) |
| 1356 | + require.Nilf(t, err, "failed to connect") |
| 1357 | + require.NotNilf(t, connPool, "conn is nil after Connect") |
| 1358 | + |
| 1359 | + defer connPool.Close() |
| 1360 | + |
| 1361 | + req := test_helpers.NewStrangerRequest() |
| 1362 | + |
| 1363 | + _, err = connPool.Do(req, connection_pool.ANY).Get() |
| 1364 | + if err == nil { |
| 1365 | + t.Fatalf("nil error catched") |
| 1366 | + } |
| 1367 | + if err.Error() != expectedErr.Error() { |
| 1368 | + t.Fatalf("Unexpected error catched") |
| 1369 | + } |
| 1370 | +} |
| 1371 | + |
1279 | 1372 | // runTestMain is a body of TestMain function
|
1280 | 1373 | // (see https://pkg.go.dev/testing#hdr-Main).
|
1281 | 1374 | // Using defer + os.Exit is not works so TestMain body
|
|
0 commit comments