github.com/koko1123/flow-go-1@v0.29.6/network/p2p/p2pbuilder/libp2pscaler_test.go (about)

     1  package p2pbuilder
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/pbnjay/memory"
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  func TestAllowedMemoryScale(t *testing.T) {
    11  	m := memory.TotalMemory()
    12  	require.True(t, m > 0)
    13  
    14  	// scaling with factor of 1 should return the total memory.
    15  	s, err := allowedMemory(1)
    16  	require.NoError(t, err)
    17  	require.Equal(t, int64(m), s)
    18  
    19  	// scaling with factor of 0 should return an error.
    20  	_, err = allowedMemory(0)
    21  	require.Error(t, err)
    22  
    23  	// scaling with factor of -1 should return an error.
    24  	_, err = allowedMemory(-1)
    25  	require.Error(t, err)
    26  
    27  	// scaling with factor of 2 should return an error.
    28  	_, err = allowedMemory(2)
    29  	require.Error(t, err)
    30  
    31  	// scaling with factor of 0.5 should return half the total memory.
    32  	s, err = allowedMemory(0.5)
    33  	require.NoError(t, err)
    34  	require.Equal(t, int64(m/2), s)
    35  
    36  	// scaling with factor of 0.1 should return 10% of the total memory.
    37  	s, err = allowedMemory(0.1)
    38  	require.NoError(t, err)
    39  	require.Equal(t, int64(m/10), s)
    40  
    41  	// scaling with factor of 0.01 should return 1% of the total memory.
    42  	s, err = allowedMemory(0.01)
    43  	require.NoError(t, err)
    44  	require.Equal(t, int64(m/100), s)
    45  
    46  	// scaling with factor of 0.001 should return 0.1% of the total memory.
    47  	s, err = allowedMemory(0.001)
    48  	require.NoError(t, err)
    49  	require.Equal(t, int64(m/1000), s)
    50  
    51  	// scaling with factor of 0.0001 should return 0.01% of the total memory.
    52  	s, err = allowedMemory(0.0001)
    53  	require.NoError(t, err)
    54  	require.Equal(t, int64(m/10000), s)
    55  }
    56  
    57  func TestAllowedFileDescriptorsScale(t *testing.T) {
    58  	// getting actual file descriptor limit.
    59  	fd, err := getNumFDs()
    60  	require.NoError(t, err)
    61  	require.True(t, fd > 0)
    62  
    63  	// scaling with factor of 1 should return the total file descriptors.
    64  	s, err := allowedFileDescriptors(1)
    65  	require.NoError(t, err)
    66  	require.Equal(t, fd, s)
    67  
    68  	// scaling with factor of 0 should return an error.
    69  	_, err = allowedFileDescriptors(0)
    70  	require.Error(t, err)
    71  
    72  	// scaling with factor of -1 should return an error.
    73  	_, err = allowedFileDescriptors(-1)
    74  	require.Error(t, err)
    75  
    76  	// scaling with factor of 2 should return an error.
    77  	_, err = allowedFileDescriptors(2)
    78  	require.Error(t, err)
    79  
    80  	// scaling with factor of 0.5 should return half the total file descriptors.
    81  	s, err = allowedFileDescriptors(0.5)
    82  	require.NoError(t, err)
    83  	require.Equal(t, fd/2, s)
    84  
    85  	// scaling with factor of 0.1 should return 10% of the total file descriptors.
    86  	s, err = allowedFileDescriptors(0.1)
    87  	require.NoError(t, err)
    88  	require.Equal(t, fd/10, s)
    89  
    90  	// scaling with factor of 0.01 should return 1% of the total file descriptors.
    91  	s, err = allowedFileDescriptors(0.01)
    92  	require.NoError(t, err)
    93  	require.Equal(t, fd/100, s)
    94  
    95  	// scaling with factor of 0.001 should return 0.1% of the total file descriptors.
    96  	s, err = allowedFileDescriptors(0.001)
    97  	require.NoError(t, err)
    98  	require.Equal(t, fd/1000, s)
    99  
   100  	// scaling with factor of 0.0001 should return 0.01% of the total file descriptors.
   101  	s, err = allowedFileDescriptors(0.0001)
   102  	require.NoError(t, err)
   103  	require.Equal(t, fd/10000, s)
   104  }