MCPcopy
hub / github.com/cortexlabs/cortex / TestSemaphoreAcquireHasCapacity

Function TestSemaphoreAcquireHasCapacity

pkg/proxy/breaker_test.go:269–292  ·  view source on GitHub ↗

Test empty semaphore, add capacity, token can be acquired

(t *testing.T)

Source from the content-addressed store, hash-verified

267
268// Test empty semaphore, add capacity, token can be acquired
269func TestSemaphoreAcquireHasCapacity(t *testing.T) {
270 gotChan := make(chan struct{}, 1)
271 want := 1
272
273 sem := newSemaphore(1, 0)
274 tryAcquire(sem, gotChan)
275 sem.updateCapacity(1) // Allows 1 acquire
276
277 for i := 0; i < want; i++ {
278 select {
279 case <-gotChan:
280 // Successfully acquired a token.
281 case <-time.After(semAcquireTimeout):
282 t.Error("Was not able to acquire token before timeout")
283 }
284 }
285
286 select {
287 case <-gotChan:
288 t.Errorf("Got more acquires than wanted, want = %d, got at least %d", want, want+1)
289 case <-time.After(semNoChangeTimeout):
290 // No change happened, success.
291 }
292}
293
294func TestSemaphoreRelease(t *testing.T) {
295 sem := newSemaphore(1, 1)

Callers

nothing calls this directly

Calls 5

newSemaphoreFunction · 0.85
tryAcquireFunction · 0.85
updateCapacityMethod · 0.80
ErrorMethod · 0.80
ErrorfMethod · 0.80

Tested by

no test coverage detected