6
6
"net"
7
7
"strings"
8
8
"sync"
9
+ "sync/atomic"
9
10
"testing"
10
11
"time"
11
12
@@ -18,18 +19,17 @@ import (
18
19
"github.com/go-mysql-org/go-mysql/mysql"
19
20
"github.com/go-mysql-org/go-mysql/test_util"
20
21
"github.com/go-mysql-org/go-mysql/test_util/test_keys"
21
- "github.com/go-mysql-org/go-mysql/utils"
22
22
)
23
23
24
- var delay = 50
25
-
26
24
// test caching for 'caching_sha2_password'
27
25
// NOTE the idea here is to plugin a throttled credential provider so that the first connection (cache miss) will take longer time
28
26
// than the second connection (cache hit). Remember to set the password for MySQL user otherwise it won't cache empty password.
29
27
func TestCachingSha2Cache (t * testing.T ) {
30
28
log .SetLevel (log .LevelDebug )
31
29
32
- remoteProvider := & RemoteThrottleProvider {NewInMemoryProvider (), delay + 50 }
30
+ remoteProvider := & RemoteThrottleProvider {
31
+ InMemoryProvider : NewInMemoryProvider (),
32
+ }
33
33
remoteProvider .AddUser (* testUser , * testPassword )
34
34
cacheServer := NewServer ("8.0.12" , mysql .DEFAULT_COLLATION_ID , mysql .AUTH_CACHING_SHA2_PASSWORD , test_keys .PubPem , tlsConf )
35
35
@@ -44,7 +44,9 @@ func TestCachingSha2Cache(t *testing.T) {
44
44
func TestCachingSha2CacheTLS (t * testing.T ) {
45
45
log .SetLevel (log .LevelDebug )
46
46
47
- remoteProvider := & RemoteThrottleProvider {NewInMemoryProvider (), delay + 50 }
47
+ remoteProvider := & RemoteThrottleProvider {
48
+ InMemoryProvider : NewInMemoryProvider (),
49
+ }
48
50
remoteProvider .AddUser (* testUser , * testPassword )
49
51
cacheServer := NewServer ("8.0.12" , mysql .DEFAULT_COLLATION_ID , mysql .AUTH_CACHING_SHA2_PASSWORD , test_keys .PubPem , tlsConf )
50
52
@@ -58,11 +60,11 @@ func TestCachingSha2CacheTLS(t *testing.T) {
58
60
59
61
type RemoteThrottleProvider struct {
60
62
* InMemoryProvider
61
- delay int // in milliseconds
63
+ getCredCallCount atomic. Int64
62
64
}
63
65
64
66
func (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 )
66
68
return m .InMemoryProvider .GetCredential (username )
67
69
}
68
70
@@ -132,35 +134,26 @@ func (s *cacheTestSuite) runSelect() {
132
134
133
135
func (s * cacheTestSuite ) TestCache () {
134
136
// first connection
135
- t1 := utils .Now ()
136
137
var err error
137
138
s .db , err = sql .Open ("mysql" , fmt .Sprintf ("%s:%s@tcp(%s)/%s?tls=%s" , * testUser , * testPassword , s .serverAddr , * testDB , s .tlsPara ))
138
139
require .NoError (s .T (), err )
139
140
s .db .SetMaxIdleConns (4 )
140
141
s .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 )
147
144
148
145
if s .db != nil {
149
146
s .db .Close ()
150
147
}
151
148
152
149
// second connection
153
- t3 := utils .Now ()
154
150
s .db , err = sql .Open ("mysql" , fmt .Sprintf ("%s:%s@tcp(%s)/%s?tls=%s" , * testUser , * testPassword , s .serverAddr , * testDB , s .tlsPara ))
155
151
require .NoError (s .T (), err )
156
152
s .db .SetMaxIdleConns (4 )
157
153
s .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 )
162
156
163
- require .Less (s .T (), d2 , delay )
164
157
if s .db != nil {
165
158
s .db .Close ()
166
159
}
0 commit comments