TestSingleflightWithError verifies that when etcd fails, only one request is made and all concurrent callers receive the error
(t *testing.T)
| 131 | // TestSingleflightWithError verifies that when etcd fails, only one request is made |
| 132 | // and all concurrent callers receive the error |
| 133 | func TestSingleflightWithError(t *testing.T) { |
| 134 | expectedErr := errors.New("etcd connection failed") |
| 135 | mock := &mockRegistry{ |
| 136 | delay: 50 * time.Millisecond, |
| 137 | err: expectedErr, |
| 138 | } |
| 139 | |
| 140 | // Type assertion to *cache is necessary to access internal state for verification |
| 141 | c := New(mock, func(o *Options) { |
| 142 | o.TTL = time.Minute |
| 143 | o.Logger = logger.DefaultLogger |
| 144 | }).(*cache) |
| 145 | |
| 146 | // Launch concurrent requests |
| 147 | const concurrency = 10 |
| 148 | var wg sync.WaitGroup |
| 149 | wg.Add(concurrency) |
| 150 | |
| 151 | errs := make([]error, concurrency) |
| 152 | |
| 153 | for i := 0; i < concurrency; i++ { |
| 154 | go func(idx int) { |
| 155 | defer wg.Done() |
| 156 | _, err := c.GetService("test.service") |
| 157 | errs[idx] = err |
| 158 | }(i) |
| 159 | } |
| 160 | |
| 161 | wg.Wait() |
| 162 | |
| 163 | // Verify that only 1 call was made to the underlying registry |
| 164 | callCount := mock.getCallCount() |
| 165 | if callCount != 1 { |
| 166 | t.Errorf("Expected 1 call to registry even on error, got %d", callCount) |
| 167 | } |
| 168 | |
| 169 | // Verify all requests got the error |
| 170 | for i := 0; i < concurrency; i++ { |
| 171 | if errs[i] == nil { |
| 172 | t.Errorf("Request %d should have failed", i) |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | // TestStaleCacheOnError verifies that stale cache is returned when registry fails |
| 178 | func TestStaleCacheOnError(t *testing.T) { |
nothing calls this directly
no test coverage detected
searching dependent graphs…