github.com/abemedia/go-don@v0.2.2-0.20240329015135-be88e32bb73b/pool_test.go (about)

     1  package don_test
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/abemedia/go-don"
     8  )
     9  
    10  func TestRequestPool(t *testing.T) {
    11  	type item struct {
    12  		String  string
    13  		Pointer *string
    14  	}
    15  
    16  	t.Run("Nil", func(t *testing.T) {
    17  		var zero any
    18  		pool := don.NewRequestPool(zero)
    19  
    20  		pool.Put(pool.Get())
    21  
    22  		if !reflect.DeepEqual(&zero, pool.Get()) {
    23  			t.Fatal("should be zero value")
    24  		}
    25  	})
    26  
    27  	t.Run("Struct", func(t *testing.T) {
    28  		zero := item{}
    29  		pool := don.NewRequestPool(zero)
    30  
    31  		for i := 0; i < 100; i++ {
    32  			v := pool.Get()
    33  			v.String = "test"
    34  			v.Pointer = &v.String
    35  			pool.Put(v)
    36  		}
    37  
    38  		for i := 0; i < 100; i++ {
    39  			if !reflect.DeepEqual(&zero, pool.Get()) {
    40  				t.Fatal("should be zero value")
    41  			}
    42  		}
    43  	})
    44  
    45  	t.Run("Pointer", func(t *testing.T) {
    46  		zero := &item{}
    47  		pool := don.NewRequestPool(zero)
    48  
    49  		for i := 0; i < 100; i++ {
    50  			p := pool.Get()
    51  			v := *p
    52  			v.String = "test"
    53  			v.Pointer = &v.String
    54  			pool.Put(p)
    55  		}
    56  
    57  		for i := 0; i < 100; i++ {
    58  			if !reflect.DeepEqual(&zero, pool.Get()) {
    59  				t.Fatal("should be zero value")
    60  			}
    61  		}
    62  	})
    63  }