mirror of
https://github.com/fluencelabs/tendermint
synced 2025-04-25 14:52:17 +00:00
Expanding tests to cover Unix sockets version of client (#3132)
* Adds a random suffix to temporary Unix sockets during testing * Adds Unix domain socket tests for client (FAILING) This adds Unix domain socket tests for the privval client. Right now, one of the tests (TestRemoteSignerRetry) fails, probably because the Unix domain socket state is known instantaneously on both sides by the OS. Committing this to collaborate on the error. * Removes extraneous logging * Completes testing of Unix sockets client version This completes the testing of the client connecting via Unix sockets. There are two specific tests (TestSocketPVDeadline and TestRemoteSignerRetryTCPOnly) that are only relevant to TCP connections. * Renames test to show TCP-specificity * Adds testing into closures for consistency (forgot previously) * Moves test specific to RemoteSigner into own file As per discussion on #3132, `TestRemoteSignerRetryTCPOnly` doesn't really belong with the client tests. This moves it into its own file related to the `RemoteSigner` class.
This commit is contained in:
parent
a2a62c9be6
commit
d4e6720541
@ -27,120 +27,170 @@ var (
|
|||||||
testHeartbeatTimeout3o2 = 6 * time.Millisecond // 3/2 of the other one
|
testHeartbeatTimeout3o2 = 6 * time.Millisecond // 3/2 of the other one
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type socketTestCase struct {
|
||||||
|
addr string
|
||||||
|
dialer Dialer
|
||||||
|
}
|
||||||
|
|
||||||
|
func socketTestCases(t *testing.T) []socketTestCase {
|
||||||
|
tcpAddr := fmt.Sprintf("tcp://%s", testFreeTCPAddr(t))
|
||||||
|
unixFilePath, err := testUnixAddr()
|
||||||
|
require.NoError(t, err)
|
||||||
|
unixAddr := fmt.Sprintf("unix://%s", unixFilePath)
|
||||||
|
return []socketTestCase{
|
||||||
|
socketTestCase{
|
||||||
|
addr: tcpAddr,
|
||||||
|
dialer: DialTCPFn(tcpAddr, testConnDeadline, ed25519.GenPrivKey()),
|
||||||
|
},
|
||||||
|
socketTestCase{
|
||||||
|
addr: unixAddr,
|
||||||
|
dialer: DialUnixFn(unixFilePath),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestSocketPVAddress(t *testing.T) {
|
func TestSocketPVAddress(t *testing.T) {
|
||||||
var (
|
for _, tc := range socketTestCases(t) {
|
||||||
chainID = cmn.RandStr(12)
|
// Execute the test within a closure to ensure the deferred statements
|
||||||
sc, rs = testSetupSocketPair(t, chainID, types.NewMockPV())
|
// are called between each for loop iteration, for isolated test cases.
|
||||||
)
|
func() {
|
||||||
defer sc.Stop()
|
var (
|
||||||
defer rs.Stop()
|
chainID = cmn.RandStr(12)
|
||||||
|
sc, rs = testSetupSocketPair(t, chainID, types.NewMockPV(), tc.addr, tc.dialer)
|
||||||
|
)
|
||||||
|
defer sc.Stop()
|
||||||
|
defer rs.Stop()
|
||||||
|
|
||||||
serverAddr := rs.privVal.GetPubKey().Address()
|
serverAddr := rs.privVal.GetPubKey().Address()
|
||||||
clientAddr := sc.GetPubKey().Address()
|
clientAddr := sc.GetPubKey().Address()
|
||||||
|
|
||||||
assert.Equal(t, serverAddr, clientAddr)
|
assert.Equal(t, serverAddr, clientAddr)
|
||||||
|
}()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSocketPVPubKey(t *testing.T) {
|
func TestSocketPVPubKey(t *testing.T) {
|
||||||
var (
|
for _, tc := range socketTestCases(t) {
|
||||||
chainID = cmn.RandStr(12)
|
func() {
|
||||||
sc, rs = testSetupSocketPair(t, chainID, types.NewMockPV())
|
var (
|
||||||
)
|
chainID = cmn.RandStr(12)
|
||||||
defer sc.Stop()
|
sc, rs = testSetupSocketPair(t, chainID, types.NewMockPV(), tc.addr, tc.dialer)
|
||||||
defer rs.Stop()
|
)
|
||||||
|
defer sc.Stop()
|
||||||
|
defer rs.Stop()
|
||||||
|
|
||||||
clientKey := sc.GetPubKey()
|
clientKey := sc.GetPubKey()
|
||||||
|
|
||||||
privvalPubKey := rs.privVal.GetPubKey()
|
privvalPubKey := rs.privVal.GetPubKey()
|
||||||
|
|
||||||
assert.Equal(t, privvalPubKey, clientKey)
|
assert.Equal(t, privvalPubKey, clientKey)
|
||||||
|
}()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSocketPVProposal(t *testing.T) {
|
func TestSocketPVProposal(t *testing.T) {
|
||||||
var (
|
for _, tc := range socketTestCases(t) {
|
||||||
chainID = cmn.RandStr(12)
|
func() {
|
||||||
sc, rs = testSetupSocketPair(t, chainID, types.NewMockPV())
|
var (
|
||||||
|
chainID = cmn.RandStr(12)
|
||||||
|
sc, rs = testSetupSocketPair(t, chainID, types.NewMockPV(), tc.addr, tc.dialer)
|
||||||
|
|
||||||
ts = time.Now()
|
ts = time.Now()
|
||||||
privProposal = &types.Proposal{Timestamp: ts}
|
privProposal = &types.Proposal{Timestamp: ts}
|
||||||
clientProposal = &types.Proposal{Timestamp: ts}
|
clientProposal = &types.Proposal{Timestamp: ts}
|
||||||
)
|
)
|
||||||
defer sc.Stop()
|
defer sc.Stop()
|
||||||
defer rs.Stop()
|
defer rs.Stop()
|
||||||
|
|
||||||
require.NoError(t, rs.privVal.SignProposal(chainID, privProposal))
|
require.NoError(t, rs.privVal.SignProposal(chainID, privProposal))
|
||||||
require.NoError(t, sc.SignProposal(chainID, clientProposal))
|
require.NoError(t, sc.SignProposal(chainID, clientProposal))
|
||||||
assert.Equal(t, privProposal.Signature, clientProposal.Signature)
|
assert.Equal(t, privProposal.Signature, clientProposal.Signature)
|
||||||
|
}()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSocketPVVote(t *testing.T) {
|
func TestSocketPVVote(t *testing.T) {
|
||||||
var (
|
for _, tc := range socketTestCases(t) {
|
||||||
chainID = cmn.RandStr(12)
|
func() {
|
||||||
sc, rs = testSetupSocketPair(t, chainID, types.NewMockPV())
|
var (
|
||||||
|
chainID = cmn.RandStr(12)
|
||||||
|
sc, rs = testSetupSocketPair(t, chainID, types.NewMockPV(), tc.addr, tc.dialer)
|
||||||
|
|
||||||
ts = time.Now()
|
ts = time.Now()
|
||||||
vType = types.PrecommitType
|
vType = types.PrecommitType
|
||||||
want = &types.Vote{Timestamp: ts, Type: vType}
|
want = &types.Vote{Timestamp: ts, Type: vType}
|
||||||
have = &types.Vote{Timestamp: ts, Type: vType}
|
have = &types.Vote{Timestamp: ts, Type: vType}
|
||||||
)
|
)
|
||||||
defer sc.Stop()
|
defer sc.Stop()
|
||||||
defer rs.Stop()
|
defer rs.Stop()
|
||||||
|
|
||||||
require.NoError(t, rs.privVal.SignVote(chainID, want))
|
require.NoError(t, rs.privVal.SignVote(chainID, want))
|
||||||
require.NoError(t, sc.SignVote(chainID, have))
|
require.NoError(t, sc.SignVote(chainID, have))
|
||||||
assert.Equal(t, want.Signature, have.Signature)
|
assert.Equal(t, want.Signature, have.Signature)
|
||||||
|
}()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSocketPVVoteResetDeadline(t *testing.T) {
|
func TestSocketPVVoteResetDeadline(t *testing.T) {
|
||||||
var (
|
for _, tc := range socketTestCases(t) {
|
||||||
chainID = cmn.RandStr(12)
|
func() {
|
||||||
sc, rs = testSetupSocketPair(t, chainID, types.NewMockPV())
|
var (
|
||||||
|
chainID = cmn.RandStr(12)
|
||||||
|
sc, rs = testSetupSocketPair(t, chainID, types.NewMockPV(), tc.addr, tc.dialer)
|
||||||
|
|
||||||
ts = time.Now()
|
ts = time.Now()
|
||||||
vType = types.PrecommitType
|
vType = types.PrecommitType
|
||||||
want = &types.Vote{Timestamp: ts, Type: vType}
|
want = &types.Vote{Timestamp: ts, Type: vType}
|
||||||
have = &types.Vote{Timestamp: ts, Type: vType}
|
have = &types.Vote{Timestamp: ts, Type: vType}
|
||||||
)
|
)
|
||||||
defer sc.Stop()
|
defer sc.Stop()
|
||||||
defer rs.Stop()
|
defer rs.Stop()
|
||||||
|
|
||||||
time.Sleep(testConnDeadline2o3)
|
time.Sleep(testConnDeadline2o3)
|
||||||
|
|
||||||
require.NoError(t, rs.privVal.SignVote(chainID, want))
|
require.NoError(t, rs.privVal.SignVote(chainID, want))
|
||||||
require.NoError(t, sc.SignVote(chainID, have))
|
require.NoError(t, sc.SignVote(chainID, have))
|
||||||
assert.Equal(t, want.Signature, have.Signature)
|
assert.Equal(t, want.Signature, have.Signature)
|
||||||
|
|
||||||
// This would exceed the deadline if it was not extended by the previous message
|
// This would exceed the deadline if it was not extended by the previous message
|
||||||
time.Sleep(testConnDeadline2o3)
|
time.Sleep(testConnDeadline2o3)
|
||||||
|
|
||||||
require.NoError(t, rs.privVal.SignVote(chainID, want))
|
require.NoError(t, rs.privVal.SignVote(chainID, want))
|
||||||
require.NoError(t, sc.SignVote(chainID, have))
|
require.NoError(t, sc.SignVote(chainID, have))
|
||||||
assert.Equal(t, want.Signature, have.Signature)
|
assert.Equal(t, want.Signature, have.Signature)
|
||||||
|
}()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSocketPVVoteKeepalive(t *testing.T) {
|
func TestSocketPVVoteKeepalive(t *testing.T) {
|
||||||
var (
|
for _, tc := range socketTestCases(t) {
|
||||||
chainID = cmn.RandStr(12)
|
func() {
|
||||||
sc, rs = testSetupSocketPair(t, chainID, types.NewMockPV())
|
var (
|
||||||
|
chainID = cmn.RandStr(12)
|
||||||
|
sc, rs = testSetupSocketPair(t, chainID, types.NewMockPV(), tc.addr, tc.dialer)
|
||||||
|
|
||||||
ts = time.Now()
|
ts = time.Now()
|
||||||
vType = types.PrecommitType
|
vType = types.PrecommitType
|
||||||
want = &types.Vote{Timestamp: ts, Type: vType}
|
want = &types.Vote{Timestamp: ts, Type: vType}
|
||||||
have = &types.Vote{Timestamp: ts, Type: vType}
|
have = &types.Vote{Timestamp: ts, Type: vType}
|
||||||
)
|
)
|
||||||
defer sc.Stop()
|
defer sc.Stop()
|
||||||
defer rs.Stop()
|
defer rs.Stop()
|
||||||
|
|
||||||
time.Sleep(testConnDeadline * 2)
|
time.Sleep(testConnDeadline * 2)
|
||||||
|
|
||||||
require.NoError(t, rs.privVal.SignVote(chainID, want))
|
require.NoError(t, rs.privVal.SignVote(chainID, want))
|
||||||
require.NoError(t, sc.SignVote(chainID, have))
|
require.NoError(t, sc.SignVote(chainID, have))
|
||||||
assert.Equal(t, want.Signature, have.Signature)
|
assert.Equal(t, want.Signature, have.Signature)
|
||||||
|
}()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSocketPVDeadline(t *testing.T) {
|
// TestSocketPVDeadlineTCPOnly is not relevant to Unix domain sockets, since the
|
||||||
|
// OS knows instantaneously the state of both sides of the connection.
|
||||||
|
func TestSocketPVDeadlineTCPOnly(t *testing.T) {
|
||||||
var (
|
var (
|
||||||
addr = testFreeAddr(t)
|
addr = testFreeTCPAddr(t)
|
||||||
listenc = make(chan struct{})
|
listenc = make(chan struct{})
|
||||||
thisConnTimeout = 100 * time.Millisecond
|
thisConnTimeout = 100 * time.Millisecond
|
||||||
sc = newSocketVal(log.TestingLogger(), addr, thisConnTimeout)
|
sc = newSocketVal(log.TestingLogger(), addr, thisConnTimeout)
|
||||||
@ -172,218 +222,195 @@ func TestSocketPVDeadline(t *testing.T) {
|
|||||||
<-listenc
|
<-listenc
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRemoteSignerRetry(t *testing.T) {
|
|
||||||
var (
|
|
||||||
attemptc = make(chan int)
|
|
||||||
retries = 2
|
|
||||||
)
|
|
||||||
|
|
||||||
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
go func(ln net.Listener, attemptc chan<- int) {
|
|
||||||
attempts := 0
|
|
||||||
|
|
||||||
for {
|
|
||||||
conn, err := ln.Accept()
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
err = conn.Close()
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
attempts++
|
|
||||||
|
|
||||||
if attempts == retries {
|
|
||||||
attemptc <- attempts
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}(ln, attemptc)
|
|
||||||
|
|
||||||
rs := NewRemoteSigner(
|
|
||||||
log.TestingLogger(),
|
|
||||||
cmn.RandStr(12),
|
|
||||||
types.NewMockPV(),
|
|
||||||
DialTCPFn(ln.Addr().String(), testConnDeadline, ed25519.GenPrivKey()),
|
|
||||||
)
|
|
||||||
defer rs.Stop()
|
|
||||||
|
|
||||||
RemoteSignerConnDeadline(time.Millisecond)(rs)
|
|
||||||
RemoteSignerConnRetries(retries)(rs)
|
|
||||||
|
|
||||||
assert.Equal(t, rs.Start(), ErrDialRetryMax)
|
|
||||||
|
|
||||||
select {
|
|
||||||
case attempts := <-attemptc:
|
|
||||||
assert.Equal(t, retries, attempts)
|
|
||||||
case <-time.After(100 * time.Millisecond):
|
|
||||||
t.Error("expected remote to observe connection attempts")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestRemoteSignVoteErrors(t *testing.T) {
|
func TestRemoteSignVoteErrors(t *testing.T) {
|
||||||
var (
|
for _, tc := range socketTestCases(t) {
|
||||||
chainID = cmn.RandStr(12)
|
func() {
|
||||||
sc, rs = testSetupSocketPair(t, chainID, types.NewErroringMockPV())
|
var (
|
||||||
|
chainID = cmn.RandStr(12)
|
||||||
|
sc, rs = testSetupSocketPair(t, chainID, types.NewErroringMockPV(), tc.addr, tc.dialer)
|
||||||
|
|
||||||
ts = time.Now()
|
ts = time.Now()
|
||||||
vType = types.PrecommitType
|
vType = types.PrecommitType
|
||||||
vote = &types.Vote{Timestamp: ts, Type: vType}
|
vote = &types.Vote{Timestamp: ts, Type: vType}
|
||||||
)
|
)
|
||||||
defer sc.Stop()
|
defer sc.Stop()
|
||||||
defer rs.Stop()
|
defer rs.Stop()
|
||||||
|
|
||||||
err := sc.SignVote("", vote)
|
err := sc.SignVote("", vote)
|
||||||
require.Equal(t, err.(*RemoteSignerError).Description, types.ErroringMockPVErr.Error())
|
require.Equal(t, err.(*RemoteSignerError).Description, types.ErroringMockPVErr.Error())
|
||||||
|
|
||||||
err = rs.privVal.SignVote(chainID, vote)
|
err = rs.privVal.SignVote(chainID, vote)
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
err = sc.SignVote(chainID, vote)
|
err = sc.SignVote(chainID, vote)
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
|
}()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRemoteSignProposalErrors(t *testing.T) {
|
func TestRemoteSignProposalErrors(t *testing.T) {
|
||||||
var (
|
for _, tc := range socketTestCases(t) {
|
||||||
chainID = cmn.RandStr(12)
|
func() {
|
||||||
sc, rs = testSetupSocketPair(t, chainID, types.NewErroringMockPV())
|
var (
|
||||||
|
chainID = cmn.RandStr(12)
|
||||||
|
sc, rs = testSetupSocketPair(t, chainID, types.NewErroringMockPV(), tc.addr, tc.dialer)
|
||||||
|
|
||||||
ts = time.Now()
|
ts = time.Now()
|
||||||
proposal = &types.Proposal{Timestamp: ts}
|
proposal = &types.Proposal{Timestamp: ts}
|
||||||
)
|
)
|
||||||
defer sc.Stop()
|
defer sc.Stop()
|
||||||
defer rs.Stop()
|
defer rs.Stop()
|
||||||
|
|
||||||
err := sc.SignProposal("", proposal)
|
err := sc.SignProposal("", proposal)
|
||||||
require.Equal(t, err.(*RemoteSignerError).Description, types.ErroringMockPVErr.Error())
|
require.Equal(t, err.(*RemoteSignerError).Description, types.ErroringMockPVErr.Error())
|
||||||
|
|
||||||
err = rs.privVal.SignProposal(chainID, proposal)
|
err = rs.privVal.SignProposal(chainID, proposal)
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
|
|
||||||
err = sc.SignProposal(chainID, proposal)
|
err = sc.SignProposal(chainID, proposal)
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
|
}()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestErrUnexpectedResponse(t *testing.T) {
|
func TestErrUnexpectedResponse(t *testing.T) {
|
||||||
var (
|
for _, tc := range socketTestCases(t) {
|
||||||
addr = testFreeAddr(t)
|
func() {
|
||||||
logger = log.TestingLogger()
|
var (
|
||||||
chainID = cmn.RandStr(12)
|
logger = log.TestingLogger()
|
||||||
readyc = make(chan struct{})
|
chainID = cmn.RandStr(12)
|
||||||
errc = make(chan error, 1)
|
readyc = make(chan struct{})
|
||||||
|
errc = make(chan error, 1)
|
||||||
|
|
||||||
rs = NewRemoteSigner(
|
rs = NewRemoteSigner(
|
||||||
logger,
|
logger,
|
||||||
chainID,
|
chainID,
|
||||||
types.NewMockPV(),
|
types.NewMockPV(),
|
||||||
DialTCPFn(addr, testConnDeadline, ed25519.GenPrivKey()),
|
tc.dialer,
|
||||||
)
|
)
|
||||||
sc = newSocketVal(logger, addr, testConnDeadline)
|
sc = newSocketVal(logger, tc.addr, testConnDeadline)
|
||||||
)
|
)
|
||||||
|
|
||||||
testStartSocketPV(t, readyc, sc)
|
testStartSocketPV(t, readyc, sc)
|
||||||
defer sc.Stop()
|
defer sc.Stop()
|
||||||
RemoteSignerConnDeadline(time.Millisecond)(rs)
|
RemoteSignerConnDeadline(time.Millisecond)(rs)
|
||||||
RemoteSignerConnRetries(100)(rs)
|
RemoteSignerConnRetries(100)(rs)
|
||||||
// we do not want to Start() the remote signer here and instead use the connection to
|
// we do not want to Start() the remote signer here and instead use the connection to
|
||||||
// reply with intentionally wrong replies below:
|
// reply with intentionally wrong replies below:
|
||||||
rsConn, err := rs.connect()
|
rsConn, err := rs.connect()
|
||||||
defer rsConn.Close()
|
defer rsConn.Close()
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.NotNil(t, rsConn)
|
require.NotNil(t, rsConn)
|
||||||
// send over public key to get the remote signer running:
|
// send over public key to get the remote signer running:
|
||||||
go testReadWriteResponse(t, &PubKeyResponse{}, rsConn)
|
go testReadWriteResponse(t, &PubKeyResponse{}, rsConn)
|
||||||
<-readyc
|
<-readyc
|
||||||
|
|
||||||
// Proposal:
|
// Proposal:
|
||||||
go func(errc chan error) {
|
go func(errc chan error) {
|
||||||
errc <- sc.SignProposal(chainID, &types.Proposal{})
|
errc <- sc.SignProposal(chainID, &types.Proposal{})
|
||||||
}(errc)
|
}(errc)
|
||||||
// read request and write wrong response:
|
// read request and write wrong response:
|
||||||
go testReadWriteResponse(t, &SignedVoteResponse{}, rsConn)
|
go testReadWriteResponse(t, &SignedVoteResponse{}, rsConn)
|
||||||
err = <-errc
|
err = <-errc
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
require.Equal(t, err, ErrUnexpectedResponse)
|
require.Equal(t, err, ErrUnexpectedResponse)
|
||||||
|
|
||||||
// Vote:
|
// Vote:
|
||||||
go func(errc chan error) {
|
go func(errc chan error) {
|
||||||
errc <- sc.SignVote(chainID, &types.Vote{})
|
errc <- sc.SignVote(chainID, &types.Vote{})
|
||||||
}(errc)
|
}(errc)
|
||||||
// read request and write wrong response:
|
// read request and write wrong response:
|
||||||
go testReadWriteResponse(t, &SignedProposalResponse{}, rsConn)
|
go testReadWriteResponse(t, &SignedProposalResponse{}, rsConn)
|
||||||
err = <-errc
|
err = <-errc
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
require.Equal(t, err, ErrUnexpectedResponse)
|
require.Equal(t, err, ErrUnexpectedResponse)
|
||||||
|
}()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRetryTCPConnToRemoteSigner(t *testing.T) {
|
func TestRetryConnToRemoteSigner(t *testing.T) {
|
||||||
var (
|
for _, tc := range socketTestCases(t) {
|
||||||
addr = testFreeAddr(t)
|
func() {
|
||||||
logger = log.TestingLogger()
|
var (
|
||||||
chainID = cmn.RandStr(12)
|
logger = log.TestingLogger()
|
||||||
readyc = make(chan struct{})
|
chainID = cmn.RandStr(12)
|
||||||
|
readyc = make(chan struct{})
|
||||||
|
|
||||||
rs = NewRemoteSigner(
|
rs = NewRemoteSigner(
|
||||||
logger,
|
logger,
|
||||||
chainID,
|
chainID,
|
||||||
types.NewMockPV(),
|
types.NewMockPV(),
|
||||||
DialTCPFn(addr, testConnDeadline, ed25519.GenPrivKey()),
|
tc.dialer,
|
||||||
)
|
)
|
||||||
thisConnTimeout = testConnDeadline
|
thisConnTimeout = testConnDeadline
|
||||||
sc = newSocketVal(logger, addr, thisConnTimeout)
|
sc = newSocketVal(logger, tc.addr, thisConnTimeout)
|
||||||
)
|
)
|
||||||
// Ping every:
|
// Ping every:
|
||||||
SocketValHeartbeat(testHeartbeatTimeout)(sc)
|
SocketValHeartbeat(testHeartbeatTimeout)(sc)
|
||||||
|
|
||||||
RemoteSignerConnDeadline(testConnDeadline)(rs)
|
RemoteSignerConnDeadline(testConnDeadline)(rs)
|
||||||
RemoteSignerConnRetries(10)(rs)
|
RemoteSignerConnRetries(10)(rs)
|
||||||
|
|
||||||
testStartSocketPV(t, readyc, sc)
|
testStartSocketPV(t, readyc, sc)
|
||||||
defer sc.Stop()
|
defer sc.Stop()
|
||||||
require.NoError(t, rs.Start())
|
require.NoError(t, rs.Start())
|
||||||
assert.True(t, rs.IsRunning())
|
assert.True(t, rs.IsRunning())
|
||||||
|
|
||||||
<-readyc
|
<-readyc
|
||||||
time.Sleep(testHeartbeatTimeout * 2)
|
time.Sleep(testHeartbeatTimeout * 2)
|
||||||
|
|
||||||
rs.Stop()
|
rs.Stop()
|
||||||
rs2 := NewRemoteSigner(
|
rs2 := NewRemoteSigner(
|
||||||
logger,
|
logger,
|
||||||
chainID,
|
chainID,
|
||||||
types.NewMockPV(),
|
types.NewMockPV(),
|
||||||
DialTCPFn(addr, testConnDeadline, ed25519.GenPrivKey()),
|
tc.dialer,
|
||||||
)
|
)
|
||||||
// let some pings pass
|
// let some pings pass
|
||||||
time.Sleep(testHeartbeatTimeout3o2)
|
time.Sleep(testHeartbeatTimeout3o2)
|
||||||
require.NoError(t, rs2.Start())
|
require.NoError(t, rs2.Start())
|
||||||
assert.True(t, rs2.IsRunning())
|
assert.True(t, rs2.IsRunning())
|
||||||
defer rs2.Stop()
|
defer rs2.Stop()
|
||||||
|
|
||||||
// give the client some time to re-establish the conn to the remote signer
|
// give the client some time to re-establish the conn to the remote signer
|
||||||
// should see sth like this in the logs:
|
// should see sth like this in the logs:
|
||||||
//
|
//
|
||||||
// E[10016-01-10|17:12:46.128] Ping err="remote signer timed out"
|
// E[10016-01-10|17:12:46.128] Ping err="remote signer timed out"
|
||||||
// I[10016-01-10|17:16:42.447] Re-created connection to remote signer impl=SocketVal
|
// I[10016-01-10|17:16:42.447] Re-created connection to remote signer impl=SocketVal
|
||||||
time.Sleep(testConnDeadline * 2)
|
time.Sleep(testConnDeadline * 2)
|
||||||
|
}()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func newSocketVal(logger log.Logger, addr string, connDeadline time.Duration) *SocketVal {
|
func newSocketVal(logger log.Logger, addr string, connDeadline time.Duration) *SocketVal {
|
||||||
ln, err := net.Listen(cmn.ProtocolAndAddress(addr))
|
proto, address := cmn.ProtocolAndAddress(addr)
|
||||||
|
ln, err := net.Listen(proto, address)
|
||||||
|
logger.Info("Listening at", "proto", proto, "address", address)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
tcpLn := NewTCPListener(ln, ed25519.GenPrivKey())
|
var svln net.Listener
|
||||||
TCPListenerAcceptDeadline(testAcceptDeadline)(tcpLn)
|
if proto == "unix" {
|
||||||
TCPListenerConnDeadline(testConnDeadline)(tcpLn)
|
unixLn := NewUnixListener(ln)
|
||||||
return NewSocketVal(logger, tcpLn)
|
UnixListenerAcceptDeadline(testAcceptDeadline)(unixLn)
|
||||||
|
UnixListenerConnDeadline(connDeadline)(unixLn)
|
||||||
|
svln = unixLn
|
||||||
|
} else {
|
||||||
|
tcpLn := NewTCPListener(ln, ed25519.GenPrivKey())
|
||||||
|
TCPListenerAcceptDeadline(testAcceptDeadline)(tcpLn)
|
||||||
|
TCPListenerConnDeadline(connDeadline)(tcpLn)
|
||||||
|
svln = tcpLn
|
||||||
|
}
|
||||||
|
return NewSocketVal(logger, svln)
|
||||||
}
|
}
|
||||||
|
|
||||||
func testSetupSocketPair(
|
func testSetupSocketPair(
|
||||||
t *testing.T,
|
t *testing.T,
|
||||||
chainID string,
|
chainID string,
|
||||||
privValidator types.PrivValidator,
|
privValidator types.PrivValidator,
|
||||||
|
addr string,
|
||||||
|
dialer Dialer,
|
||||||
) (*SocketVal, *RemoteSigner) {
|
) (*SocketVal, *RemoteSigner) {
|
||||||
var (
|
var (
|
||||||
addr = testFreeAddr(t)
|
|
||||||
logger = log.TestingLogger()
|
logger = log.TestingLogger()
|
||||||
privVal = privValidator
|
privVal = privValidator
|
||||||
readyc = make(chan struct{})
|
readyc = make(chan struct{})
|
||||||
@ -391,7 +418,7 @@ func testSetupSocketPair(
|
|||||||
logger,
|
logger,
|
||||||
chainID,
|
chainID,
|
||||||
privVal,
|
privVal,
|
||||||
DialTCPFn(addr, testConnDeadline, ed25519.GenPrivKey()),
|
dialer,
|
||||||
)
|
)
|
||||||
|
|
||||||
thisConnTimeout = testConnDeadline
|
thisConnTimeout = testConnDeadline
|
||||||
@ -429,8 +456,8 @@ func testStartSocketPV(t *testing.T, readyc chan struct{}, sc *SocketVal) {
|
|||||||
}(sc)
|
}(sc)
|
||||||
}
|
}
|
||||||
|
|
||||||
// testFreeAddr claims a free port so we don't block on listener being ready.
|
// testFreeTCPAddr claims a free port so we don't block on listener being ready.
|
||||||
func testFreeAddr(t *testing.T) string {
|
func testFreeTCPAddr(t *testing.T) string {
|
||||||
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
defer ln.Close()
|
defer ln.Close()
|
||||||
|
68
privval/remote_signer_test.go
Normal file
68
privval/remote_signer_test.go
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
package privval
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
"github.com/tendermint/tendermint/crypto/ed25519"
|
||||||
|
cmn "github.com/tendermint/tendermint/libs/common"
|
||||||
|
"github.com/tendermint/tendermint/libs/log"
|
||||||
|
"github.com/tendermint/tendermint/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TestRemoteSignerRetryTCPOnly will test connection retry attempts over TCP. We
|
||||||
|
// don't need this for Unix sockets because the OS instantly knows the state of
|
||||||
|
// both ends of the socket connection. This basically causes the
|
||||||
|
// RemoteSigner.dialer() call inside RemoteSigner.connect() to return
|
||||||
|
// successfully immediately, putting an instant stop to any retry attempts.
|
||||||
|
func TestRemoteSignerRetryTCPOnly(t *testing.T) {
|
||||||
|
var (
|
||||||
|
attemptc = make(chan int)
|
||||||
|
retries = 2
|
||||||
|
)
|
||||||
|
|
||||||
|
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
go func(ln net.Listener, attemptc chan<- int) {
|
||||||
|
attempts := 0
|
||||||
|
|
||||||
|
for {
|
||||||
|
conn, err := ln.Accept()
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
err = conn.Close()
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
attempts++
|
||||||
|
|
||||||
|
if attempts == retries {
|
||||||
|
attemptc <- attempts
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}(ln, attemptc)
|
||||||
|
|
||||||
|
rs := NewRemoteSigner(
|
||||||
|
log.TestingLogger(),
|
||||||
|
cmn.RandStr(12),
|
||||||
|
types.NewMockPV(),
|
||||||
|
DialTCPFn(ln.Addr().String(), testConnDeadline, ed25519.GenPrivKey()),
|
||||||
|
)
|
||||||
|
defer rs.Stop()
|
||||||
|
|
||||||
|
RemoteSignerConnDeadline(time.Millisecond)(rs)
|
||||||
|
RemoteSignerConnRetries(retries)(rs)
|
||||||
|
|
||||||
|
assert.Equal(t, rs.Start(), ErrDialRetryMax)
|
||||||
|
|
||||||
|
select {
|
||||||
|
case attempts := <-attemptc:
|
||||||
|
assert.Equal(t, retries, attempts)
|
||||||
|
case <-time.After(100 * time.Millisecond):
|
||||||
|
t.Error("expected remote to observe connection attempts")
|
||||||
|
}
|
||||||
|
}
|
@ -157,7 +157,7 @@ type timeoutConn struct {
|
|||||||
connDeadline time.Duration
|
connDeadline time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
// newTimeoutConn returns an instance of newTCPTimeoutConn.
|
// newTimeoutConn returns an instance of timeoutConn.
|
||||||
func newTimeoutConn(
|
func newTimeoutConn(
|
||||||
conn net.Conn,
|
conn net.Conn,
|
||||||
connDeadline time.Duration) *timeoutConn {
|
connDeadline time.Duration) *timeoutConn {
|
||||||
|
@ -29,7 +29,7 @@ type listenerTestCase struct {
|
|||||||
// testUnixAddr will attempt to obtain a platform-independent temporary file
|
// testUnixAddr will attempt to obtain a platform-independent temporary file
|
||||||
// name for a Unix socket
|
// name for a Unix socket
|
||||||
func testUnixAddr() (string, error) {
|
func testUnixAddr() (string, error) {
|
||||||
f, err := ioutil.TempFile("", "tendermint-privval-test")
|
f, err := ioutil.TempFile("", "tendermint-privval-test-*")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user