mirror of
https://github.com/argoproj/argo-cd.git
synced 2026-02-20 01:28:45 +01:00
47 lines
1.0 KiB
Go
47 lines
1.0 KiB
Go
package syncid
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestGenerate(t *testing.T) {
|
|
t.Parallel()
|
|
const goroutines = 10
|
|
const idsPerGoroutine = 50
|
|
idsCh := make(chan string, goroutines*idsPerGoroutine)
|
|
errCh := make(chan error, goroutines*idsPerGoroutine)
|
|
|
|
// Reset globalCount for deterministic test (not strictly necessary, but can help in CI)
|
|
globalCount.Store(0)
|
|
|
|
// Run goroutines in parallel to test for race conditions
|
|
for range goroutines {
|
|
go func() {
|
|
for range idsPerGoroutine {
|
|
id, err := Generate()
|
|
if err != nil {
|
|
errCh <- err
|
|
continue
|
|
}
|
|
idsCh <- id
|
|
}
|
|
}()
|
|
}
|
|
|
|
ids := make(map[string]any)
|
|
for range goroutines * idsPerGoroutine {
|
|
select {
|
|
case err := <-errCh:
|
|
require.NoError(t, err)
|
|
case id := <-idsCh:
|
|
assert.Regexp(t, `^\d{5}-[a-zA-Z0-9]{5}$`, id, "ID should match the expected format")
|
|
_, exists := ids[id]
|
|
assert.False(t, exists, "ID should be unique")
|
|
ids[id] = id
|
|
}
|
|
}
|
|
}
|