github.com/fafucoder/cilium@v1.6.11/pkg/endpoint/id/allocator_test.go (about)

     1  // Copyright 2018 Authors of Cilium
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  // +build !privileged_tests
    16  
    17  package id
    18  
    19  import (
    20  	. "gopkg.in/check.v1"
    21  )
    22  
    23  func (s *IDSuite) TestAllocation(c *C) {
    24  	ReallocatePool()
    25  
    26  	idsReturned := map[uint16]struct{}{}
    27  
    28  	for i := minID; i <= maxID; i++ {
    29  		id := Allocate()
    30  		c.Assert(id, Not(Equals), uint16(0))
    31  
    32  		// check if same ID is returned more than once
    33  		_, ok := idsReturned[id]
    34  		c.Assert(ok, Equals, false)
    35  
    36  		idsReturned[id] = struct{}{}
    37  	}
    38  
    39  	// We should be out of allocations
    40  	c.Assert(Allocate(), Equals, uint16(0))
    41  }
    42  
    43  func (s *IDSuite) TestReuse(c *C) {
    44  	ReallocatePool()
    45  
    46  	// Reusing IDs greater than the maxID is allowed
    47  	c.Assert(Reuse(uint16(maxID+10)), IsNil)
    48  
    49  	// Reusing IDs lesser than the minID is not allowed
    50  	c.Assert(Reuse(uint16(minID-1)), Not(IsNil))
    51  
    52  	idsReturned := map[uint16]struct{}{}
    53  
    54  	c.Assert(Reuse(uint16(2)), IsNil)
    55  	idsReturned[uint16(2)] = struct{}{}
    56  
    57  	c.Assert(Reuse(uint16(8)), IsNil)
    58  	idsReturned[uint16(8)] = struct{}{}
    59  
    60  	for i := minID; i <= maxID-2; i++ {
    61  		id := Allocate()
    62  		c.Assert(id, Not(Equals), uint16(0))
    63  
    64  		// check if same ID is returned more than once
    65  		_, ok := idsReturned[id]
    66  		c.Assert(ok, Equals, false)
    67  
    68  		idsReturned[id] = struct{}{}
    69  	}
    70  
    71  	// We should be out of allocations
    72  	c.Assert(Allocate(), Equals, uint16(0))
    73  
    74  	// 2nd reuse should fail
    75  	c.Assert(Reuse(uint16(2)), Not(IsNil))
    76  
    77  	// reuse of allocated id should fail
    78  	c.Assert(Reuse(uint16(3)), Not(IsNil))
    79  
    80  	// release 5
    81  	c.Assert(Release(uint16(5)), IsNil)
    82  	delete(idsReturned, uint16(5))
    83  
    84  	// release 6
    85  	c.Assert(Release(uint16(6)), IsNil)
    86  	delete(idsReturned, uint16(6))
    87  
    88  	// reuse 5 after release
    89  	c.Assert(Reuse(uint16(5)), IsNil)
    90  	idsReturned[uint16(5)] = struct{}{}
    91  
    92  	// allocate only available id 6
    93  	c.Assert(Allocate(), Equals, uint16(6))
    94  }
    95  
    96  func (s *IDSuite) TestRelease(c *C) {
    97  	ReallocatePool()
    98  
    99  	for i := minID; i <= maxID; i++ {
   100  		c.Assert(Reuse(uint16(i)), IsNil)
   101  	}
   102  
   103  	// must be out of IDs
   104  	c.Assert(Allocate(), Equals, uint16(0))
   105  
   106  	for i := minID; i <= maxID; i++ {
   107  		c.Assert(Release(uint16(i)), IsNil)
   108  	}
   109  }