github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/pool/pool_test.go (about)

     1  // Copyright 2018 The gVisor Authors.
     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  package pool
    16  
    17  import (
    18  	"testing"
    19  )
    20  
    21  func TestPoolUnique(t *testing.T) {
    22  	p := Pool{Start: 1, Limit: 3}
    23  	got := make(map[uint64]bool)
    24  
    25  	for {
    26  		n, ok := p.Get()
    27  		if !ok {
    28  			break
    29  		}
    30  
    31  		// Check unique.
    32  		if _, ok := got[n]; ok {
    33  			t.Errorf("pool spit out %v multiple times", n)
    34  		}
    35  
    36  		// Record.
    37  		got[n] = true
    38  	}
    39  }
    40  
    41  func TestExausted(t *testing.T) {
    42  	p := Pool{Start: 1, Limit: 500}
    43  	for i := 0; i < 499; i++ {
    44  		_, ok := p.Get()
    45  		if !ok {
    46  			t.Fatalf("pool exhausted before 499 items")
    47  		}
    48  	}
    49  
    50  	_, ok := p.Get()
    51  	if ok {
    52  		t.Errorf("pool not exhausted when it should be")
    53  	}
    54  }
    55  
    56  func TestPoolRecycle(t *testing.T) {
    57  	p := Pool{Start: 1, Limit: 500}
    58  	n1, _ := p.Get()
    59  	p.Put(n1)
    60  	n2, _ := p.Get()
    61  	if n1 != n2 {
    62  		t.Errorf("pool not recycling items")
    63  	}
    64  }