Skip to content

Commit 6903e12

Browse files
Fix client test race conditions
1 parent f748bb3 commit 6903e12

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

proxy/proxy/client_test.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,8 @@ func TestMaximumConnectionsCount(t *testing.T) {
194194
}
195195

196196
func TestRefreshTimer(t *testing.T) {
197-
refreshCertBuffer = time.Second
198-
timeToExpire := 2 * time.Second
197+
refreshCertBuffer = time.Millisecond * 10
198+
timeToExpire := time.Millisecond * 500
199199
b := &fakeCerts{}
200200
c := &Client{
201201
Certs: &blockingCertSource{
@@ -207,24 +207,35 @@ func TestRefreshTimer(t *testing.T) {
207207
Dialer: func(string, string) (net.Conn, error) {
208208
return nil, errFakeDial
209209
},
210-
RefreshCfgThrottle: 500 * time.Millisecond,
210+
RefreshCfgThrottle: 20 * time.Millisecond,
211211
}
212212

213213
// Call Dial to cache the cert.
214214
if _, err := c.Dial(instance); err != errFakeDial {
215215
t.Errorf("unexpected error: %v", err)
216216
}
217217

218+
c.cacheL.Lock()
218219
cached, ok := c.cfgCache[instance]
220+
c.cacheL.Unlock()
219221
if !ok {
220222
t.Error("expected instance to be cached")
221223
}
222-
223-
// Wait for cert to expire.
224-
time.Sleep(timeToExpire + time.Second)
224+
waitTil := time.After(timeToExpire + (10 * time.Millisecond))
225+
loop:
226+
for {
227+
select {
228+
case <-waitTil:
229+
break loop
230+
default:
231+
time.Sleep(100 * time.Millisecond)
232+
}
233+
}
225234

226235
// Verify cert was refreshed in the background, without calling Dial again.
236+
c.cacheL.Lock()
227237
refreshed, ok := c.cfgCache[instance]
238+
c.cacheL.Unlock()
228239
if !ok {
229240
t.Error("expected instance to be cached")
230241
}

0 commit comments

Comments
 (0)