Skip to content

Commit b39e40d

Browse files
lance6716田原
and
田原
authored
test: remove time-based checking (#1002)
* test: remove time-based checking Signed-off-by: 田原 <[email protected]> * address comment Signed-off-by: lance6716 <[email protected]> --------- Signed-off-by: 田原 <[email protected]> Signed-off-by: lance6716 <[email protected]> Co-authored-by: 田原 <[email protected]>
1 parent f53dd22 commit b39e40d

File tree

1 file changed

+13
-20
lines changed

1 file changed

+13
-20
lines changed

server/caching_sha2_cache_test.go

+13-20
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
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.
2927
func TestCachingSha2Cache(t *testing.T) {
3028
log.SetLevel(log.LevelDebug)
3129

32-
remoteProvider := &RemoteThrottleProvider{NewInMemoryProvider(), delay + 50}
30+
remoteProvider := &RemoteThrottleProvider{
31+
InMemoryProvider: NewInMemoryProvider(),
32+
}
3333
remoteProvider.AddUser(*testUser, *testPassword)
3434
cacheServer := 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) {
4444
func TestCachingSha2CacheTLS(t *testing.T) {
4545
log.SetLevel(log.LevelDebug)
4646

47-
remoteProvider := &RemoteThrottleProvider{NewInMemoryProvider(), delay + 50}
47+
remoteProvider := &RemoteThrottleProvider{
48+
InMemoryProvider: NewInMemoryProvider(),
49+
}
4850
remoteProvider.AddUser(*testUser, *testPassword)
4951
cacheServer := 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

5961
type RemoteThrottleProvider struct {
6062
*InMemoryProvider
61-
delay int // in milliseconds
63+
getCredCallCount atomic.Int64
6264
}
6365

6466
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)
6668
return m.InMemoryProvider.GetCredential(username)
6769
}
6870

@@ -132,35 +134,26 @@ func (s *cacheTestSuite) runSelect() {
132134

133135
func (s *cacheTestSuite) TestCache() {
134136
// first connection
135-
t1 := utils.Now()
136137
var err error
137138
s.db, err = sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s)/%s?tls=%s", *testUser, *testPassword, s.serverAddr, *testDB, s.tlsPara))
138139
require.NoError(s.T(), err)
139140
s.db.SetMaxIdleConns(4)
140141
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)
147144

148145
if s.db != nil {
149146
s.db.Close()
150147
}
151148

152149
// second connection
153-
t3 := utils.Now()
154150
s.db, err = sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s)/%s?tls=%s", *testUser, *testPassword, s.serverAddr, *testDB, s.tlsPara))
155151
require.NoError(s.T(), err)
156152
s.db.SetMaxIdleConns(4)
157153
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)
162156

163-
require.Less(s.T(), d2, delay)
164157
if s.db != nil {
165158
s.db.Close()
166159
}

0 commit comments

Comments
 (0)