github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/tm2/pkg/bft/privval/socket_dialers_test.go (about)

     1  package privval
     2  
     3  import (
     4  	"fmt"
     5  	"net"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  
    12  	"github.com/gnolang/gno/tm2/pkg/crypto/ed25519"
    13  	"github.com/gnolang/gno/tm2/pkg/errors"
    14  )
    15  
    16  func getDialerTestCases(t *testing.T) []dialerTestCase {
    17  	t.Helper()
    18  
    19  	l, err := net.Listen("tcp", "127.0.0.1:0")
    20  	if err != nil {
    21  		panic(fmt.Errorf("no open ports? error listening to 127.0.0.1:0: %w", err))
    22  	}
    23  	unixFilePath, err := testUnixAddr()
    24  	require.NoError(t, err)
    25  	ul, err := net.Listen("unix", unixFilePath)
    26  	if err != nil {
    27  		panic(err)
    28  	}
    29  
    30  	return []dialerTestCase{
    31  		{
    32  			listener: l,
    33  			dialer:   DialTCPFn(l.Addr().String(), testTimeoutReadWrite, ed25519.GenPrivKey()),
    34  		},
    35  		{
    36  			listener: ul,
    37  			dialer:   DialUnixFn(unixFilePath),
    38  		},
    39  	}
    40  }
    41  
    42  func TestIsConnTimeoutForFundamentalTimeouts(t *testing.T) {
    43  	t.Parallel()
    44  
    45  	// Generate a networking timeout
    46  	tcpAddr := "127.0.0.1:34985"
    47  	dialer := DialTCPFn(tcpAddr, time.Millisecond, ed25519.GenPrivKey())
    48  	_, err := dialer()
    49  	assert.Error(t, err)
    50  	assert.True(t, IsConnTimeout(err))
    51  }
    52  
    53  func TestIsConnTimeoutForWrappedConnTimeouts(t *testing.T) {
    54  	t.Parallel()
    55  
    56  	tcpAddr := "127.0.0.1:34985"
    57  	dialer := DialTCPFn(tcpAddr, time.Millisecond, ed25519.GenPrivKey())
    58  	_, err := dialer()
    59  	assert.Error(t, err)
    60  	err = errors.Wrap(ErrConnectionTimeout, err.Error())
    61  	assert.True(t, IsConnTimeout(err))
    62  }