github.com/argoproj/argo-cd/v3@v3.2.1/controller/syncid/id_test.go (about)

     1  package syncid
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  func TestGenerate(t *testing.T) {
    11  	t.Parallel()
    12  	const goroutines = 10
    13  	const idsPerGoroutine = 50
    14  	idsCh := make(chan string, goroutines*idsPerGoroutine)
    15  	errCh := make(chan error, goroutines*idsPerGoroutine)
    16  
    17  	// Reset globalCount for deterministic test (not strictly necessary, but can help in CI)
    18  	globalCount.Store(0)
    19  
    20  	// Run goroutines in parallel to test for race conditions
    21  	for g := 0; g < goroutines; g++ {
    22  		go func() {
    23  			for i := 0; i < idsPerGoroutine; i++ {
    24  				id, err := Generate()
    25  				if err != nil {
    26  					errCh <- err
    27  					continue
    28  				}
    29  				idsCh <- id
    30  			}
    31  		}()
    32  	}
    33  
    34  	ids := make(map[string]any)
    35  	for i := 0; i < goroutines*idsPerGoroutine; i++ {
    36  		select {
    37  		case err := <-errCh:
    38  			require.NoError(t, err)
    39  		case id := <-idsCh:
    40  			assert.Regexp(t, `^\d{5}-[a-zA-Z0-9]{5}$`, id, "ID should match the expected format")
    41  			_, exists := ids[id]
    42  			assert.False(t, exists, "ID should be unique")
    43  			ids[id] = id
    44  		}
    45  	}
    46  }