|
| 1 | +package token |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "path/filepath" |
| 6 | + "sync" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/Azure/azure-sdk-for-go/sdk/azidentity" |
| 10 | + "github.com/Azure/azure-sdk-for-go/sdk/azidentity/cache" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +func TestNewPersistentCache_Success(t *testing.T) { |
| 16 | + original := cacheNewFunc |
| 17 | + defer func() { cacheNewFunc = original }() |
| 18 | + |
| 19 | + called := false |
| 20 | + cacheNewFunc = func(opts *cache.Options) (azidentity.Cache, error) { |
| 21 | + called = true |
| 22 | + assert.Nil(t, opts) |
| 23 | + return azidentity.Cache{}, nil |
| 24 | + } |
| 25 | + |
| 26 | + c, err := newPersistentCache() |
| 27 | + assert.NoError(t, err) |
| 28 | + assert.Equal(t, azidentity.Cache{}, c) |
| 29 | + assert.True(t, called) |
| 30 | +} |
| 31 | + |
| 32 | +func TestNewPersistentCache_Error(t *testing.T) { |
| 33 | + original := cacheNewFunc |
| 34 | + defer func() { cacheNewFunc = original }() |
| 35 | + |
| 36 | + expectedErr := fmt.Errorf("test error") |
| 37 | + cacheNewFunc = func(opts *cache.Options) (azidentity.Cache, error) { |
| 38 | + return azidentity.Cache{}, expectedErr |
| 39 | + } |
| 40 | + |
| 41 | + c, err := newPersistentCache() |
| 42 | + assert.ErrorIs(t, err, expectedErr) |
| 43 | + assert.Equal(t, azidentity.Cache{}, c) |
| 44 | +} |
| 45 | + |
| 46 | +func TestNewPersistentCache_ConcurrentAccess(t *testing.T) { |
| 47 | + original := cacheNewFunc |
| 48 | + defer func() { cacheNewFunc = original }() |
| 49 | + |
| 50 | + var mu sync.Mutex |
| 51 | + callCount := 0 |
| 52 | + cacheNewFunc = func(opts *cache.Options) (azidentity.Cache, error) { |
| 53 | + mu.Lock() |
| 54 | + callCount++ |
| 55 | + mu.Unlock() |
| 56 | + return azidentity.Cache{}, nil |
| 57 | + } |
| 58 | + |
| 59 | + var wg sync.WaitGroup |
| 60 | + const goroutines = 10 |
| 61 | + for i := 0; i < goroutines; i++ { |
| 62 | + wg.Add(1) |
| 63 | + go func() { |
| 64 | + defer wg.Done() |
| 65 | + _, err := newPersistentCache() |
| 66 | + assert.NoError(t, err) |
| 67 | + }() |
| 68 | + } |
| 69 | + wg.Wait() |
| 70 | + assert.Equal(t, goroutines, callCount) |
| 71 | +} |
| 72 | + |
| 73 | +func TestAcquireProcessLock_ReturnsUnlockFunc(t *testing.T) { |
| 74 | + lockPath := filepath.Join(t.TempDir(), "test.lock") |
| 75 | + unlock := acquireProcessLock(lockPath) |
| 76 | + require.NotNil(t, unlock) |
| 77 | + |
| 78 | + // Release the lock — should not panic |
| 79 | + unlock() |
| 80 | +} |
| 81 | + |
| 82 | +func TestAcquireProcessLock_InvalidPath(t *testing.T) { |
| 83 | + // Use a path in a non-existent directory |
| 84 | + lockPath := filepath.Join(t.TempDir(), "nonexistent", "subdir", "test.lock") |
| 85 | + unlock := acquireProcessLock(lockPath) |
| 86 | + require.NotNil(t, unlock) |
| 87 | + |
| 88 | + // Should be a no-op function, calling it should not panic |
| 89 | + unlock() |
| 90 | +} |
0 commit comments