@@ -24,6 +24,7 @@ import (
2424 "os"
2525 "reflect"
2626 "runtime"
27+ "strconv"
2728 "strings"
2829 "sync"
2930 "sync/atomic"
@@ -3377,12 +3378,30 @@ func TestConnectionAttributes(t *testing.T) {
33773378 t .Skipf ("MySQL server not running on %s" , netAddr )
33783379 }
33793380
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" }
33853399
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 , "," ))
33863405
33873406 var db * sql.DB
33883407 if _ , err := ParseDSN (dsn ); err != errInvalidDSNUnsafeCollation {
@@ -3395,40 +3414,24 @@ func TestConnectionAttributes(t *testing.T) {
33953414
33963415 dbt := & DBTest {t , db }
33973416
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 ()
34103420
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
34193426 }
3420- rows .Close ()
34213427
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 )
34273433 }
3428- } else {
3429- dbt .Errorf ("no data" )
34303434 }
3431- rows .Close ()
34323435}
34333436
34343437func TestErrorInMultiResult (t * testing.T ) {
0 commit comments