| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "sync" |
| 5 | "testing" |
| 6 | "time" |
| 7 | ) |
| 8 | |
| 9 | // TestReloadSafeguard tests the ReloadSafeguard functionality |
| 10 | func TestReloadSafeguard(t *testing.T) { |
| 11 | rs := NewReloadSafeguard() |
| 12 | |
| 13 | // Test StartReload and FinishReload |
| 14 | if !rs.StartReload() { |
| 15 | t.Error("StartReload should return true for first call") |
| 16 | } |
| 17 | |
| 18 | // Second call should fail while first reload is in progress |
| 19 | if rs.StartReload() { |
| 20 | t.Error("StartReload should return false for second call while first reload is still in progress") |
| 21 | } |
| 22 | |
| 23 | // Finish reload and try again |
| 24 | rs.FinishReload() |
| 25 | |
| 26 | // Should be able to start a new reload now |
| 27 | if !rs.StartReload() { |
| 28 | t.Error("StartReload should return true after previous reload is finished") |
| 29 | } |
| 30 | |
| 31 | // Clean up |
| 32 | rs.FinishReload() |
| 33 | |
| 34 | // Test concurrent access to configuration |
| 35 | var wg sync.WaitGroup |
| 36 | const numReaders = 10 |
| 37 | |
| 38 | // Start readers |
| 39 | for range numReaders { |
| 40 | wg.Go(func() { |
| 41 | rs.AcquireConfigRead() |
| 42 | // Simulate read access |
| 43 | rs.ReleaseConfigRead() |
| 44 | }) |
| 45 | } |
| 46 | |
| 47 | // Try to acquire write lock while readers are active |
| 48 | writeSuccess := false |
| 49 | writeChan := make(chan bool) |
| 50 | go func() { |
| 51 | rs.AcquireConfigWrite() |
| 52 | writeSuccess = true |
| 53 | // Simulate write access |
| 54 | rs.ReleaseConfigWrite() |
| 55 | writeChan <- true |
| 56 | }() |
| 57 | |
| 58 | // Wait for all readers to finish |
| 59 | wg.Wait() |
| 60 | |
| 61 | // Wait for writer to complete |
| 62 | <-writeChan |
| 63 | |
| 64 | if !writeSuccess { |
| 65 | t.Error("Writer should have eventually acquired the lock after readers finished") |
| 66 | } |
| 67 | |
| 68 | // Test SafeReload helper |
| 69 | callCount := 0 |
| 70 | reloadFunc := func() error { |
| 71 | callCount++ |
| 72 | return nil |
| 73 | } |
| 74 | |
| 75 | // Execute reload function |
| 76 | err := rs.SafeReload(reloadFunc) |
| 77 | if err != nil { |
| 78 | t.Errorf("SafeReload returned error: %v", err) |
| 79 | } |
| 80 | |
| 81 | if callCount != 1 { |
| 82 | t.Errorf("Expected reloadFunc to be called once, got %d calls", callCount) |
| 83 | } |
| 84 | |
| 85 | // Test that we can perform another reload after the first completes |
| 86 | err = rs.SafeReload(reloadFunc) |
| 87 | if err != nil { |
| 88 | t.Errorf("Second SafeReload returned error: %v", err) |
| 89 | } |
| 90 | |
| 91 | if callCount != 2 { |
| 92 | t.Errorf("Expected reloadFunc to be called twice, got %d calls", callCount) |
| 93 | } |
| 94 | |
| 95 | // Test concurrent SafeReload calls |
| 96 | wg = sync.WaitGroup{} |
| 97 | wg.Add(2) |
| 98 | |
| 99 | var mu sync.Mutex |
| 100 | firstCalled := false |
| 101 | secondCalled := false |
| 102 | |
| 103 | go func() { |
| 104 | defer wg.Done() |
| 105 | err := rs.SafeReload(func() error { |
| 106 | mu.Lock() |
| 107 | firstCalled = true |
| 108 | mu.Unlock() |
| 109 | // Simulate some work with delay to ensure concurrency |
| 110 | time.Sleep(50 * time.Millisecond) |
| 111 | return nil |
| 112 | }) |
| 113 | if err != nil && err.Error() != "another reload operation is already in progress" { |
| 114 | t.Errorf("Concurrent SafeReload 1 returned unexpected error: %v", err) |
| 115 | } |
| 116 | }() |
| 117 | |
| 118 | // Wait a bit to ensure the first goroutine has a chance to start |
| 119 | time.Sleep(10 * time.Millisecond) |
| 120 | |
| 121 | go func() { |
| 122 | defer wg.Done() |
| 123 | err := rs.SafeReload(func() error { |
| 124 | mu.Lock() |
| 125 | secondCalled = true |
| 126 | mu.Unlock() |
| 127 | // Simulate some work |
| 128 | time.Sleep(50 * time.Millisecond) |
| 129 | return nil |
| 130 | }) |
| 131 | if err != nil && err.Error() != "another reload operation is already in progress" { |
| 132 | t.Errorf("Concurrent SafeReload 2 returned unexpected error: %v", err) |
| 133 | } |
| 134 | }() |
| 135 | |
| 136 | wg.Wait() |
| 137 | |
| 138 | // One of them should have succeeded |
| 139 | mu.Lock() |
| 140 | gotFirst := firstCalled |
| 141 | gotSecond := secondCalled |
| 142 | mu.Unlock() |
| 143 | |
| 144 | if !(gotFirst || gotSecond) { |
| 145 | t.Error("Expected at least one of the concurrent reload functions to be called") |
| 146 | } |
| 147 | |
| 148 | // Note: We can't reliably test that only one succeeds in all environments |
| 149 | // since timing can vary, but the locking mechanism should ensure correctness in real usage |
| 150 | } |