@@ -24,6 +24,7 @@ import (
24
24
"os"
25
25
"reflect"
26
26
"runtime"
27
+ "strconv"
27
28
"strings"
28
29
"sync"
29
30
"sync/atomic"
@@ -3377,12 +3378,30 @@ func TestConnectionAttributes(t *testing.T) {
3377
3378
t .Skipf ("MySQL server not running on %s" , netAddr )
3378
3379
}
3379
3380
3380
- attr1 := "attr1"
3381
- value1 := "value1"
3382
- attr2 := "fo/o"
3383
- value2 := "bo/o"
3384
- dsn += "&connectionAttributes=" + url .QueryEscape (fmt .Sprintf ("%s:%s,%s:%s" , attr1 , value1 , attr2 , value2 ))
3381
+ defaultAttrs := []string {
3382
+ connAttrClientName ,
3383
+ connAttrOS ,
3384
+ connAttrPlatform ,
3385
+ connAttrPid ,
3386
+ connAttrServerHost ,
3387
+ }
3388
+ host , _ , _ := net .SplitHostPort (addr )
3389
+ defaultAttrValues := []string {
3390
+ connAttrClientNameValue ,
3391
+ connAttrOSValue ,
3392
+ connAttrPlatformValue ,
3393
+ strconv .Itoa (os .Getpid ()),
3394
+ host ,
3395
+ }
3396
+
3397
+ customAttrs := []string {"attr1" , "fo/o" }
3398
+ customAttrValues := []string {"value1" , "bo/o" }
3385
3399
3400
+ customAttrStrs := make ([]string , len (customAttrs ))
3401
+ for i := range customAttrs {
3402
+ customAttrStrs [i ] = fmt .Sprintf ("%s:%s" , customAttrs [i ], customAttrValues [i ])
3403
+ }
3404
+ dsn += "&connectionAttributes=" + url .QueryEscape (strings .Join (customAttrStrs , "," ))
3386
3405
3387
3406
var db * sql.DB
3388
3407
if _ , err := ParseDSN (dsn ); err != errInvalidDSNUnsafeCollation {
@@ -3395,40 +3414,24 @@ func TestConnectionAttributes(t *testing.T) {
3395
3414
3396
3415
dbt := & DBTest {t , db }
3397
3416
3398
- var attrValue string
3399
- queryString := "SELECT ATTR_VALUE FROM performance_schema.session_account_connect_attrs WHERE PROCESSLIST_ID = CONNECTION_ID() and ATTR_NAME = ?"
3400
- rows := dbt .mustQuery (queryString , connAttrClientName )
3401
- if rows .Next () {
3402
- rows .Scan (& attrValue )
3403
- if attrValue != connAttrClientNameValue {
3404
- dbt .Errorf ("expected %q, got %q" , connAttrClientNameValue , attrValue )
3405
- }
3406
- } else {
3407
- dbt .Errorf ("no data" )
3408
- }
3409
- rows .Close ()
3417
+ queryString := "SELECT ATTR_NAME, ATTR_VALUE FROM performance_schema.session_account_connect_attrs WHERE PROCESSLIST_ID = CONNECTION_ID()"
3418
+ rows := dbt .mustQuery (queryString )
3419
+ defer rows .Close ()
3410
3420
3411
- rows = dbt .mustQuery (queryString , attr1 )
3412
- if rows .Next () {
3413
- rows .Scan (& attrValue )
3414
- if attrValue != value1 {
3415
- dbt .Errorf ("expected %q, got %q" , value1 , attrValue )
3416
- }
3417
- } else {
3418
- dbt .Errorf ("no data" )
3421
+ rowsMap := make (map [string ]string )
3422
+ for rows .Next () {
3423
+ var attrName , attrValue string
3424
+ rows .Scan (& attrName , & attrValue )
3425
+ rowsMap [attrName ] = attrValue
3419
3426
}
3420
- rows .Close ()
3421
3427
3422
- rows = dbt . mustQuery ( queryString , attr2 )
3423
- if rows . Next () {
3424
- rows . Scan ( & attrValue )
3425
- if attrValue != value2 {
3426
- dbt .Errorf ("expected %q, got %q" , value2 , attrValue )
3428
+ connAttrs := append ( append ([] string {}, defaultAttrs ... ), customAttrs ... )
3429
+ expectedAttrValues := append ( append ([] string {}, defaultAttrValues ... ), customAttrValues ... )
3430
+ for i := range connAttrs {
3431
+ if gotValue := rowsMap [ connAttrs [ i ]]; gotValue != expectedAttrValues [ i ] {
3432
+ dbt .Errorf ("expected %q, got %q" , expectedAttrValues [ i ], gotValue )
3427
3433
}
3428
- } else {
3429
- dbt .Errorf ("no data" )
3430
3434
}
3431
- rows .Close ()
3432
3435
}
3433
3436
3434
3437
func TestErrorInMultiResult (t * testing.T ) {
0 commit comments