Skip to content

Commit 951532d

Browse files
committed
Fix race condition in flaky certificate/CA reload tests.
1 parent bfc153c commit 951532d

File tree

3 files changed

+12
-14
lines changed

3 files changed

+12
-14
lines changed

certificate_reloader_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ func UpdateCertificateCheckIntervalForTest(t *testing.T, interval time.Duration)
3939
deduplicateWatchEvents.Store(int64(interval))
4040
}
4141

42-
func (r *CertificateReloader) WaitForReload(ctx context.Context) error {
43-
counter := r.GetReloadCounter()
42+
func (r *CertificateReloader) WaitForReload(ctx context.Context, counter uint64) error {
4443
for counter == r.GetReloadCounter() {
4544
if err := ctx.Err(); err != nil {
4645
return err
@@ -50,8 +49,7 @@ func (r *CertificateReloader) WaitForReload(ctx context.Context) error {
5049
return nil
5150
}
5251

53-
func (r *CertPoolReloader) WaitForReload(ctx context.Context) error {
54-
counter := r.GetReloadCounter()
52+
func (r *CertPoolReloader) WaitForReload(ctx context.Context, counter uint64) error {
5553
for counter == r.GetReloadCounter() {
5654
if err := ctx.Err(); err != nil {
5755
return err

grpc_common_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,20 @@ import (
3939
"github.com/stretchr/testify/require"
4040
)
4141

42-
func (c *reloadableCredentials) WaitForCertificateReload(ctx context.Context) error {
42+
func (c *reloadableCredentials) WaitForCertificateReload(ctx context.Context, counter uint64) error {
4343
if c.loader == nil {
4444
return errors.New("no certificate loaded")
4545
}
4646

47-
return c.loader.WaitForReload(ctx)
47+
return c.loader.WaitForReload(ctx, counter)
4848
}
4949

50-
func (c *reloadableCredentials) WaitForCertPoolReload(ctx context.Context) error {
50+
func (c *reloadableCredentials) WaitForCertPoolReload(ctx context.Context, counter uint64) error {
5151
if c.pool == nil {
5252
return errors.New("no certificate pool loaded")
5353
}
5454

55-
return c.pool.WaitForReload(ctx)
55+
return c.pool.WaitForReload(ctx, counter)
5656
}
5757

5858
func GenerateSelfSignedCertificateForTesting(t *testing.T, bits int, organization string, key *rsa.PrivateKey) []byte {

grpc_server_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,22 @@ import (
4343
"google.golang.org/grpc/credentials"
4444
)
4545

46-
func (s *GrpcServer) WaitForCertificateReload(ctx context.Context) error {
46+
func (s *GrpcServer) WaitForCertificateReload(ctx context.Context, counter uint64) error {
4747
c, ok := s.creds.(*reloadableCredentials)
4848
if !ok {
4949
return errors.New("no reloadable credentials found")
5050
}
5151

52-
return c.WaitForCertificateReload(ctx)
52+
return c.WaitForCertificateReload(ctx, counter)
5353
}
5454

55-
func (s *GrpcServer) WaitForCertPoolReload(ctx context.Context) error {
55+
func (s *GrpcServer) WaitForCertPoolReload(ctx context.Context, counter uint64) error {
5656
c, ok := s.creds.(*reloadableCredentials)
5757
if !ok {
5858
return errors.New("no reloadable credentials found")
5959
}
6060

61-
return c.WaitForCertPoolReload(ctx)
61+
return c.WaitForCertPoolReload(ctx, counter)
6262
}
6363

6464
func NewGrpcServerForTestWithConfig(t *testing.T, config *goconf.ConfigFile) (server *GrpcServer, addr string) {
@@ -145,7 +145,7 @@ func Test_GrpcServer_ReloadCerts(t *testing.T) {
145145
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
146146
defer cancel()
147147

148-
require.NoError(server.WaitForCertificateReload(ctx))
148+
require.NoError(server.WaitForCertificateReload(ctx, 0))
149149

150150
cp2 := x509.NewCertPool()
151151
if !cp2.AppendCertsFromPEM(cert2) {
@@ -225,7 +225,7 @@ func Test_GrpcServer_ReloadCA(t *testing.T) {
225225
clientCert2 := GenerateSelfSignedCertificateForTesting(t, 1024, org2, clientKey)
226226
replaceFile(t, caFile, clientCert2, 0755)
227227

228-
require.NoError(server.WaitForCertPoolReload(ctx1))
228+
require.NoError(server.WaitForCertPoolReload(ctx1, 0))
229229

230230
pair2, err := tls.X509KeyPair(clientCert2, pem.EncodeToMemory(&pem.Block{
231231
Type: "RSA PRIVATE KEY",

0 commit comments

Comments
 (0)