github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/src/sync/pool_test.go (about)

     1  package sync_test
     2  
     3  import (
     4  	"sync"
     5  	"testing"
     6  )
     7  
     8  type testItem struct {
     9  	val int
    10  }
    11  
    12  func TestPool(t *testing.T) {
    13  	p := sync.Pool{
    14  		New: func() interface{} {
    15  			return &testItem{}
    16  		},
    17  	}
    18  
    19  	i1P := p.Get()
    20  	if i1P == nil {
    21  		t.Error("pool with New returned nil")
    22  	}
    23  	i1 := i1P.(*testItem)
    24  	if got, want := i1.val, 0; got != want {
    25  		t.Errorf("empty pool item value: got %v, want %v", got, want)
    26  	}
    27  	i1.val = 1
    28  
    29  	i2 := p.Get().(*testItem)
    30  	if got, want := i2.val, 0; got != want {
    31  		t.Errorf("empty pool item value: got %v, want %v", got, want)
    32  	}
    33  	i2.val = 2
    34  
    35  	p.Put(i1)
    36  
    37  	i3 := p.Get().(*testItem)
    38  	if got, want := i3.val, 1; got != want {
    39  		t.Errorf("pool with item value: got %v, want %v", got, want)
    40  	}
    41  }
    42  
    43  func TestPool_noNew(t *testing.T) {
    44  	p := sync.Pool{}
    45  
    46  	i1 := p.Get()
    47  	if i1 != nil {
    48  		t.Errorf("pool without New returned %v, want nil", i1)
    49  	}
    50  }