|
| 1 | +package validator |
| 2 | + |
| 3 | +import ( |
| 4 | + "regexp" |
| 5 | + "sync" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +// TestLazyRegexCompile_Basic tests that lazyRegexCompile compiles the regex only once and caches the result. |
| 10 | +func TestLazyRegexCompile_Basic(t *testing.T) { |
| 11 | + alphaRegexString := "^[a-zA-Z]+$" |
| 12 | + alphaRegex := lazyRegexCompile(alphaRegexString) |
| 13 | + |
| 14 | + callCount := 0 |
| 15 | + originalFunc := alphaRegex |
| 16 | + alphaRegex = func() *regexp.Regexp { |
| 17 | + callCount++ |
| 18 | + return originalFunc() |
| 19 | + } |
| 20 | + |
| 21 | + // Call the function multiple times |
| 22 | + for i := 0; i < 10; i++ { |
| 23 | + result := alphaRegex() |
| 24 | + if result == nil { |
| 25 | + t.Fatalf("Expected non-nil result") |
| 26 | + } |
| 27 | + if !result.MatchString("test") { |
| 28 | + t.Fatalf("Expected regex to match 'test'") |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + if callCount != 10 { |
| 33 | + t.Fatalf("Expected call count to be 10, got %d", callCount) |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +// TestLazyRegexCompile_Concurrent tests that lazyRegexCompile works correctly when called concurrently. |
| 38 | +func TestLazyRegexCompile_Concurrent(t *testing.T) { |
| 39 | + alphaRegexString := "^[a-zA-Z]+$" |
| 40 | + alphaRegex := lazyRegexCompile(alphaRegexString) |
| 41 | + |
| 42 | + var wg sync.WaitGroup |
| 43 | + const numGoroutines = 100 |
| 44 | + |
| 45 | + // Use a map to ensure all results point to the same instance |
| 46 | + results := make(map[*regexp.Regexp]bool) |
| 47 | + var mu sync.Mutex |
| 48 | + |
| 49 | + // Call the function concurrently |
| 50 | + for i := 0; i < numGoroutines; i++ { |
| 51 | + wg.Add(1) |
| 52 | + go func() { |
| 53 | + defer wg.Done() |
| 54 | + result := alphaRegex() |
| 55 | + if result == nil { |
| 56 | + t.Errorf("Expected non-nil result") |
| 57 | + } |
| 58 | + mu.Lock() |
| 59 | + results[result] = true |
| 60 | + mu.Unlock() |
| 61 | + }() |
| 62 | + } |
| 63 | + wg.Wait() |
| 64 | + |
| 65 | + if len(results) != 1 { |
| 66 | + t.Fatalf("Expected one unique regex instance, got %d", len(results)) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +// TestLazyRegexCompile_Panic tests that if the regex compilation panics, the panic value is propagated consistently. |
| 71 | +func TestLazyRegexCompile_Panic(t *testing.T) { |
| 72 | + faultyRegexString := "[a-z" |
| 73 | + alphaRegex := lazyRegexCompile(faultyRegexString) |
| 74 | + |
| 75 | + defer func() { |
| 76 | + if r := recover(); r == nil { |
| 77 | + t.Fatalf("Expected a panic, but none occurred") |
| 78 | + } |
| 79 | + }() |
| 80 | + |
| 81 | + // Call the function, which should panic |
| 82 | + alphaRegex() |
| 83 | +} |
0 commit comments