Skip to content

Commit 83edf6d

Browse files
refactor: modernize sync/atomic usage with typed atomic apis (#3269)
Signed-off-by: chuanshanjida <chuanshanjida@outlook.com>
1 parent 2eb15bd commit 83edf6d

2 files changed

Lines changed: 12 additions & 12 deletions

File tree

pkg/signer/aws/signer_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,20 +154,20 @@ func TestSign_RetryBehavior(t *testing.T) {
154154
t.Run(name, func(t *testing.T) {
155155
_, der := generateTestEd25519DER(t)
156156

157-
var calls int32
157+
var calls atomic.Int32
158158
signer := newTestSigner(t, &mockKMSClient{
159159
keyID: awsTestKeyID,
160160
pubKeyDER: der,
161161
signFn: func(_ context.Context, _ *kms.SignInput) (*kms.SignOutput, error) {
162-
atomic.AddInt32(&calls, 1)
162+
calls.Add(1)
163163
return nil, spec.signErr
164164
},
165165
}, spec.opts)
166166

167167
_, err := signer.Sign(t.Context(), []byte("hello world"))
168168
require.Error(t, err)
169169
assert.Contains(t, err.Error(), spec.errSubstr)
170-
assert.Equal(t, spec.expectedCall, atomic.LoadInt32(&calls))
170+
assert.Equal(t, spec.expectedCall, calls.Load())
171171
})
172172
}
173173
}

pkg/signer/gcp/signer_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -164,20 +164,20 @@ func TestSign_RetryBehavior(t *testing.T) {
164164
t.Run(name, func(t *testing.T) {
165165
_, publicKeyPEM := generateTestEd25519PEM(t)
166166

167-
var calls int32
167+
var calls atomic.Int32
168168
signer := newTestSigner(t, &mockKMSClient{
169169
keyID: myKeyID,
170170
publicKeyPEM: publicKeyPEM,
171171
signFn: func(_ context.Context, _ *kmspb.AsymmetricSignRequest) (*kmspb.AsymmetricSignResponse, error) {
172-
atomic.AddInt32(&calls, 1)
172+
calls.Add(1)
173173
return nil, spec.signErr
174174
},
175175
}, spec.opts)
176176

177177
_, err := signer.Sign(t.Context(), []byte("hello world"))
178178
require.Error(t, err)
179179
assert.Contains(t, err.Error(), spec.errSubstr)
180-
assert.Equal(t, spec.expectedCall, atomic.LoadInt32(&calls))
180+
assert.Equal(t, spec.expectedCall, calls.Load())
181181
})
182182
}
183183
}
@@ -228,34 +228,34 @@ func TestSign_IntegrityFailures_RetryAndFail(t *testing.T) {
228228
t.Run(name, func(t *testing.T) {
229229
_, publicKeyPEM := generateTestEd25519PEM(t)
230230

231-
var calls int32
231+
var calls atomic.Int32
232232
signer := newTestSigner(t, &mockKMSClient{
233233
keyID: myKeyID,
234234
publicKeyPEM: publicKeyPEM,
235235
signFn: func(_ context.Context, _ *kmspb.AsymmetricSignRequest) (*kmspb.AsymmetricSignResponse, error) {
236-
atomic.AddInt32(&calls, 1)
236+
calls.Add(1)
237237
return spec.responseFn(), nil
238238
},
239239
}, &Options{MaxRetries: 1})
240240

241241
_, err := signer.Sign(t.Context(), []byte("hello world"))
242242
require.Error(t, err)
243243
assert.Contains(t, err.Error(), spec.expectedErrSubstr)
244-
assert.Equal(t, int32(2), atomic.LoadInt32(&calls), "integrity failures should be retried")
244+
assert.Equal(t, int32(2), calls.Load(), "integrity failures should be retried")
245245
})
246246
}
247247
}
248248

249249
func TestSign_IntegrityCheckRecoversOnRetry(t *testing.T) {
250250
_, publicKeyPEM := generateTestEd25519PEM(t)
251251

252-
var calls int32
252+
var calls atomic.Int32
253253
expectedSig := []byte("valid-signature")
254254
mock := &mockKMSClient{
255255
keyID: myKeyID,
256256
publicKeyPEM: publicKeyPEM,
257257
signFn: func(_ context.Context, _ *kmspb.AsymmetricSignRequest) (*kmspb.AsymmetricSignResponse, error) {
258-
attempt := atomic.AddInt32(&calls, 1)
258+
attempt := calls.Add(1)
259259
if attempt == 1 {
260260
return &kmspb.AsymmetricSignResponse{
261261
Name: myKeyID,
@@ -278,7 +278,7 @@ func TestSign_IntegrityCheckRecoversOnRetry(t *testing.T) {
278278
got, err := s.Sign(t.Context(), []byte("hello world"))
279279
require.NoError(t, err)
280280
assert.Equal(t, expectedSig, got)
281-
assert.Equal(t, int32(2), atomic.LoadInt32(&calls), "second attempt should succeed")
281+
assert.Equal(t, int32(2), calls.Load(), "second attempt should succeed")
282282
}
283283

284284
func TestRetryBackoff_Capped(t *testing.T) {

0 commit comments

Comments
 (0)