github.com/cilium/cilium@v1.16.2/pkg/endpointmanager/allocator_test.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package endpointmanager
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  // reallocatePool starts over with a new pool. This function is only used for
    13  // tests and its implementation is not optimized for production.
    14  func (a *epIDAllocator) reallocatePool(t testing.TB) {
    15  	for i := uint16(minID); i <= uint16(maxID); i++ {
    16  		a.release(i)
    17  	}
    18  }
    19  
    20  func TestAllocation(t *testing.T) {
    21  	p := newEPIDAllocator()
    22  
    23  	idsReturned := map[uint16]struct{}{}
    24  
    25  	for i := minID; i <= maxID; i++ {
    26  		id := p.allocate()
    27  		assert.NotZero(t, id)
    28  
    29  		// check if same ID is returned more than once
    30  		assert.NotContains(t, idsReturned, id)
    31  
    32  		idsReturned[id] = struct{}{}
    33  	}
    34  
    35  	// We should be out of allocations
    36  	assert.Zero(t, p.allocate())
    37  }
    38  
    39  func TestReuse(t *testing.T) {
    40  	p := newEPIDAllocator()
    41  
    42  	// Reusing IDs greater than the maxID is allowed
    43  	assert.Nil(t, p.reuse(uint16(maxID+10)))
    44  
    45  	// Reusing IDs lesser than the minID is not allowed
    46  	assert.NotNil(t, p.reuse(uint16(minID-1)))
    47  
    48  	idsReturned := map[uint16]struct{}{}
    49  
    50  	assert.Nil(t, p.reuse(uint16(2)))
    51  	idsReturned[uint16(2)] = struct{}{}
    52  
    53  	assert.Nil(t, p.reuse(uint16(8)))
    54  	idsReturned[uint16(8)] = struct{}{}
    55  
    56  	for i := minID; i <= maxID-2; i++ {
    57  		id := p.allocate()
    58  		assert.NotZero(t, id)
    59  
    60  		// check if same ID is returned more than once
    61  		assert.NotContains(t, idsReturned, id)
    62  
    63  		idsReturned[id] = struct{}{}
    64  	}
    65  
    66  	// We should be out of allocations
    67  	assert.Zero(t, p.allocate())
    68  
    69  	// 2nd reuse should fail
    70  	assert.NotNil(t, p.reuse(uint16(2)))
    71  
    72  	// reuse of allocated id should fail
    73  	assert.NotNil(t, p.reuse(uint16(3)))
    74  
    75  	// release 5
    76  	assert.Nil(t, p.release(uint16(5)))
    77  	delete(idsReturned, uint16(5))
    78  
    79  	// release 6
    80  	assert.Nil(t, p.release(uint16(6)))
    81  	delete(idsReturned, uint16(6))
    82  
    83  	// reuse 5 after release
    84  	assert.Nil(t, p.reuse(uint16(5)))
    85  	idsReturned[uint16(5)] = struct{}{}
    86  
    87  	// allocate only available id 6
    88  	assert.Equal(t, uint16(6), p.allocate())
    89  }
    90  
    91  func TestRelease(t *testing.T) {
    92  	p := newEPIDAllocator()
    93  
    94  	for i := minID; i <= maxID; i++ {
    95  		assert.Nil(t, p.reuse(uint16(i)))
    96  	}
    97  
    98  	// must be out of IDs
    99  	assert.Zero(t, p.allocate())
   100  
   101  	for i := minID; i <= maxID; i++ {
   102  		assert.Nil(t, p.release(uint16(i)))
   103  	}
   104  }