github.com/project-88388/tendermint-v0.34.14-terra.2@v1.0.0/proxy/multi_app_conn_test.go (about)

     1  package proxy
     2  
     3  import (
     4  	"errors"
     5  	"os"
     6  	"os/signal"
     7  	"syscall"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/stretchr/testify/mock"
    12  	"github.com/stretchr/testify/require"
    13  
    14  	abcimocks "github.com/tendermint/tendermint/abci/client/mocks"
    15  	"github.com/tendermint/tendermint/proxy/mocks"
    16  )
    17  
    18  func TestAppConns_Start_Stop(t *testing.T) {
    19  	quitCh := make(<-chan struct{})
    20  
    21  	clientCreatorMock := &mocks.ClientCreator{}
    22  
    23  	clientMock := &abcimocks.Client{}
    24  	clientMock.On("SetLogger", mock.Anything).Return().Times(4)
    25  	clientMock.On("Start").Return(nil).Times(4)
    26  	clientMock.On("Stop").Return(nil).Times(4)
    27  	clientMock.On("Quit").Return(quitCh).Times(4)
    28  
    29  	clientCreatorMock.On("NewABCIClient").Return(clientMock, nil).Times(4)
    30  
    31  	appConns := NewAppConns(clientCreatorMock)
    32  
    33  	err := appConns.Start()
    34  	require.NoError(t, err)
    35  
    36  	time.Sleep(100 * time.Millisecond)
    37  
    38  	err = appConns.Stop()
    39  	require.NoError(t, err)
    40  
    41  	clientMock.AssertExpectations(t)
    42  }
    43  
    44  // Upon failure, we call tmos.Kill
    45  func TestAppConns_Failure(t *testing.T) {
    46  	ok := make(chan struct{})
    47  	c := make(chan os.Signal, 1)
    48  	signal.Notify(c, syscall.SIGTERM)
    49  	go func() {
    50  		for range c {
    51  			close(ok)
    52  		}
    53  	}()
    54  
    55  	quitCh := make(chan struct{})
    56  	var recvQuitCh <-chan struct{} // nolint:gosimple
    57  	recvQuitCh = quitCh
    58  
    59  	clientCreatorMock := &mocks.ClientCreator{}
    60  
    61  	clientMock := &abcimocks.Client{}
    62  	clientMock.On("SetLogger", mock.Anything).Return()
    63  	clientMock.On("Start").Return(nil)
    64  	clientMock.On("Stop").Return(nil)
    65  
    66  	clientMock.On("Quit").Return(recvQuitCh)
    67  	clientMock.On("Error").Return(errors.New("EOF")).Once()
    68  
    69  	clientCreatorMock.On("NewABCIClient").Return(clientMock, nil)
    70  
    71  	appConns := NewAppConns(clientCreatorMock)
    72  
    73  	err := appConns.Start()
    74  	require.NoError(t, err)
    75  	t.Cleanup(func() {
    76  		if err := appConns.Stop(); err != nil {
    77  			t.Error(err)
    78  		}
    79  	})
    80  
    81  	// simulate failure
    82  	close(quitCh)
    83  
    84  	select {
    85  	case <-ok:
    86  		t.Log("SIGTERM successfully received")
    87  	case <-time.After(5 * time.Second):
    88  		t.Fatal("expected process to receive SIGTERM signal")
    89  	}
    90  }