66"net" 
77"strings" 
88"sync" 
9+ "sync/atomic" 
910"testing" 
1011"time" 
1112
@@ -18,18 +19,17 @@ import (
1819"github.com/go-mysql-org/go-mysql/mysql" 
1920"github.com/go-mysql-org/go-mysql/test_util" 
2021"github.com/go-mysql-org/go-mysql/test_util/test_keys" 
21- "github.com/go-mysql-org/go-mysql/utils" 
2222)
2323
24- var  delay  =  50 
25- 
2624// test caching for 'caching_sha2_password' 
2725// NOTE the idea here is to plugin a throttled credential provider so that the first connection (cache miss) will take longer time 
2826// than the second connection (cache hit). Remember to set the password for MySQL user otherwise it won't cache empty password. 
2927func  TestCachingSha2Cache (t  * testing.T ) {
3028log .SetLevel (log .LevelDebug )
3129
32- remoteProvider  :=  & RemoteThrottleProvider {NewInMemoryProvider (), delay  +  50 }
30+ remoteProvider  :=  & RemoteThrottleProvider {
31+ InMemoryProvider : NewInMemoryProvider (),
32+ }
3333remoteProvider .AddUser (* testUser , * testPassword )
3434cacheServer  :=  NewServer ("8.0.12" , mysql .DEFAULT_COLLATION_ID , mysql .AUTH_CACHING_SHA2_PASSWORD , test_keys .PubPem , tlsConf )
3535
@@ -44,7 +44,9 @@ func TestCachingSha2Cache(t *testing.T) {
4444func  TestCachingSha2CacheTLS (t  * testing.T ) {
4545log .SetLevel (log .LevelDebug )
4646
47- remoteProvider  :=  & RemoteThrottleProvider {NewInMemoryProvider (), delay  +  50 }
47+ remoteProvider  :=  & RemoteThrottleProvider {
48+ InMemoryProvider : NewInMemoryProvider (),
49+ }
4850remoteProvider .AddUser (* testUser , * testPassword )
4951cacheServer  :=  NewServer ("8.0.12" , mysql .DEFAULT_COLLATION_ID , mysql .AUTH_CACHING_SHA2_PASSWORD , test_keys .PubPem , tlsConf )
5052
@@ -58,11 +60,11 @@ func TestCachingSha2CacheTLS(t *testing.T) {
5860
5961type  RemoteThrottleProvider  struct  {
6062* InMemoryProvider 
61- delay   int   // in milliseconds 
63+ getCredCallCount  atomic. Int64 
6264}
6365
6466func  (m  * RemoteThrottleProvider ) GetCredential (username  string ) (password  string , found  bool , err  error ) {
65- time . Sleep ( time . Millisecond   *   time . Duration ( m . delay ) )
67+ m . getCredCallCount . Add ( 1 )
6668return  m .InMemoryProvider .GetCredential (username )
6769}
6870
@@ -132,35 +134,26 @@ func (s *cacheTestSuite) runSelect() {
132134
133135func  (s  * cacheTestSuite ) TestCache () {
134136// first connection 
135- t1  :=  utils .Now ()
136137var  err  error 
137138s .db , err  =  sql .Open ("mysql" , fmt .Sprintf ("%s:%s@tcp(%s)/%s?tls=%s" , * testUser , * testPassword , s .serverAddr , * testDB , s .tlsPara ))
138139require .NoError (s .T (), err )
139140s .db .SetMaxIdleConns (4 )
140141s .runSelect ()
141- t2  :=  utils .Now ()
142- 
143- d1  :=  int (t2 .Sub (t1 ).Nanoseconds () /  1e6 )
144- // log.Debugf("first connection took %d milliseconds", d1) 
145- 
146- require .GreaterOrEqual (s .T (), d1 , delay )
142+ got  :=  s .credProvider .(* RemoteThrottleProvider ).getCredCallCount .Load ()
143+ require .Equal (s .T (), int64 (1 ), got )
147144
148145if  s .db  !=  nil  {
149146s .db .Close ()
150147}
151148
152149// second connection 
153- t3  :=  utils .Now ()
154150s .db , err  =  sql .Open ("mysql" , fmt .Sprintf ("%s:%s@tcp(%s)/%s?tls=%s" , * testUser , * testPassword , s .serverAddr , * testDB , s .tlsPara ))
155151require .NoError (s .T (), err )
156152s .db .SetMaxIdleConns (4 )
157153s .runSelect ()
158- t4  :=  utils .Now ()
159- 
160- d2  :=  int (t4 .Sub (t3 ).Nanoseconds () /  1e6 )
161- // log.Debugf("second connection took %d milliseconds", d2) 
154+ got  =  s .credProvider .(* RemoteThrottleProvider ).getCredCallCount .Load ()
155+ require .Equal (s .T (), int64 (1 ), got )
162156
163- require .Less (s .T (), d2 , delay )
164157if  s .db  !=  nil  {
165158s .db .Close ()
166159}
0 commit comments