MCPcopy
hub / github.com/cilium/cilium / testAllocator

Function testAllocator

pkg/allocator/allocator_test.go:301–402  ·  view source on GitHub ↗
(t *testing.T, maxID idpool.ID)

Source from the content-addressed store, hash-verified

299}
300
301func testAllocator(t *testing.T, maxID idpool.ID) {
302 backend := newDummyBackend()
303 allocator, err := NewAllocator(hivetest.Logger(t), TestAllocatorKey(""), backend, WithMax(maxID), WithoutGC())
304 require.NoError(t, err)
305 require.NotNil(t, allocator)
306
307 // remove any keys which might be leftover
308 allocator.DeleteAllKeys()
309
310 // allocate all available IDs
311 for i := idpool.ID(1); i <= maxID; i++ {
312 key := TestAllocatorKey(fmt.Sprintf("key%04d", i))
313 id, new, firstUse, err := allocator.Allocate(context.Background(), key)
314 require.NoError(t, err)
315 require.NotEqual(t, 0, id)
316 require.True(t, new)
317 require.True(t, firstUse)
318
319 // refcnt must be 1
320 require.Equal(t, uint64(1), allocator.localKeys.keys[key.GetKey()].refcnt)
321 }
322
323 saved := allocator.backoffTemplate.Factor
324 allocator.backoffTemplate.Factor = 1.0
325
326 // we should be out of id space here
327 _, new, firstUse, err := allocator.Allocate(context.Background(), TestAllocatorKey(fmt.Sprintf("key%04d", maxID+1)))
328 require.Error(t, err)
329 require.False(t, new)
330 require.False(t, firstUse)
331
332 allocator.backoffTemplate.Factor = saved
333
334 // allocate all IDs again using the same set of keys, refcnt should go to 2
335 for i := idpool.ID(1); i <= maxID; i++ {
336 key := TestAllocatorKey(fmt.Sprintf("key%04d", i))
337 id, new, firstUse, err := allocator.Allocate(context.Background(), key)
338 require.NoError(t, err)
339 require.NotEqual(t, 0, id)
340 require.False(t, new)
341 require.False(t, firstUse)
342
343 // refcnt must now be 2
344 require.Equal(t, uint64(2), allocator.localKeys.keys[key.GetKey()].refcnt)
345 }
346
347 // Create a 2nd allocator, refill it
348 allocator2, err := NewAllocator(hivetest.Logger(t), TestAllocatorKey(""), backend, WithMax(maxID), WithoutGC())
349 require.NoError(t, err)
350 require.NotNil(t, allocator2)
351
352 // allocate all IDs again using the same set of keys, refcnt should go to 2
353 for i := idpool.ID(1); i <= maxID; i++ {
354 key := TestAllocatorKey(fmt.Sprintf("key%04d", i))
355 id, new, firstUse, err := allocator2.Allocate(context.Background(), key)
356 require.NoError(t, err)
357 require.NotEqual(t, 0, id)
358 require.False(t, new)

Callers 1

TestAllocateCachedFunction · 0.70

Calls 15

DeleteAllKeysMethod · 0.95
AllocateMethod · 0.95
ReleaseMethod · 0.95
RunGCMethod · 0.95
DeleteMethod · 0.95
IDTypeAlias · 0.92
NewLimiterFunction · 0.92
newDummyBackendFunction · 0.85
NewAllocatorFunction · 0.85
WithMaxFunction · 0.85
WithoutGCFunction · 0.85
TestAllocatorKeyTypeAlias · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…