trpc.group/trpc-go/trpc-go@v1.0.3/internal/allocator/allocator_test.go (about)

     1  //
     2  //
     3  // Tencent is pleased to support the open source community by making tRPC available.
     4  //
     5  // Copyright (C) 2023 THL A29 Limited, a Tencent company.
     6  // All rights reserved.
     7  //
     8  // If you have downloaded a copy of the tRPC source code from Tencent,
     9  // please note that tRPC source code is licensed under the  Apache 2.0 License,
    10  // A copy of the Apache 2.0 License is included in this file.
    11  //
    12  //
    13  
    14  package allocator_test
    15  
    16  import (
    17  	"testing"
    18  
    19  	. "trpc.group/trpc-go/trpc-go/internal/allocator"
    20  
    21  	"github.com/stretchr/testify/require"
    22  )
    23  
    24  func TestDefaultAllocator(t *testing.T) {
    25  	bs, free := Malloc(10)
    26  	require.Equal(t, 10, len(bs))
    27  	Free(free)
    28  }
    29  
    30  func TestClassAllocator(t *testing.T) {
    31  	a := NewClassAllocator()
    32  	bs, free := a.Malloc(10)
    33  	require.Equal(t, 10, len(bs))
    34  	a.Free(free)
    35  }
    36  
    37  func TestClassAllocator_InvalidMalloc(t *testing.T) {
    38  	a := NewClassAllocator()
    39  	defer func() {
    40  		require.NotEmpty(t, recover())
    41  	}()
    42  	a.Malloc(-1)
    43  }
    44  
    45  func TestClassAllocator_InvalidFree(t *testing.T) {
    46  	a := NewClassAllocator()
    47  	t.Run("free empty slice", func(t *testing.T) {
    48  		defer func() {
    49  			require.NotEmpty(t, recover())
    50  		}()
    51  		a.Free(nil)
    52  	})
    53  	t.Run("invalid slice size", func(t *testing.T) {
    54  		defer func() {
    55  			require.NotEmpty(t, recover())
    56  		}()
    57  		a.Free(make([]byte, 9))
    58  	})
    59  }