Skip to content

Commit d92bcc8

Browse files
committed
fix tests race
Signed-off-by: Angelo De Caro <adc@zurich.ibm.com>
1 parent a7d27dc commit d92bcc8

1 file changed

Lines changed: 12 additions & 16 deletions

File tree

token/services/identity/idemix/cache/cache_test.go

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"context"
1111
"errors"
1212
"sync"
13+
"sync/atomic"
1314
"testing"
1415
"time"
1516

@@ -100,9 +101,9 @@ func TestFetchIdentityFromBackendError(t *testing.T) {
100101

101102
// TestFetchIdentityFromCacheTimeout verifies on-demand generation after cache timeout.
102103
func TestFetchIdentityFromCacheTimeout(t *testing.T) {
103-
callCount := make(chan struct{}, 10)
104+
var callCount atomic.Int32
104105
c := NewIdentityCache(func(ctx context.Context, auditInfo []byte) (*idriver.IdentityDescriptor, error) {
105-
callCount <- struct{}{}
106+
callCount.Add(1)
106107
// Simulate slow backend - not strictly needed for the test
107108
// time.Sleep(10 * time.Millisecond)
108109
return &idriver.IdentityDescriptor{
@@ -118,7 +119,7 @@ func TestFetchIdentityFromCacheTimeout(t *testing.T) {
118119
require.NoError(t, err)
119120
assert.Equal(t, driver.Identity([]byte("timeout identity")), identityDescriptor.Identity)
120121
assert.Equal(t, []byte("timeout audit"), identityDescriptor.AuditInfo)
121-
assert.Len(t, callCount, 1)
122+
assert.Equal(t, int32(1), callCount.Load())
122123
}
123124

124125
// TestFetchIdentityFromCacheTimeoutError verifies error handling after cache timeout.
@@ -139,13 +140,13 @@ func TestFetchIdentityFromCacheTimeoutError(t *testing.T) {
139140

140141
// TestProvisionIdentitiesError verifies provisioning retries after errors.
141142
func TestProvisionIdentitiesError(t *testing.T) {
142-
callCount := make(chan struct{}, 100)
143-
maxCalls := 3
143+
var callCount atomic.Int32
144+
maxCalls := int32(3)
144145

145146
c := NewIdentityCache(func(ctx context.Context, auditInfo []byte) (*idriver.IdentityDescriptor, error) {
146147
// Fail 3 times then succeed
147-
callCount <- struct{}{} // send once per call
148-
if len(callCount) <= maxCalls {
148+
current := callCount.Add(1)
149+
if current <= maxCalls {
149150
return nil, errors.New("provision error")
150151
}
151152

@@ -163,15 +164,15 @@ func TestProvisionIdentitiesError(t *testing.T) {
163164
time.Sleep(50 * time.Millisecond)
164165

165166
// Verify that provisioning continued after errors
166-
assert.Greater(t, len(callCount), maxCalls)
167+
assert.Greater(t, callCount.Load(), maxCalls)
167168
}
168169

169170
// TestFetchIdentityFromCacheNilEntry verifies backend fallback for nil cache entries.
170171
func TestFetchIdentityFromCacheNilEntry(t *testing.T) {
171-
backendCalled := make(chan struct{}, 1)
172+
var backendCalledCount atomic.Int32
172173

173174
c := NewIdentityCache(func(ctx context.Context, auditInfo []byte) (*idriver.IdentityDescriptor, error) {
174-
backendCalled <- struct{}{}
175+
backendCalledCount.Add(1)
175176

176177
return &idriver.IdentityDescriptor{
177178
Identity: []byte("backend fallback"),
@@ -185,12 +186,7 @@ func TestFetchIdentityFromCacheNilEntry(t *testing.T) {
185186
identityDescriptor, err := c.Identity(context.Background(), nil)
186187
require.NoError(t, err)
187188
assert.Eventually(t, func() bool {
188-
select {
189-
case <-backendCalled:
190-
return true
191-
default:
192-
return false
193-
}
189+
return backendCalledCount.Load() > 0
194190
}, time.Second, 10*time.Millisecond)
195191
assert.Equal(t, driver.Identity([]byte("backend fallback")), identityDescriptor.Identity)
196192
}

0 commit comments

Comments
 (0)