github.com/evdatsion/aphelion-dpos-bft@v0.32.1/privval/signer_remote_test.go (about)

     1  package privval
     2  
     3  import (
     4  	"net"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  	"github.com/evdatsion/aphelion-dpos-bft/crypto/ed25519"
    11  	cmn "github.com/evdatsion/aphelion-dpos-bft/libs/common"
    12  	"github.com/evdatsion/aphelion-dpos-bft/libs/log"
    13  	"github.com/evdatsion/aphelion-dpos-bft/types"
    14  )
    15  
    16  // TestSignerRemoteRetryTCPOnly will test connection retry attempts over TCP. We
    17  // don't need this for Unix sockets because the OS instantly knows the state of
    18  // both ends of the socket connection. This basically causes the
    19  // SignerServiceEndpoint.dialer() call inside SignerServiceEndpoint.connect() to return
    20  // successfully immediately, putting an instant stop to any retry attempts.
    21  func TestSignerRemoteRetryTCPOnly(t *testing.T) {
    22  	var (
    23  		attemptCh = make(chan int)
    24  		retries   = 2
    25  	)
    26  
    27  	ln, err := net.Listen("tcp", "127.0.0.1:0")
    28  	require.NoError(t, err)
    29  
    30  	go func(ln net.Listener, attemptCh chan<- int) {
    31  		attempts := 0
    32  
    33  		for {
    34  			conn, err := ln.Accept()
    35  			require.NoError(t, err)
    36  
    37  			err = conn.Close()
    38  			require.NoError(t, err)
    39  
    40  			attempts++
    41  
    42  			if attempts == retries {
    43  				attemptCh <- attempts
    44  				break
    45  			}
    46  		}
    47  	}(ln, attemptCh)
    48  
    49  	serviceEndpoint := NewSignerServiceEndpoint(
    50  		log.TestingLogger(),
    51  		cmn.RandStr(12),
    52  		types.NewMockPV(),
    53  		DialTCPFn(ln.Addr().String(), testTimeoutReadWrite, ed25519.GenPrivKey()),
    54  	)
    55  	defer serviceEndpoint.Stop()
    56  
    57  	SignerServiceEndpointTimeoutReadWrite(time.Millisecond)(serviceEndpoint)
    58  	SignerServiceEndpointConnRetries(retries)(serviceEndpoint)
    59  
    60  	assert.Equal(t, serviceEndpoint.Start(), ErrDialRetryMax)
    61  
    62  	select {
    63  	case attempts := <-attemptCh:
    64  		assert.Equal(t, retries, attempts)
    65  	case <-time.After(100 * time.Millisecond):
    66  		t.Error("expected remote to observe connection attempts")
    67  	}
    68  }