MCPcopy
hub / github.com/micro/go-micro / TestSingleflightWithError

Function TestSingleflightWithError

registry/cache/cache_test.go:133–175  ·  view source on GitHub ↗

TestSingleflightWithError verifies that when etcd fails, only one request is made and all concurrent callers receive the error

(t *testing.T)

Source from the content-addressed store, hash-verified

131// TestSingleflightWithError verifies that when etcd fails, only one request is made
132// and all concurrent callers receive the error
133func 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
178func TestStaleCacheOnError(t *testing.T) {

Callers

nothing calls this directly

Calls 7

getCallCountMethod · 0.95
DoneMethod · 0.80
NewFunction · 0.70
AddMethod · 0.65
GetServiceMethod · 0.65
WaitMethod · 0.45
ErrorfMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…